blob: dc04a6f2bdafed59e59c9320e8bce6ed393eff39 [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.
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -080049static const int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
Jeff Brownf086ddb2014-02-11 14:28:48 -080050
Michael Wrightd02c5b62014-02-10 15:10:22 -080051// An arbitrary injector pid / uid pair that has permission to inject events.
52static const int32_t INJECTOR_PID = 999;
53static const int32_t INJECTOR_UID = 1001;
54
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +000055// An arbitrary pid of the gesture monitor window
56static constexpr int32_t MONITOR_PID = 2001;
57
chaviwd1c23182019-12-20 18:44:56 -080058struct PointF {
59 float x;
60 float y;
61};
Michael Wrightd02c5b62014-02-10 15:10:22 -080062
Gang Wang342c9272020-01-13 13:15:04 -050063/**
64 * Return a DOWN key event with KEYCODE_A.
65 */
66static KeyEvent getTestKeyEvent() {
67 KeyEvent event;
68
Garfield Tanfbe732e2020-01-24 11:26:14 -080069 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
70 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
71 ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -050072 return event;
73}
74
Michael Wrightd02c5b62014-02-10 15:10:22 -080075// --- FakeInputDispatcherPolicy ---
76
77class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
78 InputDispatcherConfiguration mConfig;
79
80protected:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100081 virtual ~FakeInputDispatcherPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -080082
83public:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100084 FakeInputDispatcherPolicy() {}
Jackal Guof9696682018-10-05 12:23:23 +080085
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080086 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080087 assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_KEY, args.eventTime, args.action,
88 args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080089 }
90
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080091 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args) {
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080092 assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_MOTION, args.eventTime, args.action,
93 args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080094 }
95
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -070096 void assertFilterInputEventWasNotCalled() {
97 std::scoped_lock lock(mLock);
98 ASSERT_EQ(nullptr, mFilteredEvent);
99 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800100
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800101 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700102 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800103 ASSERT_TRUE(mConfigurationChangedTime)
104 << "Timed out waiting for configuration changed call";
105 ASSERT_EQ(*mConfigurationChangedTime, when);
106 mConfigurationChangedTime = std::nullopt;
107 }
108
109 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700110 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800111 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800112 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800113 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
114 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
115 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
116 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
117 mLastNotifySwitch = std::nullopt;
118 }
119
chaviwfd6d3512019-03-25 13:23:49 -0700120 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700121 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800122 ASSERT_EQ(touchedToken, mOnPointerDownToken);
123 mOnPointerDownToken.clear();
124 }
125
126 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700127 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800128 ASSERT_TRUE(mOnPointerDownToken == nullptr)
129 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700130 }
131
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700132 // This function must be called soon after the expected ANR timer starts,
133 // because we are also checking how much time has passed.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500134 void assertNotifyNoFocusedWindowAnrWasCalled(
Chris Yea209fde2020-07-22 13:54:51 -0700135 std::chrono::nanoseconds timeout,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500136 const std::shared_ptr<InputApplicationHandle>& expectedApplication) {
137 std::shared_ptr<InputApplicationHandle> application;
138 { // acquire lock
139 std::unique_lock lock(mLock);
140 android::base::ScopedLockAssertion assumeLocked(mLock);
141 ASSERT_NO_FATAL_FAILURE(
142 application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock));
143 } // release lock
144 ASSERT_EQ(expectedApplication, application);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700145 }
146
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000147 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
148 const sp<IBinder>& expectedConnectionToken) {
149 sp<IBinder> connectionToken = getUnresponsiveWindowToken(timeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500150 ASSERT_EQ(expectedConnectionToken, connectionToken);
151 }
152
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000153 void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedConnectionToken) {
154 sp<IBinder> connectionToken = getResponsiveWindowToken();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500155 ASSERT_EQ(expectedConnectionToken, connectionToken);
156 }
157
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000158 void assertNotifyMonitorUnresponsiveWasCalled(std::chrono::nanoseconds timeout) {
159 int32_t pid = getUnresponsiveMonitorPid(timeout);
160 ASSERT_EQ(MONITOR_PID, pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500161 }
162
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000163 void assertNotifyMonitorResponsiveWasCalled() {
164 int32_t pid = getResponsiveMonitorPid();
165 ASSERT_EQ(MONITOR_PID, pid);
166 }
167
168 sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500169 std::unique_lock lock(mLock);
170 android::base::ScopedLockAssertion assumeLocked(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000171 return getAnrTokenLockedInterruptible(timeout, mAnrWindowTokens, lock);
172 }
173
174 sp<IBinder> getResponsiveWindowToken() {
175 std::unique_lock lock(mLock);
176 android::base::ScopedLockAssertion assumeLocked(mLock);
177 return getAnrTokenLockedInterruptible(0s, mResponsiveWindowTokens, lock);
178 }
179
180 int32_t getUnresponsiveMonitorPid(std::chrono::nanoseconds timeout) {
181 std::unique_lock lock(mLock);
182 android::base::ScopedLockAssertion assumeLocked(mLock);
183 return getAnrTokenLockedInterruptible(timeout, mAnrMonitorPids, lock);
184 }
185
186 int32_t getResponsiveMonitorPid() {
187 std::unique_lock lock(mLock);
188 android::base::ScopedLockAssertion assumeLocked(mLock);
189 return getAnrTokenLockedInterruptible(0s, mResponsiveMonitorPids, lock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500190 }
191
192 // All three ANR-related callbacks behave the same way, so we use this generic function to wait
193 // for a specific container to become non-empty. When the container is non-empty, return the
194 // first entry from the container and erase it.
195 template <class T>
196 T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage,
197 std::unique_lock<std::mutex>& lock) REQUIRES(mLock) {
198 const std::chrono::time_point start = std::chrono::steady_clock::now();
199 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700200
201 // If there is an ANR, Dispatcher won't be idle because there are still events
202 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
203 // before checking if ANR was called.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500204 // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need
205 // to provide it some time to act. 100ms seems reasonable.
206 mNotifyAnr.wait_for(lock, timeToWait,
207 [&storage]() REQUIRES(mLock) { return !storage.empty(); });
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700208 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500209 if (storage.empty()) {
210 ADD_FAILURE() << "Did not receive the ANR callback";
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000211 return {};
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700212 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700213 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
214 // the dispatcher started counting before this function was called
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700215 if (std::chrono::abs(timeout - waited) > 100ms) {
216 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
217 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
218 << "ms, but waited "
219 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
220 << "ms instead";
221 }
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500222 T token = storage.front();
223 storage.pop();
224 return token;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700225 }
226
227 void assertNotifyAnrWasNotCalled() {
228 std::scoped_lock lock(mLock);
229 ASSERT_TRUE(mAnrApplications.empty());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000230 ASSERT_TRUE(mAnrWindowTokens.empty());
231 ASSERT_TRUE(mAnrMonitorPids.empty());
232 ASSERT_TRUE(mResponsiveWindowTokens.empty())
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500233 << "ANR was not called, but please also consume the 'connection is responsive' "
234 "signal";
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000235 ASSERT_TRUE(mResponsiveMonitorPids.empty())
236 << "Monitor ANR was not called, but please also consume the 'monitor is responsive'"
237 " signal";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700238 }
239
Garfield Tan1c7bc862020-01-28 13:24:04 -0800240 void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) {
241 mConfig.keyRepeatTimeout = timeout;
242 mConfig.keyRepeatDelay = delay;
243 }
244
Prabir Pradhanac483a62021-08-06 14:01:18 +0000245 PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -0800246 std::unique_lock lock(mLock);
247 base::ScopedLockAssertion assumeLocked(mLock);
248
249 if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms,
250 [this, enabled]() REQUIRES(mLock) {
Prabir Pradhanac483a62021-08-06 14:01:18 +0000251 return mPointerCaptureRequest->enable ==
Prabir Pradhan99987712020-11-10 18:43:05 -0800252 enabled;
253 })) {
Prabir Pradhanac483a62021-08-06 14:01:18 +0000254 ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled
255 << ") to be called.";
256 return {};
Prabir Pradhan99987712020-11-10 18:43:05 -0800257 }
Prabir Pradhanac483a62021-08-06 14:01:18 +0000258 auto request = *mPointerCaptureRequest;
259 mPointerCaptureRequest.reset();
260 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -0800261 }
262
263 void assertSetPointerCaptureNotCalled() {
264 std::unique_lock lock(mLock);
265 base::ScopedLockAssertion assumeLocked(mLock);
266
267 if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) {
Prabir Pradhanac483a62021-08-06 14:01:18 +0000268 FAIL() << "Expected setPointerCapture(request) to not be called, but was called. "
Prabir Pradhan99987712020-11-10 18:43:05 -0800269 "enabled = "
Prabir Pradhanac483a62021-08-06 14:01:18 +0000270 << std::to_string(mPointerCaptureRequest->enable);
Prabir Pradhan99987712020-11-10 18:43:05 -0800271 }
Prabir Pradhanac483a62021-08-06 14:01:18 +0000272 mPointerCaptureRequest.reset();
Prabir Pradhan99987712020-11-10 18:43:05 -0800273 }
274
arthurhungf452d0b2021-01-06 00:19:52 +0800275 void assertDropTargetEquals(const sp<IBinder>& targetToken) {
276 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800277 ASSERT_TRUE(mNotifyDropWindowWasCalled);
arthurhungf452d0b2021-01-06 00:19:52 +0800278 ASSERT_EQ(targetToken, mDropTargetWindowToken);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800279 mNotifyDropWindowWasCalled = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800280 }
281
Michael Wrightd02c5b62014-02-10 15:10:22 -0800282private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700283 std::mutex mLock;
284 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
285 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
286 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
287 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800288
Prabir Pradhan99987712020-11-10 18:43:05 -0800289 std::condition_variable mPointerCaptureChangedCondition;
Prabir Pradhanac483a62021-08-06 14:01:18 +0000290
291 std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock);
Prabir Pradhan99987712020-11-10 18:43:05 -0800292
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700293 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700294 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000295 std::queue<sp<IBinder>> mAnrWindowTokens GUARDED_BY(mLock);
296 std::queue<sp<IBinder>> mResponsiveWindowTokens GUARDED_BY(mLock);
297 std::queue<int32_t> mAnrMonitorPids GUARDED_BY(mLock);
298 std::queue<int32_t> mResponsiveMonitorPids GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700299 std::condition_variable mNotifyAnr;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700300
arthurhungf452d0b2021-01-06 00:19:52 +0800301 sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800302 bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800303
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600304 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700305 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800306 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800307 }
308
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000309 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700310 std::scoped_lock lock(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000311 mAnrWindowTokens.push(connectionToken);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700312 mNotifyAnr.notify_all();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500313 }
314
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000315 void notifyMonitorUnresponsive(int32_t pid, const std::string&) override {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500316 std::scoped_lock lock(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000317 mAnrMonitorPids.push(pid);
318 mNotifyAnr.notify_all();
319 }
320
321 void notifyWindowResponsive(const sp<IBinder>& connectionToken) override {
322 std::scoped_lock lock(mLock);
323 mResponsiveWindowTokens.push(connectionToken);
324 mNotifyAnr.notify_all();
325 }
326
327 void notifyMonitorResponsive(int32_t pid) override {
328 std::scoped_lock lock(mLock);
329 mResponsiveMonitorPids.push(pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500330 mNotifyAnr.notify_all();
331 }
332
333 void notifyNoFocusedWindowAnr(
334 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
335 std::scoped_lock lock(mLock);
336 mAnrApplications.push(applicationHandle);
337 mNotifyAnr.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338 }
339
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600340 void notifyInputChannelBroken(const sp<IBinder>&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800341
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600342 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700343
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600344 void notifyUntrustedTouch(const std::string& obscuringPackage) override {}
Chris Yef59a2f42020-10-16 12:55:26 -0700345 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
346 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
347 const std::vector<float>& values) override {}
348
349 void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType,
350 InputDeviceSensorAccuracy accuracy) override {}
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000351
Chris Yefb552902021-02-03 17:18:37 -0800352 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
353
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600354 void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800355 *outConfig = mConfig;
356 }
357
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600358 bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700359 std::scoped_lock lock(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800360 switch (inputEvent->getType()) {
361 case AINPUT_EVENT_TYPE_KEY: {
362 const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800363 mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800364 break;
365 }
366
367 case AINPUT_EVENT_TYPE_MOTION: {
368 const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800369 mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800370 break;
371 }
372 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800373 return true;
374 }
375
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600376 void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800377
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600378 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800379
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600380 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800381 return 0;
382 }
383
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600384 bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800385 return false;
386 }
387
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600388 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
389 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700390 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800391 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
392 * essentially a passthrough for notifySwitch.
393 */
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800394 mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800395 }
396
Sean Stoutb4e0a592021-02-23 07:34:53 -0800397 void pokeUserActivity(nsecs_t, int32_t, int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800398
Prabir Pradhan93f342c2021-03-11 15:05:30 -0800399 bool checkInjectEventsPermissionNonReentrant(int32_t pid, int32_t uid) override {
400 return pid == INJECTOR_PID && uid == INJECTOR_UID;
401 }
Jackal Guof9696682018-10-05 12:23:23 +0800402
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600403 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700404 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700405 mOnPointerDownToken = newToken;
406 }
407
Prabir Pradhanac483a62021-08-06 14:01:18 +0000408 void setPointerCapture(const PointerCaptureRequest& request) override {
Prabir Pradhan99987712020-11-10 18:43:05 -0800409 std::scoped_lock lock(mLock);
Prabir Pradhanac483a62021-08-06 14:01:18 +0000410 mPointerCaptureRequest = {request};
Prabir Pradhan99987712020-11-10 18:43:05 -0800411 mPointerCaptureChangedCondition.notify_all();
412 }
413
arthurhungf452d0b2021-01-06 00:19:52 +0800414 void notifyDropWindow(const sp<IBinder>& token, float x, float y) override {
415 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800416 mNotifyDropWindowWasCalled = true;
arthurhungf452d0b2021-01-06 00:19:52 +0800417 mDropTargetWindowToken = token;
418 }
419
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800420 void assertFilterInputEventWasCalled(int type, nsecs_t eventTime, int32_t action,
421 int32_t displayId) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700422 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800423 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
424 ASSERT_EQ(mFilteredEvent->getType(), type);
425
426 if (type == AINPUT_EVENT_TYPE_KEY) {
427 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*mFilteredEvent);
428 EXPECT_EQ(keyEvent.getEventTime(), eventTime);
429 EXPECT_EQ(keyEvent.getAction(), action);
430 EXPECT_EQ(keyEvent.getDisplayId(), displayId);
431 } else if (type == AINPUT_EVENT_TYPE_MOTION) {
432 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*mFilteredEvent);
433 EXPECT_EQ(motionEvent.getEventTime(), eventTime);
434 EXPECT_EQ(motionEvent.getAction(), action);
435 EXPECT_EQ(motionEvent.getDisplayId(), displayId);
436 } else {
437 FAIL() << "Unknown type: " << type;
438 }
439
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800440 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800441 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800442};
443
Michael Wrightd02c5b62014-02-10 15:10:22 -0800444// --- InputDispatcherTest ---
445
446class InputDispatcherTest : public testing::Test {
447protected:
448 sp<FakeInputDispatcherPolicy> mFakePolicy;
449 sp<InputDispatcher> mDispatcher;
450
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000451 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800452 mFakePolicy = new FakeInputDispatcherPolicy();
453 mDispatcher = new InputDispatcher(mFakePolicy);
Arthur Hungb92218b2018-08-14 12:00:21 +0800454 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000455 // Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700456 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800457 }
458
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000459 void TearDown() override {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700460 ASSERT_EQ(OK, mDispatcher->stop());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800461 mFakePolicy.clear();
462 mDispatcher.clear();
463 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700464
465 /**
466 * Used for debugging when writing the test
467 */
468 void dumpDispatcherState() {
469 std::string dump;
470 mDispatcher->dump(dump);
471 std::stringstream ss(dump);
472 std::string to;
473
474 while (std::getline(ss, to, '\n')) {
475 ALOGE("%s", to.c_str());
476 }
477 }
Vishnu Nair958da932020-08-21 17:12:37 -0700478
chaviw3277faf2021-05-19 16:45:23 -0500479 void setFocusedWindow(const sp<WindowInfoHandle>& window,
480 const sp<WindowInfoHandle>& focusedWindow = nullptr) {
Vishnu Nair958da932020-08-21 17:12:37 -0700481 FocusRequest request;
482 request.token = window->getToken();
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +0000483 request.windowName = window->getName();
Vishnu Nair958da932020-08-21 17:12:37 -0700484 if (focusedWindow) {
485 request.focusedToken = focusedWindow->getToken();
486 }
487 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
488 request.displayId = window->getInfo()->displayId;
489 mDispatcher->setFocusedWindow(request);
490 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800491};
492
Michael Wrightd02c5b62014-02-10 15:10:22 -0800493TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
494 KeyEvent event;
495
496 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800497 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
498 INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600499 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
500 ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800501 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700502 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800503 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800504 << "Should reject key events with undefined action.";
505
506 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800507 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
508 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600509 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800510 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700511 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800512 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800513 << "Should reject key events with ACTION_MULTIPLE.";
514}
515
516TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
517 MotionEvent event;
518 PointerProperties pointerProperties[MAX_POINTERS + 1];
519 PointerCoords pointerCoords[MAX_POINTERS + 1];
520 for (int i = 0; i <= MAX_POINTERS; i++) {
521 pointerProperties[i].clear();
522 pointerProperties[i].id = i;
523 pointerCoords[i].clear();
524 }
525
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800526 // Some constants commonly used below
527 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
528 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
529 constexpr int32_t metaState = AMETA_NONE;
530 constexpr MotionClassification classification = MotionClassification::NONE;
531
chaviw9eaa22c2020-07-01 16:21:27 -0700532 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800533 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800534 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
chaviw9eaa22c2020-07-01 16:21:27 -0700535 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification,
536 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700537 AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0,
538 INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700539 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800540 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700541 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800542 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800543 << "Should reject motion events with undefined action.";
544
545 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800546 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700547 AMOTION_EVENT_ACTION_POINTER_DOWN |
548 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700549 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
550 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700551 ui::Transform::ROT_0, INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE,
552 ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500553 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800554 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700555 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800556 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800557 << "Should reject motion events with pointer down index too large.";
558
Garfield Tanfbe732e2020-01-24 11:26:14 -0800559 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700560 AMOTION_EVENT_ACTION_POINTER_DOWN |
561 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700562 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
563 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700564 ui::Transform::ROT_0, INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE,
565 ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500566 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800567 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700568 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800569 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800570 << "Should reject motion events with pointer down index too small.";
571
572 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800573 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700574 AMOTION_EVENT_ACTION_POINTER_UP |
575 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700576 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
577 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700578 ui::Transform::ROT_0, INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE,
579 ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500580 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800581 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700582 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800583 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800584 << "Should reject motion events with pointer up index too large.";
585
Garfield Tanfbe732e2020-01-24 11:26:14 -0800586 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700587 AMOTION_EVENT_ACTION_POINTER_UP |
588 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700589 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
590 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700591 ui::Transform::ROT_0, INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE,
592 ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500593 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800594 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700595 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800596 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800597 << "Should reject motion events with pointer up index too small.";
598
599 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800600 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
601 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700602 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700603 AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0,
604 INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700605 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800606 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700607 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800608 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800609 << "Should reject motion events with 0 pointers.";
610
Garfield Tanfbe732e2020-01-24 11:26:14 -0800611 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
612 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700613 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700614 AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0,
615 INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700616 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800617 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700618 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800619 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800620 << "Should reject motion events with more than MAX_POINTERS pointers.";
621
622 // Rejects motion events with invalid pointer ids.
623 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800624 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
625 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700626 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700627 AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0,
628 INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700629 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800630 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700631 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800632 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800633 << "Should reject motion events with pointer ids less than 0.";
634
635 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800636 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
637 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700638 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700639 AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0,
640 INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700641 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800642 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700643 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800644 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800645 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
646
647 // Rejects motion events with duplicate pointer ids.
648 pointerProperties[0].id = 1;
649 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800650 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
651 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700652 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700653 AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0,
654 INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700655 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800656 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700657 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800658 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800659 << "Should reject motion events with duplicate pointer ids.";
660}
661
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800662/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
663
664TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
665 constexpr nsecs_t eventTime = 20;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800666 NotifyConfigurationChangedArgs args(10 /*id*/, eventTime);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800667 mDispatcher->notifyConfigurationChanged(&args);
668 ASSERT_TRUE(mDispatcher->waitForIdle());
669
670 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
671}
672
673TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800674 NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/,
675 2 /*switchMask*/);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800676 mDispatcher->notifySwitch(&args);
677
678 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
679 args.policyFlags |= POLICY_FLAG_TRUSTED;
680 mFakePolicy->assertNotifySwitchWasCalled(args);
681}
682
Arthur Hungb92218b2018-08-14 12:00:21 +0800683// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700684static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700685static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s;
Arthur Hungb92218b2018-08-14 12:00:21 +0800686
687class FakeApplicationHandle : public InputApplicationHandle {
688public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700689 FakeApplicationHandle() {
690 mInfo.name = "Fake Application";
691 mInfo.token = new BBinder();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500692 mInfo.dispatchingTimeoutMillis =
693 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700694 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800695 virtual ~FakeApplicationHandle() {}
696
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000697 virtual bool updateInfo() override { return true; }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700698
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500699 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
700 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700701 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800702};
703
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800704class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800705public:
Garfield Tan15601662020-09-22 15:32:38 -0700706 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800707 : mName(name) {
Garfield Tan15601662020-09-22 15:32:38 -0700708 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800709 }
710
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800711 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700712 InputEvent* event;
713 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
714 if (!consumeSeq) {
715 return nullptr;
716 }
717 finishEvent(*consumeSeq);
718 return event;
719 }
720
721 /**
722 * Receive an event without acknowledging it.
723 * Return the sequence number that could later be used to send finished signal.
724 */
725 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800726 uint32_t consumeSeq;
727 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800728
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800729 std::chrono::time_point start = std::chrono::steady_clock::now();
730 status_t status = WOULD_BLOCK;
731 while (status == WOULD_BLOCK) {
chaviw81e2bb92019-12-18 15:03:51 -0800732 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800733 &event);
734 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
735 if (elapsed > 100ms) {
736 break;
737 }
738 }
739
740 if (status == WOULD_BLOCK) {
741 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700742 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800743 }
744
745 if (status != OK) {
746 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700747 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800748 }
749 if (event == nullptr) {
750 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700751 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800752 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700753 if (outEvent != nullptr) {
754 *outEvent = event;
755 }
756 return consumeSeq;
757 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800758
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700759 /**
760 * To be used together with "receiveEvent" to complete the consumption of an event.
761 */
762 void finishEvent(uint32_t consumeSeq) {
763 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
764 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800765 }
766
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000767 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
768 const status_t status = mConsumer->sendTimeline(inputEventId, timeline);
769 ASSERT_EQ(OK, status);
770 }
771
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000772 void consumeEvent(int32_t expectedEventType, int32_t expectedAction,
773 std::optional<int32_t> expectedDisplayId,
774 std::optional<int32_t> expectedFlags) {
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800775 InputEvent* event = consume();
776
777 ASSERT_NE(nullptr, event) << mName.c_str()
778 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800779 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700780 << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType)
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800781 << " event, got " << inputEventTypeToString(event->getType()) << " event";
Arthur Hungb92218b2018-08-14 12:00:21 +0800782
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000783 if (expectedDisplayId.has_value()) {
784 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
785 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800786
Tiger Huang8664f8c2018-10-11 19:14:35 +0800787 switch (expectedEventType) {
788 case AINPUT_EVENT_TYPE_KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800789 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
790 EXPECT_EQ(expectedAction, keyEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000791 if (expectedFlags.has_value()) {
792 EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags());
793 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800794 break;
795 }
796 case AINPUT_EVENT_TYPE_MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800797 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
798 EXPECT_EQ(expectedAction, motionEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000799 if (expectedFlags.has_value()) {
800 EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags());
801 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800802 break;
803 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100804 case AINPUT_EVENT_TYPE_FOCUS: {
805 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
806 }
Prabir Pradhan99987712020-11-10 18:43:05 -0800807 case AINPUT_EVENT_TYPE_CAPTURE: {
808 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
809 }
arthurhungb89ccb02020-12-30 16:19:01 +0800810 case AINPUT_EVENT_TYPE_DRAG: {
811 FAIL() << "Use 'consumeDragEvent' for DRAG events";
812 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800813 default: {
814 FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType;
815 }
816 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800817 }
818
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100819 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
820 InputEvent* event = consume();
821 ASSERT_NE(nullptr, event) << mName.c_str()
822 << ": consumer should have returned non-NULL event.";
823 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
824 << "Got " << inputEventTypeToString(event->getType())
825 << " event instead of FOCUS event";
826
827 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
828 << mName.c_str() << ": event displayId should always be NONE.";
829
830 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
831 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
832 EXPECT_EQ(inTouchMode, focusEvent->getInTouchMode());
833 }
834
Prabir Pradhan99987712020-11-10 18:43:05 -0800835 void consumeCaptureEvent(bool hasCapture) {
836 const InputEvent* event = consume();
837 ASSERT_NE(nullptr, event) << mName.c_str()
838 << ": consumer should have returned non-NULL event.";
839 ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType())
840 << "Got " << inputEventTypeToString(event->getType())
841 << " event instead of CAPTURE event";
842
843 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
844 << mName.c_str() << ": event displayId should always be NONE.";
845
846 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
847 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
848 }
849
arthurhungb89ccb02020-12-30 16:19:01 +0800850 void consumeDragEvent(bool isExiting, float x, float y) {
851 const InputEvent* event = consume();
852 ASSERT_NE(nullptr, event) << mName.c_str()
853 << ": consumer should have returned non-NULL event.";
854 ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType())
855 << "Got " << inputEventTypeToString(event->getType())
856 << " event instead of DRAG event";
857
858 EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
859 << mName.c_str() << ": event displayId should always be NONE.";
860
861 const auto& dragEvent = static_cast<const DragEvent&>(*event);
862 EXPECT_EQ(isExiting, dragEvent.isExiting());
863 EXPECT_EQ(x, dragEvent.getX());
864 EXPECT_EQ(y, dragEvent.getY());
865 }
866
chaviwd1c23182019-12-20 18:44:56 -0800867 void assertNoEvents() {
868 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700869 if (event == nullptr) {
870 return;
871 }
872 if (event->getType() == AINPUT_EVENT_TYPE_KEY) {
873 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
874 ADD_FAILURE() << "Received key event "
875 << KeyEvent::actionToString(keyEvent.getAction());
876 } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) {
877 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
878 ADD_FAILURE() << "Received motion event "
879 << MotionEvent::actionToString(motionEvent.getAction());
880 } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) {
881 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
882 ADD_FAILURE() << "Received focus event, hasFocus = "
883 << (focusEvent.getHasFocus() ? "true" : "false");
Prabir Pradhan99987712020-11-10 18:43:05 -0800884 } else if (event->getType() == AINPUT_EVENT_TYPE_CAPTURE) {
885 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
886 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
887 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700888 }
889 FAIL() << mName.c_str()
890 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -0800891 }
892
893 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
894
895protected:
896 std::unique_ptr<InputConsumer> mConsumer;
897 PreallocatedInputEventFactory mEventFactory;
898
899 std::string mName;
900};
901
chaviw3277faf2021-05-19 16:45:23 -0500902class FakeWindowHandle : public WindowInfoHandle {
chaviwd1c23182019-12-20 18:44:56 -0800903public:
904 static const int32_t WIDTH = 600;
905 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -0800906
Chris Yea209fde2020-07-22 13:54:51 -0700907 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
chaviwd1c23182019-12-20 18:44:56 -0800908 const sp<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500909 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -0800910 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500911 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -0700912 base::Result<std::unique_ptr<InputChannel>> channel =
913 dispatcher->createInputChannel(name);
914 token = (*channel)->getConnectionToken();
915 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -0800916 }
917
918 inputApplicationHandle->updateInfo();
919 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
920
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500921 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700922 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -0800923 mInfo.name = name;
chaviw3277faf2021-05-19 16:45:23 -0500924 mInfo.type = WindowInfo::Type::APPLICATION;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500925 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000926 mInfo.alpha = 1.0;
chaviwd1c23182019-12-20 18:44:56 -0800927 mInfo.frameLeft = 0;
928 mInfo.frameTop = 0;
929 mInfo.frameRight = WIDTH;
930 mInfo.frameBottom = HEIGHT;
chaviw1ff3d1e2020-07-01 15:53:47 -0700931 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -0800932 mInfo.globalScaleFactor = 1.0;
933 mInfo.touchableRegion.clear();
934 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
935 mInfo.visible = true;
Vishnu Nair47074b82020-08-14 11:54:47 -0700936 mInfo.focusable = false;
chaviwd1c23182019-12-20 18:44:56 -0800937 mInfo.hasWallpaper = false;
938 mInfo.paused = false;
chaviwd1c23182019-12-20 18:44:56 -0800939 mInfo.ownerPid = INJECTOR_PID;
940 mInfo.ownerUid = INJECTOR_UID;
chaviwd1c23182019-12-20 18:44:56 -0800941 mInfo.displayId = displayId;
942 }
943
Vishnu Nair47074b82020-08-14 11:54:47 -0700944 void setFocusable(bool focusable) { mInfo.focusable = focusable; }
chaviwd1c23182019-12-20 18:44:56 -0800945
Vishnu Nair958da932020-08-21 17:12:37 -0700946 void setVisible(bool visible) { mInfo.visible = visible; }
947
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700948 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500949 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700950 }
951
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700952 void setPaused(bool paused) { mInfo.paused = paused; }
953
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000954 void setAlpha(float alpha) { mInfo.alpha = alpha; }
955
chaviw3277faf2021-05-19 16:45:23 -0500956 void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; }
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000957
Bernardo Rufino7393d172021-02-26 13:56:11 +0000958 void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
959
chaviwd1c23182019-12-20 18:44:56 -0800960 void setFrame(const Rect& frame) {
961 mInfo.frameLeft = frame.left;
962 mInfo.frameTop = frame.top;
963 mInfo.frameRight = frame.right;
964 mInfo.frameBottom = frame.bottom;
arthurhungb89ccb02020-12-30 16:19:01 +0800965 mInfo.transform.set(-frame.left, -frame.top);
chaviwd1c23182019-12-20 18:44:56 -0800966 mInfo.touchableRegion.clear();
967 mInfo.addTouchableRegion(frame);
968 }
969
chaviw3277faf2021-05-19 16:45:23 -0500970 void addFlags(Flags<WindowInfo::Flag> flags) { mInfo.flags |= flags; }
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +0000971
chaviw3277faf2021-05-19 16:45:23 -0500972 void setFlags(Flags<WindowInfo::Flag> flags) { mInfo.flags = flags; }
chaviwd1c23182019-12-20 18:44:56 -0800973
chaviw3277faf2021-05-19 16:45:23 -0500974 void setInputFeatures(WindowInfo::Feature features) { mInfo.inputFeatures = features; }
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500975
chaviw9eaa22c2020-07-01 16:21:27 -0700976 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
977 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
978 }
979
980 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -0700981
yunho.shinf4a80b82020-11-16 21:13:57 +0900982 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
983
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800984 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
985 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
986 expectedFlags);
987 }
988
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700989 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
990 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
991 }
992
Svet Ganov5d3bc372020-01-26 23:11:07 -0800993 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000994 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800995 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId,
996 expectedFlags);
997 }
998
999 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001000 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -08001001 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId,
1002 expectedFlags);
1003 }
1004
1005 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001006 int32_t expectedFlags = 0) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001007 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
1008 }
1009
1010 void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
1011 std::optional<int32_t> expectedFlags = std::nullopt) {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001012 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
1013 expectedFlags);
1014 }
1015
Svet Ganov5d3bc372020-01-26 23:11:07 -08001016 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001017 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1018 int32_t expectedFlags = 0) {
1019 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
1020 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001021 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
1022 }
1023
1024 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001025 int32_t expectedFlags = 0) {
1026 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
1027 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001028 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
1029 }
1030
1031 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001032 int32_t expectedFlags = 0) {
Michael Wright3a240c42019-12-10 20:53:41 +00001033 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
1034 expectedFlags);
1035 }
1036
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001037 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1038 int32_t expectedFlags = 0) {
1039 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
1040 expectedFlags);
1041 }
1042
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001043 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
1044 ASSERT_NE(mInputReceiver, nullptr)
1045 << "Cannot consume events from a window with no receiver";
1046 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
1047 }
1048
Prabir Pradhan99987712020-11-10 18:43:05 -08001049 void consumeCaptureEvent(bool hasCapture) {
1050 ASSERT_NE(mInputReceiver, nullptr)
1051 << "Cannot consume events from a window with no receiver";
1052 mInputReceiver->consumeCaptureEvent(hasCapture);
1053 }
1054
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001055 void consumeEvent(int32_t expectedEventType, int32_t expectedAction,
1056 std::optional<int32_t> expectedDisplayId,
1057 std::optional<int32_t> expectedFlags) {
chaviwd1c23182019-12-20 18:44:56 -08001058 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
1059 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
1060 expectedFlags);
1061 }
1062
arthurhungb89ccb02020-12-30 16:19:01 +08001063 void consumeDragEvent(bool isExiting, float x, float y) {
1064 mInputReceiver->consumeDragEvent(isExiting, x, y);
1065 }
1066
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001067 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001068 if (mInputReceiver == nullptr) {
1069 ADD_FAILURE() << "Invalid receive event on window with no receiver";
1070 return std::nullopt;
1071 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001072 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001073 }
1074
1075 void finishEvent(uint32_t sequenceNum) {
1076 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1077 mInputReceiver->finishEvent(sequenceNum);
1078 }
1079
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001080 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
1081 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1082 mInputReceiver->sendTimeline(inputEventId, timeline);
1083 }
1084
chaviwaf87b3e2019-10-01 16:59:28 -07001085 InputEvent* consume() {
1086 if (mInputReceiver == nullptr) {
1087 return nullptr;
1088 }
1089 return mInputReceiver->consume();
1090 }
1091
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001092 MotionEvent* consumeMotion() {
1093 InputEvent* event = consume();
1094 if (event == nullptr) {
1095 ADD_FAILURE() << "Consume failed : no event";
1096 return nullptr;
1097 }
1098 if (event->getType() != AINPUT_EVENT_TYPE_MOTION) {
1099 ADD_FAILURE() << "Instead of motion event, got "
1100 << inputEventTypeToString(event->getType());
1101 return nullptr;
1102 }
1103 return static_cast<MotionEvent*>(event);
1104 }
1105
Arthur Hungb92218b2018-08-14 12:00:21 +08001106 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001107 if (mInputReceiver == nullptr &&
chaviw3277faf2021-05-19 16:45:23 -05001108 mInfo.inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL)) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001109 return; // Can't receive events if the window does not have input channel
1110 }
1111 ASSERT_NE(nullptr, mInputReceiver)
1112 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -08001113 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +08001114 }
1115
chaviwaf87b3e2019-10-01 16:59:28 -07001116 sp<IBinder> getToken() { return mInfo.token; }
1117
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001118 const std::string& getName() { return mName; }
1119
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001120 void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) {
1121 mInfo.ownerPid = ownerPid;
1122 mInfo.ownerUid = ownerUid;
1123 }
1124
chaviwd1c23182019-12-20 18:44:56 -08001125private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001126 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -08001127 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001128 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001129};
1130
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001131std::atomic<int32_t> FakeWindowHandle::sId{1};
1132
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001133static InputEventInjectionResult injectKey(
1134 const sp<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
1135 int32_t displayId = ADISPLAY_ID_NONE,
1136 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001137 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
1138 bool allowKeyRepeat = true) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001139 KeyEvent event;
1140 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1141
1142 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001143 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001144 INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE,
1145 repeatCount, currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001146
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001147 int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
1148 if (!allowKeyRepeat) {
1149 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
1150 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001151 // Inject event until dispatch out.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001152 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, syncMode,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001153 injectionTimeout, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001154}
1155
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001156static InputEventInjectionResult injectKeyDown(const sp<InputDispatcher>& dispatcher,
1157 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001158 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId);
1159}
1160
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001161// Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without
1162// sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it
1163// has to be woken up to process the repeating key.
1164static InputEventInjectionResult injectKeyDownNoRepeat(const sp<InputDispatcher>& dispatcher,
1165 int32_t displayId = ADISPLAY_ID_NONE) {
1166 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId,
1167 InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT,
1168 /* allowKeyRepeat */ false);
1169}
1170
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001171static InputEventInjectionResult injectKeyUp(const sp<InputDispatcher>& dispatcher,
1172 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001173 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId);
1174}
1175
Garfield Tandf26e862020-07-01 20:18:19 -07001176class PointerBuilder {
1177public:
1178 PointerBuilder(int32_t id, int32_t toolType) {
1179 mProperties.clear();
1180 mProperties.id = id;
1181 mProperties.toolType = toolType;
1182 mCoords.clear();
1183 }
1184
1185 PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); }
1186
1187 PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); }
1188
1189 PointerBuilder& axis(int32_t axis, float value) {
1190 mCoords.setAxisValue(axis, value);
1191 return *this;
1192 }
1193
1194 PointerProperties buildProperties() const { return mProperties; }
1195
1196 PointerCoords buildCoords() const { return mCoords; }
1197
1198private:
1199 PointerProperties mProperties;
1200 PointerCoords mCoords;
1201};
1202
1203class MotionEventBuilder {
1204public:
1205 MotionEventBuilder(int32_t action, int32_t source) {
1206 mAction = action;
1207 mSource = source;
1208 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1209 }
1210
1211 MotionEventBuilder& eventTime(nsecs_t eventTime) {
1212 mEventTime = eventTime;
1213 return *this;
1214 }
1215
1216 MotionEventBuilder& displayId(int32_t displayId) {
1217 mDisplayId = displayId;
1218 return *this;
1219 }
1220
1221 MotionEventBuilder& actionButton(int32_t actionButton) {
1222 mActionButton = actionButton;
1223 return *this;
1224 }
1225
arthurhung6d4bed92021-03-17 11:59:33 +08001226 MotionEventBuilder& buttonState(int32_t buttonState) {
1227 mButtonState = buttonState;
Garfield Tandf26e862020-07-01 20:18:19 -07001228 return *this;
1229 }
1230
1231 MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) {
1232 mRawXCursorPosition = rawXCursorPosition;
1233 return *this;
1234 }
1235
1236 MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) {
1237 mRawYCursorPosition = rawYCursorPosition;
1238 return *this;
1239 }
1240
1241 MotionEventBuilder& pointer(PointerBuilder pointer) {
1242 mPointers.push_back(pointer);
1243 return *this;
1244 }
1245
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001246 MotionEventBuilder& addFlag(uint32_t flags) {
1247 mFlags |= flags;
1248 return *this;
1249 }
1250
Garfield Tandf26e862020-07-01 20:18:19 -07001251 MotionEvent build() {
1252 std::vector<PointerProperties> pointerProperties;
1253 std::vector<PointerCoords> pointerCoords;
1254 for (const PointerBuilder& pointer : mPointers) {
1255 pointerProperties.push_back(pointer.buildProperties());
1256 pointerCoords.push_back(pointer.buildCoords());
1257 }
1258
1259 // Set mouse cursor position for the most common cases to avoid boilerplate.
1260 if (mSource == AINPUT_SOURCE_MOUSE &&
1261 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) &&
1262 mPointers.size() == 1) {
1263 mRawXCursorPosition = pointerCoords[0].getX();
1264 mRawYCursorPosition = pointerCoords[0].getY();
1265 }
1266
1267 MotionEvent event;
chaviw9eaa22c2020-07-01 16:21:27 -07001268 ui::Transform identityTransform;
Garfield Tandf26e862020-07-01 20:18:19 -07001269 event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001270 mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE,
chaviw9eaa22c2020-07-01 16:21:27 -07001271 mButtonState, MotionClassification::NONE, identityTransform,
1272 /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition,
Evan Rosky09576692021-07-01 12:22:09 -07001273 mRawYCursorPosition, mDisplayOrientation, mDisplayWidth, mDisplayHeight,
1274 mEventTime, mEventTime, mPointers.size(), pointerProperties.data(),
1275 pointerCoords.data());
Garfield Tandf26e862020-07-01 20:18:19 -07001276
1277 return event;
1278 }
1279
1280private:
1281 int32_t mAction;
1282 int32_t mSource;
1283 nsecs_t mEventTime;
1284 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1285 int32_t mActionButton{0};
1286 int32_t mButtonState{0};
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001287 int32_t mFlags{0};
Garfield Tandf26e862020-07-01 20:18:19 -07001288 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1289 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
Evan Rosky09576692021-07-01 12:22:09 -07001290 uint32_t mDisplayOrientation{ui::Transform::ROT_0};
chaviw3277faf2021-05-19 16:45:23 -05001291 int32_t mDisplayWidth{INVALID_DISPLAY_SIZE};
1292 int32_t mDisplayHeight{INVALID_DISPLAY_SIZE};
Garfield Tandf26e862020-07-01 20:18:19 -07001293
1294 std::vector<PointerBuilder> mPointers;
1295};
1296
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001297static InputEventInjectionResult injectMotionEvent(
Garfield Tandf26e862020-07-01 20:18:19 -07001298 const sp<InputDispatcher>& dispatcher, const MotionEvent& event,
1299 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001300 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT) {
Garfield Tandf26e862020-07-01 20:18:19 -07001301 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, injectionMode,
1302 injectionTimeout,
1303 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
1304}
1305
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001306static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001307 const sp<InputDispatcher>& dispatcher, int32_t action, int32_t source, int32_t displayId,
1308 const PointF& position,
1309 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001310 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1311 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001312 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001313 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC)) {
Garfield Tandf26e862020-07-01 20:18:19 -07001314 MotionEvent event = MotionEventBuilder(action, source)
1315 .displayId(displayId)
1316 .eventTime(eventTime)
1317 .rawXCursorPosition(cursorPosition.x)
1318 .rawYCursorPosition(cursorPosition.y)
1319 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1320 .x(position.x)
1321 .y(position.y))
1322 .build();
Arthur Hungb92218b2018-08-14 12:00:21 +08001323
1324 // Inject event until dispatch out.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001325 return injectMotionEvent(dispatcher, event, injectionTimeout, injectionMode);
Arthur Hungb92218b2018-08-14 12:00:21 +08001326}
1327
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001328static InputEventInjectionResult injectMotionDown(const sp<InputDispatcher>& dispatcher,
1329 int32_t source, int32_t displayId,
1330 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001331 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001332}
1333
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001334static InputEventInjectionResult injectMotionUp(const sp<InputDispatcher>& dispatcher,
1335 int32_t source, int32_t displayId,
1336 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001337 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001338}
1339
Jackal Guof9696682018-10-05 12:23:23 +08001340static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1341 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1342 // Define a valid key event.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001343 NotifyKeyArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1344 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A,
1345 KEY_A, AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001346
1347 return args;
1348}
1349
chaviwd1c23182019-12-20 18:44:56 -08001350static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId,
1351 const std::vector<PointF>& points) {
1352 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001353 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1354 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1355 }
1356
chaviwd1c23182019-12-20 18:44:56 -08001357 PointerProperties pointerProperties[pointerCount];
1358 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001359
chaviwd1c23182019-12-20 18:44:56 -08001360 for (size_t i = 0; i < pointerCount; i++) {
1361 pointerProperties[i].clear();
1362 pointerProperties[i].id = i;
1363 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001364
chaviwd1c23182019-12-20 18:44:56 -08001365 pointerCoords[i].clear();
1366 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1367 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1368 }
Jackal Guof9696682018-10-05 12:23:23 +08001369
1370 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1371 // Define a valid motion event.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001372 NotifyMotionArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001373 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1374 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001375 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1376 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001377 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1378 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001379
1380 return args;
1381}
1382
chaviwd1c23182019-12-20 18:44:56 -08001383static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1384 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1385}
1386
Prabir Pradhanac483a62021-08-06 14:01:18 +00001387static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(
1388 const PointerCaptureRequest& request) {
1389 return NotifyPointerCaptureChangedArgs(/* id */ 0, systemTime(SYSTEM_TIME_MONOTONIC), request);
Prabir Pradhan99987712020-11-10 18:43:05 -08001390}
1391
Arthur Hungb92218b2018-08-14 12:00:21 +08001392TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001393 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001394 sp<FakeWindowHandle> window =
1395 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001396
Arthur Hung72d8dc32020-03-28 00:48:39 +00001397 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001398 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1399 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1400 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001401
1402 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001403 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001404}
1405
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001406/**
1407 * Calling setInputWindows once with FLAG_NOT_TOUCH_MODAL should not cause any issues.
1408 * To ensure that window receives only events that were directly inside of it, add
1409 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1410 * when finding touched windows.
1411 * This test serves as a sanity check for the next test, where setInputWindows is
1412 * called twice.
1413 */
1414TEST_F(InputDispatcherTest, SetInputWindowOnce_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001415 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001416 sp<FakeWindowHandle> window =
1417 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1418 window->setFrame(Rect(0, 0, 100, 100));
chaviw3277faf2021-05-19 16:45:23 -05001419 window->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001420
1421 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001422 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001423 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1424 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001425 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001426
1427 // Window should receive motion event.
1428 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1429}
1430
1431/**
1432 * Calling setInputWindows twice, with the same info, should not cause any issues.
1433 * To ensure that window receives only events that were directly inside of it, add
1434 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1435 * when finding touched windows.
1436 */
1437TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001438 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001439 sp<FakeWindowHandle> window =
1440 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1441 window->setFrame(Rect(0, 0, 100, 100));
chaviw3277faf2021-05-19 16:45:23 -05001442 window->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001443
1444 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1445 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001446 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001447 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1448 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001449 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001450
1451 // Window should receive motion event.
1452 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1453}
1454
Arthur Hungb92218b2018-08-14 12:00:21 +08001455// The foreground window should receive the first touch down event.
1456TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001457 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001458 sp<FakeWindowHandle> windowTop =
1459 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
1460 sp<FakeWindowHandle> windowSecond =
1461 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001462
Arthur Hung72d8dc32020-03-28 00:48:39 +00001463 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001464 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1465 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1466 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001467
1468 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001469 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001470 windowSecond->assertNoEvents();
1471}
1472
Garfield Tandf26e862020-07-01 20:18:19 -07001473TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07001474 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07001475 sp<FakeWindowHandle> windowLeft =
1476 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1477 windowLeft->setFrame(Rect(0, 0, 600, 800));
chaviw3277faf2021-05-19 16:45:23 -05001478 windowLeft->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001479 sp<FakeWindowHandle> windowRight =
1480 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1481 windowRight->setFrame(Rect(600, 0, 1200, 800));
chaviw3277faf2021-05-19 16:45:23 -05001482 windowRight->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001483
1484 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1485
1486 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
1487
1488 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001489 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001490 injectMotionEvent(mDispatcher,
1491 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1492 AINPUT_SOURCE_MOUSE)
1493 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1494 .x(900)
1495 .y(400))
1496 .build()));
1497 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1498 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1499 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1500 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1501
1502 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001503 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001504 injectMotionEvent(mDispatcher,
1505 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1506 AINPUT_SOURCE_MOUSE)
1507 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1508 .x(300)
1509 .y(400))
1510 .build()));
1511 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1512 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1513 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1514 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1515 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1516 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1517
1518 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001519 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001520 injectMotionEvent(mDispatcher,
1521 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1522 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1523 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1524 .x(300)
1525 .y(400))
1526 .build()));
1527 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1528
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_BUTTON_PRESS,
1532 AINPUT_SOURCE_MOUSE)
1533 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1534 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1535 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1536 .x(300)
1537 .y(400))
1538 .build()));
1539 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1540 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1541
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001542 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001543 injectMotionEvent(mDispatcher,
1544 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1545 AINPUT_SOURCE_MOUSE)
1546 .buttonState(0)
1547 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1548 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1549 .x(300)
1550 .y(400))
1551 .build()));
1552 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1553 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1554
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001555 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001556 injectMotionEvent(mDispatcher,
1557 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
1558 .buttonState(0)
1559 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1560 .x(300)
1561 .y(400))
1562 .build()));
1563 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1564
1565 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001566 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001567 injectMotionEvent(mDispatcher,
1568 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1569 AINPUT_SOURCE_MOUSE)
1570 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1571 .x(900)
1572 .y(400))
1573 .build()));
1574 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1575 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1576 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1577 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1578 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1579 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1580}
1581
1582// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
1583// directly in this test.
1584TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07001585 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07001586 sp<FakeWindowHandle> window =
1587 new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1588 window->setFrame(Rect(0, 0, 1200, 800));
chaviw3277faf2021-05-19 16:45:23 -05001589 window->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001590
1591 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1592
1593 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1594
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001595 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001596 injectMotionEvent(mDispatcher,
1597 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
1598 AINPUT_SOURCE_MOUSE)
1599 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1600 .x(300)
1601 .y(400))
1602 .build()));
1603 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1604 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1605
1606 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001607 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001608 injectMotionEvent(mDispatcher,
1609 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1610 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1611 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1612 .x(300)
1613 .y(400))
1614 .build()));
1615 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1616
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_BUTTON_PRESS,
1620 AINPUT_SOURCE_MOUSE)
1621 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1622 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1623 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1624 .x(300)
1625 .y(400))
1626 .build()));
1627 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1628 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1629
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001630 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001631 injectMotionEvent(mDispatcher,
1632 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1633 AINPUT_SOURCE_MOUSE)
1634 .buttonState(0)
1635 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1636 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1637 .x(300)
1638 .y(400))
1639 .build()));
1640 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1641 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1642
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001643 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001644 injectMotionEvent(mDispatcher,
1645 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
1646 .buttonState(0)
1647 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1648 .x(300)
1649 .y(400))
1650 .build()));
1651 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
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_HOVER_EXIT,
1656 AINPUT_SOURCE_MOUSE)
1657 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1658 .x(300)
1659 .y(400))
1660 .build()));
1661 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1662 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1663}
1664
Garfield Tan00f511d2019-06-12 16:55:40 -07001665TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07001666 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07001667
1668 sp<FakeWindowHandle> windowLeft =
1669 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1670 windowLeft->setFrame(Rect(0, 0, 600, 800));
chaviw3277faf2021-05-19 16:45:23 -05001671 windowLeft->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07001672 sp<FakeWindowHandle> windowRight =
1673 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1674 windowRight->setFrame(Rect(600, 0, 1200, 800));
chaviw3277faf2021-05-19 16:45:23 -05001675 windowRight->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07001676
1677 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1678
Arthur Hung72d8dc32020-03-28 00:48:39 +00001679 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07001680
1681 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
1682 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001683 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07001684 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001685 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001686 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07001687 windowRight->assertNoEvents();
1688}
1689
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001690TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001691 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001692 sp<FakeWindowHandle> window =
1693 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07001694 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001695
Arthur Hung72d8dc32020-03-28 00:48:39 +00001696 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001697 setFocusedWindow(window);
1698
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001699 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001700
1701 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1702 mDispatcher->notifyKey(&keyArgs);
1703
1704 // Window should receive key down event.
1705 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1706
1707 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
1708 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001709 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001710 mDispatcher->notifyDeviceReset(&args);
1711 window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
1712 AKEY_EVENT_FLAG_CANCELED);
1713}
1714
1715TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001716 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001717 sp<FakeWindowHandle> window =
1718 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1719
Arthur Hung72d8dc32020-03-28 00:48:39 +00001720 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001721
1722 NotifyMotionArgs motionArgs =
1723 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1724 ADISPLAY_ID_DEFAULT);
1725 mDispatcher->notifyMotion(&motionArgs);
1726
1727 // Window should receive motion down event.
1728 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1729
1730 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
1731 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001732 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001733 mDispatcher->notifyDeviceReset(&args);
1734 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT,
1735 0 /*expectedFlags*/);
1736}
1737
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001738using TransferFunction =
1739 std::function<bool(sp<InputDispatcher> dispatcher, sp<IBinder>, sp<IBinder>)>;
1740
1741class TransferTouchFixture : public InputDispatcherTest,
1742 public ::testing::WithParamInterface<TransferFunction> {};
1743
1744TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07001745 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001746
1747 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001748 sp<FakeWindowHandle> firstWindow =
1749 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1750 sp<FakeWindowHandle> secondWindow =
1751 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001752
1753 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001754 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001755
1756 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001757 NotifyMotionArgs downMotionArgs =
1758 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1759 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001760 mDispatcher->notifyMotion(&downMotionArgs);
1761 // Only the first window should get the down event
1762 firstWindow->consumeMotionDown();
1763 secondWindow->assertNoEvents();
1764
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001765 // Transfer touch to the second window
1766 TransferFunction f = GetParam();
1767 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
1768 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001769 // The first window gets cancel and the second gets down
1770 firstWindow->consumeMotionCancel();
1771 secondWindow->consumeMotionDown();
1772
1773 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001774 NotifyMotionArgs upMotionArgs =
1775 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1776 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001777 mDispatcher->notifyMotion(&upMotionArgs);
1778 // The first window gets no events and the second gets up
1779 firstWindow->assertNoEvents();
1780 secondWindow->consumeMotionUp();
1781}
1782
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001783TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001784 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001785
1786 PointF touchPoint = {10, 10};
1787
1788 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001789 sp<FakeWindowHandle> firstWindow =
1790 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1791 sp<FakeWindowHandle> secondWindow =
1792 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001793
1794 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001795 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001796
1797 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001798 NotifyMotionArgs downMotionArgs =
1799 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1800 ADISPLAY_ID_DEFAULT, {touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001801 mDispatcher->notifyMotion(&downMotionArgs);
1802 // Only the first window should get the down event
1803 firstWindow->consumeMotionDown();
1804 secondWindow->assertNoEvents();
1805
1806 // Send pointer down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001807 NotifyMotionArgs pointerDownMotionArgs =
1808 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1809 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1810 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1811 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001812 mDispatcher->notifyMotion(&pointerDownMotionArgs);
1813 // Only the first window should get the pointer down event
1814 firstWindow->consumeMotionPointerDown(1);
1815 secondWindow->assertNoEvents();
1816
1817 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001818 TransferFunction f = GetParam();
1819 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
1820 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001821 // The first window gets cancel and the second gets down and pointer down
1822 firstWindow->consumeMotionCancel();
1823 secondWindow->consumeMotionDown();
1824 secondWindow->consumeMotionPointerDown(1);
1825
1826 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001827 NotifyMotionArgs pointerUpMotionArgs =
1828 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1829 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1830 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1831 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001832 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1833 // The first window gets nothing and the second gets pointer up
1834 firstWindow->assertNoEvents();
1835 secondWindow->consumeMotionPointerUp(1);
1836
1837 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001838 NotifyMotionArgs upMotionArgs =
1839 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1840 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001841 mDispatcher->notifyMotion(&upMotionArgs);
1842 // The first window gets nothing and the second gets up
1843 firstWindow->assertNoEvents();
1844 secondWindow->consumeMotionUp();
1845}
1846
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001847// For the cases of single pointer touch and two pointers non-split touch, the api's
1848// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
1849// for the case where there are multiple pointers split across several windows.
1850INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
1851 ::testing::Values(
1852 [&](sp<InputDispatcher> dispatcher, sp<IBinder> /*ignored*/,
1853 sp<IBinder> destChannelToken) {
1854 return dispatcher->transferTouch(destChannelToken);
1855 },
1856 [&](sp<InputDispatcher> dispatcher, sp<IBinder> from,
1857 sp<IBinder> to) {
1858 return dispatcher->transferTouchFocus(from, to,
1859 false /*isDragAndDrop*/);
1860 }));
1861
Svet Ganov5d3bc372020-01-26 23:11:07 -08001862TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001863 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001864
1865 // Create a non touch modal window that supports split touch
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001866 sp<FakeWindowHandle> firstWindow =
1867 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001868 firstWindow->setFrame(Rect(0, 0, 600, 400));
chaviw3277faf2021-05-19 16:45:23 -05001869 firstWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001870
1871 // Create a non touch modal window that supports split touch
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001872 sp<FakeWindowHandle> secondWindow =
1873 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001874 secondWindow->setFrame(Rect(0, 400, 600, 800));
chaviw3277faf2021-05-19 16:45:23 -05001875 secondWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001876
1877 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001878 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001879
1880 PointF pointInFirst = {300, 200};
1881 PointF pointInSecond = {300, 600};
1882
1883 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001884 NotifyMotionArgs firstDownMotionArgs =
1885 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1886 ADISPLAY_ID_DEFAULT, {pointInFirst});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001887 mDispatcher->notifyMotion(&firstDownMotionArgs);
1888 // Only the first window should get the down event
1889 firstWindow->consumeMotionDown();
1890 secondWindow->assertNoEvents();
1891
1892 // Send down to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001893 NotifyMotionArgs secondDownMotionArgs =
1894 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1895 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1896 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1897 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001898 mDispatcher->notifyMotion(&secondDownMotionArgs);
1899 // The first window gets a move and the second a down
1900 firstWindow->consumeMotionMove();
1901 secondWindow->consumeMotionDown();
1902
1903 // Transfer touch focus to the second window
1904 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1905 // The first window gets cancel and the new gets pointer down (it already saw down)
1906 firstWindow->consumeMotionCancel();
1907 secondWindow->consumeMotionPointerDown(1);
1908
1909 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001910 NotifyMotionArgs pointerUpMotionArgs =
1911 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1912 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1913 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1914 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001915 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1916 // The first window gets nothing and the second gets pointer up
1917 firstWindow->assertNoEvents();
1918 secondWindow->consumeMotionPointerUp(1);
1919
1920 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001921 NotifyMotionArgs upMotionArgs =
1922 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1923 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001924 mDispatcher->notifyMotion(&upMotionArgs);
1925 // The first window gets nothing and the second gets up
1926 firstWindow->assertNoEvents();
1927 secondWindow->consumeMotionUp();
1928}
1929
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001930// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
1931// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
1932// touch is not supported, so the touch should continue on those windows and the transferred-to
1933// window should get nothing.
1934TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
1935 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1936
1937 // Create a non touch modal window that supports split touch
1938 sp<FakeWindowHandle> firstWindow =
1939 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1940 firstWindow->setFrame(Rect(0, 0, 600, 400));
chaviw3277faf2021-05-19 16:45:23 -05001941 firstWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001942
1943 // Create a non touch modal window that supports split touch
1944 sp<FakeWindowHandle> secondWindow =
1945 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
1946 secondWindow->setFrame(Rect(0, 400, 600, 800));
chaviw3277faf2021-05-19 16:45:23 -05001947 secondWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001948
1949 // Add the windows to the dispatcher
1950 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
1951
1952 PointF pointInFirst = {300, 200};
1953 PointF pointInSecond = {300, 600};
1954
1955 // Send down to the first window
1956 NotifyMotionArgs firstDownMotionArgs =
1957 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1958 ADISPLAY_ID_DEFAULT, {pointInFirst});
1959 mDispatcher->notifyMotion(&firstDownMotionArgs);
1960 // Only the first window should get the down event
1961 firstWindow->consumeMotionDown();
1962 secondWindow->assertNoEvents();
1963
1964 // Send down to the second window
1965 NotifyMotionArgs secondDownMotionArgs =
1966 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1967 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1968 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1969 {pointInFirst, pointInSecond});
1970 mDispatcher->notifyMotion(&secondDownMotionArgs);
1971 // The first window gets a move and the second a down
1972 firstWindow->consumeMotionMove();
1973 secondWindow->consumeMotionDown();
1974
1975 // Transfer touch focus to the second window
1976 const bool transferred = mDispatcher->transferTouch(secondWindow->getToken());
1977 // The 'transferTouch' call should not succeed, because there are 2 touched windows
1978 ASSERT_FALSE(transferred);
1979 firstWindow->assertNoEvents();
1980 secondWindow->assertNoEvents();
1981
1982 // The rest of the dispatch should proceed as normal
1983 // Send pointer up to the second window
1984 NotifyMotionArgs pointerUpMotionArgs =
1985 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1986 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1987 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1988 {pointInFirst, pointInSecond});
1989 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1990 // The first window gets MOVE and the second gets pointer up
1991 firstWindow->consumeMotionMove();
1992 secondWindow->consumeMotionUp();
1993
1994 // Send up event to the first window
1995 NotifyMotionArgs upMotionArgs =
1996 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1997 ADISPLAY_ID_DEFAULT);
1998 mDispatcher->notifyMotion(&upMotionArgs);
1999 // The first window gets nothing and the second gets up
2000 firstWindow->consumeMotionUp();
2001 secondWindow->assertNoEvents();
2002}
2003
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002004TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002005 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002006 sp<FakeWindowHandle> window =
2007 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2008
Vishnu Nair47074b82020-08-14 11:54:47 -07002009 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002010 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002011 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002012
2013 window->consumeFocusEvent(true);
2014
2015 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2016 mDispatcher->notifyKey(&keyArgs);
2017
2018 // Window should receive key down event.
2019 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2020}
2021
2022TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002023 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002024 sp<FakeWindowHandle> window =
2025 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2026
Arthur Hung72d8dc32020-03-28 00:48:39 +00002027 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002028
2029 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2030 mDispatcher->notifyKey(&keyArgs);
2031 mDispatcher->waitForIdle();
2032
2033 window->assertNoEvents();
2034}
2035
2036// If a window is touchable, but does not have focus, it should receive motion events, but not keys
2037TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07002038 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002039 sp<FakeWindowHandle> window =
2040 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2041
Arthur Hung72d8dc32020-03-28 00:48:39 +00002042 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002043
2044 // Send key
2045 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2046 mDispatcher->notifyKey(&keyArgs);
2047 // Send motion
2048 NotifyMotionArgs motionArgs =
2049 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2050 ADISPLAY_ID_DEFAULT);
2051 mDispatcher->notifyMotion(&motionArgs);
2052
2053 // Window should receive only the motion event
2054 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2055 window->assertNoEvents(); // Key event or focus event will not be received
2056}
2057
arthurhungea3f4fc2020-12-21 23:18:53 +08002058TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
2059 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2060
2061 // Create first non touch modal window that supports split touch
2062 sp<FakeWindowHandle> firstWindow =
2063 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
2064 firstWindow->setFrame(Rect(0, 0, 600, 400));
chaviw3277faf2021-05-19 16:45:23 -05002065 firstWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
arthurhungea3f4fc2020-12-21 23:18:53 +08002066
2067 // Create second non touch modal window that supports split touch
2068 sp<FakeWindowHandle> secondWindow =
2069 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
2070 secondWindow->setFrame(Rect(0, 400, 600, 800));
chaviw3277faf2021-05-19 16:45:23 -05002071 secondWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
arthurhungea3f4fc2020-12-21 23:18:53 +08002072
2073 // Add the windows to the dispatcher
2074 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
2075
2076 PointF pointInFirst = {300, 200};
2077 PointF pointInSecond = {300, 600};
2078
2079 // Send down to the first window
2080 NotifyMotionArgs firstDownMotionArgs =
2081 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2082 ADISPLAY_ID_DEFAULT, {pointInFirst});
2083 mDispatcher->notifyMotion(&firstDownMotionArgs);
2084 // Only the first window should get the down event
2085 firstWindow->consumeMotionDown();
2086 secondWindow->assertNoEvents();
2087
2088 // Send down to the second window
2089 NotifyMotionArgs secondDownMotionArgs =
2090 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
2091 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2092 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2093 {pointInFirst, pointInSecond});
2094 mDispatcher->notifyMotion(&secondDownMotionArgs);
2095 // The first window gets a move and the second a down
2096 firstWindow->consumeMotionMove();
2097 secondWindow->consumeMotionDown();
2098
2099 // Send pointer cancel to the second window
2100 NotifyMotionArgs pointerUpMotionArgs =
2101 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
2102 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2103 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2104 {pointInFirst, pointInSecond});
2105 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
2106 mDispatcher->notifyMotion(&pointerUpMotionArgs);
2107 // The first window gets move and the second gets cancel.
2108 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
2109 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
2110
2111 // Send up event.
2112 NotifyMotionArgs upMotionArgs =
2113 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2114 ADISPLAY_ID_DEFAULT);
2115 mDispatcher->notifyMotion(&upMotionArgs);
2116 // The first window gets up and the second gets nothing.
2117 firstWindow->consumeMotionUp();
2118 secondWindow->assertNoEvents();
2119}
2120
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00002121TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
2122 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2123
2124 sp<FakeWindowHandle> window =
2125 new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2126 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2127 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
2128 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
2129 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
2130
2131 window->sendTimeline(1 /*inputEventId*/, graphicsTimeline);
2132 window->assertNoEvents();
2133 mDispatcher->waitForIdle();
2134}
2135
chaviwd1c23182019-12-20 18:44:56 -08002136class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00002137public:
2138 FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name,
chaviwd1c23182019-12-20 18:44:56 -08002139 int32_t displayId, bool isGestureMonitor = false) {
Garfield Tan15601662020-09-22 15:32:38 -07002140 base::Result<std::unique_ptr<InputChannel>> channel =
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +00002141 dispatcher->createInputMonitor(displayId, isGestureMonitor, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07002142 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00002143 }
2144
chaviwd1c23182019-12-20 18:44:56 -08002145 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
2146
2147 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
2148 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN,
2149 expectedDisplayId, expectedFlags);
2150 }
2151
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002152 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
2153
2154 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
2155
chaviwd1c23182019-12-20 18:44:56 -08002156 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
2157 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
2158 expectedDisplayId, expectedFlags);
2159 }
2160
2161 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
2162 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP,
2163 expectedDisplayId, expectedFlags);
2164 }
2165
Evan Rosky84f07f02021-04-16 10:42:42 -07002166 MotionEvent* consumeMotion() {
2167 InputEvent* event = mInputReceiver->consume();
2168 if (!event) {
2169 ADD_FAILURE() << "No event was produced";
2170 return nullptr;
2171 }
2172 if (event->getType() != AINPUT_EVENT_TYPE_MOTION) {
2173 ADD_FAILURE() << "Received event of type " << event->getType() << " instead of motion";
2174 return nullptr;
2175 }
2176 return static_cast<MotionEvent*>(event);
2177 }
2178
chaviwd1c23182019-12-20 18:44:56 -08002179 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
2180
2181private:
2182 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00002183};
2184
2185// Tests for gesture monitors
2186TEST_F(InputDispatcherTest, GestureMonitor_ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07002187 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00002188 sp<FakeWindowHandle> window =
2189 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002190 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00002191
chaviwd1c23182019-12-20 18:44:56 -08002192 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
2193 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00002194
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002195 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00002196 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002197 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00002198 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002199 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00002200}
2201
2202TEST_F(InputDispatcherTest, GestureMonitor_DoesNotReceiveKeyEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07002203 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00002204 sp<FakeWindowHandle> window =
2205 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2206
2207 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002208 window->setFocusable(true);
Michael Wright3a240c42019-12-10 20:53:41 +00002209
Arthur Hung72d8dc32020-03-28 00:48:39 +00002210 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002211 setFocusedWindow(window);
2212
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002213 window->consumeFocusEvent(true);
Michael Wright3a240c42019-12-10 20:53:41 +00002214
chaviwd1c23182019-12-20 18:44:56 -08002215 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
2216 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00002217
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002218 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2219 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00002220 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002221 monitor.assertNoEvents();
Michael Wright3a240c42019-12-10 20:53:41 +00002222}
2223
2224TEST_F(InputDispatcherTest, GestureMonitor_CanPilferAfterWindowIsRemovedMidStream) {
Chris Yea209fde2020-07-22 13:54:51 -07002225 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00002226 sp<FakeWindowHandle> window =
2227 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002228 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00002229
chaviwd1c23182019-12-20 18:44:56 -08002230 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
2231 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00002232
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002233 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00002234 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002235 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00002236 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002237 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00002238
2239 window->releaseChannel();
2240
chaviwd1c23182019-12-20 18:44:56 -08002241 mDispatcher->pilferPointers(monitor.getToken());
Michael Wright3a240c42019-12-10 20:53:41 +00002242
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002243 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00002244 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002245 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08002246 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00002247}
2248
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002249TEST_F(InputDispatcherTest, UnresponsiveGestureMonitor_GetsAnr) {
2250 FakeMonitorReceiver monitor =
2251 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
2252 true /*isGestureMonitor*/);
2253
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002254 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002255 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
2256 std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
2257 ASSERT_TRUE(consumeSeq);
2258
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00002259 mFakePolicy->assertNotifyMonitorUnresponsiveWasCalled(DISPATCHING_TIMEOUT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002260 monitor.finishEvent(*consumeSeq);
2261 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00002262 mFakePolicy->assertNotifyMonitorResponsiveWasCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002263}
2264
Evan Rosky84f07f02021-04-16 10:42:42 -07002265// Tests for gesture monitors
2266TEST_F(InputDispatcherTest, GestureMonitor_NoWindowTransform) {
2267 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2268 sp<FakeWindowHandle> window =
2269 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2270 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2271 window->setWindowOffset(20, 40);
2272 window->setWindowTransform(0, 1, -1, 0);
2273
2274 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
2275 true /*isGestureMonitor*/);
2276
2277 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2278 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2279 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2280 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2281 MotionEvent* event = monitor.consumeMotion();
2282 // Even though window has transform, gesture monitor must not.
2283 ASSERT_EQ(ui::Transform(), event->getTransform());
2284}
2285
chaviw81e2bb92019-12-18 15:03:51 -08002286TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002287 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
chaviw81e2bb92019-12-18 15:03:51 -08002288 sp<FakeWindowHandle> window =
2289 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2290
Arthur Hung72d8dc32020-03-28 00:48:39 +00002291 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08002292
2293 NotifyMotionArgs motionArgs =
2294 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2295 ADISPLAY_ID_DEFAULT);
2296
2297 mDispatcher->notifyMotion(&motionArgs);
2298 // Window should receive motion down event.
2299 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2300
2301 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002302 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08002303 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
2304 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
2305 motionArgs.pointerCoords[0].getX() - 10);
2306
2307 mDispatcher->notifyMotion(&motionArgs);
2308 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
2309 0 /*expectedFlags*/);
2310}
2311
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002312/**
2313 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
2314 * the device default right away. In the test scenario, we check both the default value,
2315 * and the action of enabling / disabling.
2316 */
2317TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07002318 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002319 sp<FakeWindowHandle> window =
2320 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2321
2322 // Set focused application.
2323 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002324 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002325
2326 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00002327 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002328 setFocusedWindow(window);
2329
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002330 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2331
2332 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07002333 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002334 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002335 window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/);
2336
2337 SCOPED_TRACE("Disable touch mode");
2338 mDispatcher->setInTouchMode(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07002339 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002340 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002341 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002342 window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/);
2343
2344 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07002345 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002346 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002347 window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/);
2348
2349 SCOPED_TRACE("Enable touch mode again");
2350 mDispatcher->setInTouchMode(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07002351 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002352 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002353 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002354 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2355
2356 window->assertNoEvents();
2357}
2358
Gang Wange9087892020-01-07 12:17:14 -05002359TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002360 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Gang Wange9087892020-01-07 12:17:14 -05002361 sp<FakeWindowHandle> window =
2362 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2363
2364 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002365 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05002366
Arthur Hung72d8dc32020-03-28 00:48:39 +00002367 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002368 setFocusedWindow(window);
2369
Gang Wange9087892020-01-07 12:17:14 -05002370 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2371
2372 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
2373 mDispatcher->notifyKey(&keyArgs);
2374
2375 InputEvent* event = window->consume();
2376 ASSERT_NE(event, nullptr);
2377
2378 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
2379 ASSERT_NE(verified, nullptr);
2380 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
2381
2382 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
2383 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
2384 ASSERT_EQ(keyArgs.source, verified->source);
2385 ASSERT_EQ(keyArgs.displayId, verified->displayId);
2386
2387 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
2388
2389 ASSERT_EQ(keyArgs.action, verifiedKey.action);
2390 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05002391 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
2392 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
2393 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
2394 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
2395 ASSERT_EQ(0, verifiedKey.repeatCount);
2396}
2397
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002398TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002399 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002400 sp<FakeWindowHandle> window =
2401 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2402
2403 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2404
Arthur Hung72d8dc32020-03-28 00:48:39 +00002405 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002406
2407 NotifyMotionArgs motionArgs =
2408 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2409 ADISPLAY_ID_DEFAULT);
2410 mDispatcher->notifyMotion(&motionArgs);
2411
2412 InputEvent* event = window->consume();
2413 ASSERT_NE(event, nullptr);
2414
2415 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
2416 ASSERT_NE(verified, nullptr);
2417 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
2418
2419 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
2420 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
2421 EXPECT_EQ(motionArgs.source, verified->source);
2422 EXPECT_EQ(motionArgs.displayId, verified->displayId);
2423
2424 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
2425
2426 EXPECT_EQ(motionArgs.pointerCoords[0].getX(), verifiedMotion.rawX);
2427 EXPECT_EQ(motionArgs.pointerCoords[0].getY(), verifiedMotion.rawY);
2428 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
2429 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
2430 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
2431 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
2432 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
2433}
2434
Prabir Pradhan664834b2021-05-20 16:00:42 -07002435TEST_F(InputDispatcherTest, NonPointerMotionEvent_JoystickAndTouchpadNotTransformed) {
yunho.shinf4a80b82020-11-16 21:13:57 +09002436 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2437 sp<FakeWindowHandle> window =
2438 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2439 const std::string name = window->getName();
2440
2441 // Window gets transformed by offset values.
2442 window->setWindowOffset(500.0f, 500.0f);
2443
2444 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2445 window->setFocusable(true);
2446
2447 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2448
2449 // First, we set focused window so that focusedWindowHandle is not null.
2450 setFocusedWindow(window);
2451
2452 // Second, we consume focus event if it is right or wrong according to onFocusChangedLocked.
2453 window->consumeFocusEvent(true);
2454
Prabir Pradhan664834b2021-05-20 16:00:42 -07002455 constexpr const std::array nonTransformedSources = {std::pair(AINPUT_SOURCE_TOUCHPAD,
2456 AMOTION_EVENT_ACTION_DOWN),
2457 std::pair(AINPUT_SOURCE_JOYSTICK,
2458 AMOTION_EVENT_ACTION_MOVE)};
2459 for (const auto& [source, action] : nonTransformedSources) {
2460 const NotifyMotionArgs motionArgs = generateMotionArgs(action, source, ADISPLAY_ID_DEFAULT);
Prabir Pradhanbd527712021-03-09 19:17:09 -08002461 mDispatcher->notifyMotion(&motionArgs);
yunho.shinf4a80b82020-11-16 21:13:57 +09002462
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00002463 MotionEvent* event = window->consumeMotion();
Prabir Pradhanbd527712021-03-09 19:17:09 -08002464 ASSERT_NE(event, nullptr);
yunho.shinf4a80b82020-11-16 21:13:57 +09002465
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00002466 const MotionEvent& motionEvent = *event;
Prabir Pradhan664834b2021-05-20 16:00:42 -07002467 EXPECT_EQ(action, motionEvent.getAction());
Prabir Pradhanbd527712021-03-09 19:17:09 -08002468 EXPECT_EQ(motionArgs.pointerCount, motionEvent.getPointerCount());
yunho.shinf4a80b82020-11-16 21:13:57 +09002469
Prabir Pradhanbd527712021-03-09 19:17:09 -08002470 float expectedX = motionArgs.pointerCoords[0].getX();
2471 float expectedY = motionArgs.pointerCoords[0].getY();
yunho.shinf4a80b82020-11-16 21:13:57 +09002472
Prabir Pradhanbd527712021-03-09 19:17:09 -08002473 // Ensure the axis values from the final motion event are not transformed.
2474 EXPECT_EQ(expectedX, motionEvent.getX(0))
2475 << "expected " << expectedX << " for x coord of " << name.c_str() << ", got "
2476 << motionEvent.getX(0);
2477 EXPECT_EQ(expectedY, motionEvent.getY(0))
2478 << "expected " << expectedY << " for y coord of " << name.c_str() << ", got "
2479 << motionEvent.getY(0);
2480 // Ensure the raw and transformed axis values for the motion event are the same.
2481 EXPECT_EQ(motionEvent.getRawX(0), motionEvent.getX(0))
2482 << "expected raw and transformed X-axis values to be equal";
2483 EXPECT_EQ(motionEvent.getRawY(0), motionEvent.getY(0))
2484 << "expected raw and transformed Y-axis values to be equal";
2485 }
yunho.shinf4a80b82020-11-16 21:13:57 +09002486}
2487
chaviw09c8d2d2020-08-24 15:48:26 -07002488/**
2489 * Ensure that separate calls to sign the same data are generating the same key.
2490 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
2491 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
2492 * tests.
2493 */
2494TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
2495 KeyEvent event = getTestKeyEvent();
2496 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
2497
2498 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
2499 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
2500 ASSERT_EQ(hmac1, hmac2);
2501}
2502
2503/**
2504 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
2505 */
2506TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
2507 KeyEvent event = getTestKeyEvent();
2508 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
2509 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
2510
2511 verifiedEvent.deviceId += 1;
2512 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2513
2514 verifiedEvent.source += 1;
2515 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2516
2517 verifiedEvent.eventTimeNanos += 1;
2518 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2519
2520 verifiedEvent.displayId += 1;
2521 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2522
2523 verifiedEvent.action += 1;
2524 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2525
2526 verifiedEvent.downTimeNanos += 1;
2527 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2528
2529 verifiedEvent.flags += 1;
2530 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2531
2532 verifiedEvent.keyCode += 1;
2533 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2534
2535 verifiedEvent.scanCode += 1;
2536 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2537
2538 verifiedEvent.metaState += 1;
2539 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2540
2541 verifiedEvent.repeatCount += 1;
2542 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2543}
2544
Vishnu Nair958da932020-08-21 17:12:37 -07002545TEST_F(InputDispatcherTest, SetFocusedWindow) {
2546 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2547 sp<FakeWindowHandle> windowTop =
2548 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2549 sp<FakeWindowHandle> windowSecond =
2550 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2551 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2552
2553 // Top window is also focusable but is not granted focus.
2554 windowTop->setFocusable(true);
2555 windowSecond->setFocusable(true);
2556 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2557 setFocusedWindow(windowSecond);
2558
2559 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002560 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2561 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002562
2563 // Focused window should receive event.
2564 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
2565 windowTop->assertNoEvents();
2566}
2567
2568TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
2569 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2570 sp<FakeWindowHandle> window =
2571 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2572 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2573
2574 window->setFocusable(true);
2575 // Release channel for window is no longer valid.
2576 window->releaseChannel();
2577 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2578 setFocusedWindow(window);
2579
2580 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002581 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2582 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002583
2584 // window channel is invalid, so it should not receive any input event.
2585 window->assertNoEvents();
2586}
2587
2588TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
2589 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2590 sp<FakeWindowHandle> window =
2591 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2592 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2593
2594 // Window is not focusable.
2595 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2596 setFocusedWindow(window);
2597
2598 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002599 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2600 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002601
2602 // window is invalid, so it should not receive any input event.
2603 window->assertNoEvents();
2604}
2605
2606TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
2607 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2608 sp<FakeWindowHandle> windowTop =
2609 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2610 sp<FakeWindowHandle> windowSecond =
2611 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2612 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2613
2614 windowTop->setFocusable(true);
2615 windowSecond->setFocusable(true);
2616 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2617 setFocusedWindow(windowTop);
2618 windowTop->consumeFocusEvent(true);
2619
2620 setFocusedWindow(windowSecond, windowTop);
2621 windowSecond->consumeFocusEvent(true);
2622 windowTop->consumeFocusEvent(false);
2623
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002624 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2625 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002626
2627 // Focused window should receive event.
2628 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
2629}
2630
2631TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) {
2632 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2633 sp<FakeWindowHandle> windowTop =
2634 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2635 sp<FakeWindowHandle> windowSecond =
2636 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2637 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2638
2639 windowTop->setFocusable(true);
2640 windowSecond->setFocusable(true);
2641 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2642 setFocusedWindow(windowSecond, windowTop);
2643
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002644 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2645 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002646
2647 // Event should be dropped.
2648 windowTop->assertNoEvents();
2649 windowSecond->assertNoEvents();
2650}
2651
2652TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
2653 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2654 sp<FakeWindowHandle> window =
2655 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2656 sp<FakeWindowHandle> previousFocusedWindow =
2657 new FakeWindowHandle(application, mDispatcher, "previousFocusedWindow",
2658 ADISPLAY_ID_DEFAULT);
2659 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2660
2661 window->setFocusable(true);
2662 previousFocusedWindow->setFocusable(true);
2663 window->setVisible(false);
2664 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
2665 setFocusedWindow(previousFocusedWindow);
2666 previousFocusedWindow->consumeFocusEvent(true);
2667
2668 // Requesting focus on invisible window takes focus from currently focused window.
2669 setFocusedWindow(window);
2670 previousFocusedWindow->consumeFocusEvent(false);
2671
2672 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002673 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07002674 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002675 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07002676
2677 // Window does not get focus event or key down.
2678 window->assertNoEvents();
2679
2680 // Window becomes visible.
2681 window->setVisible(true);
2682 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2683
2684 // Window receives focus event.
2685 window->consumeFocusEvent(true);
2686 // Focused window receives key down.
2687 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2688}
2689
Vishnu Nair599f1412021-06-21 10:39:58 -07002690TEST_F(InputDispatcherTest, DisplayRemoved) {
2691 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2692 sp<FakeWindowHandle> window =
2693 new FakeWindowHandle(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
2694 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2695
2696 // window is granted focus.
2697 window->setFocusable(true);
2698 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2699 setFocusedWindow(window);
2700 window->consumeFocusEvent(true);
2701
2702 // When a display is removed window loses focus.
2703 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
2704 window->consumeFocusEvent(false);
2705}
2706
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002707/**
2708 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
2709 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
2710 * of the 'slipperyEnterWindow'.
2711 *
2712 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
2713 * a way so that the touched location is no longer covered by the top window.
2714 *
2715 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
2716 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
2717 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
2718 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
2719 * with ACTION_DOWN).
2720 * Thus, the touch has been transferred from the top window into the bottom window, because the top
2721 * window moved itself away from the touched location and had Flag::SLIPPERY.
2722 *
2723 * Even though the top window moved away from the touched location, it is still obscuring the bottom
2724 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
2725 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
2726 *
2727 * In this test, we ensure that the event received by the bottom window has
2728 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
2729 */
2730TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
2731 constexpr int32_t SLIPPERY_PID = INJECTOR_PID + 1;
2732 constexpr int32_t SLIPPERY_UID = INJECTOR_UID + 1;
2733
2734 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2735 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2736
2737 sp<FakeWindowHandle> slipperyExitWindow =
2738 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviw3277faf2021-05-19 16:45:23 -05002739 slipperyExitWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SLIPPERY);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002740 // Make sure this one overlaps the bottom window
2741 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
2742 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
2743 // one. Windows with the same owner are not considered to be occluding each other.
2744 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
2745
2746 sp<FakeWindowHandle> slipperyEnterWindow =
2747 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2748 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
2749
2750 mDispatcher->setInputWindows(
2751 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
2752
2753 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
2754 NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2755 ADISPLAY_ID_DEFAULT, {{50, 50}});
2756 mDispatcher->notifyMotion(&args);
2757 slipperyExitWindow->consumeMotionDown();
2758 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
2759 mDispatcher->setInputWindows(
2760 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
2761
2762 args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2763 ADISPLAY_ID_DEFAULT, {{51, 51}});
2764 mDispatcher->notifyMotion(&args);
2765
2766 slipperyExitWindow->consumeMotionCancel();
2767
2768 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
2769 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
2770}
2771
Garfield Tan1c7bc862020-01-28 13:24:04 -08002772class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
2773protected:
2774 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
2775 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
2776
Chris Yea209fde2020-07-22 13:54:51 -07002777 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002778 sp<FakeWindowHandle> mWindow;
2779
2780 virtual void SetUp() override {
2781 mFakePolicy = new FakeInputDispatcherPolicy();
2782 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
2783 mDispatcher = new InputDispatcher(mFakePolicy);
2784 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
2785 ASSERT_EQ(OK, mDispatcher->start());
2786
2787 setUpWindow();
2788 }
2789
2790 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07002791 mApp = std::make_shared<FakeApplicationHandle>();
Garfield Tan1c7bc862020-01-28 13:24:04 -08002792 mWindow = new FakeWindowHandle(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2793
Vishnu Nair47074b82020-08-14 11:54:47 -07002794 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002795 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002796 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002797 mWindow->consumeFocusEvent(true);
2798 }
2799
Chris Ye2ad95392020-09-01 13:44:44 -07002800 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08002801 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07002802 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002803 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
2804 mDispatcher->notifyKey(&keyArgs);
2805
2806 // Window should receive key down event.
2807 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2808 }
2809
2810 void expectKeyRepeatOnce(int32_t repeatCount) {
2811 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
2812 InputEvent* repeatEvent = mWindow->consume();
2813 ASSERT_NE(nullptr, repeatEvent);
2814
2815 uint32_t eventType = repeatEvent->getType();
2816 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType);
2817
2818 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
2819 uint32_t eventAction = repeatKeyEvent->getAction();
2820 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
2821 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
2822 }
2823
Chris Ye2ad95392020-09-01 13:44:44 -07002824 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08002825 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07002826 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002827 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
2828 mDispatcher->notifyKey(&keyArgs);
2829
2830 // Window should receive key down event.
2831 mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
2832 0 /*expectedFlags*/);
2833 }
2834};
2835
2836TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Chris Ye2ad95392020-09-01 13:44:44 -07002837 sendAndConsumeKeyDown(1 /* deviceId */);
2838 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2839 expectKeyRepeatOnce(repeatCount);
2840 }
2841}
2842
2843TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
2844 sendAndConsumeKeyDown(1 /* deviceId */);
2845 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2846 expectKeyRepeatOnce(repeatCount);
2847 }
2848 sendAndConsumeKeyDown(2 /* deviceId */);
2849 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08002850 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2851 expectKeyRepeatOnce(repeatCount);
2852 }
2853}
2854
2855TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Chris Ye2ad95392020-09-01 13:44:44 -07002856 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002857 expectKeyRepeatOnce(1 /*repeatCount*/);
Chris Ye2ad95392020-09-01 13:44:44 -07002858 sendAndConsumeKeyUp(1 /* deviceId */);
2859 mWindow->assertNoEvents();
2860}
2861
2862TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
2863 sendAndConsumeKeyDown(1 /* deviceId */);
2864 expectKeyRepeatOnce(1 /*repeatCount*/);
2865 sendAndConsumeKeyDown(2 /* deviceId */);
2866 expectKeyRepeatOnce(1 /*repeatCount*/);
2867 // Stale key up from device 1.
2868 sendAndConsumeKeyUp(1 /* deviceId */);
2869 // Device 2 is still down, keep repeating
2870 expectKeyRepeatOnce(2 /*repeatCount*/);
2871 expectKeyRepeatOnce(3 /*repeatCount*/);
2872 // Device 2 key up
2873 sendAndConsumeKeyUp(2 /* deviceId */);
2874 mWindow->assertNoEvents();
2875}
2876
2877TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
2878 sendAndConsumeKeyDown(1 /* deviceId */);
2879 expectKeyRepeatOnce(1 /*repeatCount*/);
2880 sendAndConsumeKeyDown(2 /* deviceId */);
2881 expectKeyRepeatOnce(1 /*repeatCount*/);
2882 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
2883 sendAndConsumeKeyUp(2 /* deviceId */);
2884 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08002885 mWindow->assertNoEvents();
2886}
2887
2888TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Chris Ye2ad95392020-09-01 13:44:44 -07002889 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002890 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2891 InputEvent* repeatEvent = mWindow->consume();
2892 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2893 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
2894 IdGenerator::getSource(repeatEvent->getId()));
2895 }
2896}
2897
2898TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Chris Ye2ad95392020-09-01 13:44:44 -07002899 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002900
2901 std::unordered_set<int32_t> idSet;
2902 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2903 InputEvent* repeatEvent = mWindow->consume();
2904 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2905 int32_t id = repeatEvent->getId();
2906 EXPECT_EQ(idSet.end(), idSet.find(id));
2907 idSet.insert(id);
2908 }
2909}
2910
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002911/* Test InputDispatcher for MultiDisplay */
2912class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
2913public:
2914 static constexpr int32_t SECOND_DISPLAY_ID = 1;
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002915 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002916 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08002917
Chris Yea209fde2020-07-22 13:54:51 -07002918 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002919 windowInPrimary =
2920 new FakeWindowHandle(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002921
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002922 // Set focus window for primary display, but focused display would be second one.
2923 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07002924 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002925 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002926 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002927 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08002928
Chris Yea209fde2020-07-22 13:54:51 -07002929 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002930 windowInSecondary =
2931 new FakeWindowHandle(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002932 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002933 // Set focus display to second one.
2934 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
2935 // Set focus window for second display.
2936 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07002937 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002938 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002939 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002940 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002941 }
2942
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002943 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002944 InputDispatcherTest::TearDown();
2945
Chris Yea209fde2020-07-22 13:54:51 -07002946 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002947 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07002948 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002949 windowInSecondary.clear();
2950 }
2951
2952protected:
Chris Yea209fde2020-07-22 13:54:51 -07002953 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002954 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07002955 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002956 sp<FakeWindowHandle> windowInSecondary;
2957};
2958
2959TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
2960 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002961 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2962 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2963 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002964 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08002965 windowInSecondary->assertNoEvents();
2966
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002967 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002968 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2969 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
2970 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08002971 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002972 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08002973}
2974
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002975TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08002976 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08002977 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2978 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002979 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002980 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08002981 windowInSecondary->assertNoEvents();
2982
2983 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08002984 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002985 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08002986 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002987 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08002988
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002989 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00002990 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08002991
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002992 // Old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002993 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
2994 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08002995
2996 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08002997 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002998 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08002999 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003000 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08003001 windowInSecondary->assertNoEvents();
3002}
3003
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003004// Test per-display input monitors for motion event.
3005TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08003006 FakeMonitorReceiver monitorInPrimary =
3007 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
3008 FakeMonitorReceiver monitorInSecondary =
3009 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003010
3011 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003012 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3013 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
3014 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003015 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08003016 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003017 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003018 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003019
3020 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003021 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3022 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
3023 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003024 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003025 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003026 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08003027 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003028
3029 // Test inject a non-pointer motion event.
3030 // If specific a display, it will dispatch to the focused window of particular display,
3031 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003032 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3033 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
3034 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003035 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003036 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003037 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08003038 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003039}
3040
3041// Test per-display input monitors for key event.
3042TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003043 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08003044 FakeMonitorReceiver monitorInPrimary =
3045 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
3046 FakeMonitorReceiver monitorInSecondary =
3047 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003048
3049 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003050 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3051 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003052 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003053 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003054 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08003055 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003056}
3057
Vishnu Nair958da932020-08-21 17:12:37 -07003058TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
3059 sp<FakeWindowHandle> secondWindowInPrimary =
3060 new FakeWindowHandle(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
3061 secondWindowInPrimary->setFocusable(true);
3062 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
3063 setFocusedWindow(secondWindowInPrimary);
3064 windowInPrimary->consumeFocusEvent(false);
3065 secondWindowInPrimary->consumeFocusEvent(true);
3066
3067 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003068 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
3069 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003070 windowInPrimary->assertNoEvents();
3071 windowInSecondary->assertNoEvents();
3072 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3073}
3074
Jackal Guof9696682018-10-05 12:23:23 +08003075class InputFilterTest : public InputDispatcherTest {
3076protected:
3077 static constexpr int32_t SECOND_DISPLAY_ID = 1;
3078
3079 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) {
3080 NotifyMotionArgs motionArgs;
3081
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003082 motionArgs =
3083 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08003084 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003085 motionArgs =
3086 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08003087 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003088 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08003089 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08003090 mFakePolicy->assertFilterInputEventWasCalled(motionArgs);
Jackal Guof9696682018-10-05 12:23:23 +08003091 } else {
3092 mFakePolicy->assertFilterInputEventWasNotCalled();
3093 }
3094 }
3095
3096 void testNotifyKey(bool expectToBeFiltered) {
3097 NotifyKeyArgs keyArgs;
3098
3099 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
3100 mDispatcher->notifyKey(&keyArgs);
3101 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
3102 mDispatcher->notifyKey(&keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003103 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08003104
3105 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08003106 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08003107 } else {
3108 mFakePolicy->assertFilterInputEventWasNotCalled();
3109 }
3110 }
3111};
3112
3113// Test InputFilter for MotionEvent
3114TEST_F(InputFilterTest, MotionEvent_InputFilter) {
3115 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
3116 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
3117 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
3118
3119 // Enable InputFilter
3120 mDispatcher->setInputFilterEnabled(true);
3121 // Test touch on both primary and second display, and check if both events are filtered.
3122 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
3123 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
3124
3125 // Disable InputFilter
3126 mDispatcher->setInputFilterEnabled(false);
3127 // Test touch on both primary and second display, and check if both events aren't filtered.
3128 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
3129 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
3130}
3131
3132// Test InputFilter for KeyEvent
3133TEST_F(InputFilterTest, KeyEvent_InputFilter) {
3134 // Since the InputFilter is disabled by default, check if key event aren't filtered.
3135 testNotifyKey(/*expectToBeFiltered*/ false);
3136
3137 // Enable InputFilter
3138 mDispatcher->setInputFilterEnabled(true);
3139 // Send a key event, and check if it is filtered.
3140 testNotifyKey(/*expectToBeFiltered*/ true);
3141
3142 // Disable InputFilter
3143 mDispatcher->setInputFilterEnabled(false);
3144 // Send a key event, and check if it isn't filtered.
3145 testNotifyKey(/*expectToBeFiltered*/ false);
3146}
3147
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003148class InputFilterInjectionPolicyTest : public InputDispatcherTest {
3149protected:
3150 virtual void SetUp() override {
3151 InputDispatcherTest::SetUp();
3152
3153 /**
3154 * We don't need to enable input filter to test the injected event policy, but we enabled it
3155 * here to make the tests more realistic, since this policy only matters when inputfilter is
3156 * on.
3157 */
3158 mDispatcher->setInputFilterEnabled(true);
3159
3160 std::shared_ptr<InputApplicationHandle> application =
3161 std::make_shared<FakeApplicationHandle>();
3162 mWindow =
3163 new FakeWindowHandle(application, mDispatcher, "Test Window", ADISPLAY_ID_DEFAULT);
3164
3165 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3166 mWindow->setFocusable(true);
3167 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3168 setFocusedWindow(mWindow);
3169 mWindow->consumeFocusEvent(true);
3170 }
3171
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003172 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
3173 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003174 KeyEvent event;
3175
3176 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
3177 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
3178 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
3179 KEY_A, AMETA_NONE, 0 /*repeatCount*/, eventTime, eventTime);
3180 const int32_t additionalPolicyFlags =
3181 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
3182 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3183 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
3184 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
3185 policyFlags | additionalPolicyFlags));
3186
3187 InputEvent* received = mWindow->consume();
3188 ASSERT_NE(nullptr, received);
3189 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003190 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_KEY);
3191 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
3192 ASSERT_EQ(flags, keyEvent.getFlags());
3193 }
3194
3195 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
3196 int32_t flags) {
3197 MotionEvent event;
3198 PointerProperties pointerProperties[1];
3199 PointerCoords pointerCoords[1];
3200 pointerProperties[0].clear();
3201 pointerProperties[0].id = 0;
3202 pointerCoords[0].clear();
3203 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
3204 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
3205
3206 ui::Transform identityTransform;
3207 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
3208 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
3209 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
3210 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
3211 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -07003212 AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0,
3213 0 /*INVALID_DISPLAY_SIZE*/, 0 /*INVALID_DISPLAY_SIZE*/, eventTime,
3214 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003215 /*pointerCount*/ 1, pointerProperties, pointerCoords);
3216
3217 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
3218 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3219 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
3220 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
3221 policyFlags | additionalPolicyFlags));
3222
3223 InputEvent* received = mWindow->consume();
3224 ASSERT_NE(nullptr, received);
3225 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
3226 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_MOTION);
3227 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
3228 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003229 }
3230
3231private:
3232 sp<FakeWindowHandle> mWindow;
3233};
3234
3235TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003236 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
3237 // filter. Without it, the event will no different from a regularly injected event, and the
3238 // injected device id will be overwritten.
3239 testInjectedKey(POLICY_FLAG_FILTERED, 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
3240 0 /*flags*/);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003241}
3242
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003243TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003244 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003245 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
3246 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
3247}
3248
3249TEST_F(InputFilterInjectionPolicyTest,
3250 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
3251 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
3252 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
3253 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003254}
3255
3256TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
3257 testInjectedKey(0 /*policyFlags*/, 3 /*injectedDeviceId*/,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003258 VIRTUAL_KEYBOARD_ID /*resolvedDeviceId*/, 0 /*flags*/);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003259}
3260
chaviwfd6d3512019-03-25 13:23:49 -07003261class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07003262 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07003263 InputDispatcherTest::SetUp();
3264
Chris Yea209fde2020-07-22 13:54:51 -07003265 std::shared_ptr<FakeApplicationHandle> application =
3266 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003267 mUnfocusedWindow =
3268 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07003269 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
3270 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3271 // window.
chaviw3277faf2021-05-19 16:45:23 -05003272 mUnfocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07003273
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003274 mFocusedWindow =
3275 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
3276 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviw3277faf2021-05-19 16:45:23 -05003277 mFocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07003278
3279 // Set focused application.
3280 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07003281 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07003282
3283 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00003284 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003285 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003286 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07003287 }
3288
Prabir Pradhan3608aad2019-10-02 17:08:26 -07003289 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07003290 InputDispatcherTest::TearDown();
3291
3292 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003293 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07003294 }
3295
3296protected:
3297 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003298 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003299 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07003300};
3301
3302// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
3303// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
3304// the onPointerDownOutsideFocus callback.
3305TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003306 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003307 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3308 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003309 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003310 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07003311
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003312 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07003313 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
3314}
3315
3316// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
3317// DOWN on the window that doesn't have focus. Ensure no window received the
3318// onPointerDownOutsideFocus callback.
3319TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003320 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003321 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003322 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003323 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07003324
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003325 ASSERT_TRUE(mDispatcher->waitForIdle());
3326 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07003327}
3328
3329// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
3330// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
3331TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003332 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3333 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003334 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003335 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07003336
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003337 ASSERT_TRUE(mDispatcher->waitForIdle());
3338 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07003339}
3340
3341// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
3342// DOWN on the window that already has focus. Ensure no window received the
3343// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003344TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003345 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003346 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003347 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003348 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003349 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07003350
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003351 ASSERT_TRUE(mDispatcher->waitForIdle());
3352 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07003353}
3354
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08003355// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
3356// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
3357TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
3358 const MotionEvent event =
3359 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3360 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
3361 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(20).y(20))
3362 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
3363 .build();
3364 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
3365 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3366 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
3367
3368 ASSERT_TRUE(mDispatcher->waitForIdle());
3369 mFakePolicy->assertOnPointerDownWasNotCalled();
3370 // Ensure that the unfocused window did not receive any FOCUS events.
3371 mUnfocusedWindow->assertNoEvents();
3372}
3373
chaviwaf87b3e2019-10-01 16:59:28 -07003374// These tests ensures we can send touch events to a single client when there are multiple input
3375// windows that point to the same client token.
3376class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
3377 virtual void SetUp() override {
3378 InputDispatcherTest::SetUp();
3379
Chris Yea209fde2020-07-22 13:54:51 -07003380 std::shared_ptr<FakeApplicationHandle> application =
3381 std::make_shared<FakeApplicationHandle>();
chaviwaf87b3e2019-10-01 16:59:28 -07003382 mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1",
3383 ADISPLAY_ID_DEFAULT);
3384 // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window.
3385 // We also need FLAG_SPLIT_TOUCH or we won't be able to get touches for both windows.
chaviw3277faf2021-05-19 16:45:23 -05003386 mWindow1->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07003387 mWindow1->setFrame(Rect(0, 0, 100, 100));
3388
3389 mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2",
3390 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviw3277faf2021-05-19 16:45:23 -05003391 mWindow2->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07003392 mWindow2->setFrame(Rect(100, 100, 200, 200));
3393
Arthur Hung72d8dc32020-03-28 00:48:39 +00003394 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07003395 }
3396
3397protected:
3398 sp<FakeWindowHandle> mWindow1;
3399 sp<FakeWindowHandle> mWindow2;
3400
3401 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05003402 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07003403 vec2 vals = windowInfo->transform.transform(point.x, point.y);
3404 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07003405 }
3406
3407 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
3408 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003409 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07003410 InputEvent* event = window->consume();
3411
3412 ASSERT_NE(nullptr, event) << name.c_str()
3413 << ": consumer should have returned non-NULL event.";
3414
3415 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
3416 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
3417 << " event, got " << inputEventTypeToString(event->getType()) << " event";
3418
3419 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
3420 EXPECT_EQ(expectedAction, motionEvent.getAction());
3421
3422 for (size_t i = 0; i < points.size(); i++) {
3423 float expectedX = points[i].x;
3424 float expectedY = points[i].y;
3425
3426 EXPECT_EQ(expectedX, motionEvent.getX(i))
3427 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
3428 << ", got " << motionEvent.getX(i);
3429 EXPECT_EQ(expectedY, motionEvent.getY(i))
3430 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
3431 << ", got " << motionEvent.getY(i);
3432 }
3433 }
chaviw9eaa22c2020-07-01 16:21:27 -07003434
3435 void touchAndAssertPositions(int32_t action, std::vector<PointF> touchedPoints,
3436 std::vector<PointF> expectedPoints) {
3437 NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
3438 ADISPLAY_ID_DEFAULT, touchedPoints);
3439 mDispatcher->notifyMotion(&motionArgs);
3440
3441 // Always consume from window1 since it's the window that has the InputReceiver
3442 consumeMotionEvent(mWindow1, action, expectedPoints);
3443 }
chaviwaf87b3e2019-10-01 16:59:28 -07003444};
3445
3446TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
3447 // Touch Window 1
3448 PointF touchedPoint = {10, 10};
3449 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003450 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003451
3452 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07003453 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003454
3455 // Touch Window 2
3456 touchedPoint = {150, 150};
3457 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003458 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003459}
3460
chaviw9eaa22c2020-07-01 16:21:27 -07003461TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
3462 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07003463 mWindow2->setWindowScale(0.5f, 0.5f);
3464
3465 // Touch Window 1
3466 PointF touchedPoint = {10, 10};
3467 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003468 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003469 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07003470 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003471
3472 // Touch Window 2
3473 touchedPoint = {150, 150};
3474 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003475 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
3476 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003477
chaviw9eaa22c2020-07-01 16:21:27 -07003478 // Update the transform so rotation is set
3479 mWindow2->setWindowTransform(0, -1, 1, 0);
3480 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
3481 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003482}
3483
chaviw9eaa22c2020-07-01 16:21:27 -07003484TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003485 mWindow2->setWindowScale(0.5f, 0.5f);
3486
3487 // Touch Window 1
3488 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3489 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003490 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003491
3492 // Touch Window 2
3493 int32_t actionPointerDown =
3494 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003495 touchedPoints.push_back(PointF{150, 150});
3496 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3497 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003498
chaviw9eaa22c2020-07-01 16:21:27 -07003499 // Release Window 2
3500 int32_t actionPointerUp =
3501 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3502 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
3503 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003504
chaviw9eaa22c2020-07-01 16:21:27 -07003505 // Update the transform so rotation is set for Window 2
3506 mWindow2->setWindowTransform(0, -1, 1, 0);
3507 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3508 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003509}
3510
chaviw9eaa22c2020-07-01 16:21:27 -07003511TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003512 mWindow2->setWindowScale(0.5f, 0.5f);
3513
3514 // Touch Window 1
3515 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3516 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003517 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003518
3519 // Touch Window 2
3520 int32_t actionPointerDown =
3521 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003522 touchedPoints.push_back(PointF{150, 150});
3523 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003524
chaviw9eaa22c2020-07-01 16:21:27 -07003525 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003526
3527 // Move both windows
3528 touchedPoints = {{20, 20}, {175, 175}};
3529 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3530 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3531
chaviw9eaa22c2020-07-01 16:21:27 -07003532 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003533
chaviw9eaa22c2020-07-01 16:21:27 -07003534 // Release Window 2
3535 int32_t actionPointerUp =
3536 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3537 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
3538 expectedPoints.pop_back();
3539
3540 // Touch Window 2
3541 mWindow2->setWindowTransform(0, -1, 1, 0);
3542 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3543 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
3544
3545 // Move both windows
3546 touchedPoints = {{20, 20}, {175, 175}};
3547 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3548 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3549
3550 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003551}
3552
3553TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
3554 mWindow1->setWindowScale(0.5f, 0.5f);
3555
3556 // Touch Window 1
3557 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3558 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003559 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003560
3561 // Touch Window 2
3562 int32_t actionPointerDown =
3563 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003564 touchedPoints.push_back(PointF{150, 150});
3565 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003566
chaviw9eaa22c2020-07-01 16:21:27 -07003567 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003568
3569 // Move both windows
3570 touchedPoints = {{20, 20}, {175, 175}};
3571 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3572 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3573
chaviw9eaa22c2020-07-01 16:21:27 -07003574 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003575}
3576
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003577class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
3578 virtual void SetUp() override {
3579 InputDispatcherTest::SetUp();
3580
Chris Yea209fde2020-07-22 13:54:51 -07003581 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003582 mApplication->setDispatchingTimeout(20ms);
3583 mWindow =
3584 new FakeWindowHandle(mApplication, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
3585 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05003586 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07003587 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003588 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3589 // window.
chaviw3277faf2021-05-19 16:45:23 -05003590 mWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003591
3592 // Set focused application.
3593 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
3594
3595 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003596 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003597 mWindow->consumeFocusEvent(true);
3598 }
3599
3600 virtual void TearDown() override {
3601 InputDispatcherTest::TearDown();
3602 mWindow.clear();
3603 }
3604
3605protected:
Chris Yea209fde2020-07-22 13:54:51 -07003606 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003607 sp<FakeWindowHandle> mWindow;
3608 static constexpr PointF WINDOW_LOCATION = {20, 20};
3609
3610 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003611 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003612 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3613 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003614 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003615 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3616 WINDOW_LOCATION));
3617 }
3618};
3619
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003620// Send a tap and respond, which should not cause an ANR.
3621TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
3622 tapOnWindow();
3623 mWindow->consumeMotionDown();
3624 mWindow->consumeMotionUp();
3625 ASSERT_TRUE(mDispatcher->waitForIdle());
3626 mFakePolicy->assertNotifyAnrWasNotCalled();
3627}
3628
3629// Send a regular key and respond, which should not cause an ANR.
3630TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003631 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003632 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
3633 ASSERT_TRUE(mDispatcher->waitForIdle());
3634 mFakePolicy->assertNotifyAnrWasNotCalled();
3635}
3636
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003637TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
3638 mWindow->setFocusable(false);
3639 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3640 mWindow->consumeFocusEvent(false);
3641
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003642 InputEventInjectionResult result =
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003643 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003644 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/,
3645 false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003646 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003647 // Key will not go to window because we have no focused window.
3648 // The 'no focused window' ANR timer should start instead.
3649
3650 // Now, the focused application goes away.
3651 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
3652 // The key should get dropped and there should be no ANR.
3653
3654 ASSERT_TRUE(mDispatcher->waitForIdle());
3655 mFakePolicy->assertNotifyAnrWasNotCalled();
3656}
3657
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003658// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003659// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
3660// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003661TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003662 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003663 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3664 WINDOW_LOCATION));
3665
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003666 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
3667 ASSERT_TRUE(sequenceNum);
3668 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003669 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003670
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003671 mWindow->finishEvent(*sequenceNum);
3672 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3673 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003674 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003675 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003676}
3677
3678// Send a key to the app and have the app not respond right away.
3679TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
3680 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003681 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003682 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
3683 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003684 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003685 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003686 ASSERT_TRUE(mDispatcher->waitForIdle());
3687}
3688
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003689// We have a focused application, but no focused window
3690TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003691 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003692 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3693 mWindow->consumeFocusEvent(false);
3694
3695 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003696 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003697 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3698 WINDOW_LOCATION));
3699 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
3700 mDispatcher->waitForIdle();
3701 mFakePolicy->assertNotifyAnrWasNotCalled();
3702
3703 // Once a focused event arrives, we get an ANR for this application
3704 // We specify the injection timeout to be smaller than the application timeout, to ensure that
3705 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003706 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003707 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003708 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003709 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003710 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003711 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003712 ASSERT_TRUE(mDispatcher->waitForIdle());
3713}
3714
3715// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003716// Make sure that we don't notify policy twice about the same ANR.
3717TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003718 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003719 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3720 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003721
3722 // Once a focused event arrives, we get an ANR for this application
3723 // We specify the injection timeout to be smaller than the application timeout, to ensure that
3724 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003725 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003726 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003727 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003728 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003729 const std::chrono::duration appTimeout =
3730 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003731 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003732
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003733 std::this_thread::sleep_for(appTimeout);
3734 // ANR should not be raised again. It is up to policy to do that if it desires.
3735 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003736
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003737 // If we now get a focused window, the ANR should stop, but the policy handles that via
3738 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003739 ASSERT_TRUE(mDispatcher->waitForIdle());
3740}
3741
3742// We have a focused application, but no focused window
3743TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003744 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003745 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3746 mWindow->consumeFocusEvent(false);
3747
3748 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003749 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003750 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003751 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3752 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003753
3754 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003755 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003756
3757 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003758 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003759 ASSERT_TRUE(mDispatcher->waitForIdle());
3760 mWindow->assertNoEvents();
3761}
3762
3763/**
3764 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
3765 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
3766 * If we process 1 of the events, but ANR on the second event with the same timestamp,
3767 * the ANR mechanism should still work.
3768 *
3769 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
3770 * DOWN event, while not responding on the second one.
3771 */
3772TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
3773 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
3774 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3775 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
3776 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
3777 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003778 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003779
3780 // Now send ACTION_UP, with identical timestamp
3781 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3782 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
3783 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
3784 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003785 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003786
3787 // We have now sent down and up. Let's consume first event and then ANR on the second.
3788 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3789 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003790 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003791}
3792
3793// If an app is not responding to a key event, gesture monitors should continue to receive
3794// new motion events
3795TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnKey) {
3796 FakeMonitorReceiver monitor =
3797 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
3798 true /*isGestureMonitor*/);
3799
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003800 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3801 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003802 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003803 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003804
3805 // Stuck on the ACTION_UP
3806 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003807 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003808
3809 // New tap will go to the gesture monitor, but not to the window
3810 tapOnWindow();
3811 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3812 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3813
3814 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
3815 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003816 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003817 mWindow->assertNoEvents();
3818 monitor.assertNoEvents();
3819}
3820
3821// If an app is not responding to a motion event, gesture monitors should continue to receive
3822// new motion events
3823TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnMotion) {
3824 FakeMonitorReceiver monitor =
3825 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
3826 true /*isGestureMonitor*/);
3827
3828 tapOnWindow();
3829 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3830 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3831
3832 mWindow->consumeMotionDown();
3833 // Stuck on the ACTION_UP
3834 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003835 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003836
3837 // New tap will go to the gesture monitor, but not to the window
3838 tapOnWindow();
3839 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3840 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3841
3842 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
3843 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003844 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003845 mWindow->assertNoEvents();
3846 monitor.assertNoEvents();
3847}
3848
3849// If a window is unresponsive, then you get anr. if the window later catches up and starts to
3850// process events, you don't get an anr. When the window later becomes unresponsive again, you
3851// get an ANR again.
3852// 1. tap -> block on ACTION_UP -> receive ANR
3853// 2. consume all pending events (= queue becomes healthy again)
3854// 3. tap again -> block on ACTION_UP again -> receive ANR second time
3855TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
3856 tapOnWindow();
3857
3858 mWindow->consumeMotionDown();
3859 // Block on ACTION_UP
3860 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003861 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003862 mWindow->consumeMotionUp(); // Now the connection should be healthy again
3863 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003864 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003865 mWindow->assertNoEvents();
3866
3867 tapOnWindow();
3868 mWindow->consumeMotionDown();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003869 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003870 mWindow->consumeMotionUp();
3871
3872 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003873 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003874 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003875 mWindow->assertNoEvents();
3876}
3877
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003878// If a connection remains unresponsive for a while, make sure policy is only notified once about
3879// it.
3880TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003881 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003882 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3883 WINDOW_LOCATION));
3884
3885 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003886 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003887 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003888 // 'notifyConnectionUnresponsive' should only be called once per connection
3889 mFakePolicy->assertNotifyAnrWasNotCalled();
3890 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003891 mWindow->consumeMotionDown();
3892 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3893 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3894 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003895 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003896 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003897 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003898}
3899
3900/**
3901 * If a window is processing a motion event, and then a key event comes in, the key event should
3902 * not to to the focused window until the motion is processed.
3903 *
3904 * Warning!!!
3905 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
3906 * and the injection timeout that we specify when injecting the key.
3907 * We must have the injection timeout (10ms) be smaller than
3908 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
3909 *
3910 * If that value changes, this test should also change.
3911 */
3912TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
3913 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3914 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3915
3916 tapOnWindow();
3917 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3918 ASSERT_TRUE(downSequenceNum);
3919 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3920 ASSERT_TRUE(upSequenceNum);
3921 // Don't finish the events yet, and send a key
3922 // Injection will "succeed" because we will eventually give up and send the key to the focused
3923 // window even if motions are still being processed. But because the injection timeout is short,
3924 // we will receive INJECTION_TIMED_OUT as the result.
3925
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003926 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003927 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003928 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3929 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003930 // Key will not be sent to the window, yet, because the window is still processing events
3931 // and the key remains pending, waiting for the touch events to be processed
3932 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3933 ASSERT_FALSE(keySequenceNum);
3934
3935 std::this_thread::sleep_for(500ms);
3936 // if we wait long enough though, dispatcher will give up, and still send the key
3937 // to the focused window, even though we have not yet finished the motion event
3938 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3939 mWindow->finishEvent(*downSequenceNum);
3940 mWindow->finishEvent(*upSequenceNum);
3941}
3942
3943/**
3944 * If a window is processing a motion event, and then a key event comes in, the key event should
3945 * not go to the focused window until the motion is processed.
3946 * If then a new motion comes in, then the pending key event should be going to the currently
3947 * focused window right away.
3948 */
3949TEST_F(InputDispatcherSingleWindowAnr,
3950 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
3951 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3952 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3953
3954 tapOnWindow();
3955 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3956 ASSERT_TRUE(downSequenceNum);
3957 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3958 ASSERT_TRUE(upSequenceNum);
3959 // Don't finish the events yet, and send a key
3960 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003961 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003962 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003963 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003964 // At this point, key is still pending, and should not be sent to the application yet.
3965 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3966 ASSERT_FALSE(keySequenceNum);
3967
3968 // Now tap down again. It should cause the pending key to go to the focused window right away.
3969 tapOnWindow();
3970 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
3971 // the other events yet. We can finish events in any order.
3972 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
3973 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
3974 mWindow->consumeMotionDown();
3975 mWindow->consumeMotionUp();
3976 mWindow->assertNoEvents();
3977}
3978
3979class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
3980 virtual void SetUp() override {
3981 InputDispatcherTest::SetUp();
3982
Chris Yea209fde2020-07-22 13:54:51 -07003983 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003984 mApplication->setDispatchingTimeout(10ms);
3985 mUnfocusedWindow =
3986 new FakeWindowHandle(mApplication, mDispatcher, "Unfocused", ADISPLAY_ID_DEFAULT);
3987 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
3988 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3989 // window.
3990 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
chaviw3277faf2021-05-19 16:45:23 -05003991 mUnfocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL |
3992 WindowInfo::Flag::WATCH_OUTSIDE_TOUCH |
3993 WindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003994
3995 mFocusedWindow =
3996 new FakeWindowHandle(mApplication, mDispatcher, "Focused", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05003997 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003998 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviw3277faf2021-05-19 16:45:23 -05003999 mFocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004000
4001 // Set focused application.
4002 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07004003 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004004
4005 // Expect one focus window exist in display.
4006 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004007 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004008 mFocusedWindow->consumeFocusEvent(true);
4009 }
4010
4011 virtual void TearDown() override {
4012 InputDispatcherTest::TearDown();
4013
4014 mUnfocusedWindow.clear();
4015 mFocusedWindow.clear();
4016 }
4017
4018protected:
Chris Yea209fde2020-07-22 13:54:51 -07004019 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004020 sp<FakeWindowHandle> mUnfocusedWindow;
4021 sp<FakeWindowHandle> mFocusedWindow;
4022 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
4023 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
4024 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
4025
4026 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
4027
4028 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
4029
4030private:
4031 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004032 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004033 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4034 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004035 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004036 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4037 location));
4038 }
4039};
4040
4041// If we have 2 windows that are both unresponsive, the one with the shortest timeout
4042// should be ANR'd first.
4043TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004044 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004045 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4046 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004047 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004048 mFocusedWindow->consumeMotionDown();
4049 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
4050 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
4051 // We consumed all events, so no ANR
4052 ASSERT_TRUE(mDispatcher->waitForIdle());
4053 mFakePolicy->assertNotifyAnrWasNotCalled();
4054
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004055 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004056 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4057 FOCUSED_WINDOW_LOCATION));
4058 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
4059 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004060
4061 const std::chrono::duration timeout =
4062 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004063 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004064 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
4065 // sequence to make it consistent
4066 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004067 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004068 mFocusedWindow->consumeMotionDown();
4069 // This cancel is generated because the connection was unresponsive
4070 mFocusedWindow->consumeMotionCancel();
4071 mFocusedWindow->assertNoEvents();
4072 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004073 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004074 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004075 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004076}
4077
4078// If we have 2 windows with identical timeouts that are both unresponsive,
4079// it doesn't matter which order they should have ANR.
4080// But we should receive ANR for both.
4081TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
4082 // Set the timeout for unfocused window to match the focused window
4083 mUnfocusedWindow->setDispatchingTimeout(10ms);
4084 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
4085
4086 tapOnFocusedWindow();
4087 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004088 sp<IBinder> anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms);
4089 sp<IBinder> anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004090
4091 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004092 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
4093 mFocusedWindow->getToken() == anrConnectionToken2);
4094 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
4095 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004096
4097 ASSERT_TRUE(mDispatcher->waitForIdle());
4098 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004099
4100 mFocusedWindow->consumeMotionDown();
4101 mFocusedWindow->consumeMotionUp();
4102 mUnfocusedWindow->consumeMotionOutside();
4103
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004104 sp<IBinder> responsiveToken1 = mFakePolicy->getResponsiveWindowToken();
4105 sp<IBinder> responsiveToken2 = mFakePolicy->getResponsiveWindowToken();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004106
4107 // Both applications should be marked as responsive, in any order
4108 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
4109 mFocusedWindow->getToken() == responsiveToken2);
4110 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
4111 mUnfocusedWindow->getToken() == responsiveToken2);
4112 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004113}
4114
4115// If a window is already not responding, the second tap on the same window should be ignored.
4116// We should also log an error to account for the dropped event (not tested here).
4117// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
4118TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
4119 tapOnFocusedWindow();
4120 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
4121 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
4122 // Receive the events, but don't respond
4123 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
4124 ASSERT_TRUE(downEventSequenceNum);
4125 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
4126 ASSERT_TRUE(upEventSequenceNum);
4127 const std::chrono::duration timeout =
4128 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004129 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004130
4131 // Tap once again
4132 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004133 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004134 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4135 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004136 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004137 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4138 FOCUSED_WINDOW_LOCATION));
4139 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
4140 // valid touch target
4141 mUnfocusedWindow->assertNoEvents();
4142
4143 // Consume the first tap
4144 mFocusedWindow->finishEvent(*downEventSequenceNum);
4145 mFocusedWindow->finishEvent(*upEventSequenceNum);
4146 ASSERT_TRUE(mDispatcher->waitForIdle());
4147 // The second tap did not go to the focused window
4148 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004149 // Since all events are finished, connection should be deemed healthy again
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004150 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004151 mFakePolicy->assertNotifyAnrWasNotCalled();
4152}
4153
4154// If you tap outside of all windows, there will not be ANR
4155TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004156 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004157 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4158 LOCATION_OUTSIDE_ALL_WINDOWS));
4159 ASSERT_TRUE(mDispatcher->waitForIdle());
4160 mFakePolicy->assertNotifyAnrWasNotCalled();
4161}
4162
4163// Since the focused window is paused, tapping on it should not produce any events
4164TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
4165 mFocusedWindow->setPaused(true);
4166 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
4167
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004168 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004169 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4170 FOCUSED_WINDOW_LOCATION));
4171
4172 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
4173 ASSERT_TRUE(mDispatcher->waitForIdle());
4174 // Should not ANR because the window is paused, and touches shouldn't go to it
4175 mFakePolicy->assertNotifyAnrWasNotCalled();
4176
4177 mFocusedWindow->assertNoEvents();
4178 mUnfocusedWindow->assertNoEvents();
4179}
4180
4181/**
4182 * If a window is processing a motion event, and then a key event comes in, the key event should
4183 * not to to the focused window until the motion is processed.
4184 * If a different window becomes focused at this time, the key should go to that window instead.
4185 *
4186 * Warning!!!
4187 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
4188 * and the injection timeout that we specify when injecting the key.
4189 * We must have the injection timeout (10ms) be smaller than
4190 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
4191 *
4192 * If that value changes, this test should also change.
4193 */
4194TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
4195 // Set a long ANR timeout to prevent it from triggering
4196 mFocusedWindow->setDispatchingTimeout(2s);
4197 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
4198
4199 tapOnUnfocusedWindow();
4200 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
4201 ASSERT_TRUE(downSequenceNum);
4202 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
4203 ASSERT_TRUE(upSequenceNum);
4204 // Don't finish the events yet, and send a key
4205 // Injection will succeed because we will eventually give up and send the key to the focused
4206 // window even if motions are still being processed.
4207
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004208 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004209 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004210 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
4211 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004212 // Key will not be sent to the window, yet, because the window is still processing events
4213 // and the key remains pending, waiting for the touch events to be processed
4214 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
4215 ASSERT_FALSE(keySequenceNum);
4216
4217 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07004218 mFocusedWindow->setFocusable(false);
4219 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004220 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004221 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004222
4223 // Focus events should precede the key events
4224 mUnfocusedWindow->consumeFocusEvent(true);
4225 mFocusedWindow->consumeFocusEvent(false);
4226
4227 // Finish the tap events, which should unblock dispatcher
4228 mUnfocusedWindow->finishEvent(*downSequenceNum);
4229 mUnfocusedWindow->finishEvent(*upSequenceNum);
4230
4231 // Now that all queues are cleared and no backlog in the connections, the key event
4232 // can finally go to the newly focused "mUnfocusedWindow".
4233 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4234 mFocusedWindow->assertNoEvents();
4235 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004236 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004237}
4238
4239// When the touch stream is split across 2 windows, and one of them does not respond,
4240// then ANR should be raised and the touch should be canceled for the unresponsive window.
4241// The other window should not be affected by that.
4242TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
4243 // Touch Window 1
4244 NotifyMotionArgs motionArgs =
4245 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4246 ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION});
4247 mDispatcher->notifyMotion(&motionArgs);
4248 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
4249 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
4250
4251 // Touch Window 2
4252 int32_t actionPointerDown =
4253 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
4254
4255 motionArgs =
4256 generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4257 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION});
4258 mDispatcher->notifyMotion(&motionArgs);
4259
4260 const std::chrono::duration timeout =
4261 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004262 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004263
4264 mUnfocusedWindow->consumeMotionDown();
4265 mFocusedWindow->consumeMotionDown();
4266 // Focused window may or may not receive ACTION_MOVE
4267 // But it should definitely receive ACTION_CANCEL due to the ANR
4268 InputEvent* event;
4269 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
4270 ASSERT_TRUE(moveOrCancelSequenceNum);
4271 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
4272 ASSERT_NE(nullptr, event);
4273 ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION);
4274 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
4275 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
4276 mFocusedWindow->consumeMotionCancel();
4277 } else {
4278 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
4279 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004280 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004281 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004282
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004283 mUnfocusedWindow->assertNoEvents();
4284 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004285 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004286}
4287
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05004288/**
4289 * If we have no focused window, and a key comes in, we start the ANR timer.
4290 * The focused application should add a focused window before the timer runs out to prevent ANR.
4291 *
4292 * If the user touches another application during this time, the key should be dropped.
4293 * Next, if a new focused window comes in, without toggling the focused application,
4294 * then no ANR should occur.
4295 *
4296 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
4297 * but in some cases the policy may not update the focused application.
4298 */
4299TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
4300 std::shared_ptr<FakeApplicationHandle> focusedApplication =
4301 std::make_shared<FakeApplicationHandle>();
4302 focusedApplication->setDispatchingTimeout(60ms);
4303 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
4304 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
4305 mFocusedWindow->setFocusable(false);
4306
4307 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
4308 mFocusedWindow->consumeFocusEvent(false);
4309
4310 // Send a key. The ANR timer should start because there is no focused window.
4311 // 'focusedApplication' will get blamed if this timer completes.
4312 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004313 InputEventInjectionResult result =
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05004314 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004315 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/,
4316 false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004317 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05004318
4319 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
4320 // then the injected touches won't cause the focused event to get dropped.
4321 // The dispatcher only checks for whether the queue should be pruned upon queueing.
4322 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
4323 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
4324 // For this test, it means that the key would get delivered to the window once it becomes
4325 // focused.
4326 std::this_thread::sleep_for(10ms);
4327
4328 // Touch unfocused window. This should force the pending key to get dropped.
4329 NotifyMotionArgs motionArgs =
4330 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4331 ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION});
4332 mDispatcher->notifyMotion(&motionArgs);
4333
4334 // We do not consume the motion right away, because that would require dispatcher to first
4335 // process (== drop) the key event, and by that time, ANR will be raised.
4336 // Set the focused window first.
4337 mFocusedWindow->setFocusable(true);
4338 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
4339 setFocusedWindow(mFocusedWindow);
4340 mFocusedWindow->consumeFocusEvent(true);
4341 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
4342 // to another application. This could be a bug / behaviour in the policy.
4343
4344 mUnfocusedWindow->consumeMotionDown();
4345
4346 ASSERT_TRUE(mDispatcher->waitForIdle());
4347 // Should not ANR because we actually have a focused window. It was just added too slowly.
4348 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
4349}
4350
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05004351// These tests ensure we cannot send touch events to a window that's positioned behind a window
4352// that has feature NO_INPUT_CHANNEL.
4353// Layout:
4354// Top (closest to user)
4355// mNoInputWindow (above all windows)
4356// mBottomWindow
4357// Bottom (furthest from user)
4358class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
4359 virtual void SetUp() override {
4360 InputDispatcherTest::SetUp();
4361
4362 mApplication = std::make_shared<FakeApplicationHandle>();
4363 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
4364 "Window without input channel", ADISPLAY_ID_DEFAULT,
4365 std::make_optional<sp<IBinder>>(nullptr) /*token*/);
4366
chaviw3277faf2021-05-19 16:45:23 -05004367 mNoInputWindow->setInputFeatures(WindowInfo::Feature::NO_INPUT_CHANNEL);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05004368 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
4369 // It's perfectly valid for this window to not have an associated input channel
4370
4371 mBottomWindow = new FakeWindowHandle(mApplication, mDispatcher, "Bottom window",
4372 ADISPLAY_ID_DEFAULT);
4373 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
4374
4375 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
4376 }
4377
4378protected:
4379 std::shared_ptr<FakeApplicationHandle> mApplication;
4380 sp<FakeWindowHandle> mNoInputWindow;
4381 sp<FakeWindowHandle> mBottomWindow;
4382};
4383
4384TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
4385 PointF touchedPoint = {10, 10};
4386
4387 NotifyMotionArgs motionArgs =
4388 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4389 ADISPLAY_ID_DEFAULT, {touchedPoint});
4390 mDispatcher->notifyMotion(&motionArgs);
4391
4392 mNoInputWindow->assertNoEvents();
4393 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
4394 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
4395 // and therefore should prevent mBottomWindow from receiving touches
4396 mBottomWindow->assertNoEvents();
4397}
4398
4399/**
4400 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
4401 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
4402 */
4403TEST_F(InputDispatcherMultiWindowOcclusionTests,
4404 NoInputChannelFeature_DropsTouchesWithValidChannel) {
4405 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
4406 "Window with input channel and NO_INPUT_CHANNEL",
4407 ADISPLAY_ID_DEFAULT);
4408
chaviw3277faf2021-05-19 16:45:23 -05004409 mNoInputWindow->setInputFeatures(WindowInfo::Feature::NO_INPUT_CHANNEL);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05004410 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
4411 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
4412
4413 PointF touchedPoint = {10, 10};
4414
4415 NotifyMotionArgs motionArgs =
4416 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4417 ADISPLAY_ID_DEFAULT, {touchedPoint});
4418 mDispatcher->notifyMotion(&motionArgs);
4419
4420 mNoInputWindow->assertNoEvents();
4421 mBottomWindow->assertNoEvents();
4422}
4423
Vishnu Nair958da932020-08-21 17:12:37 -07004424class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
4425protected:
4426 std::shared_ptr<FakeApplicationHandle> mApp;
4427 sp<FakeWindowHandle> mWindow;
4428 sp<FakeWindowHandle> mMirror;
4429
4430 virtual void SetUp() override {
4431 InputDispatcherTest::SetUp();
4432 mApp = std::make_shared<FakeApplicationHandle>();
4433 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
4434 mMirror = new FakeWindowHandle(mApp, mDispatcher, "TestWindowMirror", ADISPLAY_ID_DEFAULT,
4435 mWindow->getToken());
4436 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
4437 mWindow->setFocusable(true);
4438 mMirror->setFocusable(true);
4439 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4440 }
4441};
4442
4443TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
4444 // Request focus on a mirrored window
4445 setFocusedWindow(mMirror);
4446
4447 // window gets focused
4448 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004449 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4450 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004451 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
4452}
4453
4454// A focused & mirrored window remains focused only if the window and its mirror are both
4455// focusable.
4456TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
4457 setFocusedWindow(mMirror);
4458
4459 // window gets focused
4460 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004461 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4462 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004463 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004464 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4465 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004466 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4467
4468 mMirror->setFocusable(false);
4469 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4470
4471 // window loses focus since one of the windows associated with the token in not focusable
4472 mWindow->consumeFocusEvent(false);
4473
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004474 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4475 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004476 mWindow->assertNoEvents();
4477}
4478
4479// A focused & mirrored window remains focused until the window and its mirror both become
4480// invisible.
4481TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
4482 setFocusedWindow(mMirror);
4483
4484 // window gets focused
4485 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004486 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4487 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004488 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004489 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4490 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004491 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4492
4493 mMirror->setVisible(false);
4494 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4495
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004496 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4497 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004498 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004499 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4500 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004501 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4502
4503 mWindow->setVisible(false);
4504 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4505
4506 // window loses focus only after all windows associated with the token become invisible.
4507 mWindow->consumeFocusEvent(false);
4508
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004509 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4510 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004511 mWindow->assertNoEvents();
4512}
4513
4514// A focused & mirrored window remains focused until both windows are removed.
4515TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
4516 setFocusedWindow(mMirror);
4517
4518 // window gets focused
4519 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004520 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4521 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004522 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004523 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4524 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004525 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4526
4527 // single window is removed but the window token remains focused
4528 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
4529
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004530 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4531 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004532 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004533 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4534 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004535 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4536
4537 // Both windows are removed
4538 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
4539 mWindow->consumeFocusEvent(false);
4540
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004541 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4542 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004543 mWindow->assertNoEvents();
4544}
4545
4546// Focus request can be pending until one window becomes visible.
4547TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
4548 // Request focus on an invisible mirror.
4549 mWindow->setVisible(false);
4550 mMirror->setVisible(false);
4551 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4552 setFocusedWindow(mMirror);
4553
4554 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004555 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07004556 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004557 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07004558
4559 mMirror->setVisible(true);
4560 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4561
4562 // window gets focused
4563 mWindow->consumeFocusEvent(true);
4564 // window gets the pending key event
4565 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4566}
Prabir Pradhan99987712020-11-10 18:43:05 -08004567
4568class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
4569protected:
4570 std::shared_ptr<FakeApplicationHandle> mApp;
4571 sp<FakeWindowHandle> mWindow;
4572 sp<FakeWindowHandle> mSecondWindow;
4573
4574 void SetUp() override {
4575 InputDispatcherTest::SetUp();
4576 mApp = std::make_shared<FakeApplicationHandle>();
4577 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
4578 mWindow->setFocusable(true);
4579 mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
4580 mSecondWindow->setFocusable(true);
4581
4582 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
4583 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
4584
4585 setFocusedWindow(mWindow);
4586 mWindow->consumeFocusEvent(true);
4587 }
4588
Prabir Pradhanac483a62021-08-06 14:01:18 +00004589 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
4590 const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08004591 mDispatcher->notifyPointerCaptureChanged(&args);
4592 }
4593
Prabir Pradhanac483a62021-08-06 14:01:18 +00004594 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
4595 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08004596 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhanac483a62021-08-06 14:01:18 +00004597 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
4598 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08004599 window->consumeCaptureEvent(enabled);
Prabir Pradhanac483a62021-08-06 14:01:18 +00004600 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08004601 }
4602};
4603
4604TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
4605 // Ensure that capture cannot be obtained for unfocused windows.
4606 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
4607 mFakePolicy->assertSetPointerCaptureNotCalled();
4608 mSecondWindow->assertNoEvents();
4609
4610 // Ensure that capture can be enabled from the focus window.
4611 requestAndVerifyPointerCapture(mWindow, true);
4612
4613 // Ensure that capture cannot be disabled from a window that does not have capture.
4614 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
4615 mFakePolicy->assertSetPointerCaptureNotCalled();
4616
4617 // Ensure that capture can be disabled from the window with capture.
4618 requestAndVerifyPointerCapture(mWindow, false);
4619}
4620
4621TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhanac483a62021-08-06 14:01:18 +00004622 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08004623
4624 setFocusedWindow(mSecondWindow);
4625
4626 // Ensure that the capture disabled event was sent first.
4627 mWindow->consumeCaptureEvent(false);
4628 mWindow->consumeFocusEvent(false);
4629 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhanac483a62021-08-06 14:01:18 +00004630 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08004631
4632 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhanac483a62021-08-06 14:01:18 +00004633 notifyPointerCaptureChanged({});
4634 notifyPointerCaptureChanged(request);
4635 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08004636 mWindow->assertNoEvents();
4637 mSecondWindow->assertNoEvents();
4638 mFakePolicy->assertSetPointerCaptureNotCalled();
4639}
4640
4641TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhanac483a62021-08-06 14:01:18 +00004642 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08004643
4644 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhanac483a62021-08-06 14:01:18 +00004645 notifyPointerCaptureChanged({});
4646 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08004647
4648 // Ensure that Pointer Capture is disabled.
Prabir Pradhanac483a62021-08-06 14:01:18 +00004649 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08004650 mWindow->consumeCaptureEvent(false);
4651 mWindow->assertNoEvents();
4652}
4653
Prabir Pradhan167e6d92021-02-04 16:18:17 -08004654TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
4655 requestAndVerifyPointerCapture(mWindow, true);
4656
4657 // The first window loses focus.
4658 setFocusedWindow(mSecondWindow);
Prabir Pradhanac483a62021-08-06 14:01:18 +00004659 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08004660 mWindow->consumeCaptureEvent(false);
4661
4662 // Request Pointer Capture from the second window before the notification from InputReader
4663 // arrives.
4664 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhanac483a62021-08-06 14:01:18 +00004665 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08004666
4667 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhanac483a62021-08-06 14:01:18 +00004668 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08004669
4670 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhanac483a62021-08-06 14:01:18 +00004671 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08004672
4673 mSecondWindow->consumeFocusEvent(true);
4674 mSecondWindow->consumeCaptureEvent(true);
4675}
4676
Prabir Pradhanac483a62021-08-06 14:01:18 +00004677TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
4678 // App repeatedly enables and disables capture.
4679 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
4680 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
4681 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
4682 mFakePolicy->assertSetPointerCaptureCalled(false);
4683 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
4684 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
4685
4686 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
4687 // first request is now stale, this should do nothing.
4688 notifyPointerCaptureChanged(firstRequest);
4689 mWindow->assertNoEvents();
4690
4691 // InputReader notifies that the second request was enabled.
4692 notifyPointerCaptureChanged(secondRequest);
4693 mWindow->consumeCaptureEvent(true);
4694}
4695
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004696class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
4697protected:
4698 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00004699
4700 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
4701 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
4702
4703 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
4704 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
4705
4706 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
4707 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
4708 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
4709 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
4710 MAXIMUM_OBSCURING_OPACITY);
4711
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004712 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004713 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004714 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004715
4716 sp<FakeWindowHandle> mTouchWindow;
4717
4718 virtual void SetUp() override {
4719 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004720 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004721 mDispatcher->setBlockUntrustedTouchesMode(android::os::BlockUntrustedTouchesMode::BLOCK);
4722 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
4723 }
4724
4725 virtual void TearDown() override {
4726 InputDispatcherTest::TearDown();
4727 mTouchWindow.clear();
4728 }
4729
chaviw3277faf2021-05-19 16:45:23 -05004730 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode,
4731 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004732 sp<FakeWindowHandle> window = getWindow(uid, name);
chaviw3277faf2021-05-19 16:45:23 -05004733 window->setFlags(WindowInfo::Flag::NOT_TOUCHABLE);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004734 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004735 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004736 return window;
4737 }
4738
4739 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
4740 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
4741 sp<FakeWindowHandle> window =
4742 new FakeWindowHandle(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
4743 // Generate an arbitrary PID based on the UID
4744 window->setOwnerInfo(1777 + (uid % 10000), uid);
4745 return window;
4746 }
4747
4748 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
4749 NotifyMotionArgs args =
4750 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4751 ADISPLAY_ID_DEFAULT, points);
4752 mDispatcher->notifyMotion(&args);
4753 }
4754};
4755
4756TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004757 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004758 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004759 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004760
4761 touch();
4762
4763 mTouchWindow->assertNoEvents();
4764}
4765
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004766TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00004767 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
4768 const sp<FakeWindowHandle>& w =
4769 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
4770 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4771
4772 touch();
4773
4774 mTouchWindow->assertNoEvents();
4775}
4776
4777TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004778 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
4779 const sp<FakeWindowHandle>& w =
4780 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
4781 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4782
4783 touch();
4784
4785 w->assertNoEvents();
4786}
4787
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004788TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004789 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
4790 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004791
4792 touch();
4793
4794 mTouchWindow->consumeAnyMotionDown();
4795}
4796
4797TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004798 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004799 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004800 w->setFrame(Rect(0, 0, 50, 50));
4801 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004802
4803 touch({PointF{100, 100}});
4804
4805 mTouchWindow->consumeAnyMotionDown();
4806}
4807
4808TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004809 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004810 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004811 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4812
4813 touch();
4814
4815 mTouchWindow->consumeAnyMotionDown();
4816}
4817
4818TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
4819 const sp<FakeWindowHandle>& w =
4820 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4821 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004822
4823 touch();
4824
4825 mTouchWindow->consumeAnyMotionDown();
4826}
4827
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004828TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
4829 const sp<FakeWindowHandle>& w =
4830 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4831 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4832
4833 touch();
4834
4835 w->assertNoEvents();
4836}
4837
4838/**
4839 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
4840 * inside) while letting them pass-through. Note that even though touch passes through the occluding
4841 * window, the occluding window will still receive ACTION_OUTSIDE event.
4842 */
4843TEST_F(InputDispatcherUntrustedTouchesTest,
4844 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
4845 const sp<FakeWindowHandle>& w =
4846 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
chaviw3277faf2021-05-19 16:45:23 -05004847 w->addFlags(WindowInfo::Flag::WATCH_OUTSIDE_TOUCH);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004848 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4849
4850 touch();
4851
4852 w->consumeMotionOutside();
4853}
4854
4855TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
4856 const sp<FakeWindowHandle>& w =
4857 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
chaviw3277faf2021-05-19 16:45:23 -05004858 w->addFlags(WindowInfo::Flag::WATCH_OUTSIDE_TOUCH);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004859 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4860
4861 touch();
4862
4863 InputEvent* event = w->consume();
4864 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType());
4865 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
4866 EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getX());
4867 EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getY());
4868}
4869
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004870TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004871 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004872 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4873 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004874 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4875
4876 touch();
4877
4878 mTouchWindow->consumeAnyMotionDown();
4879}
4880
4881TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
4882 const sp<FakeWindowHandle>& w =
4883 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4884 MAXIMUM_OBSCURING_OPACITY);
4885 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004886
4887 touch();
4888
4889 mTouchWindow->consumeAnyMotionDown();
4890}
4891
4892TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004893 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004894 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4895 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004896 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4897
4898 touch();
4899
4900 mTouchWindow->assertNoEvents();
4901}
4902
4903TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
4904 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
4905 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004906 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
4907 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004908 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004909 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
4910 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004911 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
4912
4913 touch();
4914
4915 mTouchWindow->assertNoEvents();
4916}
4917
4918TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
4919 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
4920 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004921 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
4922 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004923 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004924 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
4925 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004926 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
4927
4928 touch();
4929
4930 mTouchWindow->consumeAnyMotionDown();
4931}
4932
4933TEST_F(InputDispatcherUntrustedTouchesTest,
4934 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
4935 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004936 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4937 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004938 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004939 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
4940 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004941 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
4942
4943 touch();
4944
4945 mTouchWindow->consumeAnyMotionDown();
4946}
4947
4948TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
4949 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004950 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4951 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004952 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004953 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
4954 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004955 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004956
4957 touch();
4958
4959 mTouchWindow->assertNoEvents();
4960}
4961
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004962TEST_F(InputDispatcherUntrustedTouchesTest,
4963 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
4964 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004965 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
4966 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004967 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004968 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4969 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004970 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
4971
4972 touch();
4973
4974 mTouchWindow->assertNoEvents();
4975}
4976
4977TEST_F(InputDispatcherUntrustedTouchesTest,
4978 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
4979 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004980 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
4981 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004982 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004983 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4984 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004985 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
4986
4987 touch();
4988
4989 mTouchWindow->consumeAnyMotionDown();
4990}
4991
4992TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
4993 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004994 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
4995 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004996 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4997
4998 touch();
4999
5000 mTouchWindow->consumeAnyMotionDown();
5001}
5002
5003TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
5004 const sp<FakeWindowHandle>& w =
5005 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
5006 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5007
5008 touch();
5009
5010 mTouchWindow->consumeAnyMotionDown();
5011}
5012
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00005013TEST_F(InputDispatcherUntrustedTouchesTest,
5014 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
5015 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
5016 const sp<FakeWindowHandle>& w =
5017 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
5018 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5019
5020 touch();
5021
5022 mTouchWindow->assertNoEvents();
5023}
5024
5025TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
5026 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
5027 const sp<FakeWindowHandle>& w =
5028 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
5029 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5030
5031 touch();
5032
5033 mTouchWindow->consumeAnyMotionDown();
5034}
5035
5036TEST_F(InputDispatcherUntrustedTouchesTest,
5037 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
5038 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
5039 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005040 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
5041 OPACITY_ABOVE_THRESHOLD);
5042 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5043
5044 touch();
5045
5046 mTouchWindow->consumeAnyMotionDown();
5047}
5048
5049TEST_F(InputDispatcherUntrustedTouchesTest,
5050 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
5051 const sp<FakeWindowHandle>& w1 =
5052 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
5053 OPACITY_BELOW_THRESHOLD);
5054 const sp<FakeWindowHandle>& w2 =
5055 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
5056 OPACITY_BELOW_THRESHOLD);
5057 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
5058
5059 touch();
5060
5061 mTouchWindow->assertNoEvents();
5062}
5063
5064/**
5065 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
5066 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
5067 * (which alone would result in allowing touches) does not affect the blocking behavior.
5068 */
5069TEST_F(InputDispatcherUntrustedTouchesTest,
5070 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
5071 const sp<FakeWindowHandle>& wB =
5072 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
5073 OPACITY_BELOW_THRESHOLD);
5074 const sp<FakeWindowHandle>& wC =
5075 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
5076 OPACITY_BELOW_THRESHOLD);
5077 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
5078
5079 touch();
5080
5081 mTouchWindow->assertNoEvents();
5082}
5083
5084/**
5085 * This test is testing that a window from a different UID but with same application token doesn't
5086 * block the touch. Apps can share the application token for close UI collaboration for example.
5087 */
5088TEST_F(InputDispatcherUntrustedTouchesTest,
5089 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
5090 const sp<FakeWindowHandle>& w =
5091 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
5092 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00005093 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5094
5095 touch();
5096
5097 mTouchWindow->consumeAnyMotionDown();
5098}
5099
arthurhungb89ccb02020-12-30 16:19:01 +08005100class InputDispatcherDragTests : public InputDispatcherTest {
5101protected:
5102 std::shared_ptr<FakeApplicationHandle> mApp;
5103 sp<FakeWindowHandle> mWindow;
5104 sp<FakeWindowHandle> mSecondWindow;
5105 sp<FakeWindowHandle> mDragWindow;
5106
5107 void SetUp() override {
5108 InputDispatcherTest::SetUp();
5109 mApp = std::make_shared<FakeApplicationHandle>();
5110 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
5111 mWindow->setFrame(Rect(0, 0, 100, 100));
chaviw3277faf2021-05-19 16:45:23 -05005112 mWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
arthurhungb89ccb02020-12-30 16:19:01 +08005113
5114 mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
5115 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
chaviw3277faf2021-05-19 16:45:23 -05005116 mSecondWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
arthurhungb89ccb02020-12-30 16:19:01 +08005117
5118 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
5119 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
5120 }
5121
5122 // Start performing drag, we will create a drag window and transfer touch to it.
5123 void performDrag() {
5124 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5125 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5126 {50, 50}))
5127 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5128
5129 // Window should receive motion event.
5130 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5131
5132 // The drag window covers the entire display
5133 mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
5134 mDispatcher->setInputWindows(
5135 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
5136
5137 // Transfer touch focus to the drag window
5138 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
5139 true /* isDragDrop */);
5140 mWindow->consumeMotionCancel();
5141 mDragWindow->consumeMotionDown();
5142 }
arthurhung6d4bed92021-03-17 11:59:33 +08005143
5144 // Start performing drag, we will create a drag window and transfer touch to it.
5145 void performStylusDrag() {
5146 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5147 injectMotionEvent(mDispatcher,
5148 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
5149 AINPUT_SOURCE_STYLUS)
5150 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
5151 .pointer(PointerBuilder(0,
5152 AMOTION_EVENT_TOOL_TYPE_STYLUS)
5153 .x(50)
5154 .y(50))
5155 .build()));
5156 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5157
5158 // The drag window covers the entire display
5159 mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
5160 mDispatcher->setInputWindows(
5161 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
5162
5163 // Transfer touch focus to the drag window
5164 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
5165 true /* isDragDrop */);
5166 mWindow->consumeMotionCancel();
5167 mDragWindow->consumeMotionDown();
5168 }
arthurhungb89ccb02020-12-30 16:19:01 +08005169};
5170
5171TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
5172 performDrag();
5173
5174 // Move on window.
5175 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5176 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5177 ADISPLAY_ID_DEFAULT, {50, 50}))
5178 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5179 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5180 mWindow->consumeDragEvent(false, 50, 50);
5181 mSecondWindow->assertNoEvents();
5182
5183 // Move to another window.
5184 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5185 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5186 ADISPLAY_ID_DEFAULT, {150, 50}))
5187 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5188 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5189 mWindow->consumeDragEvent(true, 150, 50);
5190 mSecondWindow->consumeDragEvent(false, 50, 50);
5191
5192 // Move back to original window.
5193 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5194 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5195 ADISPLAY_ID_DEFAULT, {50, 50}))
5196 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5197 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5198 mWindow->consumeDragEvent(false, 50, 50);
5199 mSecondWindow->consumeDragEvent(true, -50, 50);
5200
5201 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5202 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
5203 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5204 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5205 mWindow->assertNoEvents();
5206 mSecondWindow->assertNoEvents();
5207}
5208
arthurhungf452d0b2021-01-06 00:19:52 +08005209TEST_F(InputDispatcherDragTests, DragAndDrop) {
5210 performDrag();
5211
5212 // Move on window.
5213 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5214 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5215 ADISPLAY_ID_DEFAULT, {50, 50}))
5216 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5217 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5218 mWindow->consumeDragEvent(false, 50, 50);
5219 mSecondWindow->assertNoEvents();
5220
5221 // Move to another window.
5222 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5223 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5224 ADISPLAY_ID_DEFAULT, {150, 50}))
5225 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5226 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5227 mWindow->consumeDragEvent(true, 150, 50);
5228 mSecondWindow->consumeDragEvent(false, 50, 50);
5229
5230 // drop to another window.
5231 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5232 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5233 {150, 50}))
5234 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5235 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5236 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
5237 mWindow->assertNoEvents();
5238 mSecondWindow->assertNoEvents();
5239}
5240
arthurhung6d4bed92021-03-17 11:59:33 +08005241TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
5242 performStylusDrag();
5243
5244 // Move on window and keep button pressed.
5245 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5246 injectMotionEvent(mDispatcher,
5247 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
5248 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
5249 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
5250 .x(50)
5251 .y(50))
5252 .build()))
5253 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5254 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5255 mWindow->consumeDragEvent(false, 50, 50);
5256 mSecondWindow->assertNoEvents();
5257
5258 // Move to another window and release button, expect to drop item.
5259 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5260 injectMotionEvent(mDispatcher,
5261 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
5262 .buttonState(0)
5263 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
5264 .x(150)
5265 .y(50))
5266 .build()))
5267 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5268 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5269 mWindow->assertNoEvents();
5270 mSecondWindow->assertNoEvents();
5271 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
5272
5273 // nothing to the window.
5274 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5275 injectMotionEvent(mDispatcher,
5276 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
5277 .buttonState(0)
5278 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
5279 .x(150)
5280 .y(50))
5281 .build()))
5282 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5283 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5284 mWindow->assertNoEvents();
5285 mSecondWindow->assertNoEvents();
5286}
5287
Arthur Hung6d0571e2021-04-09 20:18:16 +08005288TEST_F(InputDispatcherDragTests, DragAndDrop_InvalidWindow) {
5289 performDrag();
5290
5291 // Set second window invisible.
5292 mSecondWindow->setVisible(false);
5293 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
5294
5295 // Move on window.
5296 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5297 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5298 ADISPLAY_ID_DEFAULT, {50, 50}))
5299 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5300 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5301 mWindow->consumeDragEvent(false, 50, 50);
5302 mSecondWindow->assertNoEvents();
5303
5304 // Move to another window.
5305 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5306 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5307 ADISPLAY_ID_DEFAULT, {150, 50}))
5308 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5309 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5310 mWindow->consumeDragEvent(true, 150, 50);
5311 mSecondWindow->assertNoEvents();
5312
5313 // drop to another window.
5314 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5315 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5316 {150, 50}))
5317 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5318 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5319 mFakePolicy->assertDropTargetEquals(nullptr);
5320 mWindow->assertNoEvents();
5321 mSecondWindow->assertNoEvents();
5322}
5323
Garfield Tane84e6f92019-08-29 17:28:41 -07005324} // namespace android::inputdispatcher