blob: d1bd38e1b768d2298d7fa6e0bd002e8102bd0965 [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;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000034using android::os::TouchOcclusionMode;
Michael Wright44753b12020-07-08 13:48:11 +010035using namespace android::flag_operators;
Garfield Tan1c7bc862020-01-28 13:24:04 -080036
Garfield Tane84e6f92019-08-29 17:28:41 -070037namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080038
39// An arbitrary time value.
40static const nsecs_t ARBITRARY_TIME = 1234;
41
42// An arbitrary device id.
43static const int32_t DEVICE_ID = 1;
44
Jeff Brownf086ddb2014-02-11 14:28:48 -080045// An arbitrary display id.
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -080046static const int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
Jeff Brownf086ddb2014-02-11 14:28:48 -080047
Michael Wrightd02c5b62014-02-10 15:10:22 -080048// An arbitrary injector pid / uid pair that has permission to inject events.
49static const int32_t INJECTOR_PID = 999;
50static const int32_t INJECTOR_UID = 1001;
51
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +000052// An arbitrary pid of the gesture monitor window
53static constexpr int32_t MONITOR_PID = 2001;
54
chaviwd1c23182019-12-20 18:44:56 -080055struct PointF {
56 float x;
57 float y;
58};
Michael Wrightd02c5b62014-02-10 15:10:22 -080059
Gang Wang342c9272020-01-13 13:15:04 -050060/**
61 * Return a DOWN key event with KEYCODE_A.
62 */
63static KeyEvent getTestKeyEvent() {
64 KeyEvent event;
65
Garfield Tanfbe732e2020-01-24 11:26:14 -080066 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
67 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
68 ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -050069 return event;
70}
71
Michael Wrightd02c5b62014-02-10 15:10:22 -080072// --- FakeInputDispatcherPolicy ---
73
74class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
75 InputDispatcherConfiguration mConfig;
76
77protected:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100078 virtual ~FakeInputDispatcherPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -080079
80public:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100081 FakeInputDispatcherPolicy() {}
Jackal Guof9696682018-10-05 12:23:23 +080082
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080083 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080084 assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_KEY, args.eventTime, args.action,
85 args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080086 }
87
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080088 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args) {
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080089 assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_MOTION, args.eventTime, args.action,
90 args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080091 }
92
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -070093 void assertFilterInputEventWasNotCalled() {
94 std::scoped_lock lock(mLock);
95 ASSERT_EQ(nullptr, mFilteredEvent);
96 }
Michael Wrightd02c5b62014-02-10 15:10:22 -080097
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -080098 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -070099 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800100 ASSERT_TRUE(mConfigurationChangedTime)
101 << "Timed out waiting for configuration changed call";
102 ASSERT_EQ(*mConfigurationChangedTime, when);
103 mConfigurationChangedTime = std::nullopt;
104 }
105
106 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700107 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800108 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800109 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800110 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
111 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
112 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
113 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
114 mLastNotifySwitch = std::nullopt;
115 }
116
chaviwfd6d3512019-03-25 13:23:49 -0700117 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700118 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800119 ASSERT_EQ(touchedToken, mOnPointerDownToken);
120 mOnPointerDownToken.clear();
121 }
122
123 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700124 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800125 ASSERT_TRUE(mOnPointerDownToken == nullptr)
126 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700127 }
128
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700129 // This function must be called soon after the expected ANR timer starts,
130 // because we are also checking how much time has passed.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500131 void assertNotifyNoFocusedWindowAnrWasCalled(
Chris Yea209fde2020-07-22 13:54:51 -0700132 std::chrono::nanoseconds timeout,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500133 const std::shared_ptr<InputApplicationHandle>& expectedApplication) {
134 std::shared_ptr<InputApplicationHandle> application;
135 { // acquire lock
136 std::unique_lock lock(mLock);
137 android::base::ScopedLockAssertion assumeLocked(mLock);
138 ASSERT_NO_FATAL_FAILURE(
139 application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock));
140 } // release lock
141 ASSERT_EQ(expectedApplication, application);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700142 }
143
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000144 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
145 const sp<IBinder>& expectedConnectionToken) {
146 sp<IBinder> connectionToken = getUnresponsiveWindowToken(timeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500147 ASSERT_EQ(expectedConnectionToken, connectionToken);
148 }
149
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000150 void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedConnectionToken) {
151 sp<IBinder> connectionToken = getResponsiveWindowToken();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500152 ASSERT_EQ(expectedConnectionToken, connectionToken);
153 }
154
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000155 void assertNotifyMonitorUnresponsiveWasCalled(std::chrono::nanoseconds timeout) {
156 int32_t pid = getUnresponsiveMonitorPid(timeout);
157 ASSERT_EQ(MONITOR_PID, pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500158 }
159
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000160 void assertNotifyMonitorResponsiveWasCalled() {
161 int32_t pid = getResponsiveMonitorPid();
162 ASSERT_EQ(MONITOR_PID, pid);
163 }
164
165 sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500166 std::unique_lock lock(mLock);
167 android::base::ScopedLockAssertion assumeLocked(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000168 return getAnrTokenLockedInterruptible(timeout, mAnrWindowTokens, lock);
169 }
170
171 sp<IBinder> getResponsiveWindowToken() {
172 std::unique_lock lock(mLock);
173 android::base::ScopedLockAssertion assumeLocked(mLock);
174 return getAnrTokenLockedInterruptible(0s, mResponsiveWindowTokens, lock);
175 }
176
177 int32_t getUnresponsiveMonitorPid(std::chrono::nanoseconds timeout) {
178 std::unique_lock lock(mLock);
179 android::base::ScopedLockAssertion assumeLocked(mLock);
180 return getAnrTokenLockedInterruptible(timeout, mAnrMonitorPids, lock);
181 }
182
183 int32_t getResponsiveMonitorPid() {
184 std::unique_lock lock(mLock);
185 android::base::ScopedLockAssertion assumeLocked(mLock);
186 return getAnrTokenLockedInterruptible(0s, mResponsiveMonitorPids, lock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500187 }
188
189 // All three ANR-related callbacks behave the same way, so we use this generic function to wait
190 // for a specific container to become non-empty. When the container is non-empty, return the
191 // first entry from the container and erase it.
192 template <class T>
193 T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage,
194 std::unique_lock<std::mutex>& lock) REQUIRES(mLock) {
195 const std::chrono::time_point start = std::chrono::steady_clock::now();
196 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700197
198 // If there is an ANR, Dispatcher won't be idle because there are still events
199 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
200 // before checking if ANR was called.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500201 // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need
202 // to provide it some time to act. 100ms seems reasonable.
203 mNotifyAnr.wait_for(lock, timeToWait,
204 [&storage]() REQUIRES(mLock) { return !storage.empty(); });
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700205 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500206 if (storage.empty()) {
207 ADD_FAILURE() << "Did not receive the ANR callback";
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000208 return {};
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700209 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700210 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
211 // the dispatcher started counting before this function was called
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700212 if (std::chrono::abs(timeout - waited) > 100ms) {
213 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
214 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
215 << "ms, but waited "
216 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
217 << "ms instead";
218 }
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500219 T token = storage.front();
220 storage.pop();
221 return token;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700222 }
223
224 void assertNotifyAnrWasNotCalled() {
225 std::scoped_lock lock(mLock);
226 ASSERT_TRUE(mAnrApplications.empty());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000227 ASSERT_TRUE(mAnrWindowTokens.empty());
228 ASSERT_TRUE(mAnrMonitorPids.empty());
229 ASSERT_TRUE(mResponsiveWindowTokens.empty())
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500230 << "ANR was not called, but please also consume the 'connection is responsive' "
231 "signal";
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000232 ASSERT_TRUE(mResponsiveMonitorPids.empty())
233 << "Monitor ANR was not called, but please also consume the 'monitor is responsive'"
234 " signal";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700235 }
236
Garfield Tan1c7bc862020-01-28 13:24:04 -0800237 void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) {
238 mConfig.keyRepeatTimeout = timeout;
239 mConfig.keyRepeatDelay = delay;
240 }
241
Prabir Pradhan99987712020-11-10 18:43:05 -0800242 void waitForSetPointerCapture(bool enabled) {
243 std::unique_lock lock(mLock);
244 base::ScopedLockAssertion assumeLocked(mLock);
245
246 if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms,
247 [this, enabled]() REQUIRES(mLock) {
248 return mPointerCaptureEnabled &&
249 *mPointerCaptureEnabled ==
250 enabled;
251 })) {
252 FAIL() << "Timed out waiting for setPointerCapture(" << enabled << ") to be called.";
253 }
254 mPointerCaptureEnabled.reset();
255 }
256
257 void assertSetPointerCaptureNotCalled() {
258 std::unique_lock lock(mLock);
259 base::ScopedLockAssertion assumeLocked(mLock);
260
261 if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) {
262 FAIL() << "Expected setPointerCapture(enabled) to not be called, but was called. "
263 "enabled = "
264 << *mPointerCaptureEnabled;
265 }
266 mPointerCaptureEnabled.reset();
267 }
268
arthurhungf452d0b2021-01-06 00:19:52 +0800269 void assertDropTargetEquals(const sp<IBinder>& targetToken) {
270 std::scoped_lock lock(mLock);
271 ASSERT_EQ(targetToken, mDropTargetWindowToken);
272 }
273
Michael Wrightd02c5b62014-02-10 15:10:22 -0800274private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700275 std::mutex mLock;
276 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
277 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
278 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
279 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800280
Prabir Pradhan99987712020-11-10 18:43:05 -0800281 std::condition_variable mPointerCaptureChangedCondition;
282 std::optional<bool> mPointerCaptureEnabled GUARDED_BY(mLock);
283
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700284 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700285 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000286 std::queue<sp<IBinder>> mAnrWindowTokens GUARDED_BY(mLock);
287 std::queue<sp<IBinder>> mResponsiveWindowTokens GUARDED_BY(mLock);
288 std::queue<int32_t> mAnrMonitorPids GUARDED_BY(mLock);
289 std::queue<int32_t> mResponsiveMonitorPids GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700290 std::condition_variable mNotifyAnr;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700291
arthurhungf452d0b2021-01-06 00:19:52 +0800292 sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock);
293
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600294 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700295 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800296 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800297 }
298
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000299 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700300 std::scoped_lock lock(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000301 mAnrWindowTokens.push(connectionToken);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700302 mNotifyAnr.notify_all();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500303 }
304
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000305 void notifyMonitorUnresponsive(int32_t pid, const std::string&) override {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500306 std::scoped_lock lock(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000307 mAnrMonitorPids.push(pid);
308 mNotifyAnr.notify_all();
309 }
310
311 void notifyWindowResponsive(const sp<IBinder>& connectionToken) override {
312 std::scoped_lock lock(mLock);
313 mResponsiveWindowTokens.push(connectionToken);
314 mNotifyAnr.notify_all();
315 }
316
317 void notifyMonitorResponsive(int32_t pid) override {
318 std::scoped_lock lock(mLock);
319 mResponsiveMonitorPids.push(pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500320 mNotifyAnr.notify_all();
321 }
322
323 void notifyNoFocusedWindowAnr(
324 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
325 std::scoped_lock lock(mLock);
326 mAnrApplications.push(applicationHandle);
327 mNotifyAnr.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800328 }
329
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600330 void notifyInputChannelBroken(const sp<IBinder>&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800331
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600332 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700333
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600334 void notifyUntrustedTouch(const std::string& obscuringPackage) override {}
Chris Yef59a2f42020-10-16 12:55:26 -0700335 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
336 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
337 const std::vector<float>& values) override {}
338
339 void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType,
340 InputDeviceSensorAccuracy accuracy) override {}
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000341
Chris Yefb552902021-02-03 17:18:37 -0800342 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
343
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600344 void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800345 *outConfig = mConfig;
346 }
347
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600348 bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700349 std::scoped_lock lock(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800350 switch (inputEvent->getType()) {
351 case AINPUT_EVENT_TYPE_KEY: {
352 const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800353 mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800354 break;
355 }
356
357 case AINPUT_EVENT_TYPE_MOTION: {
358 const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800359 mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800360 break;
361 }
362 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800363 return true;
364 }
365
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600366 void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800367
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600368 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800369
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600370 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800371 return 0;
372 }
373
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600374 bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800375 return false;
376 }
377
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600378 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
379 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700380 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800381 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
382 * essentially a passthrough for notifySwitch.
383 */
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800384 mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800385 }
386
Sean Stoutb4e0a592021-02-23 07:34:53 -0800387 void pokeUserActivity(nsecs_t, int32_t, int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800388
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600389 bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) override { return false; }
Jackal Guof9696682018-10-05 12:23:23 +0800390
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600391 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700392 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700393 mOnPointerDownToken = newToken;
394 }
395
Prabir Pradhan99987712020-11-10 18:43:05 -0800396 void setPointerCapture(bool enabled) override {
397 std::scoped_lock lock(mLock);
398 mPointerCaptureEnabled = {enabled};
399 mPointerCaptureChangedCondition.notify_all();
400 }
401
arthurhungf452d0b2021-01-06 00:19:52 +0800402 void notifyDropWindow(const sp<IBinder>& token, float x, float y) override {
403 std::scoped_lock lock(mLock);
404 mDropTargetWindowToken = token;
405 }
406
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800407 void assertFilterInputEventWasCalled(int type, nsecs_t eventTime, int32_t action,
408 int32_t displayId) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700409 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800410 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
411 ASSERT_EQ(mFilteredEvent->getType(), type);
412
413 if (type == AINPUT_EVENT_TYPE_KEY) {
414 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*mFilteredEvent);
415 EXPECT_EQ(keyEvent.getEventTime(), eventTime);
416 EXPECT_EQ(keyEvent.getAction(), action);
417 EXPECT_EQ(keyEvent.getDisplayId(), displayId);
418 } else if (type == AINPUT_EVENT_TYPE_MOTION) {
419 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*mFilteredEvent);
420 EXPECT_EQ(motionEvent.getEventTime(), eventTime);
421 EXPECT_EQ(motionEvent.getAction(), action);
422 EXPECT_EQ(motionEvent.getDisplayId(), displayId);
423 } else {
424 FAIL() << "Unknown type: " << type;
425 }
426
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800427 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800428 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800429};
430
Michael Wrightd02c5b62014-02-10 15:10:22 -0800431// --- InputDispatcherTest ---
432
433class InputDispatcherTest : public testing::Test {
434protected:
435 sp<FakeInputDispatcherPolicy> mFakePolicy;
436 sp<InputDispatcher> mDispatcher;
437
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700438 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800439 mFakePolicy = new FakeInputDispatcherPolicy();
440 mDispatcher = new InputDispatcher(mFakePolicy);
Arthur Hungb92218b2018-08-14 12:00:21 +0800441 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000442 // Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700443 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800444 }
445
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700446 virtual void TearDown() override {
447 ASSERT_EQ(OK, mDispatcher->stop());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800448 mFakePolicy.clear();
449 mDispatcher.clear();
450 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700451
452 /**
453 * Used for debugging when writing the test
454 */
455 void dumpDispatcherState() {
456 std::string dump;
457 mDispatcher->dump(dump);
458 std::stringstream ss(dump);
459 std::string to;
460
461 while (std::getline(ss, to, '\n')) {
462 ALOGE("%s", to.c_str());
463 }
464 }
Vishnu Nair958da932020-08-21 17:12:37 -0700465
466 void setFocusedWindow(const sp<InputWindowHandle>& window,
467 const sp<InputWindowHandle>& focusedWindow = nullptr) {
468 FocusRequest request;
469 request.token = window->getToken();
470 if (focusedWindow) {
471 request.focusedToken = focusedWindow->getToken();
472 }
473 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
474 request.displayId = window->getInfo()->displayId;
475 mDispatcher->setFocusedWindow(request);
476 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800477};
478
Michael Wrightd02c5b62014-02-10 15:10:22 -0800479TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
480 KeyEvent event;
481
482 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800483 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
484 INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600485 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
486 ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800487 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700488 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800489 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800490 << "Should reject key events with undefined action.";
491
492 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800493 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
494 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600495 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800496 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700497 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800498 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800499 << "Should reject key events with ACTION_MULTIPLE.";
500}
501
502TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
503 MotionEvent event;
504 PointerProperties pointerProperties[MAX_POINTERS + 1];
505 PointerCoords pointerCoords[MAX_POINTERS + 1];
506 for (int i = 0; i <= MAX_POINTERS; i++) {
507 pointerProperties[i].clear();
508 pointerProperties[i].id = i;
509 pointerCoords[i].clear();
510 }
511
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800512 // Some constants commonly used below
513 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
514 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
515 constexpr int32_t metaState = AMETA_NONE;
516 constexpr MotionClassification classification = MotionClassification::NONE;
517
chaviw9eaa22c2020-07-01 16:21:27 -0700518 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800519 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800520 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
chaviw9eaa22c2020-07-01 16:21:27 -0700521 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification,
522 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600523 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700524 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800525 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700526 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800527 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800528 << "Should reject motion events with undefined action.";
529
530 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800531 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700532 AMOTION_EVENT_ACTION_POINTER_DOWN |
533 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700534 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
535 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
536 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
537 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800538 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700539 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800540 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800541 << "Should reject motion events with pointer down index too large.";
542
Garfield Tanfbe732e2020-01-24 11:26:14 -0800543 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700544 AMOTION_EVENT_ACTION_POINTER_DOWN |
545 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700546 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
547 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
548 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
549 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800550 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700551 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800552 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800553 << "Should reject motion events with pointer down index too small.";
554
555 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800556 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700557 AMOTION_EVENT_ACTION_POINTER_UP |
558 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700559 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
560 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
561 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
562 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800563 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700564 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800565 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800566 << "Should reject motion events with pointer up index too large.";
567
Garfield Tanfbe732e2020-01-24 11:26:14 -0800568 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700569 AMOTION_EVENT_ACTION_POINTER_UP |
570 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700571 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
572 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
573 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
574 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800575 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700576 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800577 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800578 << "Should reject motion events with pointer up index too small.";
579
580 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800581 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
582 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700583 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
584 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700585 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800586 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700587 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800588 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800589 << "Should reject motion events with 0 pointers.";
590
Garfield Tanfbe732e2020-01-24 11:26:14 -0800591 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
592 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700593 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
594 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700595 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800596 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700597 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800598 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800599 << "Should reject motion events with more than MAX_POINTERS pointers.";
600
601 // Rejects motion events with invalid pointer ids.
602 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800603 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
604 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700605 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
606 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700607 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800608 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700609 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800610 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800611 << "Should reject motion events with pointer ids less than 0.";
612
613 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800614 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
615 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700616 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
617 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700618 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800619 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700620 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800621 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800622 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
623
624 // Rejects motion events with duplicate pointer ids.
625 pointerProperties[0].id = 1;
626 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800627 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
628 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700629 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
630 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700631 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800632 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700633 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800634 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800635 << "Should reject motion events with duplicate pointer ids.";
636}
637
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800638/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
639
640TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
641 constexpr nsecs_t eventTime = 20;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800642 NotifyConfigurationChangedArgs args(10 /*id*/, eventTime);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800643 mDispatcher->notifyConfigurationChanged(&args);
644 ASSERT_TRUE(mDispatcher->waitForIdle());
645
646 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
647}
648
649TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800650 NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/,
651 2 /*switchMask*/);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800652 mDispatcher->notifySwitch(&args);
653
654 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
655 args.policyFlags |= POLICY_FLAG_TRUSTED;
656 mFakePolicy->assertNotifySwitchWasCalled(args);
657}
658
Arthur Hungb92218b2018-08-14 12:00:21 +0800659// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700660static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700661static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s;
Arthur Hungb92218b2018-08-14 12:00:21 +0800662
663class FakeApplicationHandle : public InputApplicationHandle {
664public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700665 FakeApplicationHandle() {
666 mInfo.name = "Fake Application";
667 mInfo.token = new BBinder();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500668 mInfo.dispatchingTimeoutMillis =
669 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700670 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800671 virtual ~FakeApplicationHandle() {}
672
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000673 virtual bool updateInfo() override { return true; }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700674
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500675 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
676 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700677 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800678};
679
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800680class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800681public:
Garfield Tan15601662020-09-22 15:32:38 -0700682 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800683 : mName(name) {
Garfield Tan15601662020-09-22 15:32:38 -0700684 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800685 }
686
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800687 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700688 InputEvent* event;
689 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
690 if (!consumeSeq) {
691 return nullptr;
692 }
693 finishEvent(*consumeSeq);
694 return event;
695 }
696
697 /**
698 * Receive an event without acknowledging it.
699 * Return the sequence number that could later be used to send finished signal.
700 */
701 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800702 uint32_t consumeSeq;
703 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800704
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800705 std::chrono::time_point start = std::chrono::steady_clock::now();
706 status_t status = WOULD_BLOCK;
707 while (status == WOULD_BLOCK) {
chaviw81e2bb92019-12-18 15:03:51 -0800708 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800709 &event);
710 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
711 if (elapsed > 100ms) {
712 break;
713 }
714 }
715
716 if (status == WOULD_BLOCK) {
717 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700718 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800719 }
720
721 if (status != OK) {
722 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700723 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800724 }
725 if (event == nullptr) {
726 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700727 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800728 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700729 if (outEvent != nullptr) {
730 *outEvent = event;
731 }
732 return consumeSeq;
733 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800734
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700735 /**
736 * To be used together with "receiveEvent" to complete the consumption of an event.
737 */
738 void finishEvent(uint32_t consumeSeq) {
739 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
740 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800741 }
742
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000743 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
744 const status_t status = mConsumer->sendTimeline(inputEventId, timeline);
745 ASSERT_EQ(OK, status);
746 }
747
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000748 void consumeEvent(int32_t expectedEventType, int32_t expectedAction,
749 std::optional<int32_t> expectedDisplayId,
750 std::optional<int32_t> expectedFlags) {
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800751 InputEvent* event = consume();
752
753 ASSERT_NE(nullptr, event) << mName.c_str()
754 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800755 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700756 << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType)
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800757 << " event, got " << inputEventTypeToString(event->getType()) << " event";
Arthur Hungb92218b2018-08-14 12:00:21 +0800758
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000759 if (expectedDisplayId.has_value()) {
760 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
761 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800762
Tiger Huang8664f8c2018-10-11 19:14:35 +0800763 switch (expectedEventType) {
764 case AINPUT_EVENT_TYPE_KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800765 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
766 EXPECT_EQ(expectedAction, keyEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000767 if (expectedFlags.has_value()) {
768 EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags());
769 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800770 break;
771 }
772 case AINPUT_EVENT_TYPE_MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800773 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
774 EXPECT_EQ(expectedAction, motionEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000775 if (expectedFlags.has_value()) {
776 EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags());
777 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800778 break;
779 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100780 case AINPUT_EVENT_TYPE_FOCUS: {
781 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
782 }
Prabir Pradhan99987712020-11-10 18:43:05 -0800783 case AINPUT_EVENT_TYPE_CAPTURE: {
784 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
785 }
arthurhungb89ccb02020-12-30 16:19:01 +0800786 case AINPUT_EVENT_TYPE_DRAG: {
787 FAIL() << "Use 'consumeDragEvent' for DRAG events";
788 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800789 default: {
790 FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType;
791 }
792 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800793 }
794
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100795 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
796 InputEvent* event = consume();
797 ASSERT_NE(nullptr, event) << mName.c_str()
798 << ": consumer should have returned non-NULL event.";
799 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
800 << "Got " << inputEventTypeToString(event->getType())
801 << " event instead of FOCUS event";
802
803 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
804 << mName.c_str() << ": event displayId should always be NONE.";
805
806 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
807 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
808 EXPECT_EQ(inTouchMode, focusEvent->getInTouchMode());
809 }
810
Prabir Pradhan99987712020-11-10 18:43:05 -0800811 void consumeCaptureEvent(bool hasCapture) {
812 const InputEvent* event = consume();
813 ASSERT_NE(nullptr, event) << mName.c_str()
814 << ": consumer should have returned non-NULL event.";
815 ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType())
816 << "Got " << inputEventTypeToString(event->getType())
817 << " event instead of CAPTURE event";
818
819 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
820 << mName.c_str() << ": event displayId should always be NONE.";
821
822 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
823 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
824 }
825
arthurhungb89ccb02020-12-30 16:19:01 +0800826 void consumeDragEvent(bool isExiting, float x, float y) {
827 const InputEvent* event = consume();
828 ASSERT_NE(nullptr, event) << mName.c_str()
829 << ": consumer should have returned non-NULL event.";
830 ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType())
831 << "Got " << inputEventTypeToString(event->getType())
832 << " event instead of DRAG event";
833
834 EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
835 << mName.c_str() << ": event displayId should always be NONE.";
836
837 const auto& dragEvent = static_cast<const DragEvent&>(*event);
838 EXPECT_EQ(isExiting, dragEvent.isExiting());
839 EXPECT_EQ(x, dragEvent.getX());
840 EXPECT_EQ(y, dragEvent.getY());
841 }
842
chaviwd1c23182019-12-20 18:44:56 -0800843 void assertNoEvents() {
844 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700845 if (event == nullptr) {
846 return;
847 }
848 if (event->getType() == AINPUT_EVENT_TYPE_KEY) {
849 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
850 ADD_FAILURE() << "Received key event "
851 << KeyEvent::actionToString(keyEvent.getAction());
852 } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) {
853 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
854 ADD_FAILURE() << "Received motion event "
855 << MotionEvent::actionToString(motionEvent.getAction());
856 } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) {
857 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
858 ADD_FAILURE() << "Received focus event, hasFocus = "
859 << (focusEvent.getHasFocus() ? "true" : "false");
Prabir Pradhan99987712020-11-10 18:43:05 -0800860 } else if (event->getType() == AINPUT_EVENT_TYPE_CAPTURE) {
861 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
862 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
863 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700864 }
865 FAIL() << mName.c_str()
866 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -0800867 }
868
869 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
870
871protected:
872 std::unique_ptr<InputConsumer> mConsumer;
873 PreallocatedInputEventFactory mEventFactory;
874
875 std::string mName;
876};
877
878class FakeWindowHandle : public InputWindowHandle {
879public:
880 static const int32_t WIDTH = 600;
881 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -0800882
Chris Yea209fde2020-07-22 13:54:51 -0700883 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
chaviwd1c23182019-12-20 18:44:56 -0800884 const sp<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500885 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -0800886 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500887 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -0700888 base::Result<std::unique_ptr<InputChannel>> channel =
889 dispatcher->createInputChannel(name);
890 token = (*channel)->getConnectionToken();
891 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -0800892 }
893
894 inputApplicationHandle->updateInfo();
895 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
896
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500897 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700898 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -0800899 mInfo.name = name;
Michael Wright44753b12020-07-08 13:48:11 +0100900 mInfo.type = InputWindowInfo::Type::APPLICATION;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500901 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000902 mInfo.alpha = 1.0;
chaviwd1c23182019-12-20 18:44:56 -0800903 mInfo.frameLeft = 0;
904 mInfo.frameTop = 0;
905 mInfo.frameRight = WIDTH;
906 mInfo.frameBottom = HEIGHT;
chaviw1ff3d1e2020-07-01 15:53:47 -0700907 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -0800908 mInfo.globalScaleFactor = 1.0;
909 mInfo.touchableRegion.clear();
910 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
911 mInfo.visible = true;
Vishnu Nair47074b82020-08-14 11:54:47 -0700912 mInfo.focusable = false;
chaviwd1c23182019-12-20 18:44:56 -0800913 mInfo.hasWallpaper = false;
914 mInfo.paused = false;
chaviwd1c23182019-12-20 18:44:56 -0800915 mInfo.ownerPid = INJECTOR_PID;
916 mInfo.ownerUid = INJECTOR_UID;
chaviwd1c23182019-12-20 18:44:56 -0800917 mInfo.displayId = displayId;
918 }
919
920 virtual bool updateInfo() { return true; }
921
Vishnu Nair47074b82020-08-14 11:54:47 -0700922 void setFocusable(bool focusable) { mInfo.focusable = focusable; }
chaviwd1c23182019-12-20 18:44:56 -0800923
Vishnu Nair958da932020-08-21 17:12:37 -0700924 void setVisible(bool visible) { mInfo.visible = visible; }
925
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700926 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500927 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700928 }
929
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700930 void setPaused(bool paused) { mInfo.paused = paused; }
931
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000932 void setAlpha(float alpha) { mInfo.alpha = alpha; }
933
934 void setTouchOcclusionMode(android::os::TouchOcclusionMode mode) {
935 mInfo.touchOcclusionMode = mode;
936 }
937
Bernardo Rufino7393d172021-02-26 13:56:11 +0000938 void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
939
chaviwd1c23182019-12-20 18:44:56 -0800940 void setFrame(const Rect& frame) {
941 mInfo.frameLeft = frame.left;
942 mInfo.frameTop = frame.top;
943 mInfo.frameRight = frame.right;
944 mInfo.frameBottom = frame.bottom;
arthurhungb89ccb02020-12-30 16:19:01 +0800945 mInfo.transform.set(-frame.left, -frame.top);
chaviwd1c23182019-12-20 18:44:56 -0800946 mInfo.touchableRegion.clear();
947 mInfo.addTouchableRegion(frame);
948 }
949
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +0000950 void addFlags(Flags<InputWindowInfo::Flag> flags) { mInfo.flags |= flags; }
951
Michael Wright44753b12020-07-08 13:48:11 +0100952 void setFlags(Flags<InputWindowInfo::Flag> flags) { mInfo.flags = flags; }
chaviwd1c23182019-12-20 18:44:56 -0800953
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500954 void setInputFeatures(InputWindowInfo::Feature features) { mInfo.inputFeatures = features; }
955
chaviw9eaa22c2020-07-01 16:21:27 -0700956 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
957 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
958 }
959
960 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -0700961
yunho.shinf4a80b82020-11-16 21:13:57 +0900962 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
963
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800964 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
965 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
966 expectedFlags);
967 }
968
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700969 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
970 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
971 }
972
Svet Ganov5d3bc372020-01-26 23:11:07 -0800973 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000974 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800975 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId,
976 expectedFlags);
977 }
978
979 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000980 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800981 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId,
982 expectedFlags);
983 }
984
985 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000986 int32_t expectedFlags = 0) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000987 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
988 }
989
990 void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
991 std::optional<int32_t> expectedFlags = std::nullopt) {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800992 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
993 expectedFlags);
994 }
995
Svet Ganov5d3bc372020-01-26 23:11:07 -0800996 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000997 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
998 int32_t expectedFlags = 0) {
999 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
1000 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001001 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
1002 }
1003
1004 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001005 int32_t expectedFlags = 0) {
1006 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
1007 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001008 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
1009 }
1010
1011 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001012 int32_t expectedFlags = 0) {
Michael Wright3a240c42019-12-10 20:53:41 +00001013 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
1014 expectedFlags);
1015 }
1016
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001017 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1018 int32_t expectedFlags = 0) {
1019 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
1020 expectedFlags);
1021 }
1022
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001023 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
1024 ASSERT_NE(mInputReceiver, nullptr)
1025 << "Cannot consume events from a window with no receiver";
1026 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
1027 }
1028
Prabir Pradhan99987712020-11-10 18:43:05 -08001029 void consumeCaptureEvent(bool hasCapture) {
1030 ASSERT_NE(mInputReceiver, nullptr)
1031 << "Cannot consume events from a window with no receiver";
1032 mInputReceiver->consumeCaptureEvent(hasCapture);
1033 }
1034
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001035 void consumeEvent(int32_t expectedEventType, int32_t expectedAction,
1036 std::optional<int32_t> expectedDisplayId,
1037 std::optional<int32_t> expectedFlags) {
chaviwd1c23182019-12-20 18:44:56 -08001038 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
1039 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
1040 expectedFlags);
1041 }
1042
arthurhungb89ccb02020-12-30 16:19:01 +08001043 void consumeDragEvent(bool isExiting, float x, float y) {
1044 mInputReceiver->consumeDragEvent(isExiting, x, y);
1045 }
1046
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001047 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001048 if (mInputReceiver == nullptr) {
1049 ADD_FAILURE() << "Invalid receive event on window with no receiver";
1050 return std::nullopt;
1051 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001052 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001053 }
1054
1055 void finishEvent(uint32_t sequenceNum) {
1056 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1057 mInputReceiver->finishEvent(sequenceNum);
1058 }
1059
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001060 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
1061 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1062 mInputReceiver->sendTimeline(inputEventId, timeline);
1063 }
1064
chaviwaf87b3e2019-10-01 16:59:28 -07001065 InputEvent* consume() {
1066 if (mInputReceiver == nullptr) {
1067 return nullptr;
1068 }
1069 return mInputReceiver->consume();
1070 }
1071
Arthur Hungb92218b2018-08-14 12:00:21 +08001072 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001073 if (mInputReceiver == nullptr &&
1074 mInfo.inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL)) {
1075 return; // Can't receive events if the window does not have input channel
1076 }
1077 ASSERT_NE(nullptr, mInputReceiver)
1078 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -08001079 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +08001080 }
1081
chaviwaf87b3e2019-10-01 16:59:28 -07001082 sp<IBinder> getToken() { return mInfo.token; }
1083
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001084 const std::string& getName() { return mName; }
1085
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001086 void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) {
1087 mInfo.ownerPid = ownerPid;
1088 mInfo.ownerUid = ownerUid;
1089 }
1090
chaviwd1c23182019-12-20 18:44:56 -08001091private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001092 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -08001093 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001094 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001095};
1096
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001097std::atomic<int32_t> FakeWindowHandle::sId{1};
1098
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001099static InputEventInjectionResult injectKey(
1100 const sp<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
1101 int32_t displayId = ADISPLAY_ID_NONE,
1102 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
1103 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001104 KeyEvent event;
1105 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1106
1107 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001108 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001109 INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE,
1110 repeatCount, currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001111
1112 // Inject event until dispatch out.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001113 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, syncMode,
1114 injectionTimeout,
1115 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
Arthur Hungb92218b2018-08-14 12:00:21 +08001116}
1117
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001118static InputEventInjectionResult injectKeyDown(const sp<InputDispatcher>& dispatcher,
1119 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001120 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId);
1121}
1122
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001123static InputEventInjectionResult injectKeyUp(const sp<InputDispatcher>& dispatcher,
1124 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001125 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId);
1126}
1127
Garfield Tandf26e862020-07-01 20:18:19 -07001128class PointerBuilder {
1129public:
1130 PointerBuilder(int32_t id, int32_t toolType) {
1131 mProperties.clear();
1132 mProperties.id = id;
1133 mProperties.toolType = toolType;
1134 mCoords.clear();
1135 }
1136
1137 PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); }
1138
1139 PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); }
1140
1141 PointerBuilder& axis(int32_t axis, float value) {
1142 mCoords.setAxisValue(axis, value);
1143 return *this;
1144 }
1145
1146 PointerProperties buildProperties() const { return mProperties; }
1147
1148 PointerCoords buildCoords() const { return mCoords; }
1149
1150private:
1151 PointerProperties mProperties;
1152 PointerCoords mCoords;
1153};
1154
1155class MotionEventBuilder {
1156public:
1157 MotionEventBuilder(int32_t action, int32_t source) {
1158 mAction = action;
1159 mSource = source;
1160 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1161 }
1162
1163 MotionEventBuilder& eventTime(nsecs_t eventTime) {
1164 mEventTime = eventTime;
1165 return *this;
1166 }
1167
1168 MotionEventBuilder& displayId(int32_t displayId) {
1169 mDisplayId = displayId;
1170 return *this;
1171 }
1172
1173 MotionEventBuilder& actionButton(int32_t actionButton) {
1174 mActionButton = actionButton;
1175 return *this;
1176 }
1177
1178 MotionEventBuilder& buttonState(int32_t actionButton) {
1179 mActionButton = actionButton;
1180 return *this;
1181 }
1182
1183 MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) {
1184 mRawXCursorPosition = rawXCursorPosition;
1185 return *this;
1186 }
1187
1188 MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) {
1189 mRawYCursorPosition = rawYCursorPosition;
1190 return *this;
1191 }
1192
1193 MotionEventBuilder& pointer(PointerBuilder pointer) {
1194 mPointers.push_back(pointer);
1195 return *this;
1196 }
1197
1198 MotionEvent build() {
1199 std::vector<PointerProperties> pointerProperties;
1200 std::vector<PointerCoords> pointerCoords;
1201 for (const PointerBuilder& pointer : mPointers) {
1202 pointerProperties.push_back(pointer.buildProperties());
1203 pointerCoords.push_back(pointer.buildCoords());
1204 }
1205
1206 // Set mouse cursor position for the most common cases to avoid boilerplate.
1207 if (mSource == AINPUT_SOURCE_MOUSE &&
1208 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) &&
1209 mPointers.size() == 1) {
1210 mRawXCursorPosition = pointerCoords[0].getX();
1211 mRawYCursorPosition = pointerCoords[0].getY();
1212 }
1213
1214 MotionEvent event;
chaviw9eaa22c2020-07-01 16:21:27 -07001215 ui::Transform identityTransform;
Garfield Tandf26e862020-07-01 20:18:19 -07001216 event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC,
1217 mAction, mActionButton, /* flags */ 0, /* edgeFlags */ 0, AMETA_NONE,
chaviw9eaa22c2020-07-01 16:21:27 -07001218 mButtonState, MotionClassification::NONE, identityTransform,
1219 /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition,
1220 mRawYCursorPosition, mEventTime, mEventTime, mPointers.size(),
1221 pointerProperties.data(), pointerCoords.data());
Garfield Tandf26e862020-07-01 20:18:19 -07001222
1223 return event;
1224 }
1225
1226private:
1227 int32_t mAction;
1228 int32_t mSource;
1229 nsecs_t mEventTime;
1230 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1231 int32_t mActionButton{0};
1232 int32_t mButtonState{0};
1233 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1234 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1235
1236 std::vector<PointerBuilder> mPointers;
1237};
1238
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001239static InputEventInjectionResult injectMotionEvent(
Garfield Tandf26e862020-07-01 20:18:19 -07001240 const sp<InputDispatcher>& dispatcher, const MotionEvent& event,
1241 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001242 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT) {
Garfield Tandf26e862020-07-01 20:18:19 -07001243 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, injectionMode,
1244 injectionTimeout,
1245 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
1246}
1247
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001248static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001249 const sp<InputDispatcher>& dispatcher, int32_t action, int32_t source, int32_t displayId,
1250 const PointF& position,
1251 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001252 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1253 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001254 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001255 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC)) {
Garfield Tandf26e862020-07-01 20:18:19 -07001256 MotionEvent event = MotionEventBuilder(action, source)
1257 .displayId(displayId)
1258 .eventTime(eventTime)
1259 .rawXCursorPosition(cursorPosition.x)
1260 .rawYCursorPosition(cursorPosition.y)
1261 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1262 .x(position.x)
1263 .y(position.y))
1264 .build();
Arthur Hungb92218b2018-08-14 12:00:21 +08001265
1266 // Inject event until dispatch out.
Garfield Tandf26e862020-07-01 20:18:19 -07001267 return injectMotionEvent(dispatcher, event);
Arthur Hungb92218b2018-08-14 12:00:21 +08001268}
1269
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001270static InputEventInjectionResult injectMotionDown(const sp<InputDispatcher>& dispatcher,
1271 int32_t source, int32_t displayId,
1272 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001273 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001274}
1275
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001276static InputEventInjectionResult injectMotionUp(const sp<InputDispatcher>& dispatcher,
1277 int32_t source, int32_t displayId,
1278 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001279 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001280}
1281
Jackal Guof9696682018-10-05 12:23:23 +08001282static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1283 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1284 // Define a valid key event.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001285 NotifyKeyArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1286 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A,
1287 KEY_A, AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001288
1289 return args;
1290}
1291
chaviwd1c23182019-12-20 18:44:56 -08001292static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId,
1293 const std::vector<PointF>& points) {
1294 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001295 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1296 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1297 }
1298
chaviwd1c23182019-12-20 18:44:56 -08001299 PointerProperties pointerProperties[pointerCount];
1300 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001301
chaviwd1c23182019-12-20 18:44:56 -08001302 for (size_t i = 0; i < pointerCount; i++) {
1303 pointerProperties[i].clear();
1304 pointerProperties[i].id = i;
1305 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001306
chaviwd1c23182019-12-20 18:44:56 -08001307 pointerCoords[i].clear();
1308 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1309 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1310 }
Jackal Guof9696682018-10-05 12:23:23 +08001311
1312 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1313 // Define a valid motion event.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001314 NotifyMotionArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001315 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1316 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001317 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1318 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001319 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1320 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001321
1322 return args;
1323}
1324
chaviwd1c23182019-12-20 18:44:56 -08001325static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1326 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1327}
1328
Prabir Pradhan99987712020-11-10 18:43:05 -08001329static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(bool enabled) {
1330 return NotifyPointerCaptureChangedArgs(/* id */ 0, systemTime(SYSTEM_TIME_MONOTONIC), enabled);
1331}
1332
Arthur Hungb92218b2018-08-14 12:00:21 +08001333TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001334 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001335 sp<FakeWindowHandle> window =
1336 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001337
Arthur Hung72d8dc32020-03-28 00:48:39 +00001338 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001339 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1340 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1341 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001342
1343 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001344 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001345}
1346
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001347/**
1348 * Calling setInputWindows once with FLAG_NOT_TOUCH_MODAL should not cause any issues.
1349 * To ensure that window receives only events that were directly inside of it, add
1350 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1351 * when finding touched windows.
1352 * This test serves as a sanity check for the next test, where setInputWindows is
1353 * called twice.
1354 */
1355TEST_F(InputDispatcherTest, SetInputWindowOnce_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001356 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001357 sp<FakeWindowHandle> window =
1358 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1359 window->setFrame(Rect(0, 0, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01001360 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001361
1362 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001363 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001364 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1365 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001366 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001367
1368 // Window should receive motion event.
1369 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1370}
1371
1372/**
1373 * Calling setInputWindows twice, with the same info, should not cause any issues.
1374 * To ensure that window receives only events that were directly inside of it, add
1375 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1376 * when finding touched windows.
1377 */
1378TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001379 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001380 sp<FakeWindowHandle> window =
1381 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1382 window->setFrame(Rect(0, 0, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01001383 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001384
1385 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1386 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001387 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001388 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1389 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001390 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001391
1392 // Window should receive motion event.
1393 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1394}
1395
Arthur Hungb92218b2018-08-14 12:00:21 +08001396// The foreground window should receive the first touch down event.
1397TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001398 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001399 sp<FakeWindowHandle> windowTop =
1400 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
1401 sp<FakeWindowHandle> windowSecond =
1402 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001403
Arthur Hung72d8dc32020-03-28 00:48:39 +00001404 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001405 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1406 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1407 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001408
1409 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001410 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001411 windowSecond->assertNoEvents();
1412}
1413
Garfield Tandf26e862020-07-01 20:18:19 -07001414TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07001415 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07001416 sp<FakeWindowHandle> windowLeft =
1417 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1418 windowLeft->setFrame(Rect(0, 0, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001419 windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001420 sp<FakeWindowHandle> windowRight =
1421 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1422 windowRight->setFrame(Rect(600, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001423 windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001424
1425 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1426
1427 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
1428
1429 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001430 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001431 injectMotionEvent(mDispatcher,
1432 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1433 AINPUT_SOURCE_MOUSE)
1434 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1435 .x(900)
1436 .y(400))
1437 .build()));
1438 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1439 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1440 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1441 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1442
1443 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001444 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001445 injectMotionEvent(mDispatcher,
1446 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1447 AINPUT_SOURCE_MOUSE)
1448 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1449 .x(300)
1450 .y(400))
1451 .build()));
1452 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1453 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1454 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1455 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1456 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1457 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1458
1459 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001460 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001461 injectMotionEvent(mDispatcher,
1462 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1463 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1464 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1465 .x(300)
1466 .y(400))
1467 .build()));
1468 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1469
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001470 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001471 injectMotionEvent(mDispatcher,
1472 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
1473 AINPUT_SOURCE_MOUSE)
1474 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1475 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1476 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1477 .x(300)
1478 .y(400))
1479 .build()));
1480 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1481 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1482
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001483 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001484 injectMotionEvent(mDispatcher,
1485 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1486 AINPUT_SOURCE_MOUSE)
1487 .buttonState(0)
1488 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1489 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1490 .x(300)
1491 .y(400))
1492 .build()));
1493 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1494 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1495
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001496 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001497 injectMotionEvent(mDispatcher,
1498 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
1499 .buttonState(0)
1500 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1501 .x(300)
1502 .y(400))
1503 .build()));
1504 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1505
1506 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001507 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001508 injectMotionEvent(mDispatcher,
1509 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1510 AINPUT_SOURCE_MOUSE)
1511 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1512 .x(900)
1513 .y(400))
1514 .build()));
1515 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1516 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1517 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1518 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1519 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1520 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1521}
1522
1523// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
1524// directly in this test.
1525TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07001526 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07001527 sp<FakeWindowHandle> window =
1528 new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1529 window->setFrame(Rect(0, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001530 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001531
1532 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1533
1534 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1535
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001536 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001537 injectMotionEvent(mDispatcher,
1538 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
1539 AINPUT_SOURCE_MOUSE)
1540 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1541 .x(300)
1542 .y(400))
1543 .build()));
1544 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1545 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1546
1547 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001548 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001549 injectMotionEvent(mDispatcher,
1550 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1551 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1552 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1553 .x(300)
1554 .y(400))
1555 .build()));
1556 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1557
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001558 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001559 injectMotionEvent(mDispatcher,
1560 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
1561 AINPUT_SOURCE_MOUSE)
1562 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1563 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1564 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1565 .x(300)
1566 .y(400))
1567 .build()));
1568 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1569 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1570
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001571 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001572 injectMotionEvent(mDispatcher,
1573 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1574 AINPUT_SOURCE_MOUSE)
1575 .buttonState(0)
1576 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1577 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1578 .x(300)
1579 .y(400))
1580 .build()));
1581 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1582 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1583
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001584 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001585 injectMotionEvent(mDispatcher,
1586 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
1587 .buttonState(0)
1588 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1589 .x(300)
1590 .y(400))
1591 .build()));
1592 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1593
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001594 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001595 injectMotionEvent(mDispatcher,
1596 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
1597 AINPUT_SOURCE_MOUSE)
1598 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1599 .x(300)
1600 .y(400))
1601 .build()));
1602 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1603 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1604}
1605
Garfield Tan00f511d2019-06-12 16:55:40 -07001606TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07001607 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07001608
1609 sp<FakeWindowHandle> windowLeft =
1610 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1611 windowLeft->setFrame(Rect(0, 0, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001612 windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07001613 sp<FakeWindowHandle> windowRight =
1614 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1615 windowRight->setFrame(Rect(600, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001616 windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07001617
1618 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1619
Arthur Hung72d8dc32020-03-28 00:48:39 +00001620 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07001621
1622 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
1623 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001624 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07001625 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001626 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001627 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07001628 windowRight->assertNoEvents();
1629}
1630
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001631TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001632 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001633 sp<FakeWindowHandle> window =
1634 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07001635 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001636
Arthur Hung72d8dc32020-03-28 00:48:39 +00001637 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001638 setFocusedWindow(window);
1639
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001640 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001641
1642 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1643 mDispatcher->notifyKey(&keyArgs);
1644
1645 // Window should receive key down event.
1646 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1647
1648 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
1649 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001650 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001651 mDispatcher->notifyDeviceReset(&args);
1652 window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
1653 AKEY_EVENT_FLAG_CANCELED);
1654}
1655
1656TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001657 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001658 sp<FakeWindowHandle> window =
1659 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1660
Arthur Hung72d8dc32020-03-28 00:48:39 +00001661 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001662
1663 NotifyMotionArgs motionArgs =
1664 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1665 ADISPLAY_ID_DEFAULT);
1666 mDispatcher->notifyMotion(&motionArgs);
1667
1668 // Window should receive motion down event.
1669 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1670
1671 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
1672 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001673 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001674 mDispatcher->notifyDeviceReset(&args);
1675 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT,
1676 0 /*expectedFlags*/);
1677}
1678
Svet Ganov5d3bc372020-01-26 23:11:07 -08001679TEST_F(InputDispatcherTest, TransferTouchFocus_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07001680 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001681
1682 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001683 sp<FakeWindowHandle> firstWindow =
1684 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1685 sp<FakeWindowHandle> secondWindow =
1686 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001687
1688 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001689 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001690
1691 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001692 NotifyMotionArgs downMotionArgs =
1693 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1694 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001695 mDispatcher->notifyMotion(&downMotionArgs);
1696 // Only the first window should get the down event
1697 firstWindow->consumeMotionDown();
1698 secondWindow->assertNoEvents();
1699
1700 // Transfer touch focus to the second window
1701 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1702 // The first window gets cancel and the second gets down
1703 firstWindow->consumeMotionCancel();
1704 secondWindow->consumeMotionDown();
1705
1706 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001707 NotifyMotionArgs upMotionArgs =
1708 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1709 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001710 mDispatcher->notifyMotion(&upMotionArgs);
1711 // The first window gets no events and the second gets up
1712 firstWindow->assertNoEvents();
1713 secondWindow->consumeMotionUp();
1714}
1715
1716TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointerNoSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001717 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001718
1719 PointF touchPoint = {10, 10};
1720
1721 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001722 sp<FakeWindowHandle> firstWindow =
1723 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1724 sp<FakeWindowHandle> secondWindow =
1725 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001726
1727 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001728 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001729
1730 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001731 NotifyMotionArgs downMotionArgs =
1732 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1733 ADISPLAY_ID_DEFAULT, {touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001734 mDispatcher->notifyMotion(&downMotionArgs);
1735 // Only the first window should get the down event
1736 firstWindow->consumeMotionDown();
1737 secondWindow->assertNoEvents();
1738
1739 // Send pointer down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001740 NotifyMotionArgs pointerDownMotionArgs =
1741 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1742 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1743 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1744 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001745 mDispatcher->notifyMotion(&pointerDownMotionArgs);
1746 // Only the first window should get the pointer down event
1747 firstWindow->consumeMotionPointerDown(1);
1748 secondWindow->assertNoEvents();
1749
1750 // Transfer touch focus to the second window
1751 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1752 // The first window gets cancel and the second gets down and pointer down
1753 firstWindow->consumeMotionCancel();
1754 secondWindow->consumeMotionDown();
1755 secondWindow->consumeMotionPointerDown(1);
1756
1757 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001758 NotifyMotionArgs pointerUpMotionArgs =
1759 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1760 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1761 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1762 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001763 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1764 // The first window gets nothing and the second gets pointer up
1765 firstWindow->assertNoEvents();
1766 secondWindow->consumeMotionPointerUp(1);
1767
1768 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001769 NotifyMotionArgs upMotionArgs =
1770 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1771 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001772 mDispatcher->notifyMotion(&upMotionArgs);
1773 // The first window gets nothing and the second gets up
1774 firstWindow->assertNoEvents();
1775 secondWindow->consumeMotionUp();
1776}
1777
1778TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001779 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001780
1781 // Create a non touch modal window that supports split touch
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001782 sp<FakeWindowHandle> firstWindow =
1783 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001784 firstWindow->setFrame(Rect(0, 0, 600, 400));
Michael Wright44753b12020-07-08 13:48:11 +01001785 firstWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1786 InputWindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001787
1788 // Create a non touch modal window that supports split touch
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001789 sp<FakeWindowHandle> secondWindow =
1790 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001791 secondWindow->setFrame(Rect(0, 400, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001792 secondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1793 InputWindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001794
1795 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001796 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001797
1798 PointF pointInFirst = {300, 200};
1799 PointF pointInSecond = {300, 600};
1800
1801 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001802 NotifyMotionArgs firstDownMotionArgs =
1803 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1804 ADISPLAY_ID_DEFAULT, {pointInFirst});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001805 mDispatcher->notifyMotion(&firstDownMotionArgs);
1806 // Only the first window should get the down event
1807 firstWindow->consumeMotionDown();
1808 secondWindow->assertNoEvents();
1809
1810 // Send down to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001811 NotifyMotionArgs secondDownMotionArgs =
1812 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1813 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1814 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1815 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001816 mDispatcher->notifyMotion(&secondDownMotionArgs);
1817 // The first window gets a move and the second a down
1818 firstWindow->consumeMotionMove();
1819 secondWindow->consumeMotionDown();
1820
1821 // Transfer touch focus to the second window
1822 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1823 // The first window gets cancel and the new gets pointer down (it already saw down)
1824 firstWindow->consumeMotionCancel();
1825 secondWindow->consumeMotionPointerDown(1);
1826
1827 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001828 NotifyMotionArgs pointerUpMotionArgs =
1829 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1830 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1831 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1832 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001833 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1834 // The first window gets nothing and the second gets pointer up
1835 firstWindow->assertNoEvents();
1836 secondWindow->consumeMotionPointerUp(1);
1837
1838 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001839 NotifyMotionArgs upMotionArgs =
1840 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1841 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001842 mDispatcher->notifyMotion(&upMotionArgs);
1843 // The first window gets nothing and the second gets up
1844 firstWindow->assertNoEvents();
1845 secondWindow->consumeMotionUp();
1846}
1847
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001848TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07001849 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001850 sp<FakeWindowHandle> window =
1851 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1852
Vishnu Nair47074b82020-08-14 11:54:47 -07001853 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001854 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001855 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001856
1857 window->consumeFocusEvent(true);
1858
1859 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1860 mDispatcher->notifyKey(&keyArgs);
1861
1862 // Window should receive key down event.
1863 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1864}
1865
1866TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07001867 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001868 sp<FakeWindowHandle> window =
1869 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1870
Arthur Hung72d8dc32020-03-28 00:48:39 +00001871 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001872
1873 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1874 mDispatcher->notifyKey(&keyArgs);
1875 mDispatcher->waitForIdle();
1876
1877 window->assertNoEvents();
1878}
1879
1880// If a window is touchable, but does not have focus, it should receive motion events, but not keys
1881TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07001882 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001883 sp<FakeWindowHandle> window =
1884 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1885
Arthur Hung72d8dc32020-03-28 00:48:39 +00001886 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001887
1888 // Send key
1889 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1890 mDispatcher->notifyKey(&keyArgs);
1891 // Send motion
1892 NotifyMotionArgs motionArgs =
1893 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1894 ADISPLAY_ID_DEFAULT);
1895 mDispatcher->notifyMotion(&motionArgs);
1896
1897 // Window should receive only the motion event
1898 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1899 window->assertNoEvents(); // Key event or focus event will not be received
1900}
1901
arthurhungea3f4fc2020-12-21 23:18:53 +08001902TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
1903 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1904
1905 // Create first non touch modal window that supports split touch
1906 sp<FakeWindowHandle> firstWindow =
1907 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1908 firstWindow->setFrame(Rect(0, 0, 600, 400));
1909 firstWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1910 InputWindowInfo::Flag::SPLIT_TOUCH);
1911
1912 // Create second non touch modal window that supports split touch
1913 sp<FakeWindowHandle> secondWindow =
1914 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
1915 secondWindow->setFrame(Rect(0, 400, 600, 800));
1916 secondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1917 InputWindowInfo::Flag::SPLIT_TOUCH);
1918
1919 // Add the windows to the dispatcher
1920 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
1921
1922 PointF pointInFirst = {300, 200};
1923 PointF pointInSecond = {300, 600};
1924
1925 // Send down to the first window
1926 NotifyMotionArgs firstDownMotionArgs =
1927 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1928 ADISPLAY_ID_DEFAULT, {pointInFirst});
1929 mDispatcher->notifyMotion(&firstDownMotionArgs);
1930 // Only the first window should get the down event
1931 firstWindow->consumeMotionDown();
1932 secondWindow->assertNoEvents();
1933
1934 // Send down to the second window
1935 NotifyMotionArgs secondDownMotionArgs =
1936 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1937 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1938 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1939 {pointInFirst, pointInSecond});
1940 mDispatcher->notifyMotion(&secondDownMotionArgs);
1941 // The first window gets a move and the second a down
1942 firstWindow->consumeMotionMove();
1943 secondWindow->consumeMotionDown();
1944
1945 // Send pointer cancel to the second window
1946 NotifyMotionArgs pointerUpMotionArgs =
1947 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1948 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1949 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1950 {pointInFirst, pointInSecond});
1951 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
1952 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1953 // The first window gets move and the second gets cancel.
1954 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
1955 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
1956
1957 // Send up event.
1958 NotifyMotionArgs upMotionArgs =
1959 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1960 ADISPLAY_ID_DEFAULT);
1961 mDispatcher->notifyMotion(&upMotionArgs);
1962 // The first window gets up and the second gets nothing.
1963 firstWindow->consumeMotionUp();
1964 secondWindow->assertNoEvents();
1965}
1966
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001967TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
1968 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1969
1970 sp<FakeWindowHandle> window =
1971 new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1972 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1973 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
1974 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
1975 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
1976
1977 window->sendTimeline(1 /*inputEventId*/, graphicsTimeline);
1978 window->assertNoEvents();
1979 mDispatcher->waitForIdle();
1980}
1981
chaviwd1c23182019-12-20 18:44:56 -08001982class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00001983public:
1984 FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name,
chaviwd1c23182019-12-20 18:44:56 -08001985 int32_t displayId, bool isGestureMonitor = false) {
Garfield Tan15601662020-09-22 15:32:38 -07001986 base::Result<std::unique_ptr<InputChannel>> channel =
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +00001987 dispatcher->createInputMonitor(displayId, isGestureMonitor, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07001988 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00001989 }
1990
chaviwd1c23182019-12-20 18:44:56 -08001991 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
1992
1993 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1994 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN,
1995 expectedDisplayId, expectedFlags);
1996 }
1997
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001998 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
1999
2000 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
2001
chaviwd1c23182019-12-20 18:44:56 -08002002 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
2003 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
2004 expectedDisplayId, expectedFlags);
2005 }
2006
2007 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
2008 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP,
2009 expectedDisplayId, expectedFlags);
2010 }
2011
2012 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
2013
2014private:
2015 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00002016};
2017
2018// Tests for gesture monitors
2019TEST_F(InputDispatcherTest, GestureMonitor_ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07002020 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00002021 sp<FakeWindowHandle> window =
2022 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002023 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00002024
chaviwd1c23182019-12-20 18:44:56 -08002025 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
2026 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00002027
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002028 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00002029 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002030 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00002031 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002032 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00002033}
2034
2035TEST_F(InputDispatcherTest, GestureMonitor_DoesNotReceiveKeyEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07002036 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00002037 sp<FakeWindowHandle> window =
2038 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2039
2040 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002041 window->setFocusable(true);
Michael Wright3a240c42019-12-10 20:53:41 +00002042
Arthur Hung72d8dc32020-03-28 00:48:39 +00002043 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002044 setFocusedWindow(window);
2045
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002046 window->consumeFocusEvent(true);
Michael Wright3a240c42019-12-10 20:53:41 +00002047
chaviwd1c23182019-12-20 18:44:56 -08002048 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
2049 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00002050
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002051 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2052 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00002053 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002054 monitor.assertNoEvents();
Michael Wright3a240c42019-12-10 20:53:41 +00002055}
2056
2057TEST_F(InputDispatcherTest, GestureMonitor_CanPilferAfterWindowIsRemovedMidStream) {
Chris Yea209fde2020-07-22 13:54:51 -07002058 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00002059 sp<FakeWindowHandle> window =
2060 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002061 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00002062
chaviwd1c23182019-12-20 18:44:56 -08002063 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
2064 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00002065
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002066 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00002067 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002068 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00002069 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002070 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00002071
2072 window->releaseChannel();
2073
chaviwd1c23182019-12-20 18:44:56 -08002074 mDispatcher->pilferPointers(monitor.getToken());
Michael Wright3a240c42019-12-10 20:53:41 +00002075
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002076 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00002077 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002078 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08002079 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00002080}
2081
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002082TEST_F(InputDispatcherTest, UnresponsiveGestureMonitor_GetsAnr) {
2083 FakeMonitorReceiver monitor =
2084 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
2085 true /*isGestureMonitor*/);
2086
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002087 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002088 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
2089 std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
2090 ASSERT_TRUE(consumeSeq);
2091
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00002092 mFakePolicy->assertNotifyMonitorUnresponsiveWasCalled(DISPATCHING_TIMEOUT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002093 monitor.finishEvent(*consumeSeq);
2094 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00002095 mFakePolicy->assertNotifyMonitorResponsiveWasCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002096}
2097
chaviw81e2bb92019-12-18 15:03:51 -08002098TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002099 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
chaviw81e2bb92019-12-18 15:03:51 -08002100 sp<FakeWindowHandle> window =
2101 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2102
Arthur Hung72d8dc32020-03-28 00:48:39 +00002103 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08002104
2105 NotifyMotionArgs motionArgs =
2106 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2107 ADISPLAY_ID_DEFAULT);
2108
2109 mDispatcher->notifyMotion(&motionArgs);
2110 // Window should receive motion down event.
2111 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2112
2113 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002114 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08002115 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
2116 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
2117 motionArgs.pointerCoords[0].getX() - 10);
2118
2119 mDispatcher->notifyMotion(&motionArgs);
2120 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
2121 0 /*expectedFlags*/);
2122}
2123
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002124/**
2125 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
2126 * the device default right away. In the test scenario, we check both the default value,
2127 * and the action of enabling / disabling.
2128 */
2129TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07002130 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002131 sp<FakeWindowHandle> window =
2132 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2133
2134 // Set focused application.
2135 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002136 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002137
2138 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00002139 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002140 setFocusedWindow(window);
2141
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002142 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2143
2144 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07002145 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002146 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002147 window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/);
2148
2149 SCOPED_TRACE("Disable touch mode");
2150 mDispatcher->setInTouchMode(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07002151 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002152 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002153 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002154 window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/);
2155
2156 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07002157 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002158 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002159 window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/);
2160
2161 SCOPED_TRACE("Enable touch mode again");
2162 mDispatcher->setInTouchMode(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07002163 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002164 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002165 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002166 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2167
2168 window->assertNoEvents();
2169}
2170
Gang Wange9087892020-01-07 12:17:14 -05002171TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002172 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Gang Wange9087892020-01-07 12:17:14 -05002173 sp<FakeWindowHandle> window =
2174 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2175
2176 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002177 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05002178
Arthur Hung72d8dc32020-03-28 00:48:39 +00002179 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002180 setFocusedWindow(window);
2181
Gang Wange9087892020-01-07 12:17:14 -05002182 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2183
2184 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
2185 mDispatcher->notifyKey(&keyArgs);
2186
2187 InputEvent* event = window->consume();
2188 ASSERT_NE(event, nullptr);
2189
2190 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
2191 ASSERT_NE(verified, nullptr);
2192 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
2193
2194 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
2195 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
2196 ASSERT_EQ(keyArgs.source, verified->source);
2197 ASSERT_EQ(keyArgs.displayId, verified->displayId);
2198
2199 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
2200
2201 ASSERT_EQ(keyArgs.action, verifiedKey.action);
2202 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05002203 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
2204 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
2205 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
2206 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
2207 ASSERT_EQ(0, verifiedKey.repeatCount);
2208}
2209
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002210TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002211 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002212 sp<FakeWindowHandle> window =
2213 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2214
2215 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2216
Arthur Hung72d8dc32020-03-28 00:48:39 +00002217 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002218
2219 NotifyMotionArgs motionArgs =
2220 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2221 ADISPLAY_ID_DEFAULT);
2222 mDispatcher->notifyMotion(&motionArgs);
2223
2224 InputEvent* event = window->consume();
2225 ASSERT_NE(event, nullptr);
2226
2227 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
2228 ASSERT_NE(verified, nullptr);
2229 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
2230
2231 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
2232 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
2233 EXPECT_EQ(motionArgs.source, verified->source);
2234 EXPECT_EQ(motionArgs.displayId, verified->displayId);
2235
2236 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
2237
2238 EXPECT_EQ(motionArgs.pointerCoords[0].getX(), verifiedMotion.rawX);
2239 EXPECT_EQ(motionArgs.pointerCoords[0].getY(), verifiedMotion.rawY);
2240 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
2241 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
2242 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
2243 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
2244 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
2245}
2246
Prabir Pradhanbd527712021-03-09 19:17:09 -08002247TEST_F(InputDispatcherTest, NonPointerMotionEvent_NotTransformed) {
yunho.shinf4a80b82020-11-16 21:13:57 +09002248 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2249 sp<FakeWindowHandle> window =
2250 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2251 const std::string name = window->getName();
2252
2253 // Window gets transformed by offset values.
2254 window->setWindowOffset(500.0f, 500.0f);
2255
2256 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2257 window->setFocusable(true);
2258
2259 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2260
2261 // First, we set focused window so that focusedWindowHandle is not null.
2262 setFocusedWindow(window);
2263
2264 // Second, we consume focus event if it is right or wrong according to onFocusChangedLocked.
2265 window->consumeFocusEvent(true);
2266
Prabir Pradhanbd527712021-03-09 19:17:09 -08002267 constexpr const std::array nonPointerSources = {AINPUT_SOURCE_TRACKBALL,
2268 AINPUT_SOURCE_MOUSE_RELATIVE,
2269 AINPUT_SOURCE_JOYSTICK};
2270 for (const int source : nonPointerSources) {
2271 // Notify motion with a non-pointer source.
2272 NotifyMotionArgs motionArgs =
2273 generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, source, ADISPLAY_ID_DEFAULT);
2274 mDispatcher->notifyMotion(&motionArgs);
yunho.shinf4a80b82020-11-16 21:13:57 +09002275
Prabir Pradhanbd527712021-03-09 19:17:09 -08002276 InputEvent* event = window->consume();
2277 ASSERT_NE(event, nullptr);
2278 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
2279 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
2280 << " event, got " << inputEventTypeToString(event->getType()) << " event";
yunho.shinf4a80b82020-11-16 21:13:57 +09002281
Prabir Pradhanbd527712021-03-09 19:17:09 -08002282 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
2283 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, motionEvent.getAction());
2284 EXPECT_EQ(motionArgs.pointerCount, motionEvent.getPointerCount());
yunho.shinf4a80b82020-11-16 21:13:57 +09002285
Prabir Pradhanbd527712021-03-09 19:17:09 -08002286 float expectedX = motionArgs.pointerCoords[0].getX();
2287 float expectedY = motionArgs.pointerCoords[0].getY();
yunho.shinf4a80b82020-11-16 21:13:57 +09002288
Prabir Pradhanbd527712021-03-09 19:17:09 -08002289 // Ensure the axis values from the final motion event are not transformed.
2290 EXPECT_EQ(expectedX, motionEvent.getX(0))
2291 << "expected " << expectedX << " for x coord of " << name.c_str() << ", got "
2292 << motionEvent.getX(0);
2293 EXPECT_EQ(expectedY, motionEvent.getY(0))
2294 << "expected " << expectedY << " for y coord of " << name.c_str() << ", got "
2295 << motionEvent.getY(0);
2296 // Ensure the raw and transformed axis values for the motion event are the same.
2297 EXPECT_EQ(motionEvent.getRawX(0), motionEvent.getX(0))
2298 << "expected raw and transformed X-axis values to be equal";
2299 EXPECT_EQ(motionEvent.getRawY(0), motionEvent.getY(0))
2300 << "expected raw and transformed Y-axis values to be equal";
2301 }
yunho.shinf4a80b82020-11-16 21:13:57 +09002302}
2303
chaviw09c8d2d2020-08-24 15:48:26 -07002304/**
2305 * Ensure that separate calls to sign the same data are generating the same key.
2306 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
2307 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
2308 * tests.
2309 */
2310TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
2311 KeyEvent event = getTestKeyEvent();
2312 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
2313
2314 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
2315 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
2316 ASSERT_EQ(hmac1, hmac2);
2317}
2318
2319/**
2320 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
2321 */
2322TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
2323 KeyEvent event = getTestKeyEvent();
2324 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
2325 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
2326
2327 verifiedEvent.deviceId += 1;
2328 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2329
2330 verifiedEvent.source += 1;
2331 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2332
2333 verifiedEvent.eventTimeNanos += 1;
2334 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2335
2336 verifiedEvent.displayId += 1;
2337 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2338
2339 verifiedEvent.action += 1;
2340 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2341
2342 verifiedEvent.downTimeNanos += 1;
2343 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2344
2345 verifiedEvent.flags += 1;
2346 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2347
2348 verifiedEvent.keyCode += 1;
2349 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2350
2351 verifiedEvent.scanCode += 1;
2352 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2353
2354 verifiedEvent.metaState += 1;
2355 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2356
2357 verifiedEvent.repeatCount += 1;
2358 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2359}
2360
Vishnu Nair958da932020-08-21 17:12:37 -07002361TEST_F(InputDispatcherTest, SetFocusedWindow) {
2362 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2363 sp<FakeWindowHandle> windowTop =
2364 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2365 sp<FakeWindowHandle> windowSecond =
2366 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2367 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2368
2369 // Top window is also focusable but is not granted focus.
2370 windowTop->setFocusable(true);
2371 windowSecond->setFocusable(true);
2372 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2373 setFocusedWindow(windowSecond);
2374
2375 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002376 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2377 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002378
2379 // Focused window should receive event.
2380 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
2381 windowTop->assertNoEvents();
2382}
2383
2384TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
2385 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2386 sp<FakeWindowHandle> window =
2387 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2388 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2389
2390 window->setFocusable(true);
2391 // Release channel for window is no longer valid.
2392 window->releaseChannel();
2393 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2394 setFocusedWindow(window);
2395
2396 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002397 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2398 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002399
2400 // window channel is invalid, so it should not receive any input event.
2401 window->assertNoEvents();
2402}
2403
2404TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
2405 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2406 sp<FakeWindowHandle> window =
2407 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2408 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2409
2410 // Window is not focusable.
2411 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2412 setFocusedWindow(window);
2413
2414 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002415 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2416 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002417
2418 // window is invalid, so it should not receive any input event.
2419 window->assertNoEvents();
2420}
2421
2422TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
2423 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2424 sp<FakeWindowHandle> windowTop =
2425 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2426 sp<FakeWindowHandle> windowSecond =
2427 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2428 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2429
2430 windowTop->setFocusable(true);
2431 windowSecond->setFocusable(true);
2432 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2433 setFocusedWindow(windowTop);
2434 windowTop->consumeFocusEvent(true);
2435
2436 setFocusedWindow(windowSecond, windowTop);
2437 windowSecond->consumeFocusEvent(true);
2438 windowTop->consumeFocusEvent(false);
2439
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002440 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2441 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002442
2443 // Focused window should receive event.
2444 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
2445}
2446
2447TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) {
2448 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2449 sp<FakeWindowHandle> windowTop =
2450 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2451 sp<FakeWindowHandle> windowSecond =
2452 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2453 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2454
2455 windowTop->setFocusable(true);
2456 windowSecond->setFocusable(true);
2457 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2458 setFocusedWindow(windowSecond, windowTop);
2459
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002460 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2461 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002462
2463 // Event should be dropped.
2464 windowTop->assertNoEvents();
2465 windowSecond->assertNoEvents();
2466}
2467
2468TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
2469 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2470 sp<FakeWindowHandle> window =
2471 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2472 sp<FakeWindowHandle> previousFocusedWindow =
2473 new FakeWindowHandle(application, mDispatcher, "previousFocusedWindow",
2474 ADISPLAY_ID_DEFAULT);
2475 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2476
2477 window->setFocusable(true);
2478 previousFocusedWindow->setFocusable(true);
2479 window->setVisible(false);
2480 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
2481 setFocusedWindow(previousFocusedWindow);
2482 previousFocusedWindow->consumeFocusEvent(true);
2483
2484 // Requesting focus on invisible window takes focus from currently focused window.
2485 setFocusedWindow(window);
2486 previousFocusedWindow->consumeFocusEvent(false);
2487
2488 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002489 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07002490 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002491 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07002492
2493 // Window does not get focus event or key down.
2494 window->assertNoEvents();
2495
2496 // Window becomes visible.
2497 window->setVisible(true);
2498 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2499
2500 // Window receives focus event.
2501 window->consumeFocusEvent(true);
2502 // Focused window receives key down.
2503 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2504}
2505
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002506/**
2507 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
2508 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
2509 * of the 'slipperyEnterWindow'.
2510 *
2511 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
2512 * a way so that the touched location is no longer covered by the top window.
2513 *
2514 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
2515 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
2516 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
2517 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
2518 * with ACTION_DOWN).
2519 * Thus, the touch has been transferred from the top window into the bottom window, because the top
2520 * window moved itself away from the touched location and had Flag::SLIPPERY.
2521 *
2522 * Even though the top window moved away from the touched location, it is still obscuring the bottom
2523 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
2524 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
2525 *
2526 * In this test, we ensure that the event received by the bottom window has
2527 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
2528 */
2529TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
2530 constexpr int32_t SLIPPERY_PID = INJECTOR_PID + 1;
2531 constexpr int32_t SLIPPERY_UID = INJECTOR_UID + 1;
2532
2533 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2534 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2535
2536 sp<FakeWindowHandle> slipperyExitWindow =
2537 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2538 slipperyExitWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
2539 InputWindowInfo::Flag::SLIPPERY);
2540 // Make sure this one overlaps the bottom window
2541 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
2542 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
2543 // one. Windows with the same owner are not considered to be occluding each other.
2544 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
2545
2546 sp<FakeWindowHandle> slipperyEnterWindow =
2547 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2548 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
2549
2550 mDispatcher->setInputWindows(
2551 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
2552
2553 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
2554 NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2555 ADISPLAY_ID_DEFAULT, {{50, 50}});
2556 mDispatcher->notifyMotion(&args);
2557 slipperyExitWindow->consumeMotionDown();
2558 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
2559 mDispatcher->setInputWindows(
2560 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
2561
2562 args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2563 ADISPLAY_ID_DEFAULT, {{51, 51}});
2564 mDispatcher->notifyMotion(&args);
2565
2566 slipperyExitWindow->consumeMotionCancel();
2567
2568 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
2569 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
2570}
2571
Garfield Tan1c7bc862020-01-28 13:24:04 -08002572class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
2573protected:
2574 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
2575 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
2576
Chris Yea209fde2020-07-22 13:54:51 -07002577 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002578 sp<FakeWindowHandle> mWindow;
2579
2580 virtual void SetUp() override {
2581 mFakePolicy = new FakeInputDispatcherPolicy();
2582 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
2583 mDispatcher = new InputDispatcher(mFakePolicy);
2584 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
2585 ASSERT_EQ(OK, mDispatcher->start());
2586
2587 setUpWindow();
2588 }
2589
2590 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07002591 mApp = std::make_shared<FakeApplicationHandle>();
Garfield Tan1c7bc862020-01-28 13:24:04 -08002592 mWindow = new FakeWindowHandle(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2593
Vishnu Nair47074b82020-08-14 11:54:47 -07002594 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002595 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002596 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002597 mWindow->consumeFocusEvent(true);
2598 }
2599
Chris Ye2ad95392020-09-01 13:44:44 -07002600 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08002601 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07002602 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002603 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
2604 mDispatcher->notifyKey(&keyArgs);
2605
2606 // Window should receive key down event.
2607 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2608 }
2609
2610 void expectKeyRepeatOnce(int32_t repeatCount) {
2611 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
2612 InputEvent* repeatEvent = mWindow->consume();
2613 ASSERT_NE(nullptr, repeatEvent);
2614
2615 uint32_t eventType = repeatEvent->getType();
2616 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType);
2617
2618 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
2619 uint32_t eventAction = repeatKeyEvent->getAction();
2620 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
2621 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
2622 }
2623
Chris Ye2ad95392020-09-01 13:44:44 -07002624 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08002625 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07002626 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002627 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
2628 mDispatcher->notifyKey(&keyArgs);
2629
2630 // Window should receive key down event.
2631 mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
2632 0 /*expectedFlags*/);
2633 }
2634};
2635
2636TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Chris Ye2ad95392020-09-01 13:44:44 -07002637 sendAndConsumeKeyDown(1 /* deviceId */);
2638 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2639 expectKeyRepeatOnce(repeatCount);
2640 }
2641}
2642
2643TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
2644 sendAndConsumeKeyDown(1 /* deviceId */);
2645 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2646 expectKeyRepeatOnce(repeatCount);
2647 }
2648 sendAndConsumeKeyDown(2 /* deviceId */);
2649 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08002650 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2651 expectKeyRepeatOnce(repeatCount);
2652 }
2653}
2654
2655TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Chris Ye2ad95392020-09-01 13:44:44 -07002656 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002657 expectKeyRepeatOnce(1 /*repeatCount*/);
Chris Ye2ad95392020-09-01 13:44:44 -07002658 sendAndConsumeKeyUp(1 /* deviceId */);
2659 mWindow->assertNoEvents();
2660}
2661
2662TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
2663 sendAndConsumeKeyDown(1 /* deviceId */);
2664 expectKeyRepeatOnce(1 /*repeatCount*/);
2665 sendAndConsumeKeyDown(2 /* deviceId */);
2666 expectKeyRepeatOnce(1 /*repeatCount*/);
2667 // Stale key up from device 1.
2668 sendAndConsumeKeyUp(1 /* deviceId */);
2669 // Device 2 is still down, keep repeating
2670 expectKeyRepeatOnce(2 /*repeatCount*/);
2671 expectKeyRepeatOnce(3 /*repeatCount*/);
2672 // Device 2 key up
2673 sendAndConsumeKeyUp(2 /* deviceId */);
2674 mWindow->assertNoEvents();
2675}
2676
2677TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
2678 sendAndConsumeKeyDown(1 /* deviceId */);
2679 expectKeyRepeatOnce(1 /*repeatCount*/);
2680 sendAndConsumeKeyDown(2 /* deviceId */);
2681 expectKeyRepeatOnce(1 /*repeatCount*/);
2682 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
2683 sendAndConsumeKeyUp(2 /* deviceId */);
2684 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08002685 mWindow->assertNoEvents();
2686}
2687
2688TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Chris Ye2ad95392020-09-01 13:44:44 -07002689 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002690 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2691 InputEvent* repeatEvent = mWindow->consume();
2692 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2693 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
2694 IdGenerator::getSource(repeatEvent->getId()));
2695 }
2696}
2697
2698TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Chris Ye2ad95392020-09-01 13:44:44 -07002699 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002700
2701 std::unordered_set<int32_t> idSet;
2702 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2703 InputEvent* repeatEvent = mWindow->consume();
2704 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2705 int32_t id = repeatEvent->getId();
2706 EXPECT_EQ(idSet.end(), idSet.find(id));
2707 idSet.insert(id);
2708 }
2709}
2710
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002711/* Test InputDispatcher for MultiDisplay */
2712class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
2713public:
2714 static constexpr int32_t SECOND_DISPLAY_ID = 1;
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002715 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002716 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08002717
Chris Yea209fde2020-07-22 13:54:51 -07002718 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002719 windowInPrimary =
2720 new FakeWindowHandle(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002721
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002722 // Set focus window for primary display, but focused display would be second one.
2723 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07002724 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002725 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002726 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002727 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08002728
Chris Yea209fde2020-07-22 13:54:51 -07002729 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002730 windowInSecondary =
2731 new FakeWindowHandle(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002732 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002733 // Set focus display to second one.
2734 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
2735 // Set focus window for second display.
2736 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07002737 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002738 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002739 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002740 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002741 }
2742
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002743 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002744 InputDispatcherTest::TearDown();
2745
Chris Yea209fde2020-07-22 13:54:51 -07002746 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002747 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07002748 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002749 windowInSecondary.clear();
2750 }
2751
2752protected:
Chris Yea209fde2020-07-22 13:54:51 -07002753 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002754 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07002755 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002756 sp<FakeWindowHandle> windowInSecondary;
2757};
2758
2759TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
2760 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002761 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2762 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2763 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002764 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08002765 windowInSecondary->assertNoEvents();
2766
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002767 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002768 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2769 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
2770 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08002771 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002772 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08002773}
2774
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002775TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08002776 // Test inject a key down with display id specified.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002777 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2778 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002779 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08002780 windowInSecondary->assertNoEvents();
2781
2782 // Test inject a key down without display id specified.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002783 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2784 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08002785 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002786 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08002787
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002788 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00002789 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08002790
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002791 // Old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002792 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
2793 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08002794
2795 // Test inject a key down, should timeout because of no target window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002796 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2797 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08002798 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002799 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08002800 windowInSecondary->assertNoEvents();
2801}
2802
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002803// Test per-display input monitors for motion event.
2804TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08002805 FakeMonitorReceiver monitorInPrimary =
2806 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
2807 FakeMonitorReceiver monitorInSecondary =
2808 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002809
2810 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002811 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2812 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2813 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002814 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002815 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002816 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002817 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002818
2819 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002820 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2821 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
2822 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002823 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002824 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002825 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08002826 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002827
2828 // Test inject a non-pointer motion event.
2829 // If specific a display, it will dispatch to the focused window of particular display,
2830 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002831 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2832 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
2833 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002834 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002835 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002836 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08002837 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002838}
2839
2840// Test per-display input monitors for key event.
2841TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002842 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08002843 FakeMonitorReceiver monitorInPrimary =
2844 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
2845 FakeMonitorReceiver monitorInSecondary =
2846 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002847
2848 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002849 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2850 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002851 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002852 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002853 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08002854 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002855}
2856
Vishnu Nair958da932020-08-21 17:12:37 -07002857TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
2858 sp<FakeWindowHandle> secondWindowInPrimary =
2859 new FakeWindowHandle(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
2860 secondWindowInPrimary->setFocusable(true);
2861 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
2862 setFocusedWindow(secondWindowInPrimary);
2863 windowInPrimary->consumeFocusEvent(false);
2864 secondWindowInPrimary->consumeFocusEvent(true);
2865
2866 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002867 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2868 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002869 windowInPrimary->assertNoEvents();
2870 windowInSecondary->assertNoEvents();
2871 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2872}
2873
Jackal Guof9696682018-10-05 12:23:23 +08002874class InputFilterTest : public InputDispatcherTest {
2875protected:
2876 static constexpr int32_t SECOND_DISPLAY_ID = 1;
2877
2878 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) {
2879 NotifyMotionArgs motionArgs;
2880
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002881 motionArgs =
2882 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08002883 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002884 motionArgs =
2885 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08002886 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002887 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08002888 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08002889 mFakePolicy->assertFilterInputEventWasCalled(motionArgs);
Jackal Guof9696682018-10-05 12:23:23 +08002890 } else {
2891 mFakePolicy->assertFilterInputEventWasNotCalled();
2892 }
2893 }
2894
2895 void testNotifyKey(bool expectToBeFiltered) {
2896 NotifyKeyArgs keyArgs;
2897
2898 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
2899 mDispatcher->notifyKey(&keyArgs);
2900 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
2901 mDispatcher->notifyKey(&keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002902 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08002903
2904 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08002905 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08002906 } else {
2907 mFakePolicy->assertFilterInputEventWasNotCalled();
2908 }
2909 }
2910};
2911
2912// Test InputFilter for MotionEvent
2913TEST_F(InputFilterTest, MotionEvent_InputFilter) {
2914 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
2915 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
2916 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
2917
2918 // Enable InputFilter
2919 mDispatcher->setInputFilterEnabled(true);
2920 // Test touch on both primary and second display, and check if both events are filtered.
2921 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
2922 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
2923
2924 // Disable InputFilter
2925 mDispatcher->setInputFilterEnabled(false);
2926 // Test touch on both primary and second display, and check if both events aren't filtered.
2927 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
2928 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
2929}
2930
2931// Test InputFilter for KeyEvent
2932TEST_F(InputFilterTest, KeyEvent_InputFilter) {
2933 // Since the InputFilter is disabled by default, check if key event aren't filtered.
2934 testNotifyKey(/*expectToBeFiltered*/ false);
2935
2936 // Enable InputFilter
2937 mDispatcher->setInputFilterEnabled(true);
2938 // Send a key event, and check if it is filtered.
2939 testNotifyKey(/*expectToBeFiltered*/ true);
2940
2941 // Disable InputFilter
2942 mDispatcher->setInputFilterEnabled(false);
2943 // Send a key event, and check if it isn't filtered.
2944 testNotifyKey(/*expectToBeFiltered*/ false);
2945}
2946
chaviwfd6d3512019-03-25 13:23:49 -07002947class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002948 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07002949 InputDispatcherTest::SetUp();
2950
Chris Yea209fde2020-07-22 13:54:51 -07002951 std::shared_ptr<FakeApplicationHandle> application =
2952 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002953 mUnfocusedWindow =
2954 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07002955 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
2956 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
2957 // window.
Michael Wright44753b12020-07-08 13:48:11 +01002958 mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07002959
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002960 mFocusedWindow =
2961 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2962 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01002963 mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07002964
2965 // Set focused application.
2966 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002967 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07002968
2969 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00002970 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002971 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002972 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07002973 }
2974
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002975 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07002976 InputDispatcherTest::TearDown();
2977
2978 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002979 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07002980 }
2981
2982protected:
2983 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002984 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002985 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07002986};
2987
2988// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
2989// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
2990// the onPointerDownOutsideFocus callback.
2991TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002992 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002993 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2994 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002995 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002996 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07002997
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002998 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07002999 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
3000}
3001
3002// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
3003// DOWN on the window that doesn't have focus. Ensure no window received the
3004// onPointerDownOutsideFocus callback.
3005TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003006 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003007 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003008 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003009 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07003010
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003011 ASSERT_TRUE(mDispatcher->waitForIdle());
3012 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07003013}
3014
3015// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
3016// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
3017TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003018 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
3019 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003020 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07003021
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003022 ASSERT_TRUE(mDispatcher->waitForIdle());
3023 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07003024}
3025
3026// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
3027// DOWN on the window that already has focus. Ensure no window received the
3028// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003029TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003030 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003031 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003032 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003033 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003034 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07003035
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003036 ASSERT_TRUE(mDispatcher->waitForIdle());
3037 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07003038}
3039
chaviwaf87b3e2019-10-01 16:59:28 -07003040// These tests ensures we can send touch events to a single client when there are multiple input
3041// windows that point to the same client token.
3042class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
3043 virtual void SetUp() override {
3044 InputDispatcherTest::SetUp();
3045
Chris Yea209fde2020-07-22 13:54:51 -07003046 std::shared_ptr<FakeApplicationHandle> application =
3047 std::make_shared<FakeApplicationHandle>();
chaviwaf87b3e2019-10-01 16:59:28 -07003048 mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1",
3049 ADISPLAY_ID_DEFAULT);
3050 // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window.
3051 // 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 +01003052 mWindow1->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3053 InputWindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07003054 mWindow1->setFrame(Rect(0, 0, 100, 100));
3055
3056 mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2",
3057 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
Michael Wright44753b12020-07-08 13:48:11 +01003058 mWindow2->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3059 InputWindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07003060 mWindow2->setFrame(Rect(100, 100, 200, 200));
3061
Arthur Hung72d8dc32020-03-28 00:48:39 +00003062 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07003063 }
3064
3065protected:
3066 sp<FakeWindowHandle> mWindow1;
3067 sp<FakeWindowHandle> mWindow2;
3068
3069 // Helper function to convert the point from screen coordinates into the window's space
3070 static PointF getPointInWindow(const InputWindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07003071 vec2 vals = windowInfo->transform.transform(point.x, point.y);
3072 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07003073 }
3074
3075 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
3076 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003077 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07003078 InputEvent* event = window->consume();
3079
3080 ASSERT_NE(nullptr, event) << name.c_str()
3081 << ": consumer should have returned non-NULL event.";
3082
3083 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
3084 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
3085 << " event, got " << inputEventTypeToString(event->getType()) << " event";
3086
3087 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
3088 EXPECT_EQ(expectedAction, motionEvent.getAction());
3089
3090 for (size_t i = 0; i < points.size(); i++) {
3091 float expectedX = points[i].x;
3092 float expectedY = points[i].y;
3093
3094 EXPECT_EQ(expectedX, motionEvent.getX(i))
3095 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
3096 << ", got " << motionEvent.getX(i);
3097 EXPECT_EQ(expectedY, motionEvent.getY(i))
3098 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
3099 << ", got " << motionEvent.getY(i);
3100 }
3101 }
chaviw9eaa22c2020-07-01 16:21:27 -07003102
3103 void touchAndAssertPositions(int32_t action, std::vector<PointF> touchedPoints,
3104 std::vector<PointF> expectedPoints) {
3105 NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
3106 ADISPLAY_ID_DEFAULT, touchedPoints);
3107 mDispatcher->notifyMotion(&motionArgs);
3108
3109 // Always consume from window1 since it's the window that has the InputReceiver
3110 consumeMotionEvent(mWindow1, action, expectedPoints);
3111 }
chaviwaf87b3e2019-10-01 16:59:28 -07003112};
3113
3114TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
3115 // Touch Window 1
3116 PointF touchedPoint = {10, 10};
3117 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003118 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003119
3120 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07003121 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003122
3123 // Touch Window 2
3124 touchedPoint = {150, 150};
3125 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003126 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003127}
3128
chaviw9eaa22c2020-07-01 16:21:27 -07003129TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
3130 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07003131 mWindow2->setWindowScale(0.5f, 0.5f);
3132
3133 // Touch Window 1
3134 PointF touchedPoint = {10, 10};
3135 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003136 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003137 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07003138 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003139
3140 // Touch Window 2
3141 touchedPoint = {150, 150};
3142 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003143 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
3144 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003145
chaviw9eaa22c2020-07-01 16:21:27 -07003146 // Update the transform so rotation is set
3147 mWindow2->setWindowTransform(0, -1, 1, 0);
3148 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
3149 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003150}
3151
chaviw9eaa22c2020-07-01 16:21:27 -07003152TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003153 mWindow2->setWindowScale(0.5f, 0.5f);
3154
3155 // Touch Window 1
3156 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3157 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003158 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003159
3160 // Touch Window 2
3161 int32_t actionPointerDown =
3162 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003163 touchedPoints.push_back(PointF{150, 150});
3164 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3165 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003166
chaviw9eaa22c2020-07-01 16:21:27 -07003167 // Release Window 2
3168 int32_t actionPointerUp =
3169 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3170 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
3171 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003172
chaviw9eaa22c2020-07-01 16:21:27 -07003173 // Update the transform so rotation is set for Window 2
3174 mWindow2->setWindowTransform(0, -1, 1, 0);
3175 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3176 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003177}
3178
chaviw9eaa22c2020-07-01 16:21:27 -07003179TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003180 mWindow2->setWindowScale(0.5f, 0.5f);
3181
3182 // Touch Window 1
3183 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3184 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003185 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003186
3187 // Touch Window 2
3188 int32_t actionPointerDown =
3189 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003190 touchedPoints.push_back(PointF{150, 150});
3191 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003192
chaviw9eaa22c2020-07-01 16:21:27 -07003193 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003194
3195 // Move both windows
3196 touchedPoints = {{20, 20}, {175, 175}};
3197 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3198 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3199
chaviw9eaa22c2020-07-01 16:21:27 -07003200 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003201
chaviw9eaa22c2020-07-01 16:21:27 -07003202 // Release Window 2
3203 int32_t actionPointerUp =
3204 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3205 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
3206 expectedPoints.pop_back();
3207
3208 // Touch Window 2
3209 mWindow2->setWindowTransform(0, -1, 1, 0);
3210 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3211 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
3212
3213 // Move both windows
3214 touchedPoints = {{20, 20}, {175, 175}};
3215 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3216 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3217
3218 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003219}
3220
3221TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
3222 mWindow1->setWindowScale(0.5f, 0.5f);
3223
3224 // Touch Window 1
3225 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3226 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003227 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003228
3229 // Touch Window 2
3230 int32_t actionPointerDown =
3231 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003232 touchedPoints.push_back(PointF{150, 150});
3233 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003234
chaviw9eaa22c2020-07-01 16:21:27 -07003235 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003236
3237 // Move both windows
3238 touchedPoints = {{20, 20}, {175, 175}};
3239 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3240 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3241
chaviw9eaa22c2020-07-01 16:21:27 -07003242 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003243}
3244
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003245class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
3246 virtual void SetUp() override {
3247 InputDispatcherTest::SetUp();
3248
Chris Yea209fde2020-07-22 13:54:51 -07003249 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003250 mApplication->setDispatchingTimeout(20ms);
3251 mWindow =
3252 new FakeWindowHandle(mApplication, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
3253 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05003254 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07003255 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003256 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3257 // window.
Michael Wright44753b12020-07-08 13:48:11 +01003258 mWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003259
3260 // Set focused application.
3261 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
3262
3263 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003264 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003265 mWindow->consumeFocusEvent(true);
3266 }
3267
3268 virtual void TearDown() override {
3269 InputDispatcherTest::TearDown();
3270 mWindow.clear();
3271 }
3272
3273protected:
Chris Yea209fde2020-07-22 13:54:51 -07003274 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003275 sp<FakeWindowHandle> mWindow;
3276 static constexpr PointF WINDOW_LOCATION = {20, 20};
3277
3278 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003279 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003280 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3281 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003282 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003283 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3284 WINDOW_LOCATION));
3285 }
3286};
3287
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003288// Send a tap and respond, which should not cause an ANR.
3289TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
3290 tapOnWindow();
3291 mWindow->consumeMotionDown();
3292 mWindow->consumeMotionUp();
3293 ASSERT_TRUE(mDispatcher->waitForIdle());
3294 mFakePolicy->assertNotifyAnrWasNotCalled();
3295}
3296
3297// Send a regular key and respond, which should not cause an ANR.
3298TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003299 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003300 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
3301 ASSERT_TRUE(mDispatcher->waitForIdle());
3302 mFakePolicy->assertNotifyAnrWasNotCalled();
3303}
3304
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003305TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
3306 mWindow->setFocusable(false);
3307 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3308 mWindow->consumeFocusEvent(false);
3309
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003310 InputEventInjectionResult result =
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003311 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003312 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
3313 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003314 // Key will not go to window because we have no focused window.
3315 // The 'no focused window' ANR timer should start instead.
3316
3317 // Now, the focused application goes away.
3318 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
3319 // The key should get dropped and there should be no ANR.
3320
3321 ASSERT_TRUE(mDispatcher->waitForIdle());
3322 mFakePolicy->assertNotifyAnrWasNotCalled();
3323}
3324
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003325// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003326// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
3327// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003328TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003329 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003330 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3331 WINDOW_LOCATION));
3332
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003333 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
3334 ASSERT_TRUE(sequenceNum);
3335 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003336 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003337
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003338 mWindow->finishEvent(*sequenceNum);
3339 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3340 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003341 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003342 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003343}
3344
3345// Send a key to the app and have the app not respond right away.
3346TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
3347 // Inject a key, and don't respond - expect that ANR is called.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003348 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003349 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
3350 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003351 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003352 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003353 ASSERT_TRUE(mDispatcher->waitForIdle());
3354}
3355
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003356// We have a focused application, but no focused window
3357TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003358 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003359 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3360 mWindow->consumeFocusEvent(false);
3361
3362 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003363 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003364 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3365 WINDOW_LOCATION));
3366 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
3367 mDispatcher->waitForIdle();
3368 mFakePolicy->assertNotifyAnrWasNotCalled();
3369
3370 // Once a focused event arrives, we get an ANR for this application
3371 // We specify the injection timeout to be smaller than the application timeout, to ensure that
3372 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003373 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003374 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003375 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3376 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003377 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003378 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003379 ASSERT_TRUE(mDispatcher->waitForIdle());
3380}
3381
3382// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003383// Make sure that we don't notify policy twice about the same ANR.
3384TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003385 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003386 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3387 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003388
3389 // Once a focused event arrives, we get an ANR for this application
3390 // We specify the injection timeout to be smaller than the application timeout, to ensure that
3391 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003392 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003393 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003394 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3395 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003396 const std::chrono::duration appTimeout =
3397 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003398 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003399
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003400 std::this_thread::sleep_for(appTimeout);
3401 // ANR should not be raised again. It is up to policy to do that if it desires.
3402 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003403
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003404 // If we now get a focused window, the ANR should stop, but the policy handles that via
3405 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003406 ASSERT_TRUE(mDispatcher->waitForIdle());
3407}
3408
3409// We have a focused application, but no focused window
3410TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003411 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003412 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3413 mWindow->consumeFocusEvent(false);
3414
3415 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003416 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003417 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003418 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3419 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003420
3421 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003422 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003423
3424 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003425 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003426 ASSERT_TRUE(mDispatcher->waitForIdle());
3427 mWindow->assertNoEvents();
3428}
3429
3430/**
3431 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
3432 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
3433 * If we process 1 of the events, but ANR on the second event with the same timestamp,
3434 * the ANR mechanism should still work.
3435 *
3436 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
3437 * DOWN event, while not responding on the second one.
3438 */
3439TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
3440 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
3441 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3442 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
3443 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
3444 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003445 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003446
3447 // Now send ACTION_UP, with identical timestamp
3448 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3449 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
3450 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
3451 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003452 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003453
3454 // We have now sent down and up. Let's consume first event and then ANR on the second.
3455 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3456 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003457 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003458}
3459
3460// If an app is not responding to a key event, gesture monitors should continue to receive
3461// new motion events
3462TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnKey) {
3463 FakeMonitorReceiver monitor =
3464 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
3465 true /*isGestureMonitor*/);
3466
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003467 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3468 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003469 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003470 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003471
3472 // Stuck on the ACTION_UP
3473 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003474 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003475
3476 // New tap will go to the gesture monitor, but not to the window
3477 tapOnWindow();
3478 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3479 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3480
3481 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
3482 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003483 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003484 mWindow->assertNoEvents();
3485 monitor.assertNoEvents();
3486}
3487
3488// If an app is not responding to a motion event, gesture monitors should continue to receive
3489// new motion events
3490TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnMotion) {
3491 FakeMonitorReceiver monitor =
3492 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
3493 true /*isGestureMonitor*/);
3494
3495 tapOnWindow();
3496 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3497 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3498
3499 mWindow->consumeMotionDown();
3500 // Stuck on the ACTION_UP
3501 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003502 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003503
3504 // New tap will go to the gesture monitor, but not to the window
3505 tapOnWindow();
3506 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3507 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3508
3509 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
3510 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003511 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003512 mWindow->assertNoEvents();
3513 monitor.assertNoEvents();
3514}
3515
3516// If a window is unresponsive, then you get anr. if the window later catches up and starts to
3517// process events, you don't get an anr. When the window later becomes unresponsive again, you
3518// get an ANR again.
3519// 1. tap -> block on ACTION_UP -> receive ANR
3520// 2. consume all pending events (= queue becomes healthy again)
3521// 3. tap again -> block on ACTION_UP again -> receive ANR second time
3522TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
3523 tapOnWindow();
3524
3525 mWindow->consumeMotionDown();
3526 // Block on ACTION_UP
3527 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003528 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003529 mWindow->consumeMotionUp(); // Now the connection should be healthy again
3530 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003531 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003532 mWindow->assertNoEvents();
3533
3534 tapOnWindow();
3535 mWindow->consumeMotionDown();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003536 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003537 mWindow->consumeMotionUp();
3538
3539 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003540 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003541 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003542 mWindow->assertNoEvents();
3543}
3544
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003545// If a connection remains unresponsive for a while, make sure policy is only notified once about
3546// it.
3547TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003548 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003549 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3550 WINDOW_LOCATION));
3551
3552 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003553 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003554 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003555 // 'notifyConnectionUnresponsive' should only be called once per connection
3556 mFakePolicy->assertNotifyAnrWasNotCalled();
3557 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003558 mWindow->consumeMotionDown();
3559 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3560 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3561 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003562 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003563 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003564 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003565}
3566
3567/**
3568 * If a window is processing a motion event, and then a key event comes in, the key event should
3569 * not to to the focused window until the motion is processed.
3570 *
3571 * Warning!!!
3572 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
3573 * and the injection timeout that we specify when injecting the key.
3574 * We must have the injection timeout (10ms) be smaller than
3575 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
3576 *
3577 * If that value changes, this test should also change.
3578 */
3579TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
3580 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3581 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3582
3583 tapOnWindow();
3584 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3585 ASSERT_TRUE(downSequenceNum);
3586 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3587 ASSERT_TRUE(upSequenceNum);
3588 // Don't finish the events yet, and send a key
3589 // Injection will "succeed" because we will eventually give up and send the key to the focused
3590 // window even if motions are still being processed. But because the injection timeout is short,
3591 // we will receive INJECTION_TIMED_OUT as the result.
3592
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003593 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003594 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003595 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3596 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003597 // Key will not be sent to the window, yet, because the window is still processing events
3598 // and the key remains pending, waiting for the touch events to be processed
3599 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3600 ASSERT_FALSE(keySequenceNum);
3601
3602 std::this_thread::sleep_for(500ms);
3603 // if we wait long enough though, dispatcher will give up, and still send the key
3604 // to the focused window, even though we have not yet finished the motion event
3605 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3606 mWindow->finishEvent(*downSequenceNum);
3607 mWindow->finishEvent(*upSequenceNum);
3608}
3609
3610/**
3611 * If a window is processing a motion event, and then a key event comes in, the key event should
3612 * not go to the focused window until the motion is processed.
3613 * If then a new motion comes in, then the pending key event should be going to the currently
3614 * focused window right away.
3615 */
3616TEST_F(InputDispatcherSingleWindowAnr,
3617 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
3618 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3619 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3620
3621 tapOnWindow();
3622 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3623 ASSERT_TRUE(downSequenceNum);
3624 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3625 ASSERT_TRUE(upSequenceNum);
3626 // Don't finish the events yet, and send a key
3627 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003628 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003629 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003630 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003631 // At this point, key is still pending, and should not be sent to the application yet.
3632 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3633 ASSERT_FALSE(keySequenceNum);
3634
3635 // Now tap down again. It should cause the pending key to go to the focused window right away.
3636 tapOnWindow();
3637 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
3638 // the other events yet. We can finish events in any order.
3639 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
3640 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
3641 mWindow->consumeMotionDown();
3642 mWindow->consumeMotionUp();
3643 mWindow->assertNoEvents();
3644}
3645
3646class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
3647 virtual void SetUp() override {
3648 InputDispatcherTest::SetUp();
3649
Chris Yea209fde2020-07-22 13:54:51 -07003650 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003651 mApplication->setDispatchingTimeout(10ms);
3652 mUnfocusedWindow =
3653 new FakeWindowHandle(mApplication, mDispatcher, "Unfocused", ADISPLAY_ID_DEFAULT);
3654 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
3655 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3656 // window.
3657 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Michael Wright44753b12020-07-08 13:48:11 +01003658 mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3659 InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH |
3660 InputWindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003661
3662 mFocusedWindow =
3663 new FakeWindowHandle(mApplication, mDispatcher, "Focused", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05003664 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003665 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01003666 mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3667 InputWindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003668
3669 // Set focused application.
3670 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07003671 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003672
3673 // Expect one focus window exist in display.
3674 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003675 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003676 mFocusedWindow->consumeFocusEvent(true);
3677 }
3678
3679 virtual void TearDown() override {
3680 InputDispatcherTest::TearDown();
3681
3682 mUnfocusedWindow.clear();
3683 mFocusedWindow.clear();
3684 }
3685
3686protected:
Chris Yea209fde2020-07-22 13:54:51 -07003687 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003688 sp<FakeWindowHandle> mUnfocusedWindow;
3689 sp<FakeWindowHandle> mFocusedWindow;
3690 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
3691 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
3692 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
3693
3694 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
3695
3696 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
3697
3698private:
3699 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003700 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003701 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3702 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003703 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003704 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3705 location));
3706 }
3707};
3708
3709// If we have 2 windows that are both unresponsive, the one with the shortest timeout
3710// should be ANR'd first.
3711TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003712 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003713 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3714 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003715 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003716 mFocusedWindow->consumeMotionDown();
3717 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3718 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3719 // We consumed all events, so no ANR
3720 ASSERT_TRUE(mDispatcher->waitForIdle());
3721 mFakePolicy->assertNotifyAnrWasNotCalled();
3722
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003723 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003724 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3725 FOCUSED_WINDOW_LOCATION));
3726 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
3727 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003728
3729 const std::chrono::duration timeout =
3730 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003731 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003732 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
3733 // sequence to make it consistent
3734 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003735 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003736 mFocusedWindow->consumeMotionDown();
3737 // This cancel is generated because the connection was unresponsive
3738 mFocusedWindow->consumeMotionCancel();
3739 mFocusedWindow->assertNoEvents();
3740 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003741 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003742 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003743 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003744}
3745
3746// If we have 2 windows with identical timeouts that are both unresponsive,
3747// it doesn't matter which order they should have ANR.
3748// But we should receive ANR for both.
3749TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
3750 // Set the timeout for unfocused window to match the focused window
3751 mUnfocusedWindow->setDispatchingTimeout(10ms);
3752 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
3753
3754 tapOnFocusedWindow();
3755 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003756 sp<IBinder> anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms);
3757 sp<IBinder> anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003758
3759 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003760 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
3761 mFocusedWindow->getToken() == anrConnectionToken2);
3762 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
3763 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003764
3765 ASSERT_TRUE(mDispatcher->waitForIdle());
3766 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003767
3768 mFocusedWindow->consumeMotionDown();
3769 mFocusedWindow->consumeMotionUp();
3770 mUnfocusedWindow->consumeMotionOutside();
3771
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003772 sp<IBinder> responsiveToken1 = mFakePolicy->getResponsiveWindowToken();
3773 sp<IBinder> responsiveToken2 = mFakePolicy->getResponsiveWindowToken();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003774
3775 // Both applications should be marked as responsive, in any order
3776 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
3777 mFocusedWindow->getToken() == responsiveToken2);
3778 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
3779 mUnfocusedWindow->getToken() == responsiveToken2);
3780 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003781}
3782
3783// If a window is already not responding, the second tap on the same window should be ignored.
3784// We should also log an error to account for the dropped event (not tested here).
3785// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
3786TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
3787 tapOnFocusedWindow();
3788 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3789 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3790 // Receive the events, but don't respond
3791 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
3792 ASSERT_TRUE(downEventSequenceNum);
3793 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
3794 ASSERT_TRUE(upEventSequenceNum);
3795 const std::chrono::duration timeout =
3796 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003797 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003798
3799 // Tap once again
3800 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003801 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003802 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3803 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003804 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003805 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3806 FOCUSED_WINDOW_LOCATION));
3807 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
3808 // valid touch target
3809 mUnfocusedWindow->assertNoEvents();
3810
3811 // Consume the first tap
3812 mFocusedWindow->finishEvent(*downEventSequenceNum);
3813 mFocusedWindow->finishEvent(*upEventSequenceNum);
3814 ASSERT_TRUE(mDispatcher->waitForIdle());
3815 // The second tap did not go to the focused window
3816 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003817 // Since all events are finished, connection should be deemed healthy again
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003818 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003819 mFakePolicy->assertNotifyAnrWasNotCalled();
3820}
3821
3822// If you tap outside of all windows, there will not be ANR
3823TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003824 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003825 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3826 LOCATION_OUTSIDE_ALL_WINDOWS));
3827 ASSERT_TRUE(mDispatcher->waitForIdle());
3828 mFakePolicy->assertNotifyAnrWasNotCalled();
3829}
3830
3831// Since the focused window is paused, tapping on it should not produce any events
3832TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
3833 mFocusedWindow->setPaused(true);
3834 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
3835
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003836 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003837 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3838 FOCUSED_WINDOW_LOCATION));
3839
3840 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
3841 ASSERT_TRUE(mDispatcher->waitForIdle());
3842 // Should not ANR because the window is paused, and touches shouldn't go to it
3843 mFakePolicy->assertNotifyAnrWasNotCalled();
3844
3845 mFocusedWindow->assertNoEvents();
3846 mUnfocusedWindow->assertNoEvents();
3847}
3848
3849/**
3850 * If a window is processing a motion event, and then a key event comes in, the key event should
3851 * not to to the focused window until the motion is processed.
3852 * If a different window becomes focused at this time, the key should go to that window instead.
3853 *
3854 * Warning!!!
3855 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
3856 * and the injection timeout that we specify when injecting the key.
3857 * We must have the injection timeout (10ms) be smaller than
3858 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
3859 *
3860 * If that value changes, this test should also change.
3861 */
3862TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
3863 // Set a long ANR timeout to prevent it from triggering
3864 mFocusedWindow->setDispatchingTimeout(2s);
3865 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
3866
3867 tapOnUnfocusedWindow();
3868 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
3869 ASSERT_TRUE(downSequenceNum);
3870 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
3871 ASSERT_TRUE(upSequenceNum);
3872 // Don't finish the events yet, and send a key
3873 // Injection will succeed because we will eventually give up and send the key to the focused
3874 // window even if motions are still being processed.
3875
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003876 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003877 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003878 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
3879 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003880 // Key will not be sent to the window, yet, because the window is still processing events
3881 // and the key remains pending, waiting for the touch events to be processed
3882 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
3883 ASSERT_FALSE(keySequenceNum);
3884
3885 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07003886 mFocusedWindow->setFocusable(false);
3887 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003888 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003889 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003890
3891 // Focus events should precede the key events
3892 mUnfocusedWindow->consumeFocusEvent(true);
3893 mFocusedWindow->consumeFocusEvent(false);
3894
3895 // Finish the tap events, which should unblock dispatcher
3896 mUnfocusedWindow->finishEvent(*downSequenceNum);
3897 mUnfocusedWindow->finishEvent(*upSequenceNum);
3898
3899 // Now that all queues are cleared and no backlog in the connections, the key event
3900 // can finally go to the newly focused "mUnfocusedWindow".
3901 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3902 mFocusedWindow->assertNoEvents();
3903 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003904 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003905}
3906
3907// When the touch stream is split across 2 windows, and one of them does not respond,
3908// then ANR should be raised and the touch should be canceled for the unresponsive window.
3909// The other window should not be affected by that.
3910TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
3911 // Touch Window 1
3912 NotifyMotionArgs motionArgs =
3913 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3914 ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION});
3915 mDispatcher->notifyMotion(&motionArgs);
3916 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3917 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3918
3919 // Touch Window 2
3920 int32_t actionPointerDown =
3921 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3922
3923 motionArgs =
3924 generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3925 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION});
3926 mDispatcher->notifyMotion(&motionArgs);
3927
3928 const std::chrono::duration timeout =
3929 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003930 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003931
3932 mUnfocusedWindow->consumeMotionDown();
3933 mFocusedWindow->consumeMotionDown();
3934 // Focused window may or may not receive ACTION_MOVE
3935 // But it should definitely receive ACTION_CANCEL due to the ANR
3936 InputEvent* event;
3937 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
3938 ASSERT_TRUE(moveOrCancelSequenceNum);
3939 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
3940 ASSERT_NE(nullptr, event);
3941 ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION);
3942 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
3943 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
3944 mFocusedWindow->consumeMotionCancel();
3945 } else {
3946 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
3947 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003948 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003949 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003950
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003951 mUnfocusedWindow->assertNoEvents();
3952 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003953 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003954}
3955
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05003956/**
3957 * If we have no focused window, and a key comes in, we start the ANR timer.
3958 * The focused application should add a focused window before the timer runs out to prevent ANR.
3959 *
3960 * If the user touches another application during this time, the key should be dropped.
3961 * Next, if a new focused window comes in, without toggling the focused application,
3962 * then no ANR should occur.
3963 *
3964 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
3965 * but in some cases the policy may not update the focused application.
3966 */
3967TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
3968 std::shared_ptr<FakeApplicationHandle> focusedApplication =
3969 std::make_shared<FakeApplicationHandle>();
3970 focusedApplication->setDispatchingTimeout(60ms);
3971 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
3972 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
3973 mFocusedWindow->setFocusable(false);
3974
3975 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
3976 mFocusedWindow->consumeFocusEvent(false);
3977
3978 // Send a key. The ANR timer should start because there is no focused window.
3979 // 'focusedApplication' will get blamed if this timer completes.
3980 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003981 InputEventInjectionResult result =
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05003982 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003983 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
3984 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05003985
3986 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
3987 // then the injected touches won't cause the focused event to get dropped.
3988 // The dispatcher only checks for whether the queue should be pruned upon queueing.
3989 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
3990 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
3991 // For this test, it means that the key would get delivered to the window once it becomes
3992 // focused.
3993 std::this_thread::sleep_for(10ms);
3994
3995 // Touch unfocused window. This should force the pending key to get dropped.
3996 NotifyMotionArgs motionArgs =
3997 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3998 ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION});
3999 mDispatcher->notifyMotion(&motionArgs);
4000
4001 // We do not consume the motion right away, because that would require dispatcher to first
4002 // process (== drop) the key event, and by that time, ANR will be raised.
4003 // Set the focused window first.
4004 mFocusedWindow->setFocusable(true);
4005 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
4006 setFocusedWindow(mFocusedWindow);
4007 mFocusedWindow->consumeFocusEvent(true);
4008 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
4009 // to another application. This could be a bug / behaviour in the policy.
4010
4011 mUnfocusedWindow->consumeMotionDown();
4012
4013 ASSERT_TRUE(mDispatcher->waitForIdle());
4014 // Should not ANR because we actually have a focused window. It was just added too slowly.
4015 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
4016}
4017
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05004018// These tests ensure we cannot send touch events to a window that's positioned behind a window
4019// that has feature NO_INPUT_CHANNEL.
4020// Layout:
4021// Top (closest to user)
4022// mNoInputWindow (above all windows)
4023// mBottomWindow
4024// Bottom (furthest from user)
4025class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
4026 virtual void SetUp() override {
4027 InputDispatcherTest::SetUp();
4028
4029 mApplication = std::make_shared<FakeApplicationHandle>();
4030 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
4031 "Window without input channel", ADISPLAY_ID_DEFAULT,
4032 std::make_optional<sp<IBinder>>(nullptr) /*token*/);
4033
4034 mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL);
4035 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
4036 // It's perfectly valid for this window to not have an associated input channel
4037
4038 mBottomWindow = new FakeWindowHandle(mApplication, mDispatcher, "Bottom window",
4039 ADISPLAY_ID_DEFAULT);
4040 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
4041
4042 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
4043 }
4044
4045protected:
4046 std::shared_ptr<FakeApplicationHandle> mApplication;
4047 sp<FakeWindowHandle> mNoInputWindow;
4048 sp<FakeWindowHandle> mBottomWindow;
4049};
4050
4051TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
4052 PointF touchedPoint = {10, 10};
4053
4054 NotifyMotionArgs motionArgs =
4055 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4056 ADISPLAY_ID_DEFAULT, {touchedPoint});
4057 mDispatcher->notifyMotion(&motionArgs);
4058
4059 mNoInputWindow->assertNoEvents();
4060 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
4061 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
4062 // and therefore should prevent mBottomWindow from receiving touches
4063 mBottomWindow->assertNoEvents();
4064}
4065
4066/**
4067 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
4068 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
4069 */
4070TEST_F(InputDispatcherMultiWindowOcclusionTests,
4071 NoInputChannelFeature_DropsTouchesWithValidChannel) {
4072 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
4073 "Window with input channel and NO_INPUT_CHANNEL",
4074 ADISPLAY_ID_DEFAULT);
4075
4076 mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL);
4077 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
4078 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
4079
4080 PointF touchedPoint = {10, 10};
4081
4082 NotifyMotionArgs motionArgs =
4083 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4084 ADISPLAY_ID_DEFAULT, {touchedPoint});
4085 mDispatcher->notifyMotion(&motionArgs);
4086
4087 mNoInputWindow->assertNoEvents();
4088 mBottomWindow->assertNoEvents();
4089}
4090
Vishnu Nair958da932020-08-21 17:12:37 -07004091class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
4092protected:
4093 std::shared_ptr<FakeApplicationHandle> mApp;
4094 sp<FakeWindowHandle> mWindow;
4095 sp<FakeWindowHandle> mMirror;
4096
4097 virtual void SetUp() override {
4098 InputDispatcherTest::SetUp();
4099 mApp = std::make_shared<FakeApplicationHandle>();
4100 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
4101 mMirror = new FakeWindowHandle(mApp, mDispatcher, "TestWindowMirror", ADISPLAY_ID_DEFAULT,
4102 mWindow->getToken());
4103 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
4104 mWindow->setFocusable(true);
4105 mMirror->setFocusable(true);
4106 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4107 }
4108};
4109
4110TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
4111 // Request focus on a mirrored window
4112 setFocusedWindow(mMirror);
4113
4114 // window gets focused
4115 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004116 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4117 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004118 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
4119}
4120
4121// A focused & mirrored window remains focused only if the window and its mirror are both
4122// focusable.
4123TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
4124 setFocusedWindow(mMirror);
4125
4126 // window gets focused
4127 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004128 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4129 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004130 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004131 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4132 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004133 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4134
4135 mMirror->setFocusable(false);
4136 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4137
4138 // window loses focus since one of the windows associated with the token in not focusable
4139 mWindow->consumeFocusEvent(false);
4140
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004141 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4142 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004143 mWindow->assertNoEvents();
4144}
4145
4146// A focused & mirrored window remains focused until the window and its mirror both become
4147// invisible.
4148TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
4149 setFocusedWindow(mMirror);
4150
4151 // window gets focused
4152 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004153 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4154 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004155 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004156 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4157 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004158 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4159
4160 mMirror->setVisible(false);
4161 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4162
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004163 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4164 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004165 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004166 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4167 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004168 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4169
4170 mWindow->setVisible(false);
4171 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4172
4173 // window loses focus only after all windows associated with the token become invisible.
4174 mWindow->consumeFocusEvent(false);
4175
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004176 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4177 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004178 mWindow->assertNoEvents();
4179}
4180
4181// A focused & mirrored window remains focused until both windows are removed.
4182TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
4183 setFocusedWindow(mMirror);
4184
4185 // window gets focused
4186 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004187 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4188 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004189 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004190 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4191 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004192 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4193
4194 // single window is removed but the window token remains focused
4195 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
4196
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004197 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4198 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004199 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004200 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4201 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004202 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4203
4204 // Both windows are removed
4205 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
4206 mWindow->consumeFocusEvent(false);
4207
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004208 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4209 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004210 mWindow->assertNoEvents();
4211}
4212
4213// Focus request can be pending until one window becomes visible.
4214TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
4215 // Request focus on an invisible mirror.
4216 mWindow->setVisible(false);
4217 mMirror->setVisible(false);
4218 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4219 setFocusedWindow(mMirror);
4220
4221 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004222 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07004223 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004224 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07004225
4226 mMirror->setVisible(true);
4227 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4228
4229 // window gets focused
4230 mWindow->consumeFocusEvent(true);
4231 // window gets the pending key event
4232 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4233}
Prabir Pradhan99987712020-11-10 18:43:05 -08004234
4235class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
4236protected:
4237 std::shared_ptr<FakeApplicationHandle> mApp;
4238 sp<FakeWindowHandle> mWindow;
4239 sp<FakeWindowHandle> mSecondWindow;
4240
4241 void SetUp() override {
4242 InputDispatcherTest::SetUp();
4243 mApp = std::make_shared<FakeApplicationHandle>();
4244 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
4245 mWindow->setFocusable(true);
4246 mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
4247 mSecondWindow->setFocusable(true);
4248
4249 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
4250 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
4251
4252 setFocusedWindow(mWindow);
4253 mWindow->consumeFocusEvent(true);
4254 }
4255
4256 void notifyPointerCaptureChanged(bool enabled) {
4257 const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(enabled);
4258 mDispatcher->notifyPointerCaptureChanged(&args);
4259 }
4260
4261 void requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, bool enabled) {
4262 mDispatcher->requestPointerCapture(window->getToken(), enabled);
4263 mFakePolicy->waitForSetPointerCapture(enabled);
4264 notifyPointerCaptureChanged(enabled);
4265 window->consumeCaptureEvent(enabled);
4266 }
4267};
4268
4269TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
4270 // Ensure that capture cannot be obtained for unfocused windows.
4271 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
4272 mFakePolicy->assertSetPointerCaptureNotCalled();
4273 mSecondWindow->assertNoEvents();
4274
4275 // Ensure that capture can be enabled from the focus window.
4276 requestAndVerifyPointerCapture(mWindow, true);
4277
4278 // Ensure that capture cannot be disabled from a window that does not have capture.
4279 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
4280 mFakePolicy->assertSetPointerCaptureNotCalled();
4281
4282 // Ensure that capture can be disabled from the window with capture.
4283 requestAndVerifyPointerCapture(mWindow, false);
4284}
4285
4286TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
4287 requestAndVerifyPointerCapture(mWindow, true);
4288
4289 setFocusedWindow(mSecondWindow);
4290
4291 // Ensure that the capture disabled event was sent first.
4292 mWindow->consumeCaptureEvent(false);
4293 mWindow->consumeFocusEvent(false);
4294 mSecondWindow->consumeFocusEvent(true);
4295 mFakePolicy->waitForSetPointerCapture(false);
4296
4297 // Ensure that additional state changes from InputReader are not sent to the window.
4298 notifyPointerCaptureChanged(false);
4299 notifyPointerCaptureChanged(true);
4300 notifyPointerCaptureChanged(false);
4301 mWindow->assertNoEvents();
4302 mSecondWindow->assertNoEvents();
4303 mFakePolicy->assertSetPointerCaptureNotCalled();
4304}
4305
4306TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
4307 requestAndVerifyPointerCapture(mWindow, true);
4308
4309 // InputReader unexpectedly disables and enables pointer capture.
4310 notifyPointerCaptureChanged(false);
4311 notifyPointerCaptureChanged(true);
4312
4313 // Ensure that Pointer Capture is disabled.
Prabir Pradhan7d030382020-12-21 07:58:35 -08004314 mFakePolicy->waitForSetPointerCapture(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08004315 mWindow->consumeCaptureEvent(false);
4316 mWindow->assertNoEvents();
4317}
4318
Prabir Pradhan167e6d92021-02-04 16:18:17 -08004319TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
4320 requestAndVerifyPointerCapture(mWindow, true);
4321
4322 // The first window loses focus.
4323 setFocusedWindow(mSecondWindow);
4324 mFakePolicy->waitForSetPointerCapture(false);
4325 mWindow->consumeCaptureEvent(false);
4326
4327 // Request Pointer Capture from the second window before the notification from InputReader
4328 // arrives.
4329 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
4330 mFakePolicy->waitForSetPointerCapture(true);
4331
4332 // InputReader notifies Pointer Capture was disabled (because of the focus change).
4333 notifyPointerCaptureChanged(false);
4334
4335 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
4336 notifyPointerCaptureChanged(true);
4337
4338 mSecondWindow->consumeFocusEvent(true);
4339 mSecondWindow->consumeCaptureEvent(true);
4340}
4341
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004342class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
4343protected:
4344 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00004345
4346 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
4347 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
4348
4349 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
4350 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
4351
4352 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
4353 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
4354 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
4355 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
4356 MAXIMUM_OBSCURING_OPACITY);
4357
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004358 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004359 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004360 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004361
4362 sp<FakeWindowHandle> mTouchWindow;
4363
4364 virtual void SetUp() override {
4365 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004366 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004367 mDispatcher->setBlockUntrustedTouchesMode(android::os::BlockUntrustedTouchesMode::BLOCK);
4368 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
4369 }
4370
4371 virtual void TearDown() override {
4372 InputDispatcherTest::TearDown();
4373 mTouchWindow.clear();
4374 }
4375
4376 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name,
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004377 os::TouchOcclusionMode mode, float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004378 sp<FakeWindowHandle> window = getWindow(uid, name);
4379 window->setFlags(InputWindowInfo::Flag::NOT_TOUCHABLE);
4380 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004381 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004382 return window;
4383 }
4384
4385 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
4386 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
4387 sp<FakeWindowHandle> window =
4388 new FakeWindowHandle(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
4389 // Generate an arbitrary PID based on the UID
4390 window->setOwnerInfo(1777 + (uid % 10000), uid);
4391 return window;
4392 }
4393
4394 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
4395 NotifyMotionArgs args =
4396 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4397 ADISPLAY_ID_DEFAULT, points);
4398 mDispatcher->notifyMotion(&args);
4399 }
4400};
4401
4402TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004403 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004404 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004405 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004406
4407 touch();
4408
4409 mTouchWindow->assertNoEvents();
4410}
4411
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004412TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00004413 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
4414 const sp<FakeWindowHandle>& w =
4415 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
4416 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4417
4418 touch();
4419
4420 mTouchWindow->assertNoEvents();
4421}
4422
4423TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004424 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
4425 const sp<FakeWindowHandle>& w =
4426 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
4427 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4428
4429 touch();
4430
4431 w->assertNoEvents();
4432}
4433
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004434TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004435 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
4436 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004437
4438 touch();
4439
4440 mTouchWindow->consumeAnyMotionDown();
4441}
4442
4443TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004444 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004445 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004446 w->setFrame(Rect(0, 0, 50, 50));
4447 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004448
4449 touch({PointF{100, 100}});
4450
4451 mTouchWindow->consumeAnyMotionDown();
4452}
4453
4454TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004455 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004456 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004457 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4458
4459 touch();
4460
4461 mTouchWindow->consumeAnyMotionDown();
4462}
4463
4464TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
4465 const sp<FakeWindowHandle>& w =
4466 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4467 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004468
4469 touch();
4470
4471 mTouchWindow->consumeAnyMotionDown();
4472}
4473
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004474TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
4475 const sp<FakeWindowHandle>& w =
4476 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4477 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4478
4479 touch();
4480
4481 w->assertNoEvents();
4482}
4483
4484/**
4485 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
4486 * inside) while letting them pass-through. Note that even though touch passes through the occluding
4487 * window, the occluding window will still receive ACTION_OUTSIDE event.
4488 */
4489TEST_F(InputDispatcherUntrustedTouchesTest,
4490 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
4491 const sp<FakeWindowHandle>& w =
4492 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4493 w->addFlags(InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH);
4494 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4495
4496 touch();
4497
4498 w->consumeMotionOutside();
4499}
4500
4501TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
4502 const sp<FakeWindowHandle>& w =
4503 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4504 w->addFlags(InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH);
4505 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4506
4507 touch();
4508
4509 InputEvent* event = w->consume();
4510 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType());
4511 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
4512 EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getX());
4513 EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getY());
4514}
4515
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004516TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004517 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004518 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4519 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004520 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4521
4522 touch();
4523
4524 mTouchWindow->consumeAnyMotionDown();
4525}
4526
4527TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
4528 const sp<FakeWindowHandle>& w =
4529 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4530 MAXIMUM_OBSCURING_OPACITY);
4531 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004532
4533 touch();
4534
4535 mTouchWindow->consumeAnyMotionDown();
4536}
4537
4538TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004539 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004540 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4541 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004542 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4543
4544 touch();
4545
4546 mTouchWindow->assertNoEvents();
4547}
4548
4549TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
4550 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
4551 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004552 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
4553 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004554 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004555 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
4556 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004557 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
4558
4559 touch();
4560
4561 mTouchWindow->assertNoEvents();
4562}
4563
4564TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
4565 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
4566 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004567 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
4568 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004569 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004570 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
4571 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004572 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
4573
4574 touch();
4575
4576 mTouchWindow->consumeAnyMotionDown();
4577}
4578
4579TEST_F(InputDispatcherUntrustedTouchesTest,
4580 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
4581 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004582 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4583 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004584 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004585 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
4586 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004587 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
4588
4589 touch();
4590
4591 mTouchWindow->consumeAnyMotionDown();
4592}
4593
4594TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
4595 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004596 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4597 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004598 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004599 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
4600 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004601 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004602
4603 touch();
4604
4605 mTouchWindow->assertNoEvents();
4606}
4607
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004608TEST_F(InputDispatcherUntrustedTouchesTest,
4609 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
4610 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004611 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
4612 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004613 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004614 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4615 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004616 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
4617
4618 touch();
4619
4620 mTouchWindow->assertNoEvents();
4621}
4622
4623TEST_F(InputDispatcherUntrustedTouchesTest,
4624 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
4625 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004626 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
4627 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004628 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004629 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4630 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004631 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
4632
4633 touch();
4634
4635 mTouchWindow->consumeAnyMotionDown();
4636}
4637
4638TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
4639 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004640 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
4641 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004642 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4643
4644 touch();
4645
4646 mTouchWindow->consumeAnyMotionDown();
4647}
4648
4649TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
4650 const sp<FakeWindowHandle>& w =
4651 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
4652 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4653
4654 touch();
4655
4656 mTouchWindow->consumeAnyMotionDown();
4657}
4658
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00004659TEST_F(InputDispatcherUntrustedTouchesTest,
4660 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
4661 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
4662 const sp<FakeWindowHandle>& w =
4663 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
4664 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4665
4666 touch();
4667
4668 mTouchWindow->assertNoEvents();
4669}
4670
4671TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
4672 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
4673 const sp<FakeWindowHandle>& w =
4674 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
4675 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4676
4677 touch();
4678
4679 mTouchWindow->consumeAnyMotionDown();
4680}
4681
4682TEST_F(InputDispatcherUntrustedTouchesTest,
4683 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
4684 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
4685 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004686 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4687 OPACITY_ABOVE_THRESHOLD);
4688 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4689
4690 touch();
4691
4692 mTouchWindow->consumeAnyMotionDown();
4693}
4694
4695TEST_F(InputDispatcherUntrustedTouchesTest,
4696 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
4697 const sp<FakeWindowHandle>& w1 =
4698 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
4699 OPACITY_BELOW_THRESHOLD);
4700 const sp<FakeWindowHandle>& w2 =
4701 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4702 OPACITY_BELOW_THRESHOLD);
4703 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
4704
4705 touch();
4706
4707 mTouchWindow->assertNoEvents();
4708}
4709
4710/**
4711 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
4712 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
4713 * (which alone would result in allowing touches) does not affect the blocking behavior.
4714 */
4715TEST_F(InputDispatcherUntrustedTouchesTest,
4716 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
4717 const sp<FakeWindowHandle>& wB =
4718 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
4719 OPACITY_BELOW_THRESHOLD);
4720 const sp<FakeWindowHandle>& wC =
4721 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
4722 OPACITY_BELOW_THRESHOLD);
4723 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
4724
4725 touch();
4726
4727 mTouchWindow->assertNoEvents();
4728}
4729
4730/**
4731 * This test is testing that a window from a different UID but with same application token doesn't
4732 * block the touch. Apps can share the application token for close UI collaboration for example.
4733 */
4734TEST_F(InputDispatcherUntrustedTouchesTest,
4735 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
4736 const sp<FakeWindowHandle>& w =
4737 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
4738 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00004739 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4740
4741 touch();
4742
4743 mTouchWindow->consumeAnyMotionDown();
4744}
4745
arthurhungb89ccb02020-12-30 16:19:01 +08004746class InputDispatcherDragTests : public InputDispatcherTest {
4747protected:
4748 std::shared_ptr<FakeApplicationHandle> mApp;
4749 sp<FakeWindowHandle> mWindow;
4750 sp<FakeWindowHandle> mSecondWindow;
4751 sp<FakeWindowHandle> mDragWindow;
4752
4753 void SetUp() override {
4754 InputDispatcherTest::SetUp();
4755 mApp = std::make_shared<FakeApplicationHandle>();
4756 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
4757 mWindow->setFrame(Rect(0, 0, 100, 100));
4758 mWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
4759
4760 mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
4761 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
4762 mSecondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
4763
4764 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
4765 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
4766 }
4767
4768 // Start performing drag, we will create a drag window and transfer touch to it.
4769 void performDrag() {
4770 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4771 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4772 {50, 50}))
4773 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4774
4775 // Window should receive motion event.
4776 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4777
4778 // The drag window covers the entire display
4779 mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
4780 mDispatcher->setInputWindows(
4781 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
4782
4783 // Transfer touch focus to the drag window
4784 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
4785 true /* isDragDrop */);
4786 mWindow->consumeMotionCancel();
4787 mDragWindow->consumeMotionDown();
4788 }
4789};
4790
4791TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
4792 performDrag();
4793
4794 // Move on window.
4795 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4796 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4797 ADISPLAY_ID_DEFAULT, {50, 50}))
4798 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4799 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
4800 mWindow->consumeDragEvent(false, 50, 50);
4801 mSecondWindow->assertNoEvents();
4802
4803 // Move to another window.
4804 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4805 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4806 ADISPLAY_ID_DEFAULT, {150, 50}))
4807 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4808 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
4809 mWindow->consumeDragEvent(true, 150, 50);
4810 mSecondWindow->consumeDragEvent(false, 50, 50);
4811
4812 // Move back to original window.
4813 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4814 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4815 ADISPLAY_ID_DEFAULT, {50, 50}))
4816 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4817 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
4818 mWindow->consumeDragEvent(false, 50, 50);
4819 mSecondWindow->consumeDragEvent(true, -50, 50);
4820
4821 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4822 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
4823 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4824 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
4825 mWindow->assertNoEvents();
4826 mSecondWindow->assertNoEvents();
4827}
4828
arthurhungf452d0b2021-01-06 00:19:52 +08004829TEST_F(InputDispatcherDragTests, DragAndDrop) {
4830 performDrag();
4831
4832 // Move on window.
4833 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4834 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4835 ADISPLAY_ID_DEFAULT, {50, 50}))
4836 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4837 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
4838 mWindow->consumeDragEvent(false, 50, 50);
4839 mSecondWindow->assertNoEvents();
4840
4841 // Move to another window.
4842 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4843 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4844 ADISPLAY_ID_DEFAULT, {150, 50}))
4845 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4846 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
4847 mWindow->consumeDragEvent(true, 150, 50);
4848 mSecondWindow->consumeDragEvent(false, 50, 50);
4849
4850 // drop to another window.
4851 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4852 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4853 {150, 50}))
4854 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4855 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
4856 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
4857 mWindow->assertNoEvents();
4858 mSecondWindow->assertNoEvents();
4859}
4860
Garfield Tane84e6f92019-08-29 17:28:41 -07004861} // namespace android::inputdispatcher