blob: 0fc470854822893b7f92beaeeec700fbe4b8e33e [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Garfield Tan0fc2fa72019-08-29 17:22:15 -070017#include "../dispatcher/InputDispatcher.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080018
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -070019#include <android-base/properties.h>
Garfield Tan1c7bc862020-01-28 13:24:04 -080020#include <android-base/stringprintf.h>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070021#include <android-base/thread_annotations.h>
Robert Carr803535b2018-08-02 16:38:15 -070022#include <binder/Binder.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080023#include <gtest/gtest.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100024#include <input/Input.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080025#include <linux/input.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100026
Garfield Tan1c7bc862020-01-28 13:24:04 -080027#include <cinttypes>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070028#include <thread>
Garfield Tan1c7bc862020-01-28 13:24:04 -080029#include <unordered_set>
chaviwd1c23182019-12-20 18:44:56 -080030#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080031
Garfield Tan1c7bc862020-01-28 13:24:04 -080032using android::base::StringPrintf;
chaviw3277faf2021-05-19 16:45:23 -050033using android::gui::FocusRequest;
34using android::gui::TouchOcclusionMode;
35using android::gui::WindowInfo;
36using android::gui::WindowInfoHandle;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080037using android::os::InputEventInjectionResult;
38using android::os::InputEventInjectionSync;
Michael Wright44753b12020-07-08 13:48:11 +010039using namespace android::flag_operators;
Garfield Tan1c7bc862020-01-28 13:24:04 -080040
Garfield Tane84e6f92019-08-29 17:28:41 -070041namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080042
43// An arbitrary time value.
44static const nsecs_t ARBITRARY_TIME = 1234;
45
46// An arbitrary device id.
47static const int32_t DEVICE_ID = 1;
48
Jeff Brownf086ddb2014-02-11 14:28:48 -080049// An arbitrary display id.
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -080050static const int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
Jeff Brownf086ddb2014-02-11 14:28:48 -080051
Michael Wrightd02c5b62014-02-10 15:10:22 -080052// An arbitrary injector pid / uid pair that has permission to inject events.
53static const int32_t INJECTOR_PID = 999;
54static const int32_t INJECTOR_UID = 1001;
55
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +000056// An arbitrary pid of the gesture monitor window
57static constexpr int32_t MONITOR_PID = 2001;
58
chaviwd1c23182019-12-20 18:44:56 -080059struct PointF {
60 float x;
61 float y;
62};
Michael Wrightd02c5b62014-02-10 15:10:22 -080063
Gang Wang342c9272020-01-13 13:15:04 -050064/**
65 * Return a DOWN key event with KEYCODE_A.
66 */
67static KeyEvent getTestKeyEvent() {
68 KeyEvent event;
69
Garfield Tanfbe732e2020-01-24 11:26:14 -080070 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
71 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
72 ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -050073 return event;
74}
75
Michael Wrightd02c5b62014-02-10 15:10:22 -080076// --- FakeInputDispatcherPolicy ---
77
78class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
79 InputDispatcherConfiguration mConfig;
80
81protected:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100082 virtual ~FakeInputDispatcherPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -080083
84public:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100085 FakeInputDispatcherPolicy() {}
Jackal Guof9696682018-10-05 12:23:23 +080086
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080087 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080088 assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_KEY, args.eventTime, args.action,
89 args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080090 }
91
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080092 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args) {
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080093 assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_MOTION, args.eventTime, args.action,
94 args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080095 }
96
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -070097 void assertFilterInputEventWasNotCalled() {
98 std::scoped_lock lock(mLock);
99 ASSERT_EQ(nullptr, mFilteredEvent);
100 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800101
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800102 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700103 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800104 ASSERT_TRUE(mConfigurationChangedTime)
105 << "Timed out waiting for configuration changed call";
106 ASSERT_EQ(*mConfigurationChangedTime, when);
107 mConfigurationChangedTime = std::nullopt;
108 }
109
110 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700111 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800112 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800113 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800114 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
115 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
116 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
117 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
118 mLastNotifySwitch = std::nullopt;
119 }
120
chaviwfd6d3512019-03-25 13:23:49 -0700121 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700122 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800123 ASSERT_EQ(touchedToken, mOnPointerDownToken);
124 mOnPointerDownToken.clear();
125 }
126
127 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700128 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800129 ASSERT_TRUE(mOnPointerDownToken == nullptr)
130 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700131 }
132
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700133 // This function must be called soon after the expected ANR timer starts,
134 // because we are also checking how much time has passed.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500135 void assertNotifyNoFocusedWindowAnrWasCalled(
Chris Yea209fde2020-07-22 13:54:51 -0700136 std::chrono::nanoseconds timeout,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500137 const std::shared_ptr<InputApplicationHandle>& expectedApplication) {
138 std::shared_ptr<InputApplicationHandle> application;
139 { // acquire lock
140 std::unique_lock lock(mLock);
141 android::base::ScopedLockAssertion assumeLocked(mLock);
142 ASSERT_NO_FATAL_FAILURE(
143 application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock));
144 } // release lock
145 ASSERT_EQ(expectedApplication, application);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700146 }
147
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000148 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
149 const sp<IBinder>& expectedConnectionToken) {
150 sp<IBinder> connectionToken = getUnresponsiveWindowToken(timeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500151 ASSERT_EQ(expectedConnectionToken, connectionToken);
152 }
153
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000154 void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedConnectionToken) {
155 sp<IBinder> connectionToken = getResponsiveWindowToken();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500156 ASSERT_EQ(expectedConnectionToken, connectionToken);
157 }
158
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000159 void assertNotifyMonitorUnresponsiveWasCalled(std::chrono::nanoseconds timeout) {
160 int32_t pid = getUnresponsiveMonitorPid(timeout);
161 ASSERT_EQ(MONITOR_PID, pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500162 }
163
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000164 void assertNotifyMonitorResponsiveWasCalled() {
165 int32_t pid = getResponsiveMonitorPid();
166 ASSERT_EQ(MONITOR_PID, pid);
167 }
168
169 sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500170 std::unique_lock lock(mLock);
171 android::base::ScopedLockAssertion assumeLocked(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000172 return getAnrTokenLockedInterruptible(timeout, mAnrWindowTokens, lock);
173 }
174
175 sp<IBinder> getResponsiveWindowToken() {
176 std::unique_lock lock(mLock);
177 android::base::ScopedLockAssertion assumeLocked(mLock);
178 return getAnrTokenLockedInterruptible(0s, mResponsiveWindowTokens, lock);
179 }
180
181 int32_t getUnresponsiveMonitorPid(std::chrono::nanoseconds timeout) {
182 std::unique_lock lock(mLock);
183 android::base::ScopedLockAssertion assumeLocked(mLock);
184 return getAnrTokenLockedInterruptible(timeout, mAnrMonitorPids, lock);
185 }
186
187 int32_t getResponsiveMonitorPid() {
188 std::unique_lock lock(mLock);
189 android::base::ScopedLockAssertion assumeLocked(mLock);
190 return getAnrTokenLockedInterruptible(0s, mResponsiveMonitorPids, lock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500191 }
192
193 // All three ANR-related callbacks behave the same way, so we use this generic function to wait
194 // for a specific container to become non-empty. When the container is non-empty, return the
195 // first entry from the container and erase it.
196 template <class T>
197 T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage,
198 std::unique_lock<std::mutex>& lock) REQUIRES(mLock) {
199 const std::chrono::time_point start = std::chrono::steady_clock::now();
200 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700201
202 // If there is an ANR, Dispatcher won't be idle because there are still events
203 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
204 // before checking if ANR was called.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500205 // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need
206 // to provide it some time to act. 100ms seems reasonable.
207 mNotifyAnr.wait_for(lock, timeToWait,
208 [&storage]() REQUIRES(mLock) { return !storage.empty(); });
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700209 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500210 if (storage.empty()) {
211 ADD_FAILURE() << "Did not receive the ANR callback";
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000212 return {};
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700213 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700214 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
215 // the dispatcher started counting before this function was called
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700216 if (std::chrono::abs(timeout - waited) > 100ms) {
217 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
218 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
219 << "ms, but waited "
220 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
221 << "ms instead";
222 }
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500223 T token = storage.front();
224 storage.pop();
225 return token;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700226 }
227
228 void assertNotifyAnrWasNotCalled() {
229 std::scoped_lock lock(mLock);
230 ASSERT_TRUE(mAnrApplications.empty());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000231 ASSERT_TRUE(mAnrWindowTokens.empty());
232 ASSERT_TRUE(mAnrMonitorPids.empty());
233 ASSERT_TRUE(mResponsiveWindowTokens.empty())
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500234 << "ANR was not called, but please also consume the 'connection is responsive' "
235 "signal";
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000236 ASSERT_TRUE(mResponsiveMonitorPids.empty())
237 << "Monitor ANR was not called, but please also consume the 'monitor is responsive'"
238 " signal";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700239 }
240
Garfield Tan1c7bc862020-01-28 13:24:04 -0800241 void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) {
242 mConfig.keyRepeatTimeout = timeout;
243 mConfig.keyRepeatDelay = delay;
244 }
245
Prabir Pradhan99987712020-11-10 18:43:05 -0800246 void waitForSetPointerCapture(bool enabled) {
247 std::unique_lock lock(mLock);
248 base::ScopedLockAssertion assumeLocked(mLock);
249
250 if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms,
251 [this, enabled]() REQUIRES(mLock) {
252 return mPointerCaptureEnabled &&
253 *mPointerCaptureEnabled ==
254 enabled;
255 })) {
256 FAIL() << "Timed out waiting for setPointerCapture(" << enabled << ") to be called.";
257 }
258 mPointerCaptureEnabled.reset();
259 }
260
261 void assertSetPointerCaptureNotCalled() {
262 std::unique_lock lock(mLock);
263 base::ScopedLockAssertion assumeLocked(mLock);
264
265 if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) {
266 FAIL() << "Expected setPointerCapture(enabled) to not be called, but was called. "
267 "enabled = "
268 << *mPointerCaptureEnabled;
269 }
270 mPointerCaptureEnabled.reset();
271 }
272
arthurhungf452d0b2021-01-06 00:19:52 +0800273 void assertDropTargetEquals(const sp<IBinder>& targetToken) {
274 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800275 ASSERT_TRUE(mNotifyDropWindowWasCalled);
arthurhungf452d0b2021-01-06 00:19:52 +0800276 ASSERT_EQ(targetToken, mDropTargetWindowToken);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800277 mNotifyDropWindowWasCalled = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800278 }
279
Michael Wrightd02c5b62014-02-10 15:10:22 -0800280private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700281 std::mutex mLock;
282 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
283 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
284 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
285 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800286
Prabir Pradhan99987712020-11-10 18:43:05 -0800287 std::condition_variable mPointerCaptureChangedCondition;
288 std::optional<bool> mPointerCaptureEnabled GUARDED_BY(mLock);
289
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700290 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700291 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000292 std::queue<sp<IBinder>> mAnrWindowTokens GUARDED_BY(mLock);
293 std::queue<sp<IBinder>> mResponsiveWindowTokens GUARDED_BY(mLock);
294 std::queue<int32_t> mAnrMonitorPids GUARDED_BY(mLock);
295 std::queue<int32_t> mResponsiveMonitorPids GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700296 std::condition_variable mNotifyAnr;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700297
arthurhungf452d0b2021-01-06 00:19:52 +0800298 sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800299 bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800300
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600301 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700302 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800303 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800304 }
305
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000306 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700307 std::scoped_lock lock(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000308 mAnrWindowTokens.push(connectionToken);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700309 mNotifyAnr.notify_all();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500310 }
311
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000312 void notifyMonitorUnresponsive(int32_t pid, const std::string&) override {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500313 std::scoped_lock lock(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000314 mAnrMonitorPids.push(pid);
315 mNotifyAnr.notify_all();
316 }
317
318 void notifyWindowResponsive(const sp<IBinder>& connectionToken) override {
319 std::scoped_lock lock(mLock);
320 mResponsiveWindowTokens.push(connectionToken);
321 mNotifyAnr.notify_all();
322 }
323
324 void notifyMonitorResponsive(int32_t pid) override {
325 std::scoped_lock lock(mLock);
326 mResponsiveMonitorPids.push(pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500327 mNotifyAnr.notify_all();
328 }
329
330 void notifyNoFocusedWindowAnr(
331 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
332 std::scoped_lock lock(mLock);
333 mAnrApplications.push(applicationHandle);
334 mNotifyAnr.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800335 }
336
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600337 void notifyInputChannelBroken(const sp<IBinder>&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600339 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700340
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600341 void notifyUntrustedTouch(const std::string& obscuringPackage) override {}
Chris Yef59a2f42020-10-16 12:55:26 -0700342 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
343 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
344 const std::vector<float>& values) override {}
345
346 void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType,
347 InputDeviceSensorAccuracy accuracy) override {}
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000348
Chris Yefb552902021-02-03 17:18:37 -0800349 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
350
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600351 void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800352 *outConfig = mConfig;
353 }
354
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600355 bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700356 std::scoped_lock lock(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800357 switch (inputEvent->getType()) {
358 case AINPUT_EVENT_TYPE_KEY: {
359 const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800360 mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800361 break;
362 }
363
364 case AINPUT_EVENT_TYPE_MOTION: {
365 const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800366 mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800367 break;
368 }
369 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800370 return true;
371 }
372
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600373 void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800374
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600375 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800376
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600377 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800378 return 0;
379 }
380
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600381 bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800382 return false;
383 }
384
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600385 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
386 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700387 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800388 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
389 * essentially a passthrough for notifySwitch.
390 */
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800391 mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800392 }
393
Sean Stoutb4e0a592021-02-23 07:34:53 -0800394 void pokeUserActivity(nsecs_t, int32_t, int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800395
Prabir Pradhan93f342c2021-03-11 15:05:30 -0800396 bool checkInjectEventsPermissionNonReentrant(int32_t pid, int32_t uid) override {
397 return pid == INJECTOR_PID && uid == INJECTOR_UID;
398 }
Jackal Guof9696682018-10-05 12:23:23 +0800399
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600400 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700401 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700402 mOnPointerDownToken = newToken;
403 }
404
Prabir Pradhan99987712020-11-10 18:43:05 -0800405 void setPointerCapture(bool enabled) override {
406 std::scoped_lock lock(mLock);
407 mPointerCaptureEnabled = {enabled};
408 mPointerCaptureChangedCondition.notify_all();
409 }
410
arthurhungf452d0b2021-01-06 00:19:52 +0800411 void notifyDropWindow(const sp<IBinder>& token, float x, float y) override {
412 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800413 mNotifyDropWindowWasCalled = true;
arthurhungf452d0b2021-01-06 00:19:52 +0800414 mDropTargetWindowToken = token;
415 }
416
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800417 void assertFilterInputEventWasCalled(int type, nsecs_t eventTime, int32_t action,
418 int32_t displayId) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700419 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800420 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
421 ASSERT_EQ(mFilteredEvent->getType(), type);
422
423 if (type == AINPUT_EVENT_TYPE_KEY) {
424 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*mFilteredEvent);
425 EXPECT_EQ(keyEvent.getEventTime(), eventTime);
426 EXPECT_EQ(keyEvent.getAction(), action);
427 EXPECT_EQ(keyEvent.getDisplayId(), displayId);
428 } else if (type == AINPUT_EVENT_TYPE_MOTION) {
429 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*mFilteredEvent);
430 EXPECT_EQ(motionEvent.getEventTime(), eventTime);
431 EXPECT_EQ(motionEvent.getAction(), action);
432 EXPECT_EQ(motionEvent.getDisplayId(), displayId);
433 } else {
434 FAIL() << "Unknown type: " << type;
435 }
436
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800437 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800438 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800439};
440
Michael Wrightd02c5b62014-02-10 15:10:22 -0800441// --- InputDispatcherTest ---
442
443class InputDispatcherTest : public testing::Test {
444protected:
445 sp<FakeInputDispatcherPolicy> mFakePolicy;
446 sp<InputDispatcher> mDispatcher;
447
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000448 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800449 mFakePolicy = new FakeInputDispatcherPolicy();
450 mDispatcher = new InputDispatcher(mFakePolicy);
Arthur Hungb92218b2018-08-14 12:00:21 +0800451 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000452 // Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700453 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800454 }
455
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000456 void TearDown() override {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700457 ASSERT_EQ(OK, mDispatcher->stop());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800458 mFakePolicy.clear();
459 mDispatcher.clear();
460 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700461
462 /**
463 * Used for debugging when writing the test
464 */
465 void dumpDispatcherState() {
466 std::string dump;
467 mDispatcher->dump(dump);
468 std::stringstream ss(dump);
469 std::string to;
470
471 while (std::getline(ss, to, '\n')) {
472 ALOGE("%s", to.c_str());
473 }
474 }
Vishnu Nair958da932020-08-21 17:12:37 -0700475
chaviw3277faf2021-05-19 16:45:23 -0500476 void setFocusedWindow(const sp<WindowInfoHandle>& window,
477 const sp<WindowInfoHandle>& focusedWindow = nullptr) {
Vishnu Nair958da932020-08-21 17:12:37 -0700478 FocusRequest request;
479 request.token = window->getToken();
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +0000480 request.windowName = window->getName();
Vishnu Nair958da932020-08-21 17:12:37 -0700481 if (focusedWindow) {
482 request.focusedToken = focusedWindow->getToken();
483 }
484 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
485 request.displayId = window->getInfo()->displayId;
486 mDispatcher->setFocusedWindow(request);
487 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800488};
489
Michael Wrightd02c5b62014-02-10 15:10:22 -0800490TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
491 KeyEvent event;
492
493 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800494 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
495 INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600496 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
497 ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800498 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700499 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800500 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800501 << "Should reject key events with undefined action.";
502
503 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800504 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
505 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600506 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800507 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700508 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800509 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800510 << "Should reject key events with ACTION_MULTIPLE.";
511}
512
513TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
514 MotionEvent event;
515 PointerProperties pointerProperties[MAX_POINTERS + 1];
516 PointerCoords pointerCoords[MAX_POINTERS + 1];
517 for (int i = 0; i <= MAX_POINTERS; i++) {
518 pointerProperties[i].clear();
519 pointerProperties[i].id = i;
520 pointerCoords[i].clear();
521 }
522
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800523 // Some constants commonly used below
524 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
525 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
526 constexpr int32_t metaState = AMETA_NONE;
527 constexpr MotionClassification classification = MotionClassification::NONE;
528
chaviw9eaa22c2020-07-01 16:21:27 -0700529 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800530 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800531 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
chaviw9eaa22c2020-07-01 16:21:27 -0700532 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification,
533 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700534 AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0,
535 INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700536 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800537 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700538 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800539 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800540 << "Should reject motion events with undefined action.";
541
542 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800543 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700544 AMOTION_EVENT_ACTION_POINTER_DOWN |
545 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700546 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
547 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700548 ui::Transform::ROT_0, INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE,
549 ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500550 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800551 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700552 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800553 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800554 << "Should reject motion events with pointer down index too large.";
555
Garfield Tanfbe732e2020-01-24 11:26:14 -0800556 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700557 AMOTION_EVENT_ACTION_POINTER_DOWN |
558 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700559 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
560 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700561 ui::Transform::ROT_0, INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE,
562 ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500563 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800564 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700565 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800566 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800567 << "Should reject motion events with pointer down index too small.";
568
569 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800570 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700571 AMOTION_EVENT_ACTION_POINTER_UP |
572 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700573 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
574 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700575 ui::Transform::ROT_0, INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE,
576 ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500577 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800578 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700579 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800580 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800581 << "Should reject motion events with pointer up index too large.";
582
Garfield Tanfbe732e2020-01-24 11:26:14 -0800583 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700584 AMOTION_EVENT_ACTION_POINTER_UP |
585 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700586 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
587 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700588 ui::Transform::ROT_0, INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE,
589 ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500590 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800591 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700592 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800593 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800594 << "Should reject motion events with pointer up index too small.";
595
596 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800597 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
598 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700599 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700600 AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0,
601 INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700602 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800603 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700604 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800605 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800606 << "Should reject motion events with 0 pointers.";
607
Garfield Tanfbe732e2020-01-24 11:26:14 -0800608 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
609 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700610 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700611 AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0,
612 INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700613 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800614 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700615 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800616 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800617 << "Should reject motion events with more than MAX_POINTERS pointers.";
618
619 // Rejects motion events with invalid pointer ids.
620 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800621 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
622 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700623 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700624 AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0,
625 INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700626 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800627 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700628 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800629 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800630 << "Should reject motion events with pointer ids less than 0.";
631
632 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800633 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
634 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700635 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700636 AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0,
637 INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700638 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800639 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700640 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800641 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800642 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
643
644 // Rejects motion events with duplicate pointer ids.
645 pointerProperties[0].id = 1;
646 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800647 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
648 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700649 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700650 AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0,
651 INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700652 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800653 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700654 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800655 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800656 << "Should reject motion events with duplicate pointer ids.";
657}
658
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800659/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
660
661TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
662 constexpr nsecs_t eventTime = 20;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800663 NotifyConfigurationChangedArgs args(10 /*id*/, eventTime);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800664 mDispatcher->notifyConfigurationChanged(&args);
665 ASSERT_TRUE(mDispatcher->waitForIdle());
666
667 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
668}
669
670TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800671 NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/,
672 2 /*switchMask*/);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800673 mDispatcher->notifySwitch(&args);
674
675 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
676 args.policyFlags |= POLICY_FLAG_TRUSTED;
677 mFakePolicy->assertNotifySwitchWasCalled(args);
678}
679
Arthur Hungb92218b2018-08-14 12:00:21 +0800680// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700681static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -0700682// Default input dispatching timeout if there is no focused application or paused window
683// from which to determine an appropriate dispatching timeout.
684static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds(
685 android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS *
686 android::base::HwTimeoutMultiplier());
Arthur Hungb92218b2018-08-14 12:00:21 +0800687
688class FakeApplicationHandle : public InputApplicationHandle {
689public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700690 FakeApplicationHandle() {
691 mInfo.name = "Fake Application";
692 mInfo.token = new BBinder();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500693 mInfo.dispatchingTimeoutMillis =
694 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700695 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800696 virtual ~FakeApplicationHandle() {}
697
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000698 virtual bool updateInfo() override { return true; }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700699
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500700 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
701 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700702 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800703};
704
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800705class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800706public:
Garfield Tan15601662020-09-22 15:32:38 -0700707 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800708 : mName(name) {
Garfield Tan15601662020-09-22 15:32:38 -0700709 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800710 }
711
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800712 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700713 InputEvent* event;
714 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
715 if (!consumeSeq) {
716 return nullptr;
717 }
718 finishEvent(*consumeSeq);
719 return event;
720 }
721
722 /**
723 * Receive an event without acknowledging it.
724 * Return the sequence number that could later be used to send finished signal.
725 */
726 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800727 uint32_t consumeSeq;
728 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800729
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800730 std::chrono::time_point start = std::chrono::steady_clock::now();
731 status_t status = WOULD_BLOCK;
732 while (status == WOULD_BLOCK) {
chaviw81e2bb92019-12-18 15:03:51 -0800733 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800734 &event);
735 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
736 if (elapsed > 100ms) {
737 break;
738 }
739 }
740
741 if (status == WOULD_BLOCK) {
742 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700743 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800744 }
745
746 if (status != OK) {
747 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700748 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800749 }
750 if (event == nullptr) {
751 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700752 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800753 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700754 if (outEvent != nullptr) {
755 *outEvent = event;
756 }
757 return consumeSeq;
758 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800759
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700760 /**
761 * To be used together with "receiveEvent" to complete the consumption of an event.
762 */
763 void finishEvent(uint32_t consumeSeq) {
764 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
765 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800766 }
767
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000768 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
769 const status_t status = mConsumer->sendTimeline(inputEventId, timeline);
770 ASSERT_EQ(OK, status);
771 }
772
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000773 void consumeEvent(int32_t expectedEventType, int32_t expectedAction,
774 std::optional<int32_t> expectedDisplayId,
775 std::optional<int32_t> expectedFlags) {
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800776 InputEvent* event = consume();
777
778 ASSERT_NE(nullptr, event) << mName.c_str()
779 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800780 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700781 << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType)
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800782 << " event, got " << inputEventTypeToString(event->getType()) << " event";
Arthur Hungb92218b2018-08-14 12:00:21 +0800783
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000784 if (expectedDisplayId.has_value()) {
785 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
786 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800787
Tiger Huang8664f8c2018-10-11 19:14:35 +0800788 switch (expectedEventType) {
789 case AINPUT_EVENT_TYPE_KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800790 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
791 EXPECT_EQ(expectedAction, keyEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000792 if (expectedFlags.has_value()) {
793 EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags());
794 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800795 break;
796 }
797 case AINPUT_EVENT_TYPE_MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800798 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
799 EXPECT_EQ(expectedAction, motionEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000800 if (expectedFlags.has_value()) {
801 EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags());
802 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800803 break;
804 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100805 case AINPUT_EVENT_TYPE_FOCUS: {
806 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
807 }
Prabir Pradhan99987712020-11-10 18:43:05 -0800808 case AINPUT_EVENT_TYPE_CAPTURE: {
809 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
810 }
arthurhungb89ccb02020-12-30 16:19:01 +0800811 case AINPUT_EVENT_TYPE_DRAG: {
812 FAIL() << "Use 'consumeDragEvent' for DRAG events";
813 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800814 default: {
815 FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType;
816 }
817 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800818 }
819
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100820 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
821 InputEvent* event = consume();
822 ASSERT_NE(nullptr, event) << mName.c_str()
823 << ": consumer should have returned non-NULL event.";
824 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
825 << "Got " << inputEventTypeToString(event->getType())
826 << " event instead of FOCUS event";
827
828 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
829 << mName.c_str() << ": event displayId should always be NONE.";
830
831 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
832 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
833 EXPECT_EQ(inTouchMode, focusEvent->getInTouchMode());
834 }
835
Prabir Pradhan99987712020-11-10 18:43:05 -0800836 void consumeCaptureEvent(bool hasCapture) {
837 const InputEvent* event = consume();
838 ASSERT_NE(nullptr, event) << mName.c_str()
839 << ": consumer should have returned non-NULL event.";
840 ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType())
841 << "Got " << inputEventTypeToString(event->getType())
842 << " event instead of CAPTURE event";
843
844 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
845 << mName.c_str() << ": event displayId should always be NONE.";
846
847 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
848 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
849 }
850
arthurhungb89ccb02020-12-30 16:19:01 +0800851 void consumeDragEvent(bool isExiting, float x, float y) {
852 const InputEvent* event = consume();
853 ASSERT_NE(nullptr, event) << mName.c_str()
854 << ": consumer should have returned non-NULL event.";
855 ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType())
856 << "Got " << inputEventTypeToString(event->getType())
857 << " event instead of DRAG event";
858
859 EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
860 << mName.c_str() << ": event displayId should always be NONE.";
861
862 const auto& dragEvent = static_cast<const DragEvent&>(*event);
863 EXPECT_EQ(isExiting, dragEvent.isExiting());
864 EXPECT_EQ(x, dragEvent.getX());
865 EXPECT_EQ(y, dragEvent.getY());
866 }
867
chaviwd1c23182019-12-20 18:44:56 -0800868 void assertNoEvents() {
869 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700870 if (event == nullptr) {
871 return;
872 }
873 if (event->getType() == AINPUT_EVENT_TYPE_KEY) {
874 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
875 ADD_FAILURE() << "Received key event "
876 << KeyEvent::actionToString(keyEvent.getAction());
877 } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) {
878 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
879 ADD_FAILURE() << "Received motion event "
880 << MotionEvent::actionToString(motionEvent.getAction());
881 } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) {
882 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
883 ADD_FAILURE() << "Received focus event, hasFocus = "
884 << (focusEvent.getHasFocus() ? "true" : "false");
Prabir Pradhan99987712020-11-10 18:43:05 -0800885 } else if (event->getType() == AINPUT_EVENT_TYPE_CAPTURE) {
886 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
887 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
888 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700889 }
890 FAIL() << mName.c_str()
891 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -0800892 }
893
894 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
895
896protected:
897 std::unique_ptr<InputConsumer> mConsumer;
898 PreallocatedInputEventFactory mEventFactory;
899
900 std::string mName;
901};
902
chaviw3277faf2021-05-19 16:45:23 -0500903class FakeWindowHandle : public WindowInfoHandle {
chaviwd1c23182019-12-20 18:44:56 -0800904public:
905 static const int32_t WIDTH = 600;
906 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -0800907
Chris Yea209fde2020-07-22 13:54:51 -0700908 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
chaviwd1c23182019-12-20 18:44:56 -0800909 const sp<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500910 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -0800911 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500912 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -0700913 base::Result<std::unique_ptr<InputChannel>> channel =
914 dispatcher->createInputChannel(name);
915 token = (*channel)->getConnectionToken();
916 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -0800917 }
918
919 inputApplicationHandle->updateInfo();
920 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
921
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500922 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700923 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -0800924 mInfo.name = name;
chaviw3277faf2021-05-19 16:45:23 -0500925 mInfo.type = WindowInfo::Type::APPLICATION;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500926 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000927 mInfo.alpha = 1.0;
chaviwd1c23182019-12-20 18:44:56 -0800928 mInfo.frameLeft = 0;
929 mInfo.frameTop = 0;
930 mInfo.frameRight = WIDTH;
931 mInfo.frameBottom = HEIGHT;
chaviw1ff3d1e2020-07-01 15:53:47 -0700932 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -0800933 mInfo.globalScaleFactor = 1.0;
934 mInfo.touchableRegion.clear();
935 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
936 mInfo.visible = true;
Vishnu Nair47074b82020-08-14 11:54:47 -0700937 mInfo.focusable = false;
chaviwd1c23182019-12-20 18:44:56 -0800938 mInfo.hasWallpaper = false;
939 mInfo.paused = false;
chaviwd1c23182019-12-20 18:44:56 -0800940 mInfo.ownerPid = INJECTOR_PID;
941 mInfo.ownerUid = INJECTOR_UID;
chaviwd1c23182019-12-20 18:44:56 -0800942 mInfo.displayId = displayId;
943 }
944
Vishnu Nair47074b82020-08-14 11:54:47 -0700945 void setFocusable(bool focusable) { mInfo.focusable = focusable; }
chaviwd1c23182019-12-20 18:44:56 -0800946
Vishnu Nair958da932020-08-21 17:12:37 -0700947 void setVisible(bool visible) { mInfo.visible = visible; }
948
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700949 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500950 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700951 }
952
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700953 void setPaused(bool paused) { mInfo.paused = paused; }
954
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000955 void setAlpha(float alpha) { mInfo.alpha = alpha; }
956
chaviw3277faf2021-05-19 16:45:23 -0500957 void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; }
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000958
Bernardo Rufino7393d172021-02-26 13:56:11 +0000959 void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
960
chaviwd1c23182019-12-20 18:44:56 -0800961 void setFrame(const Rect& frame) {
962 mInfo.frameLeft = frame.left;
963 mInfo.frameTop = frame.top;
964 mInfo.frameRight = frame.right;
965 mInfo.frameBottom = frame.bottom;
arthurhungb89ccb02020-12-30 16:19:01 +0800966 mInfo.transform.set(-frame.left, -frame.top);
chaviwd1c23182019-12-20 18:44:56 -0800967 mInfo.touchableRegion.clear();
968 mInfo.addTouchableRegion(frame);
969 }
970
chaviw3277faf2021-05-19 16:45:23 -0500971 void addFlags(Flags<WindowInfo::Flag> flags) { mInfo.flags |= flags; }
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +0000972
chaviw3277faf2021-05-19 16:45:23 -0500973 void setFlags(Flags<WindowInfo::Flag> flags) { mInfo.flags = flags; }
chaviwd1c23182019-12-20 18:44:56 -0800974
chaviw3277faf2021-05-19 16:45:23 -0500975 void setInputFeatures(WindowInfo::Feature features) { mInfo.inputFeatures = features; }
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500976
chaviw9eaa22c2020-07-01 16:21:27 -0700977 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
978 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
979 }
980
981 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -0700982
yunho.shinf4a80b82020-11-16 21:13:57 +0900983 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
984
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800985 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
986 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
987 expectedFlags);
988 }
989
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700990 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
991 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
992 }
993
Svet Ganov5d3bc372020-01-26 23:11:07 -0800994 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000995 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800996 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId,
997 expectedFlags);
998 }
999
1000 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001001 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -08001002 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId,
1003 expectedFlags);
1004 }
1005
1006 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001007 int32_t expectedFlags = 0) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001008 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
1009 }
1010
1011 void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
1012 std::optional<int32_t> expectedFlags = std::nullopt) {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001013 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
1014 expectedFlags);
1015 }
1016
Svet Ganov5d3bc372020-01-26 23:11:07 -08001017 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001018 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1019 int32_t expectedFlags = 0) {
1020 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
1021 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001022 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
1023 }
1024
1025 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001026 int32_t expectedFlags = 0) {
1027 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
1028 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001029 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
1030 }
1031
1032 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001033 int32_t expectedFlags = 0) {
Michael Wright3a240c42019-12-10 20:53:41 +00001034 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
1035 expectedFlags);
1036 }
1037
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001038 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1039 int32_t expectedFlags = 0) {
1040 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
1041 expectedFlags);
1042 }
1043
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001044 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
1045 ASSERT_NE(mInputReceiver, nullptr)
1046 << "Cannot consume events from a window with no receiver";
1047 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
1048 }
1049
Prabir Pradhan99987712020-11-10 18:43:05 -08001050 void consumeCaptureEvent(bool hasCapture) {
1051 ASSERT_NE(mInputReceiver, nullptr)
1052 << "Cannot consume events from a window with no receiver";
1053 mInputReceiver->consumeCaptureEvent(hasCapture);
1054 }
1055
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001056 void consumeEvent(int32_t expectedEventType, int32_t expectedAction,
1057 std::optional<int32_t> expectedDisplayId,
1058 std::optional<int32_t> expectedFlags) {
chaviwd1c23182019-12-20 18:44:56 -08001059 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
1060 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
1061 expectedFlags);
1062 }
1063
arthurhungb89ccb02020-12-30 16:19:01 +08001064 void consumeDragEvent(bool isExiting, float x, float y) {
1065 mInputReceiver->consumeDragEvent(isExiting, x, y);
1066 }
1067
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001068 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001069 if (mInputReceiver == nullptr) {
1070 ADD_FAILURE() << "Invalid receive event on window with no receiver";
1071 return std::nullopt;
1072 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001073 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001074 }
1075
1076 void finishEvent(uint32_t sequenceNum) {
1077 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1078 mInputReceiver->finishEvent(sequenceNum);
1079 }
1080
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001081 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
1082 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1083 mInputReceiver->sendTimeline(inputEventId, timeline);
1084 }
1085
chaviwaf87b3e2019-10-01 16:59:28 -07001086 InputEvent* consume() {
1087 if (mInputReceiver == nullptr) {
1088 return nullptr;
1089 }
1090 return mInputReceiver->consume();
1091 }
1092
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001093 MotionEvent* consumeMotion() {
1094 InputEvent* event = consume();
1095 if (event == nullptr) {
1096 ADD_FAILURE() << "Consume failed : no event";
1097 return nullptr;
1098 }
1099 if (event->getType() != AINPUT_EVENT_TYPE_MOTION) {
1100 ADD_FAILURE() << "Instead of motion event, got "
1101 << inputEventTypeToString(event->getType());
1102 return nullptr;
1103 }
1104 return static_cast<MotionEvent*>(event);
1105 }
1106
Arthur Hungb92218b2018-08-14 12:00:21 +08001107 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001108 if (mInputReceiver == nullptr &&
chaviw3277faf2021-05-19 16:45:23 -05001109 mInfo.inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL)) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001110 return; // Can't receive events if the window does not have input channel
1111 }
1112 ASSERT_NE(nullptr, mInputReceiver)
1113 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -08001114 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +08001115 }
1116
chaviwaf87b3e2019-10-01 16:59:28 -07001117 sp<IBinder> getToken() { return mInfo.token; }
1118
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001119 const std::string& getName() { return mName; }
1120
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001121 void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) {
1122 mInfo.ownerPid = ownerPid;
1123 mInfo.ownerUid = ownerUid;
1124 }
1125
chaviwd1c23182019-12-20 18:44:56 -08001126private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001127 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -08001128 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001129 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001130};
1131
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001132std::atomic<int32_t> FakeWindowHandle::sId{1};
1133
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001134static InputEventInjectionResult injectKey(
1135 const sp<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
1136 int32_t displayId = ADISPLAY_ID_NONE,
1137 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001138 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
1139 bool allowKeyRepeat = true) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001140 KeyEvent event;
1141 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1142
1143 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001144 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001145 INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE,
1146 repeatCount, currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001147
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001148 int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
1149 if (!allowKeyRepeat) {
1150 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
1151 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001152 // Inject event until dispatch out.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001153 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, syncMode,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001154 injectionTimeout, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001155}
1156
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001157static InputEventInjectionResult injectKeyDown(const sp<InputDispatcher>& dispatcher,
1158 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001159 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId);
1160}
1161
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001162// Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without
1163// sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it
1164// has to be woken up to process the repeating key.
1165static InputEventInjectionResult injectKeyDownNoRepeat(const sp<InputDispatcher>& dispatcher,
1166 int32_t displayId = ADISPLAY_ID_NONE) {
1167 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId,
1168 InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT,
1169 /* allowKeyRepeat */ false);
1170}
1171
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001172static InputEventInjectionResult injectKeyUp(const sp<InputDispatcher>& dispatcher,
1173 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001174 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId);
1175}
1176
Garfield Tandf26e862020-07-01 20:18:19 -07001177class PointerBuilder {
1178public:
1179 PointerBuilder(int32_t id, int32_t toolType) {
1180 mProperties.clear();
1181 mProperties.id = id;
1182 mProperties.toolType = toolType;
1183 mCoords.clear();
1184 }
1185
1186 PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); }
1187
1188 PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); }
1189
1190 PointerBuilder& axis(int32_t axis, float value) {
1191 mCoords.setAxisValue(axis, value);
1192 return *this;
1193 }
1194
1195 PointerProperties buildProperties() const { return mProperties; }
1196
1197 PointerCoords buildCoords() const { return mCoords; }
1198
1199private:
1200 PointerProperties mProperties;
1201 PointerCoords mCoords;
1202};
1203
1204class MotionEventBuilder {
1205public:
1206 MotionEventBuilder(int32_t action, int32_t source) {
1207 mAction = action;
1208 mSource = source;
1209 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1210 }
1211
1212 MotionEventBuilder& eventTime(nsecs_t eventTime) {
1213 mEventTime = eventTime;
1214 return *this;
1215 }
1216
1217 MotionEventBuilder& displayId(int32_t displayId) {
1218 mDisplayId = displayId;
1219 return *this;
1220 }
1221
1222 MotionEventBuilder& actionButton(int32_t actionButton) {
1223 mActionButton = actionButton;
1224 return *this;
1225 }
1226
arthurhung6d4bed92021-03-17 11:59:33 +08001227 MotionEventBuilder& buttonState(int32_t buttonState) {
1228 mButtonState = buttonState;
Garfield Tandf26e862020-07-01 20:18:19 -07001229 return *this;
1230 }
1231
1232 MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) {
1233 mRawXCursorPosition = rawXCursorPosition;
1234 return *this;
1235 }
1236
1237 MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) {
1238 mRawYCursorPosition = rawYCursorPosition;
1239 return *this;
1240 }
1241
1242 MotionEventBuilder& pointer(PointerBuilder pointer) {
1243 mPointers.push_back(pointer);
1244 return *this;
1245 }
1246
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001247 MotionEventBuilder& addFlag(uint32_t flags) {
1248 mFlags |= flags;
1249 return *this;
1250 }
1251
Garfield Tandf26e862020-07-01 20:18:19 -07001252 MotionEvent build() {
1253 std::vector<PointerProperties> pointerProperties;
1254 std::vector<PointerCoords> pointerCoords;
1255 for (const PointerBuilder& pointer : mPointers) {
1256 pointerProperties.push_back(pointer.buildProperties());
1257 pointerCoords.push_back(pointer.buildCoords());
1258 }
1259
1260 // Set mouse cursor position for the most common cases to avoid boilerplate.
1261 if (mSource == AINPUT_SOURCE_MOUSE &&
1262 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) &&
1263 mPointers.size() == 1) {
1264 mRawXCursorPosition = pointerCoords[0].getX();
1265 mRawYCursorPosition = pointerCoords[0].getY();
1266 }
1267
1268 MotionEvent event;
chaviw9eaa22c2020-07-01 16:21:27 -07001269 ui::Transform identityTransform;
Garfield Tandf26e862020-07-01 20:18:19 -07001270 event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001271 mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE,
chaviw9eaa22c2020-07-01 16:21:27 -07001272 mButtonState, MotionClassification::NONE, identityTransform,
1273 /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition,
Evan Rosky09576692021-07-01 12:22:09 -07001274 mRawYCursorPosition, mDisplayOrientation, mDisplayWidth, mDisplayHeight,
1275 mEventTime, mEventTime, mPointers.size(), pointerProperties.data(),
1276 pointerCoords.data());
Garfield Tandf26e862020-07-01 20:18:19 -07001277
1278 return event;
1279 }
1280
1281private:
1282 int32_t mAction;
1283 int32_t mSource;
1284 nsecs_t mEventTime;
1285 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1286 int32_t mActionButton{0};
1287 int32_t mButtonState{0};
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001288 int32_t mFlags{0};
Garfield Tandf26e862020-07-01 20:18:19 -07001289 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1290 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
Evan Rosky09576692021-07-01 12:22:09 -07001291 uint32_t mDisplayOrientation{ui::Transform::ROT_0};
chaviw3277faf2021-05-19 16:45:23 -05001292 int32_t mDisplayWidth{INVALID_DISPLAY_SIZE};
1293 int32_t mDisplayHeight{INVALID_DISPLAY_SIZE};
Garfield Tandf26e862020-07-01 20:18:19 -07001294
1295 std::vector<PointerBuilder> mPointers;
1296};
1297
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001298static InputEventInjectionResult injectMotionEvent(
Garfield Tandf26e862020-07-01 20:18:19 -07001299 const sp<InputDispatcher>& dispatcher, const MotionEvent& event,
1300 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001301 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT) {
Garfield Tandf26e862020-07-01 20:18:19 -07001302 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, injectionMode,
1303 injectionTimeout,
1304 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
1305}
1306
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001307static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001308 const sp<InputDispatcher>& dispatcher, int32_t action, int32_t source, int32_t displayId,
1309 const PointF& position,
1310 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001311 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1312 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001313 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001314 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC)) {
Garfield Tandf26e862020-07-01 20:18:19 -07001315 MotionEvent event = MotionEventBuilder(action, source)
1316 .displayId(displayId)
1317 .eventTime(eventTime)
1318 .rawXCursorPosition(cursorPosition.x)
1319 .rawYCursorPosition(cursorPosition.y)
1320 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1321 .x(position.x)
1322 .y(position.y))
1323 .build();
Arthur Hungb92218b2018-08-14 12:00:21 +08001324
1325 // Inject event until dispatch out.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001326 return injectMotionEvent(dispatcher, event, injectionTimeout, injectionMode);
Arthur Hungb92218b2018-08-14 12:00:21 +08001327}
1328
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001329static InputEventInjectionResult injectMotionDown(const sp<InputDispatcher>& dispatcher,
1330 int32_t source, int32_t displayId,
1331 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001332 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001333}
1334
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001335static InputEventInjectionResult injectMotionUp(const sp<InputDispatcher>& dispatcher,
1336 int32_t source, int32_t displayId,
1337 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001338 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001339}
1340
Jackal Guof9696682018-10-05 12:23:23 +08001341static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1342 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1343 // Define a valid key event.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001344 NotifyKeyArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1345 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A,
1346 KEY_A, AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001347
1348 return args;
1349}
1350
chaviwd1c23182019-12-20 18:44:56 -08001351static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId,
1352 const std::vector<PointF>& points) {
1353 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001354 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1355 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1356 }
1357
chaviwd1c23182019-12-20 18:44:56 -08001358 PointerProperties pointerProperties[pointerCount];
1359 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001360
chaviwd1c23182019-12-20 18:44:56 -08001361 for (size_t i = 0; i < pointerCount; i++) {
1362 pointerProperties[i].clear();
1363 pointerProperties[i].id = i;
1364 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001365
chaviwd1c23182019-12-20 18:44:56 -08001366 pointerCoords[i].clear();
1367 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1368 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1369 }
Jackal Guof9696682018-10-05 12:23:23 +08001370
1371 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1372 // Define a valid motion event.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001373 NotifyMotionArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001374 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1375 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001376 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1377 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001378 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1379 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001380
1381 return args;
1382}
1383
chaviwd1c23182019-12-20 18:44:56 -08001384static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1385 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1386}
1387
Prabir Pradhan99987712020-11-10 18:43:05 -08001388static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(bool enabled) {
1389 return NotifyPointerCaptureChangedArgs(/* id */ 0, systemTime(SYSTEM_TIME_MONOTONIC), enabled);
1390}
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
liushenxiang42232912021-05-21 20:24:09 +08002888TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
2889 sendAndConsumeKeyDown(DEVICE_ID);
2890 expectKeyRepeatOnce(1 /*repeatCount*/);
2891 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
2892 mDispatcher->notifyDeviceReset(&args);
2893 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
2894 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
2895 mWindow->assertNoEvents();
2896}
2897
Garfield Tan1c7bc862020-01-28 13:24:04 -08002898TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Chris Ye2ad95392020-09-01 13:44:44 -07002899 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002900 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2901 InputEvent* repeatEvent = mWindow->consume();
2902 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2903 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
2904 IdGenerator::getSource(repeatEvent->getId()));
2905 }
2906}
2907
2908TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Chris Ye2ad95392020-09-01 13:44:44 -07002909 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002910
2911 std::unordered_set<int32_t> idSet;
2912 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2913 InputEvent* repeatEvent = mWindow->consume();
2914 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2915 int32_t id = repeatEvent->getId();
2916 EXPECT_EQ(idSet.end(), idSet.find(id));
2917 idSet.insert(id);
2918 }
2919}
2920
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002921/* Test InputDispatcher for MultiDisplay */
2922class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
2923public:
2924 static constexpr int32_t SECOND_DISPLAY_ID = 1;
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002925 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002926 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08002927
Chris Yea209fde2020-07-22 13:54:51 -07002928 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002929 windowInPrimary =
2930 new FakeWindowHandle(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002931
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002932 // Set focus window for primary display, but focused display would be second one.
2933 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07002934 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002935 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002936 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002937 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08002938
Chris Yea209fde2020-07-22 13:54:51 -07002939 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002940 windowInSecondary =
2941 new FakeWindowHandle(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002942 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002943 // Set focus display to second one.
2944 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
2945 // Set focus window for second display.
2946 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07002947 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002948 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002949 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002950 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002951 }
2952
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002953 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002954 InputDispatcherTest::TearDown();
2955
Chris Yea209fde2020-07-22 13:54:51 -07002956 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002957 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07002958 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002959 windowInSecondary.clear();
2960 }
2961
2962protected:
Chris Yea209fde2020-07-22 13:54:51 -07002963 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002964 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07002965 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002966 sp<FakeWindowHandle> windowInSecondary;
2967};
2968
2969TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
2970 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002971 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2972 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2973 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002974 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08002975 windowInSecondary->assertNoEvents();
2976
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002977 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002978 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2979 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
2980 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08002981 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002982 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08002983}
2984
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002985TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08002986 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08002987 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2988 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002989 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002990 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08002991 windowInSecondary->assertNoEvents();
2992
2993 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08002994 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002995 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08002996 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002997 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08002998
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002999 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00003000 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08003001
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003002 // Old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003003 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
3004 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08003005
3006 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003007 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003008 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08003009 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003010 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08003011 windowInSecondary->assertNoEvents();
3012}
3013
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003014// Test per-display input monitors for motion event.
3015TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08003016 FakeMonitorReceiver monitorInPrimary =
3017 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
3018 FakeMonitorReceiver monitorInSecondary =
3019 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003020
3021 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003022 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3023 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
3024 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003025 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08003026 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003027 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003028 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003029
3030 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003031 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3032 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
3033 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003034 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003035 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003036 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08003037 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003038
3039 // Test inject a non-pointer motion event.
3040 // If specific a display, it will dispatch to the focused window of particular display,
3041 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003042 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3043 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
3044 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003045 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003046 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003047 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08003048 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003049}
3050
3051// Test per-display input monitors for key event.
3052TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003053 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08003054 FakeMonitorReceiver monitorInPrimary =
3055 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
3056 FakeMonitorReceiver monitorInSecondary =
3057 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003058
3059 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003060 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3061 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003062 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003063 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003064 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08003065 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003066}
3067
Vishnu Nair958da932020-08-21 17:12:37 -07003068TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
3069 sp<FakeWindowHandle> secondWindowInPrimary =
3070 new FakeWindowHandle(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
3071 secondWindowInPrimary->setFocusable(true);
3072 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
3073 setFocusedWindow(secondWindowInPrimary);
3074 windowInPrimary->consumeFocusEvent(false);
3075 secondWindowInPrimary->consumeFocusEvent(true);
3076
3077 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003078 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
3079 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003080 windowInPrimary->assertNoEvents();
3081 windowInSecondary->assertNoEvents();
3082 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3083}
3084
Jackal Guof9696682018-10-05 12:23:23 +08003085class InputFilterTest : public InputDispatcherTest {
3086protected:
3087 static constexpr int32_t SECOND_DISPLAY_ID = 1;
3088
3089 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) {
3090 NotifyMotionArgs motionArgs;
3091
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003092 motionArgs =
3093 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08003094 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003095 motionArgs =
3096 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08003097 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003098 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08003099 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08003100 mFakePolicy->assertFilterInputEventWasCalled(motionArgs);
Jackal Guof9696682018-10-05 12:23:23 +08003101 } else {
3102 mFakePolicy->assertFilterInputEventWasNotCalled();
3103 }
3104 }
3105
3106 void testNotifyKey(bool expectToBeFiltered) {
3107 NotifyKeyArgs keyArgs;
3108
3109 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
3110 mDispatcher->notifyKey(&keyArgs);
3111 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
3112 mDispatcher->notifyKey(&keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003113 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08003114
3115 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08003116 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08003117 } else {
3118 mFakePolicy->assertFilterInputEventWasNotCalled();
3119 }
3120 }
3121};
3122
3123// Test InputFilter for MotionEvent
3124TEST_F(InputFilterTest, MotionEvent_InputFilter) {
3125 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
3126 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
3127 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
3128
3129 // Enable InputFilter
3130 mDispatcher->setInputFilterEnabled(true);
3131 // Test touch on both primary and second display, and check if both events are filtered.
3132 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
3133 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
3134
3135 // Disable InputFilter
3136 mDispatcher->setInputFilterEnabled(false);
3137 // Test touch on both primary and second display, and check if both events aren't filtered.
3138 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
3139 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
3140}
3141
3142// Test InputFilter for KeyEvent
3143TEST_F(InputFilterTest, KeyEvent_InputFilter) {
3144 // Since the InputFilter is disabled by default, check if key event aren't filtered.
3145 testNotifyKey(/*expectToBeFiltered*/ false);
3146
3147 // Enable InputFilter
3148 mDispatcher->setInputFilterEnabled(true);
3149 // Send a key event, and check if it is filtered.
3150 testNotifyKey(/*expectToBeFiltered*/ true);
3151
3152 // Disable InputFilter
3153 mDispatcher->setInputFilterEnabled(false);
3154 // Send a key event, and check if it isn't filtered.
3155 testNotifyKey(/*expectToBeFiltered*/ false);
3156}
3157
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003158class InputFilterInjectionPolicyTest : public InputDispatcherTest {
3159protected:
3160 virtual void SetUp() override {
3161 InputDispatcherTest::SetUp();
3162
3163 /**
3164 * We don't need to enable input filter to test the injected event policy, but we enabled it
3165 * here to make the tests more realistic, since this policy only matters when inputfilter is
3166 * on.
3167 */
3168 mDispatcher->setInputFilterEnabled(true);
3169
3170 std::shared_ptr<InputApplicationHandle> application =
3171 std::make_shared<FakeApplicationHandle>();
3172 mWindow =
3173 new FakeWindowHandle(application, mDispatcher, "Test Window", ADISPLAY_ID_DEFAULT);
3174
3175 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3176 mWindow->setFocusable(true);
3177 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3178 setFocusedWindow(mWindow);
3179 mWindow->consumeFocusEvent(true);
3180 }
3181
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003182 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
3183 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003184 KeyEvent event;
3185
3186 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
3187 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
3188 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
3189 KEY_A, AMETA_NONE, 0 /*repeatCount*/, eventTime, eventTime);
3190 const int32_t additionalPolicyFlags =
3191 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
3192 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3193 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
3194 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
3195 policyFlags | additionalPolicyFlags));
3196
3197 InputEvent* received = mWindow->consume();
3198 ASSERT_NE(nullptr, received);
3199 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003200 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_KEY);
3201 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
3202 ASSERT_EQ(flags, keyEvent.getFlags());
3203 }
3204
3205 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
3206 int32_t flags) {
3207 MotionEvent event;
3208 PointerProperties pointerProperties[1];
3209 PointerCoords pointerCoords[1];
3210 pointerProperties[0].clear();
3211 pointerProperties[0].id = 0;
3212 pointerCoords[0].clear();
3213 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
3214 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
3215
3216 ui::Transform identityTransform;
3217 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
3218 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
3219 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
3220 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
3221 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -07003222 AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0,
3223 0 /*INVALID_DISPLAY_SIZE*/, 0 /*INVALID_DISPLAY_SIZE*/, eventTime,
3224 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003225 /*pointerCount*/ 1, pointerProperties, pointerCoords);
3226
3227 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
3228 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3229 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
3230 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
3231 policyFlags | additionalPolicyFlags));
3232
3233 InputEvent* received = mWindow->consume();
3234 ASSERT_NE(nullptr, received);
3235 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
3236 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_MOTION);
3237 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
3238 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003239 }
3240
3241private:
3242 sp<FakeWindowHandle> mWindow;
3243};
3244
3245TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003246 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
3247 // filter. Without it, the event will no different from a regularly injected event, and the
3248 // injected device id will be overwritten.
3249 testInjectedKey(POLICY_FLAG_FILTERED, 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
3250 0 /*flags*/);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003251}
3252
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003253TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003254 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003255 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
3256 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
3257}
3258
3259TEST_F(InputFilterInjectionPolicyTest,
3260 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
3261 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
3262 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
3263 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003264}
3265
3266TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
3267 testInjectedKey(0 /*policyFlags*/, 3 /*injectedDeviceId*/,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003268 VIRTUAL_KEYBOARD_ID /*resolvedDeviceId*/, 0 /*flags*/);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003269}
3270
chaviwfd6d3512019-03-25 13:23:49 -07003271class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07003272 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07003273 InputDispatcherTest::SetUp();
3274
Chris Yea209fde2020-07-22 13:54:51 -07003275 std::shared_ptr<FakeApplicationHandle> application =
3276 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003277 mUnfocusedWindow =
3278 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07003279 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
3280 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3281 // window.
chaviw3277faf2021-05-19 16:45:23 -05003282 mUnfocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07003283
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003284 mFocusedWindow =
3285 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
3286 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviw3277faf2021-05-19 16:45:23 -05003287 mFocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07003288
3289 // Set focused application.
3290 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07003291 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07003292
3293 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00003294 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003295 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003296 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07003297 }
3298
Prabir Pradhan3608aad2019-10-02 17:08:26 -07003299 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07003300 InputDispatcherTest::TearDown();
3301
3302 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003303 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07003304 }
3305
3306protected:
3307 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003308 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003309 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07003310};
3311
3312// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
3313// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
3314// the onPointerDownOutsideFocus callback.
3315TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003316 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003317 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3318 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003319 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003320 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07003321
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003322 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07003323 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
3324}
3325
3326// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
3327// DOWN on the window that doesn't have focus. Ensure no window received the
3328// onPointerDownOutsideFocus callback.
3329TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003330 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003331 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003332 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003333 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07003334
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003335 ASSERT_TRUE(mDispatcher->waitForIdle());
3336 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07003337}
3338
3339// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
3340// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
3341TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003342 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3343 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003344 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003345 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07003346
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003347 ASSERT_TRUE(mDispatcher->waitForIdle());
3348 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07003349}
3350
3351// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
3352// DOWN on the window that already has focus. Ensure no window received the
3353// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003354TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003355 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003356 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003357 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003358 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003359 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07003360
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003361 ASSERT_TRUE(mDispatcher->waitForIdle());
3362 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07003363}
3364
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08003365// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
3366// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
3367TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
3368 const MotionEvent event =
3369 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3370 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
3371 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(20).y(20))
3372 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
3373 .build();
3374 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
3375 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3376 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
3377
3378 ASSERT_TRUE(mDispatcher->waitForIdle());
3379 mFakePolicy->assertOnPointerDownWasNotCalled();
3380 // Ensure that the unfocused window did not receive any FOCUS events.
3381 mUnfocusedWindow->assertNoEvents();
3382}
3383
chaviwaf87b3e2019-10-01 16:59:28 -07003384// These tests ensures we can send touch events to a single client when there are multiple input
3385// windows that point to the same client token.
3386class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
3387 virtual void SetUp() override {
3388 InputDispatcherTest::SetUp();
3389
Chris Yea209fde2020-07-22 13:54:51 -07003390 std::shared_ptr<FakeApplicationHandle> application =
3391 std::make_shared<FakeApplicationHandle>();
chaviwaf87b3e2019-10-01 16:59:28 -07003392 mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1",
3393 ADISPLAY_ID_DEFAULT);
3394 // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window.
3395 // We also need FLAG_SPLIT_TOUCH or we won't be able to get touches for both windows.
chaviw3277faf2021-05-19 16:45:23 -05003396 mWindow1->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07003397 mWindow1->setFrame(Rect(0, 0, 100, 100));
3398
3399 mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2",
3400 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviw3277faf2021-05-19 16:45:23 -05003401 mWindow2->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07003402 mWindow2->setFrame(Rect(100, 100, 200, 200));
3403
Arthur Hung72d8dc32020-03-28 00:48:39 +00003404 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07003405 }
3406
3407protected:
3408 sp<FakeWindowHandle> mWindow1;
3409 sp<FakeWindowHandle> mWindow2;
3410
3411 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05003412 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07003413 vec2 vals = windowInfo->transform.transform(point.x, point.y);
3414 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07003415 }
3416
3417 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
3418 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003419 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07003420 InputEvent* event = window->consume();
3421
3422 ASSERT_NE(nullptr, event) << name.c_str()
3423 << ": consumer should have returned non-NULL event.";
3424
3425 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
3426 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
3427 << " event, got " << inputEventTypeToString(event->getType()) << " event";
3428
3429 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
3430 EXPECT_EQ(expectedAction, motionEvent.getAction());
3431
3432 for (size_t i = 0; i < points.size(); i++) {
3433 float expectedX = points[i].x;
3434 float expectedY = points[i].y;
3435
3436 EXPECT_EQ(expectedX, motionEvent.getX(i))
3437 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
3438 << ", got " << motionEvent.getX(i);
3439 EXPECT_EQ(expectedY, motionEvent.getY(i))
3440 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
3441 << ", got " << motionEvent.getY(i);
3442 }
3443 }
chaviw9eaa22c2020-07-01 16:21:27 -07003444
3445 void touchAndAssertPositions(int32_t action, std::vector<PointF> touchedPoints,
3446 std::vector<PointF> expectedPoints) {
3447 NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
3448 ADISPLAY_ID_DEFAULT, touchedPoints);
3449 mDispatcher->notifyMotion(&motionArgs);
3450
3451 // Always consume from window1 since it's the window that has the InputReceiver
3452 consumeMotionEvent(mWindow1, action, expectedPoints);
3453 }
chaviwaf87b3e2019-10-01 16:59:28 -07003454};
3455
3456TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
3457 // Touch Window 1
3458 PointF touchedPoint = {10, 10};
3459 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003460 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003461
3462 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07003463 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003464
3465 // Touch Window 2
3466 touchedPoint = {150, 150};
3467 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003468 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003469}
3470
chaviw9eaa22c2020-07-01 16:21:27 -07003471TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
3472 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07003473 mWindow2->setWindowScale(0.5f, 0.5f);
3474
3475 // Touch Window 1
3476 PointF touchedPoint = {10, 10};
3477 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003478 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003479 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07003480 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003481
3482 // Touch Window 2
3483 touchedPoint = {150, 150};
3484 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003485 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
3486 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003487
chaviw9eaa22c2020-07-01 16:21:27 -07003488 // Update the transform so rotation is set
3489 mWindow2->setWindowTransform(0, -1, 1, 0);
3490 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
3491 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003492}
3493
chaviw9eaa22c2020-07-01 16:21:27 -07003494TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003495 mWindow2->setWindowScale(0.5f, 0.5f);
3496
3497 // Touch Window 1
3498 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3499 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003500 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003501
3502 // Touch Window 2
3503 int32_t actionPointerDown =
3504 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003505 touchedPoints.push_back(PointF{150, 150});
3506 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3507 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003508
chaviw9eaa22c2020-07-01 16:21:27 -07003509 // Release Window 2
3510 int32_t actionPointerUp =
3511 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3512 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
3513 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003514
chaviw9eaa22c2020-07-01 16:21:27 -07003515 // Update the transform so rotation is set for Window 2
3516 mWindow2->setWindowTransform(0, -1, 1, 0);
3517 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3518 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003519}
3520
chaviw9eaa22c2020-07-01 16:21:27 -07003521TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003522 mWindow2->setWindowScale(0.5f, 0.5f);
3523
3524 // Touch Window 1
3525 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3526 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003527 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003528
3529 // Touch Window 2
3530 int32_t actionPointerDown =
3531 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003532 touchedPoints.push_back(PointF{150, 150});
3533 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003534
chaviw9eaa22c2020-07-01 16:21:27 -07003535 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003536
3537 // Move both windows
3538 touchedPoints = {{20, 20}, {175, 175}};
3539 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3540 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3541
chaviw9eaa22c2020-07-01 16:21:27 -07003542 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003543
chaviw9eaa22c2020-07-01 16:21:27 -07003544 // Release Window 2
3545 int32_t actionPointerUp =
3546 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3547 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
3548 expectedPoints.pop_back();
3549
3550 // Touch Window 2
3551 mWindow2->setWindowTransform(0, -1, 1, 0);
3552 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3553 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
3554
3555 // Move both windows
3556 touchedPoints = {{20, 20}, {175, 175}};
3557 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3558 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3559
3560 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003561}
3562
3563TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
3564 mWindow1->setWindowScale(0.5f, 0.5f);
3565
3566 // Touch Window 1
3567 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3568 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003569 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003570
3571 // Touch Window 2
3572 int32_t actionPointerDown =
3573 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003574 touchedPoints.push_back(PointF{150, 150});
3575 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003576
chaviw9eaa22c2020-07-01 16:21:27 -07003577 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003578
3579 // Move both windows
3580 touchedPoints = {{20, 20}, {175, 175}};
3581 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3582 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3583
chaviw9eaa22c2020-07-01 16:21:27 -07003584 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003585}
3586
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003587class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
3588 virtual void SetUp() override {
3589 InputDispatcherTest::SetUp();
3590
Chris Yea209fde2020-07-22 13:54:51 -07003591 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003592 mApplication->setDispatchingTimeout(20ms);
3593 mWindow =
3594 new FakeWindowHandle(mApplication, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
3595 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05003596 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07003597 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003598 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3599 // window.
chaviw3277faf2021-05-19 16:45:23 -05003600 mWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003601
3602 // Set focused application.
3603 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
3604
3605 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003606 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003607 mWindow->consumeFocusEvent(true);
3608 }
3609
3610 virtual void TearDown() override {
3611 InputDispatcherTest::TearDown();
3612 mWindow.clear();
3613 }
3614
3615protected:
Chris Yea209fde2020-07-22 13:54:51 -07003616 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003617 sp<FakeWindowHandle> mWindow;
3618 static constexpr PointF WINDOW_LOCATION = {20, 20};
3619
3620 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003621 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003622 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3623 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003624 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003625 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3626 WINDOW_LOCATION));
3627 }
3628};
3629
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003630// Send a tap and respond, which should not cause an ANR.
3631TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
3632 tapOnWindow();
3633 mWindow->consumeMotionDown();
3634 mWindow->consumeMotionUp();
3635 ASSERT_TRUE(mDispatcher->waitForIdle());
3636 mFakePolicy->assertNotifyAnrWasNotCalled();
3637}
3638
3639// Send a regular key and respond, which should not cause an ANR.
3640TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003641 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003642 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
3643 ASSERT_TRUE(mDispatcher->waitForIdle());
3644 mFakePolicy->assertNotifyAnrWasNotCalled();
3645}
3646
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003647TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
3648 mWindow->setFocusable(false);
3649 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3650 mWindow->consumeFocusEvent(false);
3651
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003652 InputEventInjectionResult result =
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003653 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003654 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/,
3655 false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003656 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003657 // Key will not go to window because we have no focused window.
3658 // The 'no focused window' ANR timer should start instead.
3659
3660 // Now, the focused application goes away.
3661 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
3662 // The key should get dropped and there should be no ANR.
3663
3664 ASSERT_TRUE(mDispatcher->waitForIdle());
3665 mFakePolicy->assertNotifyAnrWasNotCalled();
3666}
3667
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003668// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003669// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
3670// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003671TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003672 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003673 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3674 WINDOW_LOCATION));
3675
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003676 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
3677 ASSERT_TRUE(sequenceNum);
3678 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003679 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003680
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003681 mWindow->finishEvent(*sequenceNum);
3682 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3683 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003684 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003685 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003686}
3687
3688// Send a key to the app and have the app not respond right away.
3689TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
3690 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003691 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003692 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
3693 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003694 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003695 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003696 ASSERT_TRUE(mDispatcher->waitForIdle());
3697}
3698
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003699// We have a focused application, but no focused window
3700TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003701 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003702 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3703 mWindow->consumeFocusEvent(false);
3704
3705 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003706 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003707 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3708 WINDOW_LOCATION));
3709 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
3710 mDispatcher->waitForIdle();
3711 mFakePolicy->assertNotifyAnrWasNotCalled();
3712
3713 // Once a focused event arrives, we get an ANR for this application
3714 // We specify the injection timeout to be smaller than the application timeout, to ensure that
3715 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003716 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003717 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003718 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003719 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003720 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003721 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003722 ASSERT_TRUE(mDispatcher->waitForIdle());
3723}
3724
3725// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003726// Make sure that we don't notify policy twice about the same ANR.
3727TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003728 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003729 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3730 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003731
3732 // Once a focused event arrives, we get an ANR for this application
3733 // We specify the injection timeout to be smaller than the application timeout, to ensure that
3734 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003735 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003736 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003737 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003738 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003739 const std::chrono::duration appTimeout =
3740 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003741 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003742
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003743 std::this_thread::sleep_for(appTimeout);
3744 // ANR should not be raised again. It is up to policy to do that if it desires.
3745 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003746
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003747 // If we now get a focused window, the ANR should stop, but the policy handles that via
3748 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003749 ASSERT_TRUE(mDispatcher->waitForIdle());
3750}
3751
3752// We have a focused application, but no focused window
3753TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003754 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003755 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3756 mWindow->consumeFocusEvent(false);
3757
3758 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003759 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003760 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003761 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3762 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003763
3764 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003765 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003766
3767 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003768 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003769 ASSERT_TRUE(mDispatcher->waitForIdle());
3770 mWindow->assertNoEvents();
3771}
3772
3773/**
3774 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
3775 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
3776 * If we process 1 of the events, but ANR on the second event with the same timestamp,
3777 * the ANR mechanism should still work.
3778 *
3779 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
3780 * DOWN event, while not responding on the second one.
3781 */
3782TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
3783 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
3784 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3785 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
3786 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
3787 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003788 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003789
3790 // Now send ACTION_UP, with identical timestamp
3791 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3792 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
3793 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
3794 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003795 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003796
3797 // We have now sent down and up. Let's consume first event and then ANR on the second.
3798 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3799 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003800 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003801}
3802
3803// If an app is not responding to a key event, gesture monitors should continue to receive
3804// new motion events
3805TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnKey) {
3806 FakeMonitorReceiver monitor =
3807 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
3808 true /*isGestureMonitor*/);
3809
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003810 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3811 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003812 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003813 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003814
3815 // Stuck on the ACTION_UP
3816 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003817 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003818
3819 // New tap will go to the gesture monitor, but not to the window
3820 tapOnWindow();
3821 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3822 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3823
3824 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
3825 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003826 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003827 mWindow->assertNoEvents();
3828 monitor.assertNoEvents();
3829}
3830
3831// If an app is not responding to a motion event, gesture monitors should continue to receive
3832// new motion events
3833TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnMotion) {
3834 FakeMonitorReceiver monitor =
3835 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
3836 true /*isGestureMonitor*/);
3837
3838 tapOnWindow();
3839 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3840 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3841
3842 mWindow->consumeMotionDown();
3843 // Stuck on the ACTION_UP
3844 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003845 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003846
3847 // New tap will go to the gesture monitor, but not to the window
3848 tapOnWindow();
3849 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3850 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3851
3852 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
3853 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003854 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003855 mWindow->assertNoEvents();
3856 monitor.assertNoEvents();
3857}
3858
3859// If a window is unresponsive, then you get anr. if the window later catches up and starts to
3860// process events, you don't get an anr. When the window later becomes unresponsive again, you
3861// get an ANR again.
3862// 1. tap -> block on ACTION_UP -> receive ANR
3863// 2. consume all pending events (= queue becomes healthy again)
3864// 3. tap again -> block on ACTION_UP again -> receive ANR second time
3865TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
3866 tapOnWindow();
3867
3868 mWindow->consumeMotionDown();
3869 // Block on ACTION_UP
3870 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003871 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003872 mWindow->consumeMotionUp(); // Now the connection should be healthy again
3873 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003874 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003875 mWindow->assertNoEvents();
3876
3877 tapOnWindow();
3878 mWindow->consumeMotionDown();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003879 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003880 mWindow->consumeMotionUp();
3881
3882 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003883 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003884 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003885 mWindow->assertNoEvents();
3886}
3887
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003888// If a connection remains unresponsive for a while, make sure policy is only notified once about
3889// it.
3890TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003891 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003892 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3893 WINDOW_LOCATION));
3894
3895 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003896 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003897 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003898 // 'notifyConnectionUnresponsive' should only be called once per connection
3899 mFakePolicy->assertNotifyAnrWasNotCalled();
3900 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003901 mWindow->consumeMotionDown();
3902 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3903 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3904 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003905 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003906 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003907 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003908}
3909
3910/**
3911 * If a window is processing a motion event, and then a key event comes in, the key event should
3912 * not to to the focused window until the motion is processed.
3913 *
3914 * Warning!!!
3915 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
3916 * and the injection timeout that we specify when injecting the key.
3917 * We must have the injection timeout (10ms) be smaller than
3918 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
3919 *
3920 * If that value changes, this test should also change.
3921 */
3922TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
3923 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3924 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3925
3926 tapOnWindow();
3927 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3928 ASSERT_TRUE(downSequenceNum);
3929 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3930 ASSERT_TRUE(upSequenceNum);
3931 // Don't finish the events yet, and send a key
3932 // Injection will "succeed" because we will eventually give up and send the key to the focused
3933 // window even if motions are still being processed. But because the injection timeout is short,
3934 // we will receive INJECTION_TIMED_OUT as the result.
3935
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003936 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003937 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003938 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3939 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003940 // Key will not be sent to the window, yet, because the window is still processing events
3941 // and the key remains pending, waiting for the touch events to be processed
3942 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3943 ASSERT_FALSE(keySequenceNum);
3944
3945 std::this_thread::sleep_for(500ms);
3946 // if we wait long enough though, dispatcher will give up, and still send the key
3947 // to the focused window, even though we have not yet finished the motion event
3948 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3949 mWindow->finishEvent(*downSequenceNum);
3950 mWindow->finishEvent(*upSequenceNum);
3951}
3952
3953/**
3954 * If a window is processing a motion event, and then a key event comes in, the key event should
3955 * not go to the focused window until the motion is processed.
3956 * If then a new motion comes in, then the pending key event should be going to the currently
3957 * focused window right away.
3958 */
3959TEST_F(InputDispatcherSingleWindowAnr,
3960 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
3961 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3962 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3963
3964 tapOnWindow();
3965 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3966 ASSERT_TRUE(downSequenceNum);
3967 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3968 ASSERT_TRUE(upSequenceNum);
3969 // Don't finish the events yet, and send a key
3970 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003971 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003972 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003973 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003974 // At this point, key is still pending, and should not be sent to the application yet.
3975 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3976 ASSERT_FALSE(keySequenceNum);
3977
3978 // Now tap down again. It should cause the pending key to go to the focused window right away.
3979 tapOnWindow();
3980 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
3981 // the other events yet. We can finish events in any order.
3982 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
3983 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
3984 mWindow->consumeMotionDown();
3985 mWindow->consumeMotionUp();
3986 mWindow->assertNoEvents();
3987}
3988
3989class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
3990 virtual void SetUp() override {
3991 InputDispatcherTest::SetUp();
3992
Chris Yea209fde2020-07-22 13:54:51 -07003993 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003994 mApplication->setDispatchingTimeout(10ms);
3995 mUnfocusedWindow =
3996 new FakeWindowHandle(mApplication, mDispatcher, "Unfocused", ADISPLAY_ID_DEFAULT);
3997 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
3998 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3999 // window.
4000 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
chaviw3277faf2021-05-19 16:45:23 -05004001 mUnfocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL |
4002 WindowInfo::Flag::WATCH_OUTSIDE_TOUCH |
4003 WindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004004
4005 mFocusedWindow =
4006 new FakeWindowHandle(mApplication, mDispatcher, "Focused", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05004007 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004008 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviw3277faf2021-05-19 16:45:23 -05004009 mFocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004010
4011 // Set focused application.
4012 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07004013 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004014
4015 // Expect one focus window exist in display.
4016 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004017 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004018 mFocusedWindow->consumeFocusEvent(true);
4019 }
4020
4021 virtual void TearDown() override {
4022 InputDispatcherTest::TearDown();
4023
4024 mUnfocusedWindow.clear();
4025 mFocusedWindow.clear();
4026 }
4027
4028protected:
Chris Yea209fde2020-07-22 13:54:51 -07004029 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004030 sp<FakeWindowHandle> mUnfocusedWindow;
4031 sp<FakeWindowHandle> mFocusedWindow;
4032 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
4033 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
4034 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
4035
4036 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
4037
4038 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
4039
4040private:
4041 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004042 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004043 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4044 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004045 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004046 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4047 location));
4048 }
4049};
4050
4051// If we have 2 windows that are both unresponsive, the one with the shortest timeout
4052// should be ANR'd first.
4053TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004054 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004055 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4056 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004057 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004058 mFocusedWindow->consumeMotionDown();
4059 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
4060 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
4061 // We consumed all events, so no ANR
4062 ASSERT_TRUE(mDispatcher->waitForIdle());
4063 mFakePolicy->assertNotifyAnrWasNotCalled();
4064
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004065 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004066 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4067 FOCUSED_WINDOW_LOCATION));
4068 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
4069 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004070
4071 const std::chrono::duration timeout =
4072 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004073 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004074 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
4075 // sequence to make it consistent
4076 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004077 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004078 mFocusedWindow->consumeMotionDown();
4079 // This cancel is generated because the connection was unresponsive
4080 mFocusedWindow->consumeMotionCancel();
4081 mFocusedWindow->assertNoEvents();
4082 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004083 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004084 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004085 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004086}
4087
4088// If we have 2 windows with identical timeouts that are both unresponsive,
4089// it doesn't matter which order they should have ANR.
4090// But we should receive ANR for both.
4091TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
4092 // Set the timeout for unfocused window to match the focused window
4093 mUnfocusedWindow->setDispatchingTimeout(10ms);
4094 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
4095
4096 tapOnFocusedWindow();
4097 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004098 sp<IBinder> anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms);
4099 sp<IBinder> anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004100
4101 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004102 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
4103 mFocusedWindow->getToken() == anrConnectionToken2);
4104 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
4105 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004106
4107 ASSERT_TRUE(mDispatcher->waitForIdle());
4108 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004109
4110 mFocusedWindow->consumeMotionDown();
4111 mFocusedWindow->consumeMotionUp();
4112 mUnfocusedWindow->consumeMotionOutside();
4113
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004114 sp<IBinder> responsiveToken1 = mFakePolicy->getResponsiveWindowToken();
4115 sp<IBinder> responsiveToken2 = mFakePolicy->getResponsiveWindowToken();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004116
4117 // Both applications should be marked as responsive, in any order
4118 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
4119 mFocusedWindow->getToken() == responsiveToken2);
4120 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
4121 mUnfocusedWindow->getToken() == responsiveToken2);
4122 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004123}
4124
4125// If a window is already not responding, the second tap on the same window should be ignored.
4126// We should also log an error to account for the dropped event (not tested here).
4127// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
4128TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
4129 tapOnFocusedWindow();
4130 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
4131 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
4132 // Receive the events, but don't respond
4133 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
4134 ASSERT_TRUE(downEventSequenceNum);
4135 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
4136 ASSERT_TRUE(upEventSequenceNum);
4137 const std::chrono::duration timeout =
4138 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004139 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004140
4141 // Tap once again
4142 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004143 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004144 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4145 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004146 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004147 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4148 FOCUSED_WINDOW_LOCATION));
4149 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
4150 // valid touch target
4151 mUnfocusedWindow->assertNoEvents();
4152
4153 // Consume the first tap
4154 mFocusedWindow->finishEvent(*downEventSequenceNum);
4155 mFocusedWindow->finishEvent(*upEventSequenceNum);
4156 ASSERT_TRUE(mDispatcher->waitForIdle());
4157 // The second tap did not go to the focused window
4158 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004159 // Since all events are finished, connection should be deemed healthy again
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004160 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004161 mFakePolicy->assertNotifyAnrWasNotCalled();
4162}
4163
4164// If you tap outside of all windows, there will not be ANR
4165TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004166 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004167 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4168 LOCATION_OUTSIDE_ALL_WINDOWS));
4169 ASSERT_TRUE(mDispatcher->waitForIdle());
4170 mFakePolicy->assertNotifyAnrWasNotCalled();
4171}
4172
4173// Since the focused window is paused, tapping on it should not produce any events
4174TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
4175 mFocusedWindow->setPaused(true);
4176 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
4177
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004178 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004179 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4180 FOCUSED_WINDOW_LOCATION));
4181
4182 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
4183 ASSERT_TRUE(mDispatcher->waitForIdle());
4184 // Should not ANR because the window is paused, and touches shouldn't go to it
4185 mFakePolicy->assertNotifyAnrWasNotCalled();
4186
4187 mFocusedWindow->assertNoEvents();
4188 mUnfocusedWindow->assertNoEvents();
4189}
4190
4191/**
4192 * If a window is processing a motion event, and then a key event comes in, the key event should
4193 * not to to the focused window until the motion is processed.
4194 * If a different window becomes focused at this time, the key should go to that window instead.
4195 *
4196 * Warning!!!
4197 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
4198 * and the injection timeout that we specify when injecting the key.
4199 * We must have the injection timeout (10ms) be smaller than
4200 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
4201 *
4202 * If that value changes, this test should also change.
4203 */
4204TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
4205 // Set a long ANR timeout to prevent it from triggering
4206 mFocusedWindow->setDispatchingTimeout(2s);
4207 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
4208
4209 tapOnUnfocusedWindow();
4210 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
4211 ASSERT_TRUE(downSequenceNum);
4212 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
4213 ASSERT_TRUE(upSequenceNum);
4214 // Don't finish the events yet, and send a key
4215 // Injection will succeed because we will eventually give up and send the key to the focused
4216 // window even if motions are still being processed.
4217
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004218 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004219 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004220 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
4221 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004222 // Key will not be sent to the window, yet, because the window is still processing events
4223 // and the key remains pending, waiting for the touch events to be processed
4224 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
4225 ASSERT_FALSE(keySequenceNum);
4226
4227 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07004228 mFocusedWindow->setFocusable(false);
4229 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004230 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004231 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004232
4233 // Focus events should precede the key events
4234 mUnfocusedWindow->consumeFocusEvent(true);
4235 mFocusedWindow->consumeFocusEvent(false);
4236
4237 // Finish the tap events, which should unblock dispatcher
4238 mUnfocusedWindow->finishEvent(*downSequenceNum);
4239 mUnfocusedWindow->finishEvent(*upSequenceNum);
4240
4241 // Now that all queues are cleared and no backlog in the connections, the key event
4242 // can finally go to the newly focused "mUnfocusedWindow".
4243 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4244 mFocusedWindow->assertNoEvents();
4245 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004246 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004247}
4248
4249// When the touch stream is split across 2 windows, and one of them does not respond,
4250// then ANR should be raised and the touch should be canceled for the unresponsive window.
4251// The other window should not be affected by that.
4252TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
4253 // Touch Window 1
4254 NotifyMotionArgs motionArgs =
4255 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4256 ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION});
4257 mDispatcher->notifyMotion(&motionArgs);
4258 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
4259 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
4260
4261 // Touch Window 2
4262 int32_t actionPointerDown =
4263 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
4264
4265 motionArgs =
4266 generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4267 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION});
4268 mDispatcher->notifyMotion(&motionArgs);
4269
4270 const std::chrono::duration timeout =
4271 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004272 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004273
4274 mUnfocusedWindow->consumeMotionDown();
4275 mFocusedWindow->consumeMotionDown();
4276 // Focused window may or may not receive ACTION_MOVE
4277 // But it should definitely receive ACTION_CANCEL due to the ANR
4278 InputEvent* event;
4279 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
4280 ASSERT_TRUE(moveOrCancelSequenceNum);
4281 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
4282 ASSERT_NE(nullptr, event);
4283 ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION);
4284 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
4285 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
4286 mFocusedWindow->consumeMotionCancel();
4287 } else {
4288 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
4289 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004290 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004291 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004292
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004293 mUnfocusedWindow->assertNoEvents();
4294 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004295 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004296}
4297
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05004298/**
4299 * If we have no focused window, and a key comes in, we start the ANR timer.
4300 * The focused application should add a focused window before the timer runs out to prevent ANR.
4301 *
4302 * If the user touches another application during this time, the key should be dropped.
4303 * Next, if a new focused window comes in, without toggling the focused application,
4304 * then no ANR should occur.
4305 *
4306 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
4307 * but in some cases the policy may not update the focused application.
4308 */
4309TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
4310 std::shared_ptr<FakeApplicationHandle> focusedApplication =
4311 std::make_shared<FakeApplicationHandle>();
4312 focusedApplication->setDispatchingTimeout(60ms);
4313 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
4314 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
4315 mFocusedWindow->setFocusable(false);
4316
4317 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
4318 mFocusedWindow->consumeFocusEvent(false);
4319
4320 // Send a key. The ANR timer should start because there is no focused window.
4321 // 'focusedApplication' will get blamed if this timer completes.
4322 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004323 InputEventInjectionResult result =
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05004324 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004325 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/,
4326 false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004327 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05004328
4329 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
4330 // then the injected touches won't cause the focused event to get dropped.
4331 // The dispatcher only checks for whether the queue should be pruned upon queueing.
4332 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
4333 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
4334 // For this test, it means that the key would get delivered to the window once it becomes
4335 // focused.
4336 std::this_thread::sleep_for(10ms);
4337
4338 // Touch unfocused window. This should force the pending key to get dropped.
4339 NotifyMotionArgs motionArgs =
4340 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4341 ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION});
4342 mDispatcher->notifyMotion(&motionArgs);
4343
4344 // We do not consume the motion right away, because that would require dispatcher to first
4345 // process (== drop) the key event, and by that time, ANR will be raised.
4346 // Set the focused window first.
4347 mFocusedWindow->setFocusable(true);
4348 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
4349 setFocusedWindow(mFocusedWindow);
4350 mFocusedWindow->consumeFocusEvent(true);
4351 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
4352 // to another application. This could be a bug / behaviour in the policy.
4353
4354 mUnfocusedWindow->consumeMotionDown();
4355
4356 ASSERT_TRUE(mDispatcher->waitForIdle());
4357 // Should not ANR because we actually have a focused window. It was just added too slowly.
4358 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
4359}
4360
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05004361// These tests ensure we cannot send touch events to a window that's positioned behind a window
4362// that has feature NO_INPUT_CHANNEL.
4363// Layout:
4364// Top (closest to user)
4365// mNoInputWindow (above all windows)
4366// mBottomWindow
4367// Bottom (furthest from user)
4368class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
4369 virtual void SetUp() override {
4370 InputDispatcherTest::SetUp();
4371
4372 mApplication = std::make_shared<FakeApplicationHandle>();
4373 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
4374 "Window without input channel", ADISPLAY_ID_DEFAULT,
4375 std::make_optional<sp<IBinder>>(nullptr) /*token*/);
4376
chaviw3277faf2021-05-19 16:45:23 -05004377 mNoInputWindow->setInputFeatures(WindowInfo::Feature::NO_INPUT_CHANNEL);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05004378 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
4379 // It's perfectly valid for this window to not have an associated input channel
4380
4381 mBottomWindow = new FakeWindowHandle(mApplication, mDispatcher, "Bottom window",
4382 ADISPLAY_ID_DEFAULT);
4383 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
4384
4385 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
4386 }
4387
4388protected:
4389 std::shared_ptr<FakeApplicationHandle> mApplication;
4390 sp<FakeWindowHandle> mNoInputWindow;
4391 sp<FakeWindowHandle> mBottomWindow;
4392};
4393
4394TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
4395 PointF touchedPoint = {10, 10};
4396
4397 NotifyMotionArgs motionArgs =
4398 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4399 ADISPLAY_ID_DEFAULT, {touchedPoint});
4400 mDispatcher->notifyMotion(&motionArgs);
4401
4402 mNoInputWindow->assertNoEvents();
4403 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
4404 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
4405 // and therefore should prevent mBottomWindow from receiving touches
4406 mBottomWindow->assertNoEvents();
4407}
4408
4409/**
4410 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
4411 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
4412 */
4413TEST_F(InputDispatcherMultiWindowOcclusionTests,
4414 NoInputChannelFeature_DropsTouchesWithValidChannel) {
4415 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
4416 "Window with input channel and NO_INPUT_CHANNEL",
4417 ADISPLAY_ID_DEFAULT);
4418
chaviw3277faf2021-05-19 16:45:23 -05004419 mNoInputWindow->setInputFeatures(WindowInfo::Feature::NO_INPUT_CHANNEL);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05004420 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
4421 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
4422
4423 PointF touchedPoint = {10, 10};
4424
4425 NotifyMotionArgs motionArgs =
4426 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4427 ADISPLAY_ID_DEFAULT, {touchedPoint});
4428 mDispatcher->notifyMotion(&motionArgs);
4429
4430 mNoInputWindow->assertNoEvents();
4431 mBottomWindow->assertNoEvents();
4432}
4433
Vishnu Nair958da932020-08-21 17:12:37 -07004434class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
4435protected:
4436 std::shared_ptr<FakeApplicationHandle> mApp;
4437 sp<FakeWindowHandle> mWindow;
4438 sp<FakeWindowHandle> mMirror;
4439
4440 virtual void SetUp() override {
4441 InputDispatcherTest::SetUp();
4442 mApp = std::make_shared<FakeApplicationHandle>();
4443 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
4444 mMirror = new FakeWindowHandle(mApp, mDispatcher, "TestWindowMirror", ADISPLAY_ID_DEFAULT,
4445 mWindow->getToken());
4446 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
4447 mWindow->setFocusable(true);
4448 mMirror->setFocusable(true);
4449 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4450 }
4451};
4452
4453TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
4454 // Request focus on a mirrored window
4455 setFocusedWindow(mMirror);
4456
4457 // window gets focused
4458 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004459 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4460 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004461 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
4462}
4463
4464// A focused & mirrored window remains focused only if the window and its mirror are both
4465// focusable.
4466TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
4467 setFocusedWindow(mMirror);
4468
4469 // window gets focused
4470 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004471 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4472 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004473 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004474 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4475 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004476 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4477
4478 mMirror->setFocusable(false);
4479 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4480
4481 // window loses focus since one of the windows associated with the token in not focusable
4482 mWindow->consumeFocusEvent(false);
4483
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004484 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4485 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004486 mWindow->assertNoEvents();
4487}
4488
4489// A focused & mirrored window remains focused until the window and its mirror both become
4490// invisible.
4491TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
4492 setFocusedWindow(mMirror);
4493
4494 // window gets focused
4495 mWindow->consumeFocusEvent(true);
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 mMirror->setVisible(false);
4504 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4505
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004506 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4507 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004508 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004509 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4510 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004511 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4512
4513 mWindow->setVisible(false);
4514 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4515
4516 // window loses focus only after all windows associated with the token become invisible.
4517 mWindow->consumeFocusEvent(false);
4518
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004519 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4520 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004521 mWindow->assertNoEvents();
4522}
4523
4524// A focused & mirrored window remains focused until both windows are removed.
4525TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
4526 setFocusedWindow(mMirror);
4527
4528 // window gets focused
4529 mWindow->consumeFocusEvent(true);
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 // single window is removed but the window token remains focused
4538 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
4539
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004540 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4541 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004542 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004543 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4544 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004545 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4546
4547 // Both windows are removed
4548 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
4549 mWindow->consumeFocusEvent(false);
4550
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004551 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4552 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004553 mWindow->assertNoEvents();
4554}
4555
4556// Focus request can be pending until one window becomes visible.
4557TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
4558 // Request focus on an invisible mirror.
4559 mWindow->setVisible(false);
4560 mMirror->setVisible(false);
4561 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4562 setFocusedWindow(mMirror);
4563
4564 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004565 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07004566 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004567 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07004568
4569 mMirror->setVisible(true);
4570 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4571
4572 // window gets focused
4573 mWindow->consumeFocusEvent(true);
4574 // window gets the pending key event
4575 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4576}
Prabir Pradhan99987712020-11-10 18:43:05 -08004577
4578class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
4579protected:
4580 std::shared_ptr<FakeApplicationHandle> mApp;
4581 sp<FakeWindowHandle> mWindow;
4582 sp<FakeWindowHandle> mSecondWindow;
4583
4584 void SetUp() override {
4585 InputDispatcherTest::SetUp();
4586 mApp = std::make_shared<FakeApplicationHandle>();
4587 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
4588 mWindow->setFocusable(true);
4589 mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
4590 mSecondWindow->setFocusable(true);
4591
4592 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
4593 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
4594
4595 setFocusedWindow(mWindow);
4596 mWindow->consumeFocusEvent(true);
4597 }
4598
4599 void notifyPointerCaptureChanged(bool enabled) {
4600 const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(enabled);
4601 mDispatcher->notifyPointerCaptureChanged(&args);
4602 }
4603
4604 void requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, bool enabled) {
4605 mDispatcher->requestPointerCapture(window->getToken(), enabled);
4606 mFakePolicy->waitForSetPointerCapture(enabled);
4607 notifyPointerCaptureChanged(enabled);
4608 window->consumeCaptureEvent(enabled);
4609 }
4610};
4611
4612TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
4613 // Ensure that capture cannot be obtained for unfocused windows.
4614 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
4615 mFakePolicy->assertSetPointerCaptureNotCalled();
4616 mSecondWindow->assertNoEvents();
4617
4618 // Ensure that capture can be enabled from the focus window.
4619 requestAndVerifyPointerCapture(mWindow, true);
4620
4621 // Ensure that capture cannot be disabled from a window that does not have capture.
4622 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
4623 mFakePolicy->assertSetPointerCaptureNotCalled();
4624
4625 // Ensure that capture can be disabled from the window with capture.
4626 requestAndVerifyPointerCapture(mWindow, false);
4627}
4628
4629TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
4630 requestAndVerifyPointerCapture(mWindow, true);
4631
4632 setFocusedWindow(mSecondWindow);
4633
4634 // Ensure that the capture disabled event was sent first.
4635 mWindow->consumeCaptureEvent(false);
4636 mWindow->consumeFocusEvent(false);
4637 mSecondWindow->consumeFocusEvent(true);
4638 mFakePolicy->waitForSetPointerCapture(false);
4639
4640 // Ensure that additional state changes from InputReader are not sent to the window.
4641 notifyPointerCaptureChanged(false);
4642 notifyPointerCaptureChanged(true);
4643 notifyPointerCaptureChanged(false);
4644 mWindow->assertNoEvents();
4645 mSecondWindow->assertNoEvents();
4646 mFakePolicy->assertSetPointerCaptureNotCalled();
4647}
4648
4649TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
4650 requestAndVerifyPointerCapture(mWindow, true);
4651
4652 // InputReader unexpectedly disables and enables pointer capture.
4653 notifyPointerCaptureChanged(false);
4654 notifyPointerCaptureChanged(true);
4655
4656 // Ensure that Pointer Capture is disabled.
Prabir Pradhan7d030382020-12-21 07:58:35 -08004657 mFakePolicy->waitForSetPointerCapture(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08004658 mWindow->consumeCaptureEvent(false);
4659 mWindow->assertNoEvents();
4660}
4661
Prabir Pradhan167e6d92021-02-04 16:18:17 -08004662TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
4663 requestAndVerifyPointerCapture(mWindow, true);
4664
4665 // The first window loses focus.
4666 setFocusedWindow(mSecondWindow);
4667 mFakePolicy->waitForSetPointerCapture(false);
4668 mWindow->consumeCaptureEvent(false);
4669
4670 // Request Pointer Capture from the second window before the notification from InputReader
4671 // arrives.
4672 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
4673 mFakePolicy->waitForSetPointerCapture(true);
4674
4675 // InputReader notifies Pointer Capture was disabled (because of the focus change).
4676 notifyPointerCaptureChanged(false);
4677
4678 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
4679 notifyPointerCaptureChanged(true);
4680
4681 mSecondWindow->consumeFocusEvent(true);
4682 mSecondWindow->consumeCaptureEvent(true);
4683}
4684
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004685class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
4686protected:
4687 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00004688
4689 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
4690 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
4691
4692 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
4693 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
4694
4695 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
4696 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
4697 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
4698 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
4699 MAXIMUM_OBSCURING_OPACITY);
4700
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004701 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004702 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004703 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004704
4705 sp<FakeWindowHandle> mTouchWindow;
4706
4707 virtual void SetUp() override {
4708 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004709 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004710 mDispatcher->setBlockUntrustedTouchesMode(android::os::BlockUntrustedTouchesMode::BLOCK);
4711 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
4712 }
4713
4714 virtual void TearDown() override {
4715 InputDispatcherTest::TearDown();
4716 mTouchWindow.clear();
4717 }
4718
chaviw3277faf2021-05-19 16:45:23 -05004719 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode,
4720 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004721 sp<FakeWindowHandle> window = getWindow(uid, name);
chaviw3277faf2021-05-19 16:45:23 -05004722 window->setFlags(WindowInfo::Flag::NOT_TOUCHABLE);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004723 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004724 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004725 return window;
4726 }
4727
4728 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
4729 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
4730 sp<FakeWindowHandle> window =
4731 new FakeWindowHandle(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
4732 // Generate an arbitrary PID based on the UID
4733 window->setOwnerInfo(1777 + (uid % 10000), uid);
4734 return window;
4735 }
4736
4737 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
4738 NotifyMotionArgs args =
4739 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4740 ADISPLAY_ID_DEFAULT, points);
4741 mDispatcher->notifyMotion(&args);
4742 }
4743};
4744
4745TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004746 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004747 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004748 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004749
4750 touch();
4751
4752 mTouchWindow->assertNoEvents();
4753}
4754
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004755TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00004756 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
4757 const sp<FakeWindowHandle>& w =
4758 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
4759 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4760
4761 touch();
4762
4763 mTouchWindow->assertNoEvents();
4764}
4765
4766TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004767 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
4768 const sp<FakeWindowHandle>& w =
4769 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
4770 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4771
4772 touch();
4773
4774 w->assertNoEvents();
4775}
4776
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004777TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004778 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
4779 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004780
4781 touch();
4782
4783 mTouchWindow->consumeAnyMotionDown();
4784}
4785
4786TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004787 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004788 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004789 w->setFrame(Rect(0, 0, 50, 50));
4790 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004791
4792 touch({PointF{100, 100}});
4793
4794 mTouchWindow->consumeAnyMotionDown();
4795}
4796
4797TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004798 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004799 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004800 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4801
4802 touch();
4803
4804 mTouchWindow->consumeAnyMotionDown();
4805}
4806
4807TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
4808 const sp<FakeWindowHandle>& w =
4809 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4810 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004811
4812 touch();
4813
4814 mTouchWindow->consumeAnyMotionDown();
4815}
4816
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004817TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
4818 const sp<FakeWindowHandle>& w =
4819 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4820 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4821
4822 touch();
4823
4824 w->assertNoEvents();
4825}
4826
4827/**
4828 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
4829 * inside) while letting them pass-through. Note that even though touch passes through the occluding
4830 * window, the occluding window will still receive ACTION_OUTSIDE event.
4831 */
4832TEST_F(InputDispatcherUntrustedTouchesTest,
4833 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
4834 const sp<FakeWindowHandle>& w =
4835 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
chaviw3277faf2021-05-19 16:45:23 -05004836 w->addFlags(WindowInfo::Flag::WATCH_OUTSIDE_TOUCH);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004837 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4838
4839 touch();
4840
4841 w->consumeMotionOutside();
4842}
4843
4844TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
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 InputEvent* event = w->consume();
4853 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType());
4854 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
4855 EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getX());
4856 EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getY());
4857}
4858
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004859TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004860 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004861 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4862 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004863 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4864
4865 touch();
4866
4867 mTouchWindow->consumeAnyMotionDown();
4868}
4869
4870TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
4871 const sp<FakeWindowHandle>& w =
4872 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4873 MAXIMUM_OBSCURING_OPACITY);
4874 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004875
4876 touch();
4877
4878 mTouchWindow->consumeAnyMotionDown();
4879}
4880
4881TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004882 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004883 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4884 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004885 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4886
4887 touch();
4888
4889 mTouchWindow->assertNoEvents();
4890}
4891
4892TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
4893 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
4894 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004895 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
4896 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004897 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004898 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
4899 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004900 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
4901
4902 touch();
4903
4904 mTouchWindow->assertNoEvents();
4905}
4906
4907TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
4908 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
4909 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004910 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
4911 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004912 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004913 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
4914 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004915 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
4916
4917 touch();
4918
4919 mTouchWindow->consumeAnyMotionDown();
4920}
4921
4922TEST_F(InputDispatcherUntrustedTouchesTest,
4923 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
4924 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004925 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4926 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004927 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004928 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
4929 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004930 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
4931
4932 touch();
4933
4934 mTouchWindow->consumeAnyMotionDown();
4935}
4936
4937TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
4938 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004939 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4940 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004941 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004942 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
4943 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004944 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004945
4946 touch();
4947
4948 mTouchWindow->assertNoEvents();
4949}
4950
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004951TEST_F(InputDispatcherUntrustedTouchesTest,
4952 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
4953 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004954 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
4955 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004956 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004957 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4958 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004959 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
4960
4961 touch();
4962
4963 mTouchWindow->assertNoEvents();
4964}
4965
4966TEST_F(InputDispatcherUntrustedTouchesTest,
4967 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
4968 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004969 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
4970 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004971 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004972 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4973 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004974 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
4975
4976 touch();
4977
4978 mTouchWindow->consumeAnyMotionDown();
4979}
4980
4981TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
4982 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004983 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
4984 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004985 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4986
4987 touch();
4988
4989 mTouchWindow->consumeAnyMotionDown();
4990}
4991
4992TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
4993 const sp<FakeWindowHandle>& w =
4994 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
4995 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4996
4997 touch();
4998
4999 mTouchWindow->consumeAnyMotionDown();
5000}
5001
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00005002TEST_F(InputDispatcherUntrustedTouchesTest,
5003 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
5004 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
5005 const sp<FakeWindowHandle>& w =
5006 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
5007 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5008
5009 touch();
5010
5011 mTouchWindow->assertNoEvents();
5012}
5013
5014TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
5015 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
5016 const sp<FakeWindowHandle>& w =
5017 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
5018 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5019
5020 touch();
5021
5022 mTouchWindow->consumeAnyMotionDown();
5023}
5024
5025TEST_F(InputDispatcherUntrustedTouchesTest,
5026 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
5027 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
5028 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005029 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
5030 OPACITY_ABOVE_THRESHOLD);
5031 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5032
5033 touch();
5034
5035 mTouchWindow->consumeAnyMotionDown();
5036}
5037
5038TEST_F(InputDispatcherUntrustedTouchesTest,
5039 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
5040 const sp<FakeWindowHandle>& w1 =
5041 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
5042 OPACITY_BELOW_THRESHOLD);
5043 const sp<FakeWindowHandle>& w2 =
5044 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
5045 OPACITY_BELOW_THRESHOLD);
5046 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
5047
5048 touch();
5049
5050 mTouchWindow->assertNoEvents();
5051}
5052
5053/**
5054 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
5055 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
5056 * (which alone would result in allowing touches) does not affect the blocking behavior.
5057 */
5058TEST_F(InputDispatcherUntrustedTouchesTest,
5059 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
5060 const sp<FakeWindowHandle>& wB =
5061 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
5062 OPACITY_BELOW_THRESHOLD);
5063 const sp<FakeWindowHandle>& wC =
5064 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
5065 OPACITY_BELOW_THRESHOLD);
5066 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
5067
5068 touch();
5069
5070 mTouchWindow->assertNoEvents();
5071}
5072
5073/**
5074 * This test is testing that a window from a different UID but with same application token doesn't
5075 * block the touch. Apps can share the application token for close UI collaboration for example.
5076 */
5077TEST_F(InputDispatcherUntrustedTouchesTest,
5078 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
5079 const sp<FakeWindowHandle>& w =
5080 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
5081 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00005082 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5083
5084 touch();
5085
5086 mTouchWindow->consumeAnyMotionDown();
5087}
5088
arthurhungb89ccb02020-12-30 16:19:01 +08005089class InputDispatcherDragTests : public InputDispatcherTest {
5090protected:
5091 std::shared_ptr<FakeApplicationHandle> mApp;
5092 sp<FakeWindowHandle> mWindow;
5093 sp<FakeWindowHandle> mSecondWindow;
5094 sp<FakeWindowHandle> mDragWindow;
5095
5096 void SetUp() override {
5097 InputDispatcherTest::SetUp();
5098 mApp = std::make_shared<FakeApplicationHandle>();
5099 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
5100 mWindow->setFrame(Rect(0, 0, 100, 100));
chaviw3277faf2021-05-19 16:45:23 -05005101 mWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
arthurhungb89ccb02020-12-30 16:19:01 +08005102
5103 mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
5104 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
chaviw3277faf2021-05-19 16:45:23 -05005105 mSecondWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
arthurhungb89ccb02020-12-30 16:19:01 +08005106
5107 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
5108 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
5109 }
5110
5111 // Start performing drag, we will create a drag window and transfer touch to it.
5112 void performDrag() {
5113 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5114 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5115 {50, 50}))
5116 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5117
5118 // Window should receive motion event.
5119 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5120
5121 // The drag window covers the entire display
5122 mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
5123 mDispatcher->setInputWindows(
5124 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
5125
5126 // Transfer touch focus to the drag window
5127 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
5128 true /* isDragDrop */);
5129 mWindow->consumeMotionCancel();
5130 mDragWindow->consumeMotionDown();
5131 }
arthurhung6d4bed92021-03-17 11:59:33 +08005132
5133 // Start performing drag, we will create a drag window and transfer touch to it.
5134 void performStylusDrag() {
5135 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5136 injectMotionEvent(mDispatcher,
5137 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
5138 AINPUT_SOURCE_STYLUS)
5139 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
5140 .pointer(PointerBuilder(0,
5141 AMOTION_EVENT_TOOL_TYPE_STYLUS)
5142 .x(50)
5143 .y(50))
5144 .build()));
5145 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5146
5147 // The drag window covers the entire display
5148 mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
5149 mDispatcher->setInputWindows(
5150 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
5151
5152 // Transfer touch focus to the drag window
5153 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
5154 true /* isDragDrop */);
5155 mWindow->consumeMotionCancel();
5156 mDragWindow->consumeMotionDown();
5157 }
arthurhungb89ccb02020-12-30 16:19:01 +08005158};
5159
5160TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
5161 performDrag();
5162
5163 // Move on window.
5164 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5165 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5166 ADISPLAY_ID_DEFAULT, {50, 50}))
5167 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5168 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5169 mWindow->consumeDragEvent(false, 50, 50);
5170 mSecondWindow->assertNoEvents();
5171
5172 // Move to another window.
5173 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5174 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5175 ADISPLAY_ID_DEFAULT, {150, 50}))
5176 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5177 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5178 mWindow->consumeDragEvent(true, 150, 50);
5179 mSecondWindow->consumeDragEvent(false, 50, 50);
5180
5181 // Move back to original window.
5182 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5183 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5184 ADISPLAY_ID_DEFAULT, {50, 50}))
5185 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5186 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5187 mWindow->consumeDragEvent(false, 50, 50);
5188 mSecondWindow->consumeDragEvent(true, -50, 50);
5189
5190 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5191 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
5192 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5193 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5194 mWindow->assertNoEvents();
5195 mSecondWindow->assertNoEvents();
5196}
5197
arthurhungf452d0b2021-01-06 00:19:52 +08005198TEST_F(InputDispatcherDragTests, DragAndDrop) {
5199 performDrag();
5200
5201 // Move on window.
5202 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5203 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5204 ADISPLAY_ID_DEFAULT, {50, 50}))
5205 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5206 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5207 mWindow->consumeDragEvent(false, 50, 50);
5208 mSecondWindow->assertNoEvents();
5209
5210 // Move to another window.
5211 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5212 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5213 ADISPLAY_ID_DEFAULT, {150, 50}))
5214 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5215 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5216 mWindow->consumeDragEvent(true, 150, 50);
5217 mSecondWindow->consumeDragEvent(false, 50, 50);
5218
5219 // drop to another window.
5220 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5221 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5222 {150, 50}))
5223 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5224 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5225 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
5226 mWindow->assertNoEvents();
5227 mSecondWindow->assertNoEvents();
5228}
5229
arthurhung6d4bed92021-03-17 11:59:33 +08005230TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
5231 performStylusDrag();
5232
5233 // Move on window and keep button pressed.
5234 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5235 injectMotionEvent(mDispatcher,
5236 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
5237 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
5238 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
5239 .x(50)
5240 .y(50))
5241 .build()))
5242 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5243 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5244 mWindow->consumeDragEvent(false, 50, 50);
5245 mSecondWindow->assertNoEvents();
5246
5247 // Move to another window and release button, expect to drop item.
5248 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5249 injectMotionEvent(mDispatcher,
5250 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
5251 .buttonState(0)
5252 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
5253 .x(150)
5254 .y(50))
5255 .build()))
5256 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5257 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5258 mWindow->assertNoEvents();
5259 mSecondWindow->assertNoEvents();
5260 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
5261
5262 // nothing to the window.
5263 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5264 injectMotionEvent(mDispatcher,
5265 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
5266 .buttonState(0)
5267 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
5268 .x(150)
5269 .y(50))
5270 .build()))
5271 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5272 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5273 mWindow->assertNoEvents();
5274 mSecondWindow->assertNoEvents();
5275}
5276
Arthur Hung6d0571e2021-04-09 20:18:16 +08005277TEST_F(InputDispatcherDragTests, DragAndDrop_InvalidWindow) {
5278 performDrag();
5279
5280 // Set second window invisible.
5281 mSecondWindow->setVisible(false);
5282 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
5283
5284 // Move on window.
5285 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5286 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5287 ADISPLAY_ID_DEFAULT, {50, 50}))
5288 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5289 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5290 mWindow->consumeDragEvent(false, 50, 50);
5291 mSecondWindow->assertNoEvents();
5292
5293 // Move to another window.
5294 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5295 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5296 ADISPLAY_ID_DEFAULT, {150, 50}))
5297 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5298 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5299 mWindow->consumeDragEvent(true, 150, 50);
5300 mSecondWindow->assertNoEvents();
5301
5302 // drop to another window.
5303 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5304 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5305 {150, 50}))
5306 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5307 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5308 mFakePolicy->assertDropTargetEquals(nullptr);
5309 mWindow->assertNoEvents();
5310 mSecondWindow->assertNoEvents();
5311}
5312
Garfield Tane84e6f92019-08-29 17:28:41 -07005313} // namespace android::inputdispatcher