blob: c7e05eb86a053b13d8769255d9204d37b2b3ea32 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Garfield Tan0fc2fa72019-08-29 17:22:15 -070017#include "../dispatcher/InputDispatcher.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080018
Garfield Tan1c7bc862020-01-28 13:24:04 -080019#include <android-base/stringprintf.h>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070020#include <android-base/thread_annotations.h>
Robert Carr803535b2018-08-02 16:38:15 -070021#include <binder/Binder.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080022#include <gtest/gtest.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100023#include <input/Input.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080024#include <linux/input.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100025
Garfield Tan1c7bc862020-01-28 13:24:04 -080026#include <cinttypes>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070027#include <thread>
Garfield Tan1c7bc862020-01-28 13:24:04 -080028#include <unordered_set>
chaviwd1c23182019-12-20 18:44:56 -080029#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080030
Garfield Tan1c7bc862020-01-28 13:24:04 -080031using android::base::StringPrintf;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080032using android::os::InputEventInjectionResult;
33using android::os::InputEventInjectionSync;
Michael Wright44753b12020-07-08 13:48:11 +010034using namespace android::flag_operators;
Garfield Tan1c7bc862020-01-28 13:24:04 -080035
Garfield Tane84e6f92019-08-29 17:28:41 -070036namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080037
38// An arbitrary time value.
39static const nsecs_t ARBITRARY_TIME = 1234;
40
41// An arbitrary device id.
42static const int32_t DEVICE_ID = 1;
43
Jeff Brownf086ddb2014-02-11 14:28:48 -080044// An arbitrary display id.
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -080045static const int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
Jeff Brownf086ddb2014-02-11 14:28:48 -080046
Michael Wrightd02c5b62014-02-10 15:10:22 -080047// An arbitrary injector pid / uid pair that has permission to inject events.
48static const int32_t INJECTOR_PID = 999;
49static const int32_t INJECTOR_UID = 1001;
50
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +000051// An arbitrary pid of the gesture monitor window
52static constexpr int32_t MONITOR_PID = 2001;
53
chaviwd1c23182019-12-20 18:44:56 -080054struct PointF {
55 float x;
56 float y;
57};
Michael Wrightd02c5b62014-02-10 15:10:22 -080058
Gang Wang342c9272020-01-13 13:15:04 -050059/**
60 * Return a DOWN key event with KEYCODE_A.
61 */
62static KeyEvent getTestKeyEvent() {
63 KeyEvent event;
64
Garfield Tanfbe732e2020-01-24 11:26:14 -080065 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
66 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
67 ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -050068 return event;
69}
70
Michael Wrightd02c5b62014-02-10 15:10:22 -080071// --- FakeInputDispatcherPolicy ---
72
73class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
74 InputDispatcherConfiguration mConfig;
75
76protected:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100077 virtual ~FakeInputDispatcherPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -080078
79public:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100080 FakeInputDispatcherPolicy() {}
Jackal Guof9696682018-10-05 12:23:23 +080081
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080082 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080083 assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_KEY, args.eventTime, args.action,
84 args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080085 }
86
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080087 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args) {
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080088 assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_MOTION, args.eventTime, args.action,
89 args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080090 }
91
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -070092 void assertFilterInputEventWasNotCalled() {
93 std::scoped_lock lock(mLock);
94 ASSERT_EQ(nullptr, mFilteredEvent);
95 }
Michael Wrightd02c5b62014-02-10 15:10:22 -080096
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -080097 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -070098 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -080099 ASSERT_TRUE(mConfigurationChangedTime)
100 << "Timed out waiting for configuration changed call";
101 ASSERT_EQ(*mConfigurationChangedTime, when);
102 mConfigurationChangedTime = std::nullopt;
103 }
104
105 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700106 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800107 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800108 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800109 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
110 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
111 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
112 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
113 mLastNotifySwitch = std::nullopt;
114 }
115
chaviwfd6d3512019-03-25 13:23:49 -0700116 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700117 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800118 ASSERT_EQ(touchedToken, mOnPointerDownToken);
119 mOnPointerDownToken.clear();
120 }
121
122 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700123 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800124 ASSERT_TRUE(mOnPointerDownToken == nullptr)
125 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700126 }
127
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700128 // This function must be called soon after the expected ANR timer starts,
129 // because we are also checking how much time has passed.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500130 void assertNotifyNoFocusedWindowAnrWasCalled(
Chris Yea209fde2020-07-22 13:54:51 -0700131 std::chrono::nanoseconds timeout,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500132 const std::shared_ptr<InputApplicationHandle>& expectedApplication) {
133 std::shared_ptr<InputApplicationHandle> application;
134 { // acquire lock
135 std::unique_lock lock(mLock);
136 android::base::ScopedLockAssertion assumeLocked(mLock);
137 ASSERT_NO_FATAL_FAILURE(
138 application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock));
139 } // release lock
140 ASSERT_EQ(expectedApplication, application);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700141 }
142
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000143 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
144 const sp<IBinder>& expectedConnectionToken) {
145 sp<IBinder> connectionToken = getUnresponsiveWindowToken(timeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500146 ASSERT_EQ(expectedConnectionToken, connectionToken);
147 }
148
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000149 void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedConnectionToken) {
150 sp<IBinder> connectionToken = getResponsiveWindowToken();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500151 ASSERT_EQ(expectedConnectionToken, connectionToken);
152 }
153
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000154 void assertNotifyMonitorUnresponsiveWasCalled(std::chrono::nanoseconds timeout) {
155 int32_t pid = getUnresponsiveMonitorPid(timeout);
156 ASSERT_EQ(MONITOR_PID, pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500157 }
158
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000159 void assertNotifyMonitorResponsiveWasCalled() {
160 int32_t pid = getResponsiveMonitorPid();
161 ASSERT_EQ(MONITOR_PID, pid);
162 }
163
164 sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500165 std::unique_lock lock(mLock);
166 android::base::ScopedLockAssertion assumeLocked(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000167 return getAnrTokenLockedInterruptible(timeout, mAnrWindowTokens, lock);
168 }
169
170 sp<IBinder> getResponsiveWindowToken() {
171 std::unique_lock lock(mLock);
172 android::base::ScopedLockAssertion assumeLocked(mLock);
173 return getAnrTokenLockedInterruptible(0s, mResponsiveWindowTokens, lock);
174 }
175
176 int32_t getUnresponsiveMonitorPid(std::chrono::nanoseconds timeout) {
177 std::unique_lock lock(mLock);
178 android::base::ScopedLockAssertion assumeLocked(mLock);
179 return getAnrTokenLockedInterruptible(timeout, mAnrMonitorPids, lock);
180 }
181
182 int32_t getResponsiveMonitorPid() {
183 std::unique_lock lock(mLock);
184 android::base::ScopedLockAssertion assumeLocked(mLock);
185 return getAnrTokenLockedInterruptible(0s, mResponsiveMonitorPids, lock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500186 }
187
188 // All three ANR-related callbacks behave the same way, so we use this generic function to wait
189 // for a specific container to become non-empty. When the container is non-empty, return the
190 // first entry from the container and erase it.
191 template <class T>
192 T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage,
193 std::unique_lock<std::mutex>& lock) REQUIRES(mLock) {
194 const std::chrono::time_point start = std::chrono::steady_clock::now();
195 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700196
197 // If there is an ANR, Dispatcher won't be idle because there are still events
198 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
199 // before checking if ANR was called.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500200 // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need
201 // to provide it some time to act. 100ms seems reasonable.
202 mNotifyAnr.wait_for(lock, timeToWait,
203 [&storage]() REQUIRES(mLock) { return !storage.empty(); });
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700204 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500205 if (storage.empty()) {
206 ADD_FAILURE() << "Did not receive the ANR callback";
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000207 return {};
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700208 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700209 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
210 // the dispatcher started counting before this function was called
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700211 if (std::chrono::abs(timeout - waited) > 100ms) {
212 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
213 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
214 << "ms, but waited "
215 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
216 << "ms instead";
217 }
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500218 T token = storage.front();
219 storage.pop();
220 return token;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700221 }
222
223 void assertNotifyAnrWasNotCalled() {
224 std::scoped_lock lock(mLock);
225 ASSERT_TRUE(mAnrApplications.empty());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000226 ASSERT_TRUE(mAnrWindowTokens.empty());
227 ASSERT_TRUE(mAnrMonitorPids.empty());
228 ASSERT_TRUE(mResponsiveWindowTokens.empty())
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500229 << "ANR was not called, but please also consume the 'connection is responsive' "
230 "signal";
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000231 ASSERT_TRUE(mResponsiveMonitorPids.empty())
232 << "Monitor ANR was not called, but please also consume the 'monitor is responsive'"
233 " signal";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700234 }
235
Garfield Tan1c7bc862020-01-28 13:24:04 -0800236 void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) {
237 mConfig.keyRepeatTimeout = timeout;
238 mConfig.keyRepeatDelay = delay;
239 }
240
Prabir Pradhan99987712020-11-10 18:43:05 -0800241 void waitForSetPointerCapture(bool enabled) {
242 std::unique_lock lock(mLock);
243 base::ScopedLockAssertion assumeLocked(mLock);
244
245 if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms,
246 [this, enabled]() REQUIRES(mLock) {
247 return mPointerCaptureEnabled &&
248 *mPointerCaptureEnabled ==
249 enabled;
250 })) {
251 FAIL() << "Timed out waiting for setPointerCapture(" << enabled << ") to be called.";
252 }
253 mPointerCaptureEnabled.reset();
254 }
255
256 void assertSetPointerCaptureNotCalled() {
257 std::unique_lock lock(mLock);
258 base::ScopedLockAssertion assumeLocked(mLock);
259
260 if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) {
261 FAIL() << "Expected setPointerCapture(enabled) to not be called, but was called. "
262 "enabled = "
263 << *mPointerCaptureEnabled;
264 }
265 mPointerCaptureEnabled.reset();
266 }
267
Michael Wrightd02c5b62014-02-10 15:10:22 -0800268private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700269 std::mutex mLock;
270 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
271 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
272 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
273 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800274
Prabir Pradhan99987712020-11-10 18:43:05 -0800275 std::condition_variable mPointerCaptureChangedCondition;
276 std::optional<bool> mPointerCaptureEnabled GUARDED_BY(mLock);
277
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700278 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700279 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000280 std::queue<sp<IBinder>> mAnrWindowTokens GUARDED_BY(mLock);
281 std::queue<sp<IBinder>> mResponsiveWindowTokens GUARDED_BY(mLock);
282 std::queue<int32_t> mAnrMonitorPids GUARDED_BY(mLock);
283 std::queue<int32_t> mResponsiveMonitorPids GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700284 std::condition_variable mNotifyAnr;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700285
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600286 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700287 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800288 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800289 }
290
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000291 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700292 std::scoped_lock lock(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000293 mAnrWindowTokens.push(connectionToken);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700294 mNotifyAnr.notify_all();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500295 }
296
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000297 void notifyMonitorUnresponsive(int32_t pid, const std::string&) override {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500298 std::scoped_lock lock(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000299 mAnrMonitorPids.push(pid);
300 mNotifyAnr.notify_all();
301 }
302
303 void notifyWindowResponsive(const sp<IBinder>& connectionToken) override {
304 std::scoped_lock lock(mLock);
305 mResponsiveWindowTokens.push(connectionToken);
306 mNotifyAnr.notify_all();
307 }
308
309 void notifyMonitorResponsive(int32_t pid) override {
310 std::scoped_lock lock(mLock);
311 mResponsiveMonitorPids.push(pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500312 mNotifyAnr.notify_all();
313 }
314
315 void notifyNoFocusedWindowAnr(
316 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
317 std::scoped_lock lock(mLock);
318 mAnrApplications.push(applicationHandle);
319 mNotifyAnr.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800320 }
321
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600322 void notifyInputChannelBroken(const sp<IBinder>&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800323
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600324 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700325
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600326 void notifyUntrustedTouch(const std::string& obscuringPackage) override {}
Chris Yef59a2f42020-10-16 12:55:26 -0700327 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
328 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
329 const std::vector<float>& values) override {}
330
331 void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType,
332 InputDeviceSensorAccuracy accuracy) override {}
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000333
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600334 void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800335 *outConfig = mConfig;
336 }
337
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600338 bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700339 std::scoped_lock lock(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800340 switch (inputEvent->getType()) {
341 case AINPUT_EVENT_TYPE_KEY: {
342 const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800343 mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800344 break;
345 }
346
347 case AINPUT_EVENT_TYPE_MOTION: {
348 const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800349 mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800350 break;
351 }
352 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800353 return true;
354 }
355
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600356 void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800357
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600358 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800359
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600360 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800361 return 0;
362 }
363
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600364 bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800365 return false;
366 }
367
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600368 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
369 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700370 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800371 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
372 * essentially a passthrough for notifySwitch.
373 */
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800374 mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800375 }
376
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600377 void pokeUserActivity(nsecs_t, int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800378
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600379 bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) override { return false; }
Jackal Guof9696682018-10-05 12:23:23 +0800380
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600381 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700382 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700383 mOnPointerDownToken = newToken;
384 }
385
Prabir Pradhan99987712020-11-10 18:43:05 -0800386 void setPointerCapture(bool enabled) override {
387 std::scoped_lock lock(mLock);
388 mPointerCaptureEnabled = {enabled};
389 mPointerCaptureChangedCondition.notify_all();
390 }
391
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800392 void assertFilterInputEventWasCalled(int type, nsecs_t eventTime, int32_t action,
393 int32_t displayId) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700394 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800395 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
396 ASSERT_EQ(mFilteredEvent->getType(), type);
397
398 if (type == AINPUT_EVENT_TYPE_KEY) {
399 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*mFilteredEvent);
400 EXPECT_EQ(keyEvent.getEventTime(), eventTime);
401 EXPECT_EQ(keyEvent.getAction(), action);
402 EXPECT_EQ(keyEvent.getDisplayId(), displayId);
403 } else if (type == AINPUT_EVENT_TYPE_MOTION) {
404 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*mFilteredEvent);
405 EXPECT_EQ(motionEvent.getEventTime(), eventTime);
406 EXPECT_EQ(motionEvent.getAction(), action);
407 EXPECT_EQ(motionEvent.getDisplayId(), displayId);
408 } else {
409 FAIL() << "Unknown type: " << type;
410 }
411
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800412 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800413 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800414};
415
Michael Wrightd02c5b62014-02-10 15:10:22 -0800416// --- InputDispatcherTest ---
417
418class InputDispatcherTest : public testing::Test {
419protected:
420 sp<FakeInputDispatcherPolicy> mFakePolicy;
421 sp<InputDispatcher> mDispatcher;
422
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700423 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800424 mFakePolicy = new FakeInputDispatcherPolicy();
425 mDispatcher = new InputDispatcher(mFakePolicy);
Arthur Hungb92218b2018-08-14 12:00:21 +0800426 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000427 // Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700428 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800429 }
430
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700431 virtual void TearDown() override {
432 ASSERT_EQ(OK, mDispatcher->stop());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800433 mFakePolicy.clear();
434 mDispatcher.clear();
435 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700436
437 /**
438 * Used for debugging when writing the test
439 */
440 void dumpDispatcherState() {
441 std::string dump;
442 mDispatcher->dump(dump);
443 std::stringstream ss(dump);
444 std::string to;
445
446 while (std::getline(ss, to, '\n')) {
447 ALOGE("%s", to.c_str());
448 }
449 }
Vishnu Nair958da932020-08-21 17:12:37 -0700450
451 void setFocusedWindow(const sp<InputWindowHandle>& window,
452 const sp<InputWindowHandle>& focusedWindow = nullptr) {
453 FocusRequest request;
454 request.token = window->getToken();
455 if (focusedWindow) {
456 request.focusedToken = focusedWindow->getToken();
457 }
458 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
459 request.displayId = window->getInfo()->displayId;
460 mDispatcher->setFocusedWindow(request);
461 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800462};
463
Michael Wrightd02c5b62014-02-10 15:10:22 -0800464TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
465 KeyEvent event;
466
467 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800468 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
469 INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600470 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
471 ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800472 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700473 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800474 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800475 << "Should reject key events with undefined action.";
476
477 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800478 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
479 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600480 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800481 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700482 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800483 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800484 << "Should reject key events with ACTION_MULTIPLE.";
485}
486
487TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
488 MotionEvent event;
489 PointerProperties pointerProperties[MAX_POINTERS + 1];
490 PointerCoords pointerCoords[MAX_POINTERS + 1];
491 for (int i = 0; i <= MAX_POINTERS; i++) {
492 pointerProperties[i].clear();
493 pointerProperties[i].id = i;
494 pointerCoords[i].clear();
495 }
496
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800497 // Some constants commonly used below
498 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
499 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
500 constexpr int32_t metaState = AMETA_NONE;
501 constexpr MotionClassification classification = MotionClassification::NONE;
502
chaviw9eaa22c2020-07-01 16:21:27 -0700503 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800504 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800505 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
chaviw9eaa22c2020-07-01 16:21:27 -0700506 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification,
507 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600508 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700509 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800510 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700511 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800512 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800513 << "Should reject motion events with undefined action.";
514
515 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800516 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700517 AMOTION_EVENT_ACTION_POINTER_DOWN |
518 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700519 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
520 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
521 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
522 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800523 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700524 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800525 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800526 << "Should reject motion events with pointer down index too large.";
527
Garfield Tanfbe732e2020-01-24 11:26:14 -0800528 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700529 AMOTION_EVENT_ACTION_POINTER_DOWN |
530 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700531 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
532 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
533 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
534 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800535 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700536 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800537 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800538 << "Should reject motion events with pointer down index too small.";
539
540 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800541 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700542 AMOTION_EVENT_ACTION_POINTER_UP |
543 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700544 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
545 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
546 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
547 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800548 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700549 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800550 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800551 << "Should reject motion events with pointer up index too large.";
552
Garfield Tanfbe732e2020-01-24 11:26:14 -0800553 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700554 AMOTION_EVENT_ACTION_POINTER_UP |
555 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700556 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
557 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
558 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
559 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800560 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700561 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800562 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800563 << "Should reject motion events with pointer up index too small.";
564
565 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800566 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
567 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700568 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
569 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700570 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800571 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700572 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800573 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800574 << "Should reject motion events with 0 pointers.";
575
Garfield Tanfbe732e2020-01-24 11:26:14 -0800576 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
577 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700578 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
579 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700580 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800581 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700582 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800583 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800584 << "Should reject motion events with more than MAX_POINTERS pointers.";
585
586 // Rejects motion events with invalid pointer ids.
587 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800588 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
589 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700590 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
591 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700592 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800593 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700594 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800595 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596 << "Should reject motion events with pointer ids less than 0.";
597
598 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800599 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
600 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700601 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
602 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700603 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800604 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700605 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800606 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800607 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
608
609 // Rejects motion events with duplicate pointer ids.
610 pointerProperties[0].id = 1;
611 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800612 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
613 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700614 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
615 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700616 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800617 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700618 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800619 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800620 << "Should reject motion events with duplicate pointer ids.";
621}
622
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800623/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
624
625TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
626 constexpr nsecs_t eventTime = 20;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800627 NotifyConfigurationChangedArgs args(10 /*id*/, eventTime);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800628 mDispatcher->notifyConfigurationChanged(&args);
629 ASSERT_TRUE(mDispatcher->waitForIdle());
630
631 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
632}
633
634TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800635 NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/,
636 2 /*switchMask*/);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800637 mDispatcher->notifySwitch(&args);
638
639 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
640 args.policyFlags |= POLICY_FLAG_TRUSTED;
641 mFakePolicy->assertNotifySwitchWasCalled(args);
642}
643
Arthur Hungb92218b2018-08-14 12:00:21 +0800644// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700645static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700646static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s;
Arthur Hungb92218b2018-08-14 12:00:21 +0800647
648class FakeApplicationHandle : public InputApplicationHandle {
649public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700650 FakeApplicationHandle() {
651 mInfo.name = "Fake Application";
652 mInfo.token = new BBinder();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500653 mInfo.dispatchingTimeoutMillis =
654 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700655 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800656 virtual ~FakeApplicationHandle() {}
657
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000658 virtual bool updateInfo() override { return true; }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700659
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500660 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
661 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700662 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800663};
664
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800665class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800666public:
Garfield Tan15601662020-09-22 15:32:38 -0700667 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800668 : mName(name) {
Garfield Tan15601662020-09-22 15:32:38 -0700669 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800670 }
671
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800672 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700673 InputEvent* event;
674 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
675 if (!consumeSeq) {
676 return nullptr;
677 }
678 finishEvent(*consumeSeq);
679 return event;
680 }
681
682 /**
683 * Receive an event without acknowledging it.
684 * Return the sequence number that could later be used to send finished signal.
685 */
686 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800687 uint32_t consumeSeq;
688 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800689
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800690 std::chrono::time_point start = std::chrono::steady_clock::now();
691 status_t status = WOULD_BLOCK;
692 while (status == WOULD_BLOCK) {
chaviw81e2bb92019-12-18 15:03:51 -0800693 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800694 &event);
695 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
696 if (elapsed > 100ms) {
697 break;
698 }
699 }
700
701 if (status == WOULD_BLOCK) {
702 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700703 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800704 }
705
706 if (status != OK) {
707 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700708 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800709 }
710 if (event == nullptr) {
711 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700712 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800713 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700714 if (outEvent != nullptr) {
715 *outEvent = event;
716 }
717 return consumeSeq;
718 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800719
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700720 /**
721 * To be used together with "receiveEvent" to complete the consumption of an event.
722 */
723 void finishEvent(uint32_t consumeSeq) {
724 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
725 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800726 }
727
728 void consumeEvent(int32_t expectedEventType, int32_t expectedAction, int32_t expectedDisplayId,
729 int32_t expectedFlags) {
730 InputEvent* event = consume();
731
732 ASSERT_NE(nullptr, event) << mName.c_str()
733 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800734 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700735 << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType)
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800736 << " event, got " << inputEventTypeToString(event->getType()) << " event";
Arthur Hungb92218b2018-08-14 12:00:21 +0800737
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800738 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
Tiger Huang8664f8c2018-10-11 19:14:35 +0800739
Tiger Huang8664f8c2018-10-11 19:14:35 +0800740 switch (expectedEventType) {
741 case AINPUT_EVENT_TYPE_KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800742 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
743 EXPECT_EQ(expectedAction, keyEvent.getAction());
744 EXPECT_EQ(expectedFlags, keyEvent.getFlags());
Tiger Huang8664f8c2018-10-11 19:14:35 +0800745 break;
746 }
747 case AINPUT_EVENT_TYPE_MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800748 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
749 EXPECT_EQ(expectedAction, motionEvent.getAction());
750 EXPECT_EQ(expectedFlags, motionEvent.getFlags());
Tiger Huang8664f8c2018-10-11 19:14:35 +0800751 break;
752 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100753 case AINPUT_EVENT_TYPE_FOCUS: {
754 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
755 }
Prabir Pradhan99987712020-11-10 18:43:05 -0800756 case AINPUT_EVENT_TYPE_CAPTURE: {
757 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
758 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800759 default: {
760 FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType;
761 }
762 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800763 }
764
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100765 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
766 InputEvent* event = consume();
767 ASSERT_NE(nullptr, event) << mName.c_str()
768 << ": consumer should have returned non-NULL event.";
769 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
770 << "Got " << inputEventTypeToString(event->getType())
771 << " event instead of FOCUS event";
772
773 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
774 << mName.c_str() << ": event displayId should always be NONE.";
775
776 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
777 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
778 EXPECT_EQ(inTouchMode, focusEvent->getInTouchMode());
779 }
780
Prabir Pradhan99987712020-11-10 18:43:05 -0800781 void consumeCaptureEvent(bool hasCapture) {
782 const InputEvent* event = consume();
783 ASSERT_NE(nullptr, event) << mName.c_str()
784 << ": consumer should have returned non-NULL event.";
785 ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType())
786 << "Got " << inputEventTypeToString(event->getType())
787 << " event instead of CAPTURE event";
788
789 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
790 << mName.c_str() << ": event displayId should always be NONE.";
791
792 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
793 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
794 }
795
chaviwd1c23182019-12-20 18:44:56 -0800796 void assertNoEvents() {
797 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700798 if (event == nullptr) {
799 return;
800 }
801 if (event->getType() == AINPUT_EVENT_TYPE_KEY) {
802 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
803 ADD_FAILURE() << "Received key event "
804 << KeyEvent::actionToString(keyEvent.getAction());
805 } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) {
806 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
807 ADD_FAILURE() << "Received motion event "
808 << MotionEvent::actionToString(motionEvent.getAction());
809 } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) {
810 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
811 ADD_FAILURE() << "Received focus event, hasFocus = "
812 << (focusEvent.getHasFocus() ? "true" : "false");
Prabir Pradhan99987712020-11-10 18:43:05 -0800813 } else if (event->getType() == AINPUT_EVENT_TYPE_CAPTURE) {
814 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
815 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
816 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700817 }
818 FAIL() << mName.c_str()
819 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -0800820 }
821
822 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
823
824protected:
825 std::unique_ptr<InputConsumer> mConsumer;
826 PreallocatedInputEventFactory mEventFactory;
827
828 std::string mName;
829};
830
831class FakeWindowHandle : public InputWindowHandle {
832public:
833 static const int32_t WIDTH = 600;
834 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -0800835
Chris Yea209fde2020-07-22 13:54:51 -0700836 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
chaviwd1c23182019-12-20 18:44:56 -0800837 const sp<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500838 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -0800839 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500840 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -0700841 base::Result<std::unique_ptr<InputChannel>> channel =
842 dispatcher->createInputChannel(name);
843 token = (*channel)->getConnectionToken();
844 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -0800845 }
846
847 inputApplicationHandle->updateInfo();
848 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
849
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500850 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700851 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -0800852 mInfo.name = name;
Michael Wright44753b12020-07-08 13:48:11 +0100853 mInfo.type = InputWindowInfo::Type::APPLICATION;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500854 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
chaviwd1c23182019-12-20 18:44:56 -0800855 mInfo.frameLeft = 0;
856 mInfo.frameTop = 0;
857 mInfo.frameRight = WIDTH;
858 mInfo.frameBottom = HEIGHT;
chaviw1ff3d1e2020-07-01 15:53:47 -0700859 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -0800860 mInfo.globalScaleFactor = 1.0;
861 mInfo.touchableRegion.clear();
862 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
863 mInfo.visible = true;
Vishnu Nair47074b82020-08-14 11:54:47 -0700864 mInfo.focusable = false;
chaviwd1c23182019-12-20 18:44:56 -0800865 mInfo.hasWallpaper = false;
866 mInfo.paused = false;
chaviwd1c23182019-12-20 18:44:56 -0800867 mInfo.ownerPid = INJECTOR_PID;
868 mInfo.ownerUid = INJECTOR_UID;
chaviwd1c23182019-12-20 18:44:56 -0800869 mInfo.displayId = displayId;
870 }
871
872 virtual bool updateInfo() { return true; }
873
Vishnu Nair47074b82020-08-14 11:54:47 -0700874 void setFocusable(bool focusable) { mInfo.focusable = focusable; }
chaviwd1c23182019-12-20 18:44:56 -0800875
Vishnu Nair958da932020-08-21 17:12:37 -0700876 void setVisible(bool visible) { mInfo.visible = visible; }
877
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700878 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500879 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700880 }
881
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700882 void setPaused(bool paused) { mInfo.paused = paused; }
883
chaviwd1c23182019-12-20 18:44:56 -0800884 void setFrame(const Rect& frame) {
885 mInfo.frameLeft = frame.left;
886 mInfo.frameTop = frame.top;
887 mInfo.frameRight = frame.right;
888 mInfo.frameBottom = frame.bottom;
chaviw1ff3d1e2020-07-01 15:53:47 -0700889 mInfo.transform.set(frame.left, frame.top);
chaviwd1c23182019-12-20 18:44:56 -0800890 mInfo.touchableRegion.clear();
891 mInfo.addTouchableRegion(frame);
892 }
893
Michael Wright44753b12020-07-08 13:48:11 +0100894 void setFlags(Flags<InputWindowInfo::Flag> flags) { mInfo.flags = flags; }
chaviwd1c23182019-12-20 18:44:56 -0800895
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500896 void setInputFeatures(InputWindowInfo::Feature features) { mInfo.inputFeatures = features; }
897
chaviw9eaa22c2020-07-01 16:21:27 -0700898 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
899 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
900 }
901
902 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -0700903
yunho.shinf4a80b82020-11-16 21:13:57 +0900904 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
905
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800906 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
907 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
908 expectedFlags);
909 }
910
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700911 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
912 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
913 }
914
Svet Ganov5d3bc372020-01-26 23:11:07 -0800915 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000916 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800917 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId,
918 expectedFlags);
919 }
920
921 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000922 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800923 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId,
924 expectedFlags);
925 }
926
927 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000928 int32_t expectedFlags = 0) {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800929 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
930 expectedFlags);
931 }
932
Svet Ganov5d3bc372020-01-26 23:11:07 -0800933 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000934 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
935 int32_t expectedFlags = 0) {
936 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
937 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -0800938 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
939 }
940
941 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000942 int32_t expectedFlags = 0) {
943 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
944 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -0800945 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
946 }
947
948 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000949 int32_t expectedFlags = 0) {
Michael Wright3a240c42019-12-10 20:53:41 +0000950 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
951 expectedFlags);
952 }
953
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500954 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
955 int32_t expectedFlags = 0) {
956 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
957 expectedFlags);
958 }
959
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100960 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
961 ASSERT_NE(mInputReceiver, nullptr)
962 << "Cannot consume events from a window with no receiver";
963 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
964 }
965
Prabir Pradhan99987712020-11-10 18:43:05 -0800966 void consumeCaptureEvent(bool hasCapture) {
967 ASSERT_NE(mInputReceiver, nullptr)
968 << "Cannot consume events from a window with no receiver";
969 mInputReceiver->consumeCaptureEvent(hasCapture);
970 }
971
chaviwd1c23182019-12-20 18:44:56 -0800972 void consumeEvent(int32_t expectedEventType, int32_t expectedAction, int32_t expectedDisplayId,
973 int32_t expectedFlags) {
974 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
975 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
976 expectedFlags);
977 }
978
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700979 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700980 if (mInputReceiver == nullptr) {
981 ADD_FAILURE() << "Invalid receive event on window with no receiver";
982 return std::nullopt;
983 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700984 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700985 }
986
987 void finishEvent(uint32_t sequenceNum) {
988 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
989 mInputReceiver->finishEvent(sequenceNum);
990 }
991
chaviwaf87b3e2019-10-01 16:59:28 -0700992 InputEvent* consume() {
993 if (mInputReceiver == nullptr) {
994 return nullptr;
995 }
996 return mInputReceiver->consume();
997 }
998
Arthur Hungb92218b2018-08-14 12:00:21 +0800999 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001000 if (mInputReceiver == nullptr &&
1001 mInfo.inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL)) {
1002 return; // Can't receive events if the window does not have input channel
1003 }
1004 ASSERT_NE(nullptr, mInputReceiver)
1005 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -08001006 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +08001007 }
1008
chaviwaf87b3e2019-10-01 16:59:28 -07001009 sp<IBinder> getToken() { return mInfo.token; }
1010
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001011 const std::string& getName() { return mName; }
1012
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001013 void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) {
1014 mInfo.ownerPid = ownerPid;
1015 mInfo.ownerUid = ownerUid;
1016 }
1017
chaviwd1c23182019-12-20 18:44:56 -08001018private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001019 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -08001020 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001021 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001022};
1023
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001024std::atomic<int32_t> FakeWindowHandle::sId{1};
1025
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001026static InputEventInjectionResult injectKey(
1027 const sp<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
1028 int32_t displayId = ADISPLAY_ID_NONE,
1029 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
1030 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001031 KeyEvent event;
1032 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1033
1034 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001035 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001036 INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE,
1037 repeatCount, currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001038
1039 // Inject event until dispatch out.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001040 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, syncMode,
1041 injectionTimeout,
1042 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
Arthur Hungb92218b2018-08-14 12:00:21 +08001043}
1044
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001045static InputEventInjectionResult injectKeyDown(const sp<InputDispatcher>& dispatcher,
1046 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001047 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId);
1048}
1049
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001050static InputEventInjectionResult injectKeyUp(const sp<InputDispatcher>& dispatcher,
1051 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001052 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId);
1053}
1054
Garfield Tandf26e862020-07-01 20:18:19 -07001055class PointerBuilder {
1056public:
1057 PointerBuilder(int32_t id, int32_t toolType) {
1058 mProperties.clear();
1059 mProperties.id = id;
1060 mProperties.toolType = toolType;
1061 mCoords.clear();
1062 }
1063
1064 PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); }
1065
1066 PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); }
1067
1068 PointerBuilder& axis(int32_t axis, float value) {
1069 mCoords.setAxisValue(axis, value);
1070 return *this;
1071 }
1072
1073 PointerProperties buildProperties() const { return mProperties; }
1074
1075 PointerCoords buildCoords() const { return mCoords; }
1076
1077private:
1078 PointerProperties mProperties;
1079 PointerCoords mCoords;
1080};
1081
1082class MotionEventBuilder {
1083public:
1084 MotionEventBuilder(int32_t action, int32_t source) {
1085 mAction = action;
1086 mSource = source;
1087 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1088 }
1089
1090 MotionEventBuilder& eventTime(nsecs_t eventTime) {
1091 mEventTime = eventTime;
1092 return *this;
1093 }
1094
1095 MotionEventBuilder& displayId(int32_t displayId) {
1096 mDisplayId = displayId;
1097 return *this;
1098 }
1099
1100 MotionEventBuilder& actionButton(int32_t actionButton) {
1101 mActionButton = actionButton;
1102 return *this;
1103 }
1104
1105 MotionEventBuilder& buttonState(int32_t actionButton) {
1106 mActionButton = actionButton;
1107 return *this;
1108 }
1109
1110 MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) {
1111 mRawXCursorPosition = rawXCursorPosition;
1112 return *this;
1113 }
1114
1115 MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) {
1116 mRawYCursorPosition = rawYCursorPosition;
1117 return *this;
1118 }
1119
1120 MotionEventBuilder& pointer(PointerBuilder pointer) {
1121 mPointers.push_back(pointer);
1122 return *this;
1123 }
1124
1125 MotionEvent build() {
1126 std::vector<PointerProperties> pointerProperties;
1127 std::vector<PointerCoords> pointerCoords;
1128 for (const PointerBuilder& pointer : mPointers) {
1129 pointerProperties.push_back(pointer.buildProperties());
1130 pointerCoords.push_back(pointer.buildCoords());
1131 }
1132
1133 // Set mouse cursor position for the most common cases to avoid boilerplate.
1134 if (mSource == AINPUT_SOURCE_MOUSE &&
1135 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) &&
1136 mPointers.size() == 1) {
1137 mRawXCursorPosition = pointerCoords[0].getX();
1138 mRawYCursorPosition = pointerCoords[0].getY();
1139 }
1140
1141 MotionEvent event;
chaviw9eaa22c2020-07-01 16:21:27 -07001142 ui::Transform identityTransform;
Garfield Tandf26e862020-07-01 20:18:19 -07001143 event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC,
1144 mAction, mActionButton, /* flags */ 0, /* edgeFlags */ 0, AMETA_NONE,
chaviw9eaa22c2020-07-01 16:21:27 -07001145 mButtonState, MotionClassification::NONE, identityTransform,
1146 /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition,
1147 mRawYCursorPosition, mEventTime, mEventTime, mPointers.size(),
1148 pointerProperties.data(), pointerCoords.data());
Garfield Tandf26e862020-07-01 20:18:19 -07001149
1150 return event;
1151 }
1152
1153private:
1154 int32_t mAction;
1155 int32_t mSource;
1156 nsecs_t mEventTime;
1157 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1158 int32_t mActionButton{0};
1159 int32_t mButtonState{0};
1160 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1161 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1162
1163 std::vector<PointerBuilder> mPointers;
1164};
1165
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001166static InputEventInjectionResult injectMotionEvent(
Garfield Tandf26e862020-07-01 20:18:19 -07001167 const sp<InputDispatcher>& dispatcher, const MotionEvent& event,
1168 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001169 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT) {
Garfield Tandf26e862020-07-01 20:18:19 -07001170 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, injectionMode,
1171 injectionTimeout,
1172 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
1173}
1174
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001175static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001176 const sp<InputDispatcher>& dispatcher, int32_t action, int32_t source, int32_t displayId,
1177 const PointF& position,
1178 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001179 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1180 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001181 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001182 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC)) {
Garfield Tandf26e862020-07-01 20:18:19 -07001183 MotionEvent event = MotionEventBuilder(action, source)
1184 .displayId(displayId)
1185 .eventTime(eventTime)
1186 .rawXCursorPosition(cursorPosition.x)
1187 .rawYCursorPosition(cursorPosition.y)
1188 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1189 .x(position.x)
1190 .y(position.y))
1191 .build();
Arthur Hungb92218b2018-08-14 12:00:21 +08001192
1193 // Inject event until dispatch out.
Garfield Tandf26e862020-07-01 20:18:19 -07001194 return injectMotionEvent(dispatcher, event);
Arthur Hungb92218b2018-08-14 12:00:21 +08001195}
1196
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001197static InputEventInjectionResult injectMotionDown(const sp<InputDispatcher>& dispatcher,
1198 int32_t source, int32_t displayId,
1199 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001200 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001201}
1202
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001203static InputEventInjectionResult injectMotionUp(const sp<InputDispatcher>& dispatcher,
1204 int32_t source, int32_t displayId,
1205 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001206 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001207}
1208
Jackal Guof9696682018-10-05 12:23:23 +08001209static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1210 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1211 // Define a valid key event.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001212 NotifyKeyArgs args(/* id */ 0, currentTime, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
1213 POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, KEY_A,
1214 AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001215
1216 return args;
1217}
1218
chaviwd1c23182019-12-20 18:44:56 -08001219static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId,
1220 const std::vector<PointF>& points) {
1221 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001222 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1223 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1224 }
1225
chaviwd1c23182019-12-20 18:44:56 -08001226 PointerProperties pointerProperties[pointerCount];
1227 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001228
chaviwd1c23182019-12-20 18:44:56 -08001229 for (size_t i = 0; i < pointerCount; i++) {
1230 pointerProperties[i].clear();
1231 pointerProperties[i].id = i;
1232 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001233
chaviwd1c23182019-12-20 18:44:56 -08001234 pointerCoords[i].clear();
1235 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1236 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1237 }
Jackal Guof9696682018-10-05 12:23:23 +08001238
1239 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1240 // Define a valid motion event.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001241 NotifyMotionArgs args(/* id */ 0, currentTime, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001242 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1243 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001244 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1245 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001246 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1247 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001248
1249 return args;
1250}
1251
chaviwd1c23182019-12-20 18:44:56 -08001252static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1253 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1254}
1255
Prabir Pradhan99987712020-11-10 18:43:05 -08001256static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(bool enabled) {
1257 return NotifyPointerCaptureChangedArgs(/* id */ 0, systemTime(SYSTEM_TIME_MONOTONIC), enabled);
1258}
1259
Arthur Hungb92218b2018-08-14 12:00:21 +08001260TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001261 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001262 sp<FakeWindowHandle> window =
1263 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001264
Arthur Hung72d8dc32020-03-28 00:48:39 +00001265 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001266 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1267 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1268 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001269
1270 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001271 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001272}
1273
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001274/**
1275 * Calling setInputWindows once with FLAG_NOT_TOUCH_MODAL should not cause any issues.
1276 * To ensure that window receives only events that were directly inside of it, add
1277 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1278 * when finding touched windows.
1279 * This test serves as a sanity check for the next test, where setInputWindows is
1280 * called twice.
1281 */
1282TEST_F(InputDispatcherTest, SetInputWindowOnce_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001283 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001284 sp<FakeWindowHandle> window =
1285 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1286 window->setFrame(Rect(0, 0, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01001287 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001288
1289 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001290 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001291 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1292 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001293 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001294
1295 // Window should receive motion event.
1296 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1297}
1298
1299/**
1300 * Calling setInputWindows twice, with the same info, should not cause any issues.
1301 * To ensure that window receives only events that were directly inside of it, add
1302 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1303 * when finding touched windows.
1304 */
1305TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001306 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001307 sp<FakeWindowHandle> window =
1308 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1309 window->setFrame(Rect(0, 0, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01001310 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001311
1312 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1313 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001314 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001315 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1316 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001317 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001318
1319 // Window should receive motion event.
1320 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1321}
1322
Arthur Hungb92218b2018-08-14 12:00:21 +08001323// The foreground window should receive the first touch down event.
1324TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001325 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001326 sp<FakeWindowHandle> windowTop =
1327 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
1328 sp<FakeWindowHandle> windowSecond =
1329 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001330
Arthur Hung72d8dc32020-03-28 00:48:39 +00001331 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001332 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1333 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1334 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001335
1336 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001337 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001338 windowSecond->assertNoEvents();
1339}
1340
Garfield Tandf26e862020-07-01 20:18:19 -07001341TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07001342 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07001343 sp<FakeWindowHandle> windowLeft =
1344 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1345 windowLeft->setFrame(Rect(0, 0, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001346 windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001347 sp<FakeWindowHandle> windowRight =
1348 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1349 windowRight->setFrame(Rect(600, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001350 windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001351
1352 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1353
1354 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
1355
1356 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001357 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001358 injectMotionEvent(mDispatcher,
1359 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1360 AINPUT_SOURCE_MOUSE)
1361 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1362 .x(900)
1363 .y(400))
1364 .build()));
1365 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1366 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1367 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1368 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1369
1370 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001371 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001372 injectMotionEvent(mDispatcher,
1373 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1374 AINPUT_SOURCE_MOUSE)
1375 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1376 .x(300)
1377 .y(400))
1378 .build()));
1379 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1380 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1381 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1382 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1383 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1384 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1385
1386 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001387 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001388 injectMotionEvent(mDispatcher,
1389 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1390 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1391 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1392 .x(300)
1393 .y(400))
1394 .build()));
1395 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1396
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001397 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001398 injectMotionEvent(mDispatcher,
1399 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
1400 AINPUT_SOURCE_MOUSE)
1401 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1402 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1403 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1404 .x(300)
1405 .y(400))
1406 .build()));
1407 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1408 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1409
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001410 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001411 injectMotionEvent(mDispatcher,
1412 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1413 AINPUT_SOURCE_MOUSE)
1414 .buttonState(0)
1415 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1416 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1417 .x(300)
1418 .y(400))
1419 .build()));
1420 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1421 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1422
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001423 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001424 injectMotionEvent(mDispatcher,
1425 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
1426 .buttonState(0)
1427 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1428 .x(300)
1429 .y(400))
1430 .build()));
1431 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1432
1433 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001434 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001435 injectMotionEvent(mDispatcher,
1436 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1437 AINPUT_SOURCE_MOUSE)
1438 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1439 .x(900)
1440 .y(400))
1441 .build()));
1442 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1443 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1444 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1445 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1446 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1447 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1448}
1449
1450// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
1451// directly in this test.
1452TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07001453 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07001454 sp<FakeWindowHandle> window =
1455 new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1456 window->setFrame(Rect(0, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001457 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001458
1459 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1460
1461 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1462
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001463 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001464 injectMotionEvent(mDispatcher,
1465 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
1466 AINPUT_SOURCE_MOUSE)
1467 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1468 .x(300)
1469 .y(400))
1470 .build()));
1471 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1472 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1473
1474 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001475 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001476 injectMotionEvent(mDispatcher,
1477 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1478 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1479 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1480 .x(300)
1481 .y(400))
1482 .build()));
1483 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1484
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001485 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001486 injectMotionEvent(mDispatcher,
1487 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
1488 AINPUT_SOURCE_MOUSE)
1489 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1490 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1491 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1492 .x(300)
1493 .y(400))
1494 .build()));
1495 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1496 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1497
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001498 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001499 injectMotionEvent(mDispatcher,
1500 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1501 AINPUT_SOURCE_MOUSE)
1502 .buttonState(0)
1503 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1504 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1505 .x(300)
1506 .y(400))
1507 .build()));
1508 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1509 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1510
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001511 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001512 injectMotionEvent(mDispatcher,
1513 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
1514 .buttonState(0)
1515 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1516 .x(300)
1517 .y(400))
1518 .build()));
1519 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1520
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001521 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001522 injectMotionEvent(mDispatcher,
1523 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
1524 AINPUT_SOURCE_MOUSE)
1525 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1526 .x(300)
1527 .y(400))
1528 .build()));
1529 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1530 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1531}
1532
Garfield Tan00f511d2019-06-12 16:55:40 -07001533TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07001534 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07001535
1536 sp<FakeWindowHandle> windowLeft =
1537 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1538 windowLeft->setFrame(Rect(0, 0, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001539 windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07001540 sp<FakeWindowHandle> windowRight =
1541 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1542 windowRight->setFrame(Rect(600, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001543 windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07001544
1545 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1546
Arthur Hung72d8dc32020-03-28 00:48:39 +00001547 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07001548
1549 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
1550 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001551 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07001552 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001553 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001554 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07001555 windowRight->assertNoEvents();
1556}
1557
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001558TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001559 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001560 sp<FakeWindowHandle> window =
1561 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07001562 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001563
Arthur Hung72d8dc32020-03-28 00:48:39 +00001564 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001565 setFocusedWindow(window);
1566
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001567 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001568
1569 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1570 mDispatcher->notifyKey(&keyArgs);
1571
1572 // Window should receive key down event.
1573 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1574
1575 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
1576 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001577 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001578 mDispatcher->notifyDeviceReset(&args);
1579 window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
1580 AKEY_EVENT_FLAG_CANCELED);
1581}
1582
1583TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001584 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001585 sp<FakeWindowHandle> window =
1586 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1587
Arthur Hung72d8dc32020-03-28 00:48:39 +00001588 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001589
1590 NotifyMotionArgs motionArgs =
1591 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1592 ADISPLAY_ID_DEFAULT);
1593 mDispatcher->notifyMotion(&motionArgs);
1594
1595 // Window should receive motion down event.
1596 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1597
1598 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
1599 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001600 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001601 mDispatcher->notifyDeviceReset(&args);
1602 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT,
1603 0 /*expectedFlags*/);
1604}
1605
Svet Ganov5d3bc372020-01-26 23:11:07 -08001606TEST_F(InputDispatcherTest, TransferTouchFocus_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07001607 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001608
1609 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001610 sp<FakeWindowHandle> firstWindow =
1611 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1612 sp<FakeWindowHandle> secondWindow =
1613 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001614
1615 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001616 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001617
1618 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001619 NotifyMotionArgs downMotionArgs =
1620 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1621 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001622 mDispatcher->notifyMotion(&downMotionArgs);
1623 // Only the first window should get the down event
1624 firstWindow->consumeMotionDown();
1625 secondWindow->assertNoEvents();
1626
1627 // Transfer touch focus to the second window
1628 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1629 // The first window gets cancel and the second gets down
1630 firstWindow->consumeMotionCancel();
1631 secondWindow->consumeMotionDown();
1632
1633 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001634 NotifyMotionArgs upMotionArgs =
1635 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1636 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001637 mDispatcher->notifyMotion(&upMotionArgs);
1638 // The first window gets no events and the second gets up
1639 firstWindow->assertNoEvents();
1640 secondWindow->consumeMotionUp();
1641}
1642
1643TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointerNoSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001644 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001645
1646 PointF touchPoint = {10, 10};
1647
1648 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001649 sp<FakeWindowHandle> firstWindow =
1650 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1651 sp<FakeWindowHandle> secondWindow =
1652 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001653
1654 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001655 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001656
1657 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001658 NotifyMotionArgs downMotionArgs =
1659 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1660 ADISPLAY_ID_DEFAULT, {touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001661 mDispatcher->notifyMotion(&downMotionArgs);
1662 // Only the first window should get the down event
1663 firstWindow->consumeMotionDown();
1664 secondWindow->assertNoEvents();
1665
1666 // Send pointer down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001667 NotifyMotionArgs pointerDownMotionArgs =
1668 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1669 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1670 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1671 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001672 mDispatcher->notifyMotion(&pointerDownMotionArgs);
1673 // Only the first window should get the pointer down event
1674 firstWindow->consumeMotionPointerDown(1);
1675 secondWindow->assertNoEvents();
1676
1677 // Transfer touch focus to the second window
1678 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1679 // The first window gets cancel and the second gets down and pointer down
1680 firstWindow->consumeMotionCancel();
1681 secondWindow->consumeMotionDown();
1682 secondWindow->consumeMotionPointerDown(1);
1683
1684 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001685 NotifyMotionArgs pointerUpMotionArgs =
1686 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1687 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1688 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1689 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001690 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1691 // The first window gets nothing and the second gets pointer up
1692 firstWindow->assertNoEvents();
1693 secondWindow->consumeMotionPointerUp(1);
1694
1695 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001696 NotifyMotionArgs upMotionArgs =
1697 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1698 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001699 mDispatcher->notifyMotion(&upMotionArgs);
1700 // The first window gets nothing and the second gets up
1701 firstWindow->assertNoEvents();
1702 secondWindow->consumeMotionUp();
1703}
1704
1705TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001706 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001707
1708 // Create a non touch modal window that supports split touch
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001709 sp<FakeWindowHandle> firstWindow =
1710 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001711 firstWindow->setFrame(Rect(0, 0, 600, 400));
Michael Wright44753b12020-07-08 13:48:11 +01001712 firstWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1713 InputWindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001714
1715 // Create a non touch modal window that supports split touch
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001716 sp<FakeWindowHandle> secondWindow =
1717 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001718 secondWindow->setFrame(Rect(0, 400, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001719 secondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1720 InputWindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001721
1722 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001723 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001724
1725 PointF pointInFirst = {300, 200};
1726 PointF pointInSecond = {300, 600};
1727
1728 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001729 NotifyMotionArgs firstDownMotionArgs =
1730 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1731 ADISPLAY_ID_DEFAULT, {pointInFirst});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001732 mDispatcher->notifyMotion(&firstDownMotionArgs);
1733 // Only the first window should get the down event
1734 firstWindow->consumeMotionDown();
1735 secondWindow->assertNoEvents();
1736
1737 // Send down to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001738 NotifyMotionArgs secondDownMotionArgs =
1739 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1740 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1741 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1742 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001743 mDispatcher->notifyMotion(&secondDownMotionArgs);
1744 // The first window gets a move and the second a down
1745 firstWindow->consumeMotionMove();
1746 secondWindow->consumeMotionDown();
1747
1748 // Transfer touch focus to the second window
1749 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1750 // The first window gets cancel and the new gets pointer down (it already saw down)
1751 firstWindow->consumeMotionCancel();
1752 secondWindow->consumeMotionPointerDown(1);
1753
1754 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001755 NotifyMotionArgs pointerUpMotionArgs =
1756 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1757 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1758 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1759 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001760 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1761 // The first window gets nothing and the second gets pointer up
1762 firstWindow->assertNoEvents();
1763 secondWindow->consumeMotionPointerUp(1);
1764
1765 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001766 NotifyMotionArgs upMotionArgs =
1767 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1768 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001769 mDispatcher->notifyMotion(&upMotionArgs);
1770 // The first window gets nothing and the second gets up
1771 firstWindow->assertNoEvents();
1772 secondWindow->consumeMotionUp();
1773}
1774
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001775TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07001776 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001777 sp<FakeWindowHandle> window =
1778 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1779
Vishnu Nair47074b82020-08-14 11:54:47 -07001780 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001781 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001782 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001783
1784 window->consumeFocusEvent(true);
1785
1786 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1787 mDispatcher->notifyKey(&keyArgs);
1788
1789 // Window should receive key down event.
1790 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1791}
1792
1793TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07001794 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001795 sp<FakeWindowHandle> window =
1796 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1797
Arthur Hung72d8dc32020-03-28 00:48:39 +00001798 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001799
1800 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1801 mDispatcher->notifyKey(&keyArgs);
1802 mDispatcher->waitForIdle();
1803
1804 window->assertNoEvents();
1805}
1806
1807// If a window is touchable, but does not have focus, it should receive motion events, but not keys
1808TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07001809 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001810 sp<FakeWindowHandle> window =
1811 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1812
Arthur Hung72d8dc32020-03-28 00:48:39 +00001813 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001814
1815 // Send key
1816 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1817 mDispatcher->notifyKey(&keyArgs);
1818 // Send motion
1819 NotifyMotionArgs motionArgs =
1820 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1821 ADISPLAY_ID_DEFAULT);
1822 mDispatcher->notifyMotion(&motionArgs);
1823
1824 // Window should receive only the motion event
1825 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1826 window->assertNoEvents(); // Key event or focus event will not be received
1827}
1828
arthurhungea3f4fc2020-12-21 23:18:53 +08001829TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
1830 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1831
1832 // Create first non touch modal window that supports split touch
1833 sp<FakeWindowHandle> firstWindow =
1834 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1835 firstWindow->setFrame(Rect(0, 0, 600, 400));
1836 firstWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1837 InputWindowInfo::Flag::SPLIT_TOUCH);
1838
1839 // Create second non touch modal window that supports split touch
1840 sp<FakeWindowHandle> secondWindow =
1841 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
1842 secondWindow->setFrame(Rect(0, 400, 600, 800));
1843 secondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1844 InputWindowInfo::Flag::SPLIT_TOUCH);
1845
1846 // Add the windows to the dispatcher
1847 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
1848
1849 PointF pointInFirst = {300, 200};
1850 PointF pointInSecond = {300, 600};
1851
1852 // Send down to the first window
1853 NotifyMotionArgs firstDownMotionArgs =
1854 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1855 ADISPLAY_ID_DEFAULT, {pointInFirst});
1856 mDispatcher->notifyMotion(&firstDownMotionArgs);
1857 // Only the first window should get the down event
1858 firstWindow->consumeMotionDown();
1859 secondWindow->assertNoEvents();
1860
1861 // Send down to the second window
1862 NotifyMotionArgs secondDownMotionArgs =
1863 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1864 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1865 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1866 {pointInFirst, pointInSecond});
1867 mDispatcher->notifyMotion(&secondDownMotionArgs);
1868 // The first window gets a move and the second a down
1869 firstWindow->consumeMotionMove();
1870 secondWindow->consumeMotionDown();
1871
1872 // Send pointer cancel to the second window
1873 NotifyMotionArgs pointerUpMotionArgs =
1874 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1875 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1876 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1877 {pointInFirst, pointInSecond});
1878 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
1879 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1880 // The first window gets move and the second gets cancel.
1881 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
1882 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
1883
1884 // Send up event.
1885 NotifyMotionArgs upMotionArgs =
1886 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1887 ADISPLAY_ID_DEFAULT);
1888 mDispatcher->notifyMotion(&upMotionArgs);
1889 // The first window gets up and the second gets nothing.
1890 firstWindow->consumeMotionUp();
1891 secondWindow->assertNoEvents();
1892}
1893
chaviwd1c23182019-12-20 18:44:56 -08001894class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00001895public:
1896 FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name,
chaviwd1c23182019-12-20 18:44:56 -08001897 int32_t displayId, bool isGestureMonitor = false) {
Garfield Tan15601662020-09-22 15:32:38 -07001898 base::Result<std::unique_ptr<InputChannel>> channel =
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +00001899 dispatcher->createInputMonitor(displayId, isGestureMonitor, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07001900 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00001901 }
1902
chaviwd1c23182019-12-20 18:44:56 -08001903 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
1904
1905 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1906 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN,
1907 expectedDisplayId, expectedFlags);
1908 }
1909
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001910 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
1911
1912 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
1913
chaviwd1c23182019-12-20 18:44:56 -08001914 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1915 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
1916 expectedDisplayId, expectedFlags);
1917 }
1918
1919 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1920 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP,
1921 expectedDisplayId, expectedFlags);
1922 }
1923
1924 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
1925
1926private:
1927 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00001928};
1929
1930// Tests for gesture monitors
1931TEST_F(InputDispatcherTest, GestureMonitor_ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07001932 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00001933 sp<FakeWindowHandle> window =
1934 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001935 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00001936
chaviwd1c23182019-12-20 18:44:56 -08001937 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
1938 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00001939
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001940 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00001941 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001942 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00001943 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001944 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00001945}
1946
1947TEST_F(InputDispatcherTest, GestureMonitor_DoesNotReceiveKeyEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07001948 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00001949 sp<FakeWindowHandle> window =
1950 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1951
1952 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07001953 window->setFocusable(true);
Michael Wright3a240c42019-12-10 20:53:41 +00001954
Arthur Hung72d8dc32020-03-28 00:48:39 +00001955 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001956 setFocusedWindow(window);
1957
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001958 window->consumeFocusEvent(true);
Michael Wright3a240c42019-12-10 20:53:41 +00001959
chaviwd1c23182019-12-20 18:44:56 -08001960 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
1961 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00001962
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001963 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
1964 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00001965 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001966 monitor.assertNoEvents();
Michael Wright3a240c42019-12-10 20:53:41 +00001967}
1968
1969TEST_F(InputDispatcherTest, GestureMonitor_CanPilferAfterWindowIsRemovedMidStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001970 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00001971 sp<FakeWindowHandle> window =
1972 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001973 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00001974
chaviwd1c23182019-12-20 18:44:56 -08001975 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
1976 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00001977
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001978 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00001979 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001980 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00001981 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001982 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00001983
1984 window->releaseChannel();
1985
chaviwd1c23182019-12-20 18:44:56 -08001986 mDispatcher->pilferPointers(monitor.getToken());
Michael Wright3a240c42019-12-10 20:53:41 +00001987
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001988 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00001989 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001990 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08001991 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00001992}
1993
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001994TEST_F(InputDispatcherTest, UnresponsiveGestureMonitor_GetsAnr) {
1995 FakeMonitorReceiver monitor =
1996 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
1997 true /*isGestureMonitor*/);
1998
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001999 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002000 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
2001 std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
2002 ASSERT_TRUE(consumeSeq);
2003
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00002004 mFakePolicy->assertNotifyMonitorUnresponsiveWasCalled(DISPATCHING_TIMEOUT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002005 monitor.finishEvent(*consumeSeq);
2006 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00002007 mFakePolicy->assertNotifyMonitorResponsiveWasCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002008}
2009
chaviw81e2bb92019-12-18 15:03:51 -08002010TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002011 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
chaviw81e2bb92019-12-18 15:03:51 -08002012 sp<FakeWindowHandle> window =
2013 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2014
Arthur Hung72d8dc32020-03-28 00:48:39 +00002015 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08002016
2017 NotifyMotionArgs motionArgs =
2018 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2019 ADISPLAY_ID_DEFAULT);
2020
2021 mDispatcher->notifyMotion(&motionArgs);
2022 // Window should receive motion down event.
2023 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2024
2025 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002026 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08002027 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
2028 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
2029 motionArgs.pointerCoords[0].getX() - 10);
2030
2031 mDispatcher->notifyMotion(&motionArgs);
2032 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
2033 0 /*expectedFlags*/);
2034}
2035
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002036/**
2037 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
2038 * the device default right away. In the test scenario, we check both the default value,
2039 * and the action of enabling / disabling.
2040 */
2041TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07002042 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002043 sp<FakeWindowHandle> window =
2044 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2045
2046 // Set focused application.
2047 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002048 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002049
2050 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00002051 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002052 setFocusedWindow(window);
2053
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002054 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2055
2056 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07002057 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002058 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002059 window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/);
2060
2061 SCOPED_TRACE("Disable touch mode");
2062 mDispatcher->setInTouchMode(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07002063 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002064 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002065 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002066 window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/);
2067
2068 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07002069 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002070 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002071 window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/);
2072
2073 SCOPED_TRACE("Enable touch mode again");
2074 mDispatcher->setInTouchMode(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07002075 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002076 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002077 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002078 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2079
2080 window->assertNoEvents();
2081}
2082
Gang Wange9087892020-01-07 12:17:14 -05002083TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002084 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Gang Wange9087892020-01-07 12:17:14 -05002085 sp<FakeWindowHandle> window =
2086 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2087
2088 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002089 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05002090
Arthur Hung72d8dc32020-03-28 00:48:39 +00002091 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002092 setFocusedWindow(window);
2093
Gang Wange9087892020-01-07 12:17:14 -05002094 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2095
2096 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
2097 mDispatcher->notifyKey(&keyArgs);
2098
2099 InputEvent* event = window->consume();
2100 ASSERT_NE(event, nullptr);
2101
2102 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
2103 ASSERT_NE(verified, nullptr);
2104 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
2105
2106 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
2107 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
2108 ASSERT_EQ(keyArgs.source, verified->source);
2109 ASSERT_EQ(keyArgs.displayId, verified->displayId);
2110
2111 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
2112
2113 ASSERT_EQ(keyArgs.action, verifiedKey.action);
2114 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05002115 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
2116 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
2117 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
2118 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
2119 ASSERT_EQ(0, verifiedKey.repeatCount);
2120}
2121
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002122TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002123 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002124 sp<FakeWindowHandle> window =
2125 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2126
2127 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2128
Arthur Hung72d8dc32020-03-28 00:48:39 +00002129 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002130
2131 NotifyMotionArgs motionArgs =
2132 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2133 ADISPLAY_ID_DEFAULT);
2134 mDispatcher->notifyMotion(&motionArgs);
2135
2136 InputEvent* event = window->consume();
2137 ASSERT_NE(event, nullptr);
2138
2139 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
2140 ASSERT_NE(verified, nullptr);
2141 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
2142
2143 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
2144 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
2145 EXPECT_EQ(motionArgs.source, verified->source);
2146 EXPECT_EQ(motionArgs.displayId, verified->displayId);
2147
2148 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
2149
2150 EXPECT_EQ(motionArgs.pointerCoords[0].getX(), verifiedMotion.rawX);
2151 EXPECT_EQ(motionArgs.pointerCoords[0].getY(), verifiedMotion.rawY);
2152 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
2153 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
2154 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
2155 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
2156 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
2157}
2158
yunho.shinf4a80b82020-11-16 21:13:57 +09002159TEST_F(InputDispatcherTest, NonPointerMotionEvent_JoystickNotTransformed) {
2160 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2161 sp<FakeWindowHandle> window =
2162 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2163 const std::string name = window->getName();
2164
2165 // Window gets transformed by offset values.
2166 window->setWindowOffset(500.0f, 500.0f);
2167
2168 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2169 window->setFocusable(true);
2170
2171 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2172
2173 // First, we set focused window so that focusedWindowHandle is not null.
2174 setFocusedWindow(window);
2175
2176 // Second, we consume focus event if it is right or wrong according to onFocusChangedLocked.
2177 window->consumeFocusEvent(true);
2178
2179 NotifyMotionArgs motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE,
2180 AINPUT_SOURCE_JOYSTICK, ADISPLAY_ID_DEFAULT);
2181 mDispatcher->notifyMotion(&motionArgs);
2182
2183 // Third, we consume motion event.
2184 InputEvent* event = window->consume();
2185 ASSERT_NE(event, nullptr);
2186 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
2187 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
2188 << " event, got " << inputEventTypeToString(event->getType()) << " event";
2189
2190 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
2191 EXPECT_EQ(AINPUT_EVENT_TYPE_MOTION, motionEvent.getAction());
2192
2193 float expectedX = motionArgs.pointerCoords[0].getX();
2194 float expectedY = motionArgs.pointerCoords[0].getY();
2195
2196 // Finally we test if the axis values from the final motion event are not transformed
2197 EXPECT_EQ(expectedX, motionEvent.getX(0)) << "expected " << expectedX << " for x coord of "
2198 << name.c_str() << ", got " << motionEvent.getX(0);
2199 EXPECT_EQ(expectedY, motionEvent.getY(0)) << "expected " << expectedY << " for y coord of "
2200 << name.c_str() << ", got " << motionEvent.getY(0);
2201}
2202
chaviw09c8d2d2020-08-24 15:48:26 -07002203/**
2204 * Ensure that separate calls to sign the same data are generating the same key.
2205 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
2206 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
2207 * tests.
2208 */
2209TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
2210 KeyEvent event = getTestKeyEvent();
2211 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
2212
2213 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
2214 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
2215 ASSERT_EQ(hmac1, hmac2);
2216}
2217
2218/**
2219 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
2220 */
2221TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
2222 KeyEvent event = getTestKeyEvent();
2223 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
2224 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
2225
2226 verifiedEvent.deviceId += 1;
2227 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2228
2229 verifiedEvent.source += 1;
2230 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2231
2232 verifiedEvent.eventTimeNanos += 1;
2233 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2234
2235 verifiedEvent.displayId += 1;
2236 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2237
2238 verifiedEvent.action += 1;
2239 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2240
2241 verifiedEvent.downTimeNanos += 1;
2242 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2243
2244 verifiedEvent.flags += 1;
2245 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2246
2247 verifiedEvent.keyCode += 1;
2248 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2249
2250 verifiedEvent.scanCode += 1;
2251 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2252
2253 verifiedEvent.metaState += 1;
2254 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2255
2256 verifiedEvent.repeatCount += 1;
2257 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2258}
2259
Vishnu Nair958da932020-08-21 17:12:37 -07002260TEST_F(InputDispatcherTest, SetFocusedWindow) {
2261 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2262 sp<FakeWindowHandle> windowTop =
2263 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2264 sp<FakeWindowHandle> windowSecond =
2265 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2266 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2267
2268 // Top window is also focusable but is not granted focus.
2269 windowTop->setFocusable(true);
2270 windowSecond->setFocusable(true);
2271 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2272 setFocusedWindow(windowSecond);
2273
2274 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002275 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2276 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002277
2278 // Focused window should receive event.
2279 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
2280 windowTop->assertNoEvents();
2281}
2282
2283TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
2284 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2285 sp<FakeWindowHandle> window =
2286 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2287 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2288
2289 window->setFocusable(true);
2290 // Release channel for window is no longer valid.
2291 window->releaseChannel();
2292 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2293 setFocusedWindow(window);
2294
2295 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002296 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2297 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002298
2299 // window channel is invalid, so it should not receive any input event.
2300 window->assertNoEvents();
2301}
2302
2303TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
2304 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2305 sp<FakeWindowHandle> window =
2306 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2307 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2308
2309 // Window is not focusable.
2310 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2311 setFocusedWindow(window);
2312
2313 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002314 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2315 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002316
2317 // window is invalid, so it should not receive any input event.
2318 window->assertNoEvents();
2319}
2320
2321TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
2322 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2323 sp<FakeWindowHandle> windowTop =
2324 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2325 sp<FakeWindowHandle> windowSecond =
2326 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2327 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2328
2329 windowTop->setFocusable(true);
2330 windowSecond->setFocusable(true);
2331 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2332 setFocusedWindow(windowTop);
2333 windowTop->consumeFocusEvent(true);
2334
2335 setFocusedWindow(windowSecond, windowTop);
2336 windowSecond->consumeFocusEvent(true);
2337 windowTop->consumeFocusEvent(false);
2338
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002339 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2340 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002341
2342 // Focused window should receive event.
2343 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
2344}
2345
2346TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) {
2347 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2348 sp<FakeWindowHandle> windowTop =
2349 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2350 sp<FakeWindowHandle> windowSecond =
2351 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2352 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2353
2354 windowTop->setFocusable(true);
2355 windowSecond->setFocusable(true);
2356 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2357 setFocusedWindow(windowSecond, windowTop);
2358
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002359 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2360 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002361
2362 // Event should be dropped.
2363 windowTop->assertNoEvents();
2364 windowSecond->assertNoEvents();
2365}
2366
2367TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
2368 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2369 sp<FakeWindowHandle> window =
2370 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2371 sp<FakeWindowHandle> previousFocusedWindow =
2372 new FakeWindowHandle(application, mDispatcher, "previousFocusedWindow",
2373 ADISPLAY_ID_DEFAULT);
2374 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2375
2376 window->setFocusable(true);
2377 previousFocusedWindow->setFocusable(true);
2378 window->setVisible(false);
2379 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
2380 setFocusedWindow(previousFocusedWindow);
2381 previousFocusedWindow->consumeFocusEvent(true);
2382
2383 // Requesting focus on invisible window takes focus from currently focused window.
2384 setFocusedWindow(window);
2385 previousFocusedWindow->consumeFocusEvent(false);
2386
2387 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002388 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07002389 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002390 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07002391
2392 // Window does not get focus event or key down.
2393 window->assertNoEvents();
2394
2395 // Window becomes visible.
2396 window->setVisible(true);
2397 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2398
2399 // Window receives focus event.
2400 window->consumeFocusEvent(true);
2401 // Focused window receives key down.
2402 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2403}
2404
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002405/**
2406 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
2407 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
2408 * of the 'slipperyEnterWindow'.
2409 *
2410 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
2411 * a way so that the touched location is no longer covered by the top window.
2412 *
2413 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
2414 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
2415 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
2416 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
2417 * with ACTION_DOWN).
2418 * Thus, the touch has been transferred from the top window into the bottom window, because the top
2419 * window moved itself away from the touched location and had Flag::SLIPPERY.
2420 *
2421 * Even though the top window moved away from the touched location, it is still obscuring the bottom
2422 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
2423 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
2424 *
2425 * In this test, we ensure that the event received by the bottom window has
2426 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
2427 */
2428TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
2429 constexpr int32_t SLIPPERY_PID = INJECTOR_PID + 1;
2430 constexpr int32_t SLIPPERY_UID = INJECTOR_UID + 1;
2431
2432 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2433 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2434
2435 sp<FakeWindowHandle> slipperyExitWindow =
2436 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2437 slipperyExitWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
2438 InputWindowInfo::Flag::SLIPPERY);
2439 // Make sure this one overlaps the bottom window
2440 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
2441 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
2442 // one. Windows with the same owner are not considered to be occluding each other.
2443 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
2444
2445 sp<FakeWindowHandle> slipperyEnterWindow =
2446 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2447 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
2448
2449 mDispatcher->setInputWindows(
2450 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
2451
2452 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
2453 NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2454 ADISPLAY_ID_DEFAULT, {{50, 50}});
2455 mDispatcher->notifyMotion(&args);
2456 slipperyExitWindow->consumeMotionDown();
2457 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
2458 mDispatcher->setInputWindows(
2459 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
2460
2461 args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2462 ADISPLAY_ID_DEFAULT, {{51, 51}});
2463 mDispatcher->notifyMotion(&args);
2464
2465 slipperyExitWindow->consumeMotionCancel();
2466
2467 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
2468 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
2469}
2470
Garfield Tan1c7bc862020-01-28 13:24:04 -08002471class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
2472protected:
2473 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
2474 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
2475
Chris Yea209fde2020-07-22 13:54:51 -07002476 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002477 sp<FakeWindowHandle> mWindow;
2478
2479 virtual void SetUp() override {
2480 mFakePolicy = new FakeInputDispatcherPolicy();
2481 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
2482 mDispatcher = new InputDispatcher(mFakePolicy);
2483 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
2484 ASSERT_EQ(OK, mDispatcher->start());
2485
2486 setUpWindow();
2487 }
2488
2489 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07002490 mApp = std::make_shared<FakeApplicationHandle>();
Garfield Tan1c7bc862020-01-28 13:24:04 -08002491 mWindow = new FakeWindowHandle(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2492
Vishnu Nair47074b82020-08-14 11:54:47 -07002493 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002494 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002495 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002496 mWindow->consumeFocusEvent(true);
2497 }
2498
Chris Ye2ad95392020-09-01 13:44:44 -07002499 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08002500 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07002501 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002502 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
2503 mDispatcher->notifyKey(&keyArgs);
2504
2505 // Window should receive key down event.
2506 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2507 }
2508
2509 void expectKeyRepeatOnce(int32_t repeatCount) {
2510 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
2511 InputEvent* repeatEvent = mWindow->consume();
2512 ASSERT_NE(nullptr, repeatEvent);
2513
2514 uint32_t eventType = repeatEvent->getType();
2515 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType);
2516
2517 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
2518 uint32_t eventAction = repeatKeyEvent->getAction();
2519 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
2520 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
2521 }
2522
Chris Ye2ad95392020-09-01 13:44:44 -07002523 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08002524 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07002525 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002526 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
2527 mDispatcher->notifyKey(&keyArgs);
2528
2529 // Window should receive key down event.
2530 mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
2531 0 /*expectedFlags*/);
2532 }
2533};
2534
2535TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Chris Ye2ad95392020-09-01 13:44:44 -07002536 sendAndConsumeKeyDown(1 /* deviceId */);
2537 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2538 expectKeyRepeatOnce(repeatCount);
2539 }
2540}
2541
2542TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
2543 sendAndConsumeKeyDown(1 /* deviceId */);
2544 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2545 expectKeyRepeatOnce(repeatCount);
2546 }
2547 sendAndConsumeKeyDown(2 /* deviceId */);
2548 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08002549 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2550 expectKeyRepeatOnce(repeatCount);
2551 }
2552}
2553
2554TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Chris Ye2ad95392020-09-01 13:44:44 -07002555 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002556 expectKeyRepeatOnce(1 /*repeatCount*/);
Chris Ye2ad95392020-09-01 13:44:44 -07002557 sendAndConsumeKeyUp(1 /* deviceId */);
2558 mWindow->assertNoEvents();
2559}
2560
2561TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
2562 sendAndConsumeKeyDown(1 /* deviceId */);
2563 expectKeyRepeatOnce(1 /*repeatCount*/);
2564 sendAndConsumeKeyDown(2 /* deviceId */);
2565 expectKeyRepeatOnce(1 /*repeatCount*/);
2566 // Stale key up from device 1.
2567 sendAndConsumeKeyUp(1 /* deviceId */);
2568 // Device 2 is still down, keep repeating
2569 expectKeyRepeatOnce(2 /*repeatCount*/);
2570 expectKeyRepeatOnce(3 /*repeatCount*/);
2571 // Device 2 key up
2572 sendAndConsumeKeyUp(2 /* deviceId */);
2573 mWindow->assertNoEvents();
2574}
2575
2576TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
2577 sendAndConsumeKeyDown(1 /* deviceId */);
2578 expectKeyRepeatOnce(1 /*repeatCount*/);
2579 sendAndConsumeKeyDown(2 /* deviceId */);
2580 expectKeyRepeatOnce(1 /*repeatCount*/);
2581 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
2582 sendAndConsumeKeyUp(2 /* deviceId */);
2583 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08002584 mWindow->assertNoEvents();
2585}
2586
2587TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Chris Ye2ad95392020-09-01 13:44:44 -07002588 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002589 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2590 InputEvent* repeatEvent = mWindow->consume();
2591 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2592 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
2593 IdGenerator::getSource(repeatEvent->getId()));
2594 }
2595}
2596
2597TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Chris Ye2ad95392020-09-01 13:44:44 -07002598 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002599
2600 std::unordered_set<int32_t> idSet;
2601 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2602 InputEvent* repeatEvent = mWindow->consume();
2603 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2604 int32_t id = repeatEvent->getId();
2605 EXPECT_EQ(idSet.end(), idSet.find(id));
2606 idSet.insert(id);
2607 }
2608}
2609
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002610/* Test InputDispatcher for MultiDisplay */
2611class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
2612public:
2613 static constexpr int32_t SECOND_DISPLAY_ID = 1;
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002614 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002615 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08002616
Chris Yea209fde2020-07-22 13:54:51 -07002617 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002618 windowInPrimary =
2619 new FakeWindowHandle(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002620
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002621 // Set focus window for primary display, but focused display would be second one.
2622 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07002623 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002624 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002625 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002626 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08002627
Chris Yea209fde2020-07-22 13:54:51 -07002628 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002629 windowInSecondary =
2630 new FakeWindowHandle(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002631 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002632 // Set focus display to second one.
2633 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
2634 // Set focus window for second display.
2635 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07002636 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002637 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002638 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002639 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002640 }
2641
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002642 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002643 InputDispatcherTest::TearDown();
2644
Chris Yea209fde2020-07-22 13:54:51 -07002645 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002646 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07002647 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002648 windowInSecondary.clear();
2649 }
2650
2651protected:
Chris Yea209fde2020-07-22 13:54:51 -07002652 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002653 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07002654 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002655 sp<FakeWindowHandle> windowInSecondary;
2656};
2657
2658TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
2659 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002660 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2661 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2662 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002663 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08002664 windowInSecondary->assertNoEvents();
2665
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002666 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002667 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2668 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
2669 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08002670 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002671 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08002672}
2673
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002674TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08002675 // Test inject a key down with display id specified.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002676 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2677 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002678 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08002679 windowInSecondary->assertNoEvents();
2680
2681 // Test inject a key down without display id specified.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002682 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2683 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08002684 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002685 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08002686
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002687 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00002688 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08002689
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002690 // Old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002691 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
2692 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08002693
2694 // Test inject a key down, should timeout because of no target window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002695 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2696 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08002697 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002698 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08002699 windowInSecondary->assertNoEvents();
2700}
2701
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002702// Test per-display input monitors for motion event.
2703TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08002704 FakeMonitorReceiver monitorInPrimary =
2705 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
2706 FakeMonitorReceiver monitorInSecondary =
2707 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002708
2709 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002710 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2711 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2712 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002713 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002714 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002715 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002716 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002717
2718 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002719 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2720 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
2721 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002722 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002723 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002724 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08002725 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002726
2727 // Test inject a non-pointer motion event.
2728 // If specific a display, it will dispatch to the focused window of particular display,
2729 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002730 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2731 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
2732 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002733 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002734 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002735 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08002736 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002737}
2738
2739// Test per-display input monitors for key event.
2740TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002741 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08002742 FakeMonitorReceiver monitorInPrimary =
2743 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
2744 FakeMonitorReceiver monitorInSecondary =
2745 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002746
2747 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002748 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2749 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002750 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002751 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002752 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08002753 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002754}
2755
Vishnu Nair958da932020-08-21 17:12:37 -07002756TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
2757 sp<FakeWindowHandle> secondWindowInPrimary =
2758 new FakeWindowHandle(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
2759 secondWindowInPrimary->setFocusable(true);
2760 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
2761 setFocusedWindow(secondWindowInPrimary);
2762 windowInPrimary->consumeFocusEvent(false);
2763 secondWindowInPrimary->consumeFocusEvent(true);
2764
2765 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002766 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2767 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002768 windowInPrimary->assertNoEvents();
2769 windowInSecondary->assertNoEvents();
2770 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2771}
2772
Jackal Guof9696682018-10-05 12:23:23 +08002773class InputFilterTest : public InputDispatcherTest {
2774protected:
2775 static constexpr int32_t SECOND_DISPLAY_ID = 1;
2776
2777 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) {
2778 NotifyMotionArgs motionArgs;
2779
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002780 motionArgs =
2781 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08002782 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002783 motionArgs =
2784 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08002785 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002786 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08002787 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08002788 mFakePolicy->assertFilterInputEventWasCalled(motionArgs);
Jackal Guof9696682018-10-05 12:23:23 +08002789 } else {
2790 mFakePolicy->assertFilterInputEventWasNotCalled();
2791 }
2792 }
2793
2794 void testNotifyKey(bool expectToBeFiltered) {
2795 NotifyKeyArgs keyArgs;
2796
2797 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
2798 mDispatcher->notifyKey(&keyArgs);
2799 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
2800 mDispatcher->notifyKey(&keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002801 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08002802
2803 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08002804 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08002805 } else {
2806 mFakePolicy->assertFilterInputEventWasNotCalled();
2807 }
2808 }
2809};
2810
2811// Test InputFilter for MotionEvent
2812TEST_F(InputFilterTest, MotionEvent_InputFilter) {
2813 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
2814 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
2815 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
2816
2817 // Enable InputFilter
2818 mDispatcher->setInputFilterEnabled(true);
2819 // Test touch on both primary and second display, and check if both events are filtered.
2820 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
2821 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
2822
2823 // Disable InputFilter
2824 mDispatcher->setInputFilterEnabled(false);
2825 // Test touch on both primary and second display, and check if both events aren't filtered.
2826 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
2827 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
2828}
2829
2830// Test InputFilter for KeyEvent
2831TEST_F(InputFilterTest, KeyEvent_InputFilter) {
2832 // Since the InputFilter is disabled by default, check if key event aren't filtered.
2833 testNotifyKey(/*expectToBeFiltered*/ false);
2834
2835 // Enable InputFilter
2836 mDispatcher->setInputFilterEnabled(true);
2837 // Send a key event, and check if it is filtered.
2838 testNotifyKey(/*expectToBeFiltered*/ true);
2839
2840 // Disable InputFilter
2841 mDispatcher->setInputFilterEnabled(false);
2842 // Send a key event, and check if it isn't filtered.
2843 testNotifyKey(/*expectToBeFiltered*/ false);
2844}
2845
chaviwfd6d3512019-03-25 13:23:49 -07002846class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002847 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07002848 InputDispatcherTest::SetUp();
2849
Chris Yea209fde2020-07-22 13:54:51 -07002850 std::shared_ptr<FakeApplicationHandle> application =
2851 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002852 mUnfocusedWindow =
2853 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07002854 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
2855 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
2856 // window.
Michael Wright44753b12020-07-08 13:48:11 +01002857 mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07002858
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002859 mFocusedWindow =
2860 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2861 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01002862 mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07002863
2864 // Set focused application.
2865 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002866 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07002867
2868 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00002869 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002870 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002871 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07002872 }
2873
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002874 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07002875 InputDispatcherTest::TearDown();
2876
2877 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002878 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07002879 }
2880
2881protected:
2882 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002883 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002884 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07002885};
2886
2887// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
2888// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
2889// the onPointerDownOutsideFocus callback.
2890TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002891 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002892 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2893 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002894 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002895 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07002896
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002897 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07002898 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
2899}
2900
2901// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
2902// DOWN on the window that doesn't have focus. Ensure no window received the
2903// onPointerDownOutsideFocus callback.
2904TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002905 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002906 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002907 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002908 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07002909
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002910 ASSERT_TRUE(mDispatcher->waitForIdle());
2911 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07002912}
2913
2914// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
2915// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
2916TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002917 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2918 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002919 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07002920
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002921 ASSERT_TRUE(mDispatcher->waitForIdle());
2922 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07002923}
2924
2925// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
2926// DOWN on the window that already has focus. Ensure no window received the
2927// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002928TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002929 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002930 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002931 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002932 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002933 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07002934
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002935 ASSERT_TRUE(mDispatcher->waitForIdle());
2936 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07002937}
2938
chaviwaf87b3e2019-10-01 16:59:28 -07002939// These tests ensures we can send touch events to a single client when there are multiple input
2940// windows that point to the same client token.
2941class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
2942 virtual void SetUp() override {
2943 InputDispatcherTest::SetUp();
2944
Chris Yea209fde2020-07-22 13:54:51 -07002945 std::shared_ptr<FakeApplicationHandle> application =
2946 std::make_shared<FakeApplicationHandle>();
chaviwaf87b3e2019-10-01 16:59:28 -07002947 mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1",
2948 ADISPLAY_ID_DEFAULT);
2949 // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window.
2950 // We also need FLAG_SPLIT_TOUCH or we won't be able to get touches for both windows.
Michael Wright44753b12020-07-08 13:48:11 +01002951 mWindow1->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
2952 InputWindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07002953 mWindow1->setFrame(Rect(0, 0, 100, 100));
2954
2955 mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2",
2956 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
Michael Wright44753b12020-07-08 13:48:11 +01002957 mWindow2->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
2958 InputWindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07002959 mWindow2->setFrame(Rect(100, 100, 200, 200));
2960
Arthur Hung72d8dc32020-03-28 00:48:39 +00002961 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07002962 }
2963
2964protected:
2965 sp<FakeWindowHandle> mWindow1;
2966 sp<FakeWindowHandle> mWindow2;
2967
2968 // Helper function to convert the point from screen coordinates into the window's space
2969 static PointF getPointInWindow(const InputWindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07002970 vec2 vals = windowInfo->transform.transform(point.x, point.y);
2971 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07002972 }
2973
2974 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
2975 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002976 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07002977 InputEvent* event = window->consume();
2978
2979 ASSERT_NE(nullptr, event) << name.c_str()
2980 << ": consumer should have returned non-NULL event.";
2981
2982 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
2983 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
2984 << " event, got " << inputEventTypeToString(event->getType()) << " event";
2985
2986 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
2987 EXPECT_EQ(expectedAction, motionEvent.getAction());
2988
2989 for (size_t i = 0; i < points.size(); i++) {
2990 float expectedX = points[i].x;
2991 float expectedY = points[i].y;
2992
2993 EXPECT_EQ(expectedX, motionEvent.getX(i))
2994 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
2995 << ", got " << motionEvent.getX(i);
2996 EXPECT_EQ(expectedY, motionEvent.getY(i))
2997 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
2998 << ", got " << motionEvent.getY(i);
2999 }
3000 }
chaviw9eaa22c2020-07-01 16:21:27 -07003001
3002 void touchAndAssertPositions(int32_t action, std::vector<PointF> touchedPoints,
3003 std::vector<PointF> expectedPoints) {
3004 NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
3005 ADISPLAY_ID_DEFAULT, touchedPoints);
3006 mDispatcher->notifyMotion(&motionArgs);
3007
3008 // Always consume from window1 since it's the window that has the InputReceiver
3009 consumeMotionEvent(mWindow1, action, expectedPoints);
3010 }
chaviwaf87b3e2019-10-01 16:59:28 -07003011};
3012
3013TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
3014 // Touch Window 1
3015 PointF touchedPoint = {10, 10};
3016 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003017 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003018
3019 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07003020 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003021
3022 // Touch Window 2
3023 touchedPoint = {150, 150};
3024 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003025 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003026}
3027
chaviw9eaa22c2020-07-01 16:21:27 -07003028TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
3029 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07003030 mWindow2->setWindowScale(0.5f, 0.5f);
3031
3032 // Touch Window 1
3033 PointF touchedPoint = {10, 10};
3034 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003035 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003036 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07003037 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003038
3039 // Touch Window 2
3040 touchedPoint = {150, 150};
3041 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003042 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
3043 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003044
chaviw9eaa22c2020-07-01 16:21:27 -07003045 // Update the transform so rotation is set
3046 mWindow2->setWindowTransform(0, -1, 1, 0);
3047 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
3048 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003049}
3050
chaviw9eaa22c2020-07-01 16:21:27 -07003051TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003052 mWindow2->setWindowScale(0.5f, 0.5f);
3053
3054 // Touch Window 1
3055 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3056 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003057 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003058
3059 // Touch Window 2
3060 int32_t actionPointerDown =
3061 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003062 touchedPoints.push_back(PointF{150, 150});
3063 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3064 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003065
chaviw9eaa22c2020-07-01 16:21:27 -07003066 // Release Window 2
3067 int32_t actionPointerUp =
3068 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3069 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
3070 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003071
chaviw9eaa22c2020-07-01 16:21:27 -07003072 // Update the transform so rotation is set for Window 2
3073 mWindow2->setWindowTransform(0, -1, 1, 0);
3074 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3075 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003076}
3077
chaviw9eaa22c2020-07-01 16:21:27 -07003078TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003079 mWindow2->setWindowScale(0.5f, 0.5f);
3080
3081 // Touch Window 1
3082 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3083 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003084 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003085
3086 // Touch Window 2
3087 int32_t actionPointerDown =
3088 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003089 touchedPoints.push_back(PointF{150, 150});
3090 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003091
chaviw9eaa22c2020-07-01 16:21:27 -07003092 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003093
3094 // Move both windows
3095 touchedPoints = {{20, 20}, {175, 175}};
3096 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3097 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3098
chaviw9eaa22c2020-07-01 16:21:27 -07003099 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003100
chaviw9eaa22c2020-07-01 16:21:27 -07003101 // Release Window 2
3102 int32_t actionPointerUp =
3103 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3104 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
3105 expectedPoints.pop_back();
3106
3107 // Touch Window 2
3108 mWindow2->setWindowTransform(0, -1, 1, 0);
3109 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3110 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
3111
3112 // Move both windows
3113 touchedPoints = {{20, 20}, {175, 175}};
3114 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3115 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3116
3117 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003118}
3119
3120TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
3121 mWindow1->setWindowScale(0.5f, 0.5f);
3122
3123 // Touch Window 1
3124 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3125 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003126 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003127
3128 // Touch Window 2
3129 int32_t actionPointerDown =
3130 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003131 touchedPoints.push_back(PointF{150, 150});
3132 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003133
chaviw9eaa22c2020-07-01 16:21:27 -07003134 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003135
3136 // Move both windows
3137 touchedPoints = {{20, 20}, {175, 175}};
3138 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3139 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3140
chaviw9eaa22c2020-07-01 16:21:27 -07003141 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003142}
3143
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003144class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
3145 virtual void SetUp() override {
3146 InputDispatcherTest::SetUp();
3147
Chris Yea209fde2020-07-22 13:54:51 -07003148 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003149 mApplication->setDispatchingTimeout(20ms);
3150 mWindow =
3151 new FakeWindowHandle(mApplication, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
3152 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05003153 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07003154 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003155 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3156 // window.
Michael Wright44753b12020-07-08 13:48:11 +01003157 mWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003158
3159 // Set focused application.
3160 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
3161
3162 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003163 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003164 mWindow->consumeFocusEvent(true);
3165 }
3166
3167 virtual void TearDown() override {
3168 InputDispatcherTest::TearDown();
3169 mWindow.clear();
3170 }
3171
3172protected:
Chris Yea209fde2020-07-22 13:54:51 -07003173 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003174 sp<FakeWindowHandle> mWindow;
3175 static constexpr PointF WINDOW_LOCATION = {20, 20};
3176
3177 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003178 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003179 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3180 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003181 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003182 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3183 WINDOW_LOCATION));
3184 }
3185};
3186
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003187// Send a tap and respond, which should not cause an ANR.
3188TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
3189 tapOnWindow();
3190 mWindow->consumeMotionDown();
3191 mWindow->consumeMotionUp();
3192 ASSERT_TRUE(mDispatcher->waitForIdle());
3193 mFakePolicy->assertNotifyAnrWasNotCalled();
3194}
3195
3196// Send a regular key and respond, which should not cause an ANR.
3197TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003198 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003199 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
3200 ASSERT_TRUE(mDispatcher->waitForIdle());
3201 mFakePolicy->assertNotifyAnrWasNotCalled();
3202}
3203
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003204TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
3205 mWindow->setFocusable(false);
3206 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3207 mWindow->consumeFocusEvent(false);
3208
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003209 InputEventInjectionResult result =
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003210 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003211 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
3212 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003213 // Key will not go to window because we have no focused window.
3214 // The 'no focused window' ANR timer should start instead.
3215
3216 // Now, the focused application goes away.
3217 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
3218 // The key should get dropped and there should be no ANR.
3219
3220 ASSERT_TRUE(mDispatcher->waitForIdle());
3221 mFakePolicy->assertNotifyAnrWasNotCalled();
3222}
3223
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003224// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003225// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
3226// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003227TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003228 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003229 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3230 WINDOW_LOCATION));
3231
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003232 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
3233 ASSERT_TRUE(sequenceNum);
3234 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003235 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003236
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003237 mWindow->finishEvent(*sequenceNum);
3238 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3239 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003240 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003241 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003242}
3243
3244// Send a key to the app and have the app not respond right away.
3245TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
3246 // Inject a key, and don't respond - expect that ANR is called.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003247 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003248 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
3249 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003250 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003251 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003252 ASSERT_TRUE(mDispatcher->waitForIdle());
3253}
3254
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003255// We have a focused application, but no focused window
3256TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003257 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003258 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3259 mWindow->consumeFocusEvent(false);
3260
3261 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003262 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003263 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3264 WINDOW_LOCATION));
3265 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
3266 mDispatcher->waitForIdle();
3267 mFakePolicy->assertNotifyAnrWasNotCalled();
3268
3269 // Once a focused event arrives, we get an ANR for this application
3270 // We specify the injection timeout to be smaller than the application timeout, to ensure that
3271 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003272 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003273 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003274 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3275 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003276 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003277 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003278 ASSERT_TRUE(mDispatcher->waitForIdle());
3279}
3280
3281// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003282// Make sure that we don't notify policy twice about the same ANR.
3283TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003284 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003285 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3286 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003287
3288 // Once a focused event arrives, we get an ANR for this application
3289 // We specify the injection timeout to be smaller than the application timeout, to ensure that
3290 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003291 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003292 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003293 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3294 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003295 const std::chrono::duration appTimeout =
3296 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003297 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003298
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003299 std::this_thread::sleep_for(appTimeout);
3300 // ANR should not be raised again. It is up to policy to do that if it desires.
3301 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003302
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003303 // If we now get a focused window, the ANR should stop, but the policy handles that via
3304 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003305 ASSERT_TRUE(mDispatcher->waitForIdle());
3306}
3307
3308// We have a focused application, but no focused window
3309TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003310 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003311 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3312 mWindow->consumeFocusEvent(false);
3313
3314 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003315 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003316 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003317 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3318 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003319
3320 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003321 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003322
3323 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003324 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003325 ASSERT_TRUE(mDispatcher->waitForIdle());
3326 mWindow->assertNoEvents();
3327}
3328
3329/**
3330 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
3331 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
3332 * If we process 1 of the events, but ANR on the second event with the same timestamp,
3333 * the ANR mechanism should still work.
3334 *
3335 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
3336 * DOWN event, while not responding on the second one.
3337 */
3338TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
3339 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
3340 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3341 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
3342 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
3343 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003344 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003345
3346 // Now send ACTION_UP, with identical timestamp
3347 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3348 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
3349 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
3350 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003351 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003352
3353 // We have now sent down and up. Let's consume first event and then ANR on the second.
3354 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3355 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003356 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003357}
3358
3359// If an app is not responding to a key event, gesture monitors should continue to receive
3360// new motion events
3361TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnKey) {
3362 FakeMonitorReceiver monitor =
3363 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
3364 true /*isGestureMonitor*/);
3365
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003366 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3367 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003368 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003369 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003370
3371 // Stuck on the ACTION_UP
3372 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003373 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003374
3375 // New tap will go to the gesture monitor, but not to the window
3376 tapOnWindow();
3377 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3378 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3379
3380 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
3381 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003382 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003383 mWindow->assertNoEvents();
3384 monitor.assertNoEvents();
3385}
3386
3387// If an app is not responding to a motion event, gesture monitors should continue to receive
3388// new motion events
3389TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnMotion) {
3390 FakeMonitorReceiver monitor =
3391 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
3392 true /*isGestureMonitor*/);
3393
3394 tapOnWindow();
3395 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3396 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3397
3398 mWindow->consumeMotionDown();
3399 // Stuck on the ACTION_UP
3400 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003401 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003402
3403 // New tap will go to the gesture monitor, but not to the window
3404 tapOnWindow();
3405 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3406 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3407
3408 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
3409 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003410 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003411 mWindow->assertNoEvents();
3412 monitor.assertNoEvents();
3413}
3414
3415// If a window is unresponsive, then you get anr. if the window later catches up and starts to
3416// process events, you don't get an anr. When the window later becomes unresponsive again, you
3417// get an ANR again.
3418// 1. tap -> block on ACTION_UP -> receive ANR
3419// 2. consume all pending events (= queue becomes healthy again)
3420// 3. tap again -> block on ACTION_UP again -> receive ANR second time
3421TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
3422 tapOnWindow();
3423
3424 mWindow->consumeMotionDown();
3425 // Block on ACTION_UP
3426 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003427 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003428 mWindow->consumeMotionUp(); // Now the connection should be healthy again
3429 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003430 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003431 mWindow->assertNoEvents();
3432
3433 tapOnWindow();
3434 mWindow->consumeMotionDown();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003435 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003436 mWindow->consumeMotionUp();
3437
3438 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003439 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003440 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003441 mWindow->assertNoEvents();
3442}
3443
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003444// If a connection remains unresponsive for a while, make sure policy is only notified once about
3445// it.
3446TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003447 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003448 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3449 WINDOW_LOCATION));
3450
3451 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003452 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003453 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003454 // 'notifyConnectionUnresponsive' should only be called once per connection
3455 mFakePolicy->assertNotifyAnrWasNotCalled();
3456 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003457 mWindow->consumeMotionDown();
3458 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3459 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3460 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003461 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003462 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003463 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003464}
3465
3466/**
3467 * If a window is processing a motion event, and then a key event comes in, the key event should
3468 * not to to the focused window until the motion is processed.
3469 *
3470 * Warning!!!
3471 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
3472 * and the injection timeout that we specify when injecting the key.
3473 * We must have the injection timeout (10ms) be smaller than
3474 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
3475 *
3476 * If that value changes, this test should also change.
3477 */
3478TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
3479 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3480 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3481
3482 tapOnWindow();
3483 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3484 ASSERT_TRUE(downSequenceNum);
3485 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3486 ASSERT_TRUE(upSequenceNum);
3487 // Don't finish the events yet, and send a key
3488 // Injection will "succeed" because we will eventually give up and send the key to the focused
3489 // window even if motions are still being processed. But because the injection timeout is short,
3490 // we will receive INJECTION_TIMED_OUT as the result.
3491
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003492 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003493 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003494 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3495 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003496 // Key will not be sent to the window, yet, because the window is still processing events
3497 // and the key remains pending, waiting for the touch events to be processed
3498 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3499 ASSERT_FALSE(keySequenceNum);
3500
3501 std::this_thread::sleep_for(500ms);
3502 // if we wait long enough though, dispatcher will give up, and still send the key
3503 // to the focused window, even though we have not yet finished the motion event
3504 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3505 mWindow->finishEvent(*downSequenceNum);
3506 mWindow->finishEvent(*upSequenceNum);
3507}
3508
3509/**
3510 * If a window is processing a motion event, and then a key event comes in, the key event should
3511 * not go to the focused window until the motion is processed.
3512 * If then a new motion comes in, then the pending key event should be going to the currently
3513 * focused window right away.
3514 */
3515TEST_F(InputDispatcherSingleWindowAnr,
3516 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
3517 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3518 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3519
3520 tapOnWindow();
3521 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3522 ASSERT_TRUE(downSequenceNum);
3523 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3524 ASSERT_TRUE(upSequenceNum);
3525 // Don't finish the events yet, and send a key
3526 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003527 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003528 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003529 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003530 // At this point, key is still pending, and should not be sent to the application yet.
3531 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3532 ASSERT_FALSE(keySequenceNum);
3533
3534 // Now tap down again. It should cause the pending key to go to the focused window right away.
3535 tapOnWindow();
3536 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
3537 // the other events yet. We can finish events in any order.
3538 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
3539 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
3540 mWindow->consumeMotionDown();
3541 mWindow->consumeMotionUp();
3542 mWindow->assertNoEvents();
3543}
3544
3545class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
3546 virtual void SetUp() override {
3547 InputDispatcherTest::SetUp();
3548
Chris Yea209fde2020-07-22 13:54:51 -07003549 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003550 mApplication->setDispatchingTimeout(10ms);
3551 mUnfocusedWindow =
3552 new FakeWindowHandle(mApplication, mDispatcher, "Unfocused", ADISPLAY_ID_DEFAULT);
3553 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
3554 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3555 // window.
3556 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Michael Wright44753b12020-07-08 13:48:11 +01003557 mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3558 InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH |
3559 InputWindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003560
3561 mFocusedWindow =
3562 new FakeWindowHandle(mApplication, mDispatcher, "Focused", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05003563 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003564 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01003565 mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3566 InputWindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003567
3568 // Set focused application.
3569 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07003570 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003571
3572 // Expect one focus window exist in display.
3573 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003574 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003575 mFocusedWindow->consumeFocusEvent(true);
3576 }
3577
3578 virtual void TearDown() override {
3579 InputDispatcherTest::TearDown();
3580
3581 mUnfocusedWindow.clear();
3582 mFocusedWindow.clear();
3583 }
3584
3585protected:
Chris Yea209fde2020-07-22 13:54:51 -07003586 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003587 sp<FakeWindowHandle> mUnfocusedWindow;
3588 sp<FakeWindowHandle> mFocusedWindow;
3589 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
3590 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
3591 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
3592
3593 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
3594
3595 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
3596
3597private:
3598 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003599 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003600 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3601 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003602 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003603 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3604 location));
3605 }
3606};
3607
3608// If we have 2 windows that are both unresponsive, the one with the shortest timeout
3609// should be ANR'd first.
3610TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003611 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003612 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3613 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003614 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003615 mFocusedWindow->consumeMotionDown();
3616 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3617 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3618 // We consumed all events, so no ANR
3619 ASSERT_TRUE(mDispatcher->waitForIdle());
3620 mFakePolicy->assertNotifyAnrWasNotCalled();
3621
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003622 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003623 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3624 FOCUSED_WINDOW_LOCATION));
3625 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
3626 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003627
3628 const std::chrono::duration timeout =
3629 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003630 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003631 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
3632 // sequence to make it consistent
3633 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003634 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003635 mFocusedWindow->consumeMotionDown();
3636 // This cancel is generated because the connection was unresponsive
3637 mFocusedWindow->consumeMotionCancel();
3638 mFocusedWindow->assertNoEvents();
3639 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003640 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003641 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003642 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003643}
3644
3645// If we have 2 windows with identical timeouts that are both unresponsive,
3646// it doesn't matter which order they should have ANR.
3647// But we should receive ANR for both.
3648TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
3649 // Set the timeout for unfocused window to match the focused window
3650 mUnfocusedWindow->setDispatchingTimeout(10ms);
3651 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
3652
3653 tapOnFocusedWindow();
3654 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003655 sp<IBinder> anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms);
3656 sp<IBinder> anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003657
3658 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003659 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
3660 mFocusedWindow->getToken() == anrConnectionToken2);
3661 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
3662 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003663
3664 ASSERT_TRUE(mDispatcher->waitForIdle());
3665 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003666
3667 mFocusedWindow->consumeMotionDown();
3668 mFocusedWindow->consumeMotionUp();
3669 mUnfocusedWindow->consumeMotionOutside();
3670
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003671 sp<IBinder> responsiveToken1 = mFakePolicy->getResponsiveWindowToken();
3672 sp<IBinder> responsiveToken2 = mFakePolicy->getResponsiveWindowToken();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003673
3674 // Both applications should be marked as responsive, in any order
3675 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
3676 mFocusedWindow->getToken() == responsiveToken2);
3677 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
3678 mUnfocusedWindow->getToken() == responsiveToken2);
3679 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003680}
3681
3682// If a window is already not responding, the second tap on the same window should be ignored.
3683// We should also log an error to account for the dropped event (not tested here).
3684// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
3685TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
3686 tapOnFocusedWindow();
3687 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3688 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3689 // Receive the events, but don't respond
3690 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
3691 ASSERT_TRUE(downEventSequenceNum);
3692 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
3693 ASSERT_TRUE(upEventSequenceNum);
3694 const std::chrono::duration timeout =
3695 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003696 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003697
3698 // Tap once again
3699 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003700 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003701 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3702 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003703 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003704 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3705 FOCUSED_WINDOW_LOCATION));
3706 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
3707 // valid touch target
3708 mUnfocusedWindow->assertNoEvents();
3709
3710 // Consume the first tap
3711 mFocusedWindow->finishEvent(*downEventSequenceNum);
3712 mFocusedWindow->finishEvent(*upEventSequenceNum);
3713 ASSERT_TRUE(mDispatcher->waitForIdle());
3714 // The second tap did not go to the focused window
3715 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003716 // Since all events are finished, connection should be deemed healthy again
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003717 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003718 mFakePolicy->assertNotifyAnrWasNotCalled();
3719}
3720
3721// If you tap outside of all windows, there will not be ANR
3722TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003723 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003724 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3725 LOCATION_OUTSIDE_ALL_WINDOWS));
3726 ASSERT_TRUE(mDispatcher->waitForIdle());
3727 mFakePolicy->assertNotifyAnrWasNotCalled();
3728}
3729
3730// Since the focused window is paused, tapping on it should not produce any events
3731TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
3732 mFocusedWindow->setPaused(true);
3733 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
3734
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003735 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003736 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3737 FOCUSED_WINDOW_LOCATION));
3738
3739 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
3740 ASSERT_TRUE(mDispatcher->waitForIdle());
3741 // Should not ANR because the window is paused, and touches shouldn't go to it
3742 mFakePolicy->assertNotifyAnrWasNotCalled();
3743
3744 mFocusedWindow->assertNoEvents();
3745 mUnfocusedWindow->assertNoEvents();
3746}
3747
3748/**
3749 * If a window is processing a motion event, and then a key event comes in, the key event should
3750 * not to to the focused window until the motion is processed.
3751 * If a different window becomes focused at this time, the key should go to that window instead.
3752 *
3753 * Warning!!!
3754 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
3755 * and the injection timeout that we specify when injecting the key.
3756 * We must have the injection timeout (10ms) be smaller than
3757 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
3758 *
3759 * If that value changes, this test should also change.
3760 */
3761TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
3762 // Set a long ANR timeout to prevent it from triggering
3763 mFocusedWindow->setDispatchingTimeout(2s);
3764 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
3765
3766 tapOnUnfocusedWindow();
3767 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
3768 ASSERT_TRUE(downSequenceNum);
3769 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
3770 ASSERT_TRUE(upSequenceNum);
3771 // Don't finish the events yet, and send a key
3772 // Injection will succeed because we will eventually give up and send the key to the focused
3773 // window even if motions are still being processed.
3774
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003775 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003776 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003777 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
3778 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003779 // Key will not be sent to the window, yet, because the window is still processing events
3780 // and the key remains pending, waiting for the touch events to be processed
3781 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
3782 ASSERT_FALSE(keySequenceNum);
3783
3784 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07003785 mFocusedWindow->setFocusable(false);
3786 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003787 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003788 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003789
3790 // Focus events should precede the key events
3791 mUnfocusedWindow->consumeFocusEvent(true);
3792 mFocusedWindow->consumeFocusEvent(false);
3793
3794 // Finish the tap events, which should unblock dispatcher
3795 mUnfocusedWindow->finishEvent(*downSequenceNum);
3796 mUnfocusedWindow->finishEvent(*upSequenceNum);
3797
3798 // Now that all queues are cleared and no backlog in the connections, the key event
3799 // can finally go to the newly focused "mUnfocusedWindow".
3800 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3801 mFocusedWindow->assertNoEvents();
3802 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003803 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003804}
3805
3806// When the touch stream is split across 2 windows, and one of them does not respond,
3807// then ANR should be raised and the touch should be canceled for the unresponsive window.
3808// The other window should not be affected by that.
3809TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
3810 // Touch Window 1
3811 NotifyMotionArgs motionArgs =
3812 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3813 ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION});
3814 mDispatcher->notifyMotion(&motionArgs);
3815 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3816 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3817
3818 // Touch Window 2
3819 int32_t actionPointerDown =
3820 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3821
3822 motionArgs =
3823 generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3824 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION});
3825 mDispatcher->notifyMotion(&motionArgs);
3826
3827 const std::chrono::duration timeout =
3828 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003829 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003830
3831 mUnfocusedWindow->consumeMotionDown();
3832 mFocusedWindow->consumeMotionDown();
3833 // Focused window may or may not receive ACTION_MOVE
3834 // But it should definitely receive ACTION_CANCEL due to the ANR
3835 InputEvent* event;
3836 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
3837 ASSERT_TRUE(moveOrCancelSequenceNum);
3838 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
3839 ASSERT_NE(nullptr, event);
3840 ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION);
3841 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
3842 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
3843 mFocusedWindow->consumeMotionCancel();
3844 } else {
3845 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
3846 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003847 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003848 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003849
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003850 mUnfocusedWindow->assertNoEvents();
3851 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003852 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003853}
3854
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05003855/**
3856 * If we have no focused window, and a key comes in, we start the ANR timer.
3857 * The focused application should add a focused window before the timer runs out to prevent ANR.
3858 *
3859 * If the user touches another application during this time, the key should be dropped.
3860 * Next, if a new focused window comes in, without toggling the focused application,
3861 * then no ANR should occur.
3862 *
3863 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
3864 * but in some cases the policy may not update the focused application.
3865 */
3866TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
3867 std::shared_ptr<FakeApplicationHandle> focusedApplication =
3868 std::make_shared<FakeApplicationHandle>();
3869 focusedApplication->setDispatchingTimeout(60ms);
3870 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
3871 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
3872 mFocusedWindow->setFocusable(false);
3873
3874 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
3875 mFocusedWindow->consumeFocusEvent(false);
3876
3877 // Send a key. The ANR timer should start because there is no focused window.
3878 // 'focusedApplication' will get blamed if this timer completes.
3879 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003880 InputEventInjectionResult result =
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05003881 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003882 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
3883 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05003884
3885 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
3886 // then the injected touches won't cause the focused event to get dropped.
3887 // The dispatcher only checks for whether the queue should be pruned upon queueing.
3888 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
3889 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
3890 // For this test, it means that the key would get delivered to the window once it becomes
3891 // focused.
3892 std::this_thread::sleep_for(10ms);
3893
3894 // Touch unfocused window. This should force the pending key to get dropped.
3895 NotifyMotionArgs motionArgs =
3896 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3897 ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION});
3898 mDispatcher->notifyMotion(&motionArgs);
3899
3900 // We do not consume the motion right away, because that would require dispatcher to first
3901 // process (== drop) the key event, and by that time, ANR will be raised.
3902 // Set the focused window first.
3903 mFocusedWindow->setFocusable(true);
3904 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
3905 setFocusedWindow(mFocusedWindow);
3906 mFocusedWindow->consumeFocusEvent(true);
3907 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
3908 // to another application. This could be a bug / behaviour in the policy.
3909
3910 mUnfocusedWindow->consumeMotionDown();
3911
3912 ASSERT_TRUE(mDispatcher->waitForIdle());
3913 // Should not ANR because we actually have a focused window. It was just added too slowly.
3914 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
3915}
3916
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05003917// These tests ensure we cannot send touch events to a window that's positioned behind a window
3918// that has feature NO_INPUT_CHANNEL.
3919// Layout:
3920// Top (closest to user)
3921// mNoInputWindow (above all windows)
3922// mBottomWindow
3923// Bottom (furthest from user)
3924class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
3925 virtual void SetUp() override {
3926 InputDispatcherTest::SetUp();
3927
3928 mApplication = std::make_shared<FakeApplicationHandle>();
3929 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
3930 "Window without input channel", ADISPLAY_ID_DEFAULT,
3931 std::make_optional<sp<IBinder>>(nullptr) /*token*/);
3932
3933 mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL);
3934 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
3935 // It's perfectly valid for this window to not have an associated input channel
3936
3937 mBottomWindow = new FakeWindowHandle(mApplication, mDispatcher, "Bottom window",
3938 ADISPLAY_ID_DEFAULT);
3939 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
3940
3941 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
3942 }
3943
3944protected:
3945 std::shared_ptr<FakeApplicationHandle> mApplication;
3946 sp<FakeWindowHandle> mNoInputWindow;
3947 sp<FakeWindowHandle> mBottomWindow;
3948};
3949
3950TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
3951 PointF touchedPoint = {10, 10};
3952
3953 NotifyMotionArgs motionArgs =
3954 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3955 ADISPLAY_ID_DEFAULT, {touchedPoint});
3956 mDispatcher->notifyMotion(&motionArgs);
3957
3958 mNoInputWindow->assertNoEvents();
3959 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
3960 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
3961 // and therefore should prevent mBottomWindow from receiving touches
3962 mBottomWindow->assertNoEvents();
3963}
3964
3965/**
3966 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
3967 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
3968 */
3969TEST_F(InputDispatcherMultiWindowOcclusionTests,
3970 NoInputChannelFeature_DropsTouchesWithValidChannel) {
3971 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
3972 "Window with input channel and NO_INPUT_CHANNEL",
3973 ADISPLAY_ID_DEFAULT);
3974
3975 mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL);
3976 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
3977 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
3978
3979 PointF touchedPoint = {10, 10};
3980
3981 NotifyMotionArgs motionArgs =
3982 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3983 ADISPLAY_ID_DEFAULT, {touchedPoint});
3984 mDispatcher->notifyMotion(&motionArgs);
3985
3986 mNoInputWindow->assertNoEvents();
3987 mBottomWindow->assertNoEvents();
3988}
3989
Vishnu Nair958da932020-08-21 17:12:37 -07003990class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
3991protected:
3992 std::shared_ptr<FakeApplicationHandle> mApp;
3993 sp<FakeWindowHandle> mWindow;
3994 sp<FakeWindowHandle> mMirror;
3995
3996 virtual void SetUp() override {
3997 InputDispatcherTest::SetUp();
3998 mApp = std::make_shared<FakeApplicationHandle>();
3999 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
4000 mMirror = new FakeWindowHandle(mApp, mDispatcher, "TestWindowMirror", ADISPLAY_ID_DEFAULT,
4001 mWindow->getToken());
4002 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
4003 mWindow->setFocusable(true);
4004 mMirror->setFocusable(true);
4005 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4006 }
4007};
4008
4009TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
4010 // Request focus on a mirrored window
4011 setFocusedWindow(mMirror);
4012
4013 // window gets focused
4014 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004015 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4016 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004017 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
4018}
4019
4020// A focused & mirrored window remains focused only if the window and its mirror are both
4021// focusable.
4022TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
4023 setFocusedWindow(mMirror);
4024
4025 // window gets focused
4026 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004027 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4028 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004029 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004030 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4031 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004032 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4033
4034 mMirror->setFocusable(false);
4035 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4036
4037 // window loses focus since one of the windows associated with the token in not focusable
4038 mWindow->consumeFocusEvent(false);
4039
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004040 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4041 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004042 mWindow->assertNoEvents();
4043}
4044
4045// A focused & mirrored window remains focused until the window and its mirror both become
4046// invisible.
4047TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
4048 setFocusedWindow(mMirror);
4049
4050 // window gets focused
4051 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004052 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4053 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004054 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004055 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4056 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004057 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4058
4059 mMirror->setVisible(false);
4060 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4061
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004062 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4063 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004064 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004065 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4066 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004067 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4068
4069 mWindow->setVisible(false);
4070 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4071
4072 // window loses focus only after all windows associated with the token become invisible.
4073 mWindow->consumeFocusEvent(false);
4074
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004075 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4076 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004077 mWindow->assertNoEvents();
4078}
4079
4080// A focused & mirrored window remains focused until both windows are removed.
4081TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
4082 setFocusedWindow(mMirror);
4083
4084 // window gets focused
4085 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004086 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4087 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004088 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004089 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4090 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004091 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4092
4093 // single window is removed but the window token remains focused
4094 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
4095
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004096 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4097 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004098 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004099 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4100 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004101 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4102
4103 // Both windows are removed
4104 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
4105 mWindow->consumeFocusEvent(false);
4106
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004107 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4108 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004109 mWindow->assertNoEvents();
4110}
4111
4112// Focus request can be pending until one window becomes visible.
4113TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
4114 // Request focus on an invisible mirror.
4115 mWindow->setVisible(false);
4116 mMirror->setVisible(false);
4117 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4118 setFocusedWindow(mMirror);
4119
4120 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004121 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07004122 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004123 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07004124
4125 mMirror->setVisible(true);
4126 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4127
4128 // window gets focused
4129 mWindow->consumeFocusEvent(true);
4130 // window gets the pending key event
4131 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4132}
Prabir Pradhan99987712020-11-10 18:43:05 -08004133
4134class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
4135protected:
4136 std::shared_ptr<FakeApplicationHandle> mApp;
4137 sp<FakeWindowHandle> mWindow;
4138 sp<FakeWindowHandle> mSecondWindow;
4139
4140 void SetUp() override {
4141 InputDispatcherTest::SetUp();
4142 mApp = std::make_shared<FakeApplicationHandle>();
4143 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
4144 mWindow->setFocusable(true);
4145 mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
4146 mSecondWindow->setFocusable(true);
4147
4148 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
4149 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
4150
4151 setFocusedWindow(mWindow);
4152 mWindow->consumeFocusEvent(true);
4153 }
4154
4155 void notifyPointerCaptureChanged(bool enabled) {
4156 const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(enabled);
4157 mDispatcher->notifyPointerCaptureChanged(&args);
4158 }
4159
4160 void requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, bool enabled) {
4161 mDispatcher->requestPointerCapture(window->getToken(), enabled);
4162 mFakePolicy->waitForSetPointerCapture(enabled);
4163 notifyPointerCaptureChanged(enabled);
4164 window->consumeCaptureEvent(enabled);
4165 }
4166};
4167
4168TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
4169 // Ensure that capture cannot be obtained for unfocused windows.
4170 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
4171 mFakePolicy->assertSetPointerCaptureNotCalled();
4172 mSecondWindow->assertNoEvents();
4173
4174 // Ensure that capture can be enabled from the focus window.
4175 requestAndVerifyPointerCapture(mWindow, true);
4176
4177 // Ensure that capture cannot be disabled from a window that does not have capture.
4178 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
4179 mFakePolicy->assertSetPointerCaptureNotCalled();
4180
4181 // Ensure that capture can be disabled from the window with capture.
4182 requestAndVerifyPointerCapture(mWindow, false);
4183}
4184
4185TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
4186 requestAndVerifyPointerCapture(mWindow, true);
4187
4188 setFocusedWindow(mSecondWindow);
4189
4190 // Ensure that the capture disabled event was sent first.
4191 mWindow->consumeCaptureEvent(false);
4192 mWindow->consumeFocusEvent(false);
4193 mSecondWindow->consumeFocusEvent(true);
4194 mFakePolicy->waitForSetPointerCapture(false);
4195
4196 // Ensure that additional state changes from InputReader are not sent to the window.
4197 notifyPointerCaptureChanged(false);
4198 notifyPointerCaptureChanged(true);
4199 notifyPointerCaptureChanged(false);
4200 mWindow->assertNoEvents();
4201 mSecondWindow->assertNoEvents();
4202 mFakePolicy->assertSetPointerCaptureNotCalled();
4203}
4204
4205TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
4206 requestAndVerifyPointerCapture(mWindow, true);
4207
4208 // InputReader unexpectedly disables and enables pointer capture.
4209 notifyPointerCaptureChanged(false);
4210 notifyPointerCaptureChanged(true);
4211
4212 // Ensure that Pointer Capture is disabled.
Prabir Pradhan7d030382020-12-21 07:58:35 -08004213 mFakePolicy->waitForSetPointerCapture(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08004214 mWindow->consumeCaptureEvent(false);
4215 mWindow->assertNoEvents();
4216}
4217
Garfield Tane84e6f92019-08-29 17:28:41 -07004218} // namespace android::inputdispatcher