blob: 50e185469feef897771bce0e227d7e83a6608909 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Garfield Tan0fc2fa72019-08-29 17:22:15 -070017#include "../dispatcher/InputDispatcher.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080018
Garfield Tan1c7bc862020-01-28 13:24:04 -080019#include <android-base/stringprintf.h>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070020#include <android-base/thread_annotations.h>
Robert Carr803535b2018-08-02 16:38:15 -070021#include <binder/Binder.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080022#include <gtest/gtest.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100023#include <input/Input.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080024#include <linux/input.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100025
Garfield Tan1c7bc862020-01-28 13:24:04 -080026#include <cinttypes>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070027#include <thread>
Garfield Tan1c7bc862020-01-28 13:24:04 -080028#include <unordered_set>
chaviwd1c23182019-12-20 18:44:56 -080029#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080030
Garfield Tan1c7bc862020-01-28 13:24:04 -080031using android::base::StringPrintf;
chaviw3277faf2021-05-19 16:45:23 -050032using android::gui::FocusRequest;
33using android::gui::TouchOcclusionMode;
34using android::gui::WindowInfo;
35using android::gui::WindowInfoHandle;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080036using android::os::InputEventInjectionResult;
37using android::os::InputEventInjectionSync;
Michael Wright44753b12020-07-08 13:48:11 +010038using namespace android::flag_operators;
Garfield Tan1c7bc862020-01-28 13:24:04 -080039
Garfield Tane84e6f92019-08-29 17:28:41 -070040namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080041
42// An arbitrary time value.
43static const nsecs_t ARBITRARY_TIME = 1234;
44
45// An arbitrary device id.
46static const int32_t DEVICE_ID = 1;
47
Jeff Brownf086ddb2014-02-11 14:28:48 -080048// An arbitrary display id.
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -080049static const int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
Jeff Brownf086ddb2014-02-11 14:28:48 -080050
Michael Wrightd02c5b62014-02-10 15:10:22 -080051// An arbitrary injector pid / uid pair that has permission to inject events.
52static const int32_t INJECTOR_PID = 999;
53static const int32_t INJECTOR_UID = 1001;
54
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +000055// An arbitrary pid of the gesture monitor window
56static constexpr int32_t MONITOR_PID = 2001;
57
chaviwd1c23182019-12-20 18:44:56 -080058struct PointF {
59 float x;
60 float y;
61};
Michael Wrightd02c5b62014-02-10 15:10:22 -080062
Gang Wang342c9272020-01-13 13:15:04 -050063/**
64 * Return a DOWN key event with KEYCODE_A.
65 */
66static KeyEvent getTestKeyEvent() {
67 KeyEvent event;
68
Garfield Tanfbe732e2020-01-24 11:26:14 -080069 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
70 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
71 ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -050072 return event;
73}
74
Michael Wrightd02c5b62014-02-10 15:10:22 -080075// --- FakeInputDispatcherPolicy ---
76
77class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
78 InputDispatcherConfiguration mConfig;
79
80protected:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100081 virtual ~FakeInputDispatcherPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -080082
83public:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100084 FakeInputDispatcherPolicy() {}
Jackal Guof9696682018-10-05 12:23:23 +080085
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080086 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080087 assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_KEY, args.eventTime, args.action,
88 args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080089 }
90
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080091 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args) {
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080092 assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_MOTION, args.eventTime, args.action,
93 args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080094 }
95
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -070096 void assertFilterInputEventWasNotCalled() {
97 std::scoped_lock lock(mLock);
98 ASSERT_EQ(nullptr, mFilteredEvent);
99 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800100
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800101 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700102 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800103 ASSERT_TRUE(mConfigurationChangedTime)
104 << "Timed out waiting for configuration changed call";
105 ASSERT_EQ(*mConfigurationChangedTime, when);
106 mConfigurationChangedTime = std::nullopt;
107 }
108
109 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700110 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800111 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800112 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800113 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
114 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
115 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
116 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
117 mLastNotifySwitch = std::nullopt;
118 }
119
chaviwfd6d3512019-03-25 13:23:49 -0700120 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700121 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800122 ASSERT_EQ(touchedToken, mOnPointerDownToken);
123 mOnPointerDownToken.clear();
124 }
125
126 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700127 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800128 ASSERT_TRUE(mOnPointerDownToken == nullptr)
129 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700130 }
131
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700132 // This function must be called soon after the expected ANR timer starts,
133 // because we are also checking how much time has passed.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500134 void assertNotifyNoFocusedWindowAnrWasCalled(
Chris Yea209fde2020-07-22 13:54:51 -0700135 std::chrono::nanoseconds timeout,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500136 const std::shared_ptr<InputApplicationHandle>& expectedApplication) {
137 std::shared_ptr<InputApplicationHandle> application;
138 { // acquire lock
139 std::unique_lock lock(mLock);
140 android::base::ScopedLockAssertion assumeLocked(mLock);
141 ASSERT_NO_FATAL_FAILURE(
142 application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock));
143 } // release lock
144 ASSERT_EQ(expectedApplication, application);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700145 }
146
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000147 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
148 const sp<IBinder>& expectedConnectionToken) {
149 sp<IBinder> connectionToken = getUnresponsiveWindowToken(timeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500150 ASSERT_EQ(expectedConnectionToken, connectionToken);
151 }
152
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000153 void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedConnectionToken) {
154 sp<IBinder> connectionToken = getResponsiveWindowToken();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500155 ASSERT_EQ(expectedConnectionToken, connectionToken);
156 }
157
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000158 void assertNotifyMonitorUnresponsiveWasCalled(std::chrono::nanoseconds timeout) {
159 int32_t pid = getUnresponsiveMonitorPid(timeout);
160 ASSERT_EQ(MONITOR_PID, pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500161 }
162
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000163 void assertNotifyMonitorResponsiveWasCalled() {
164 int32_t pid = getResponsiveMonitorPid();
165 ASSERT_EQ(MONITOR_PID, pid);
166 }
167
168 sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500169 std::unique_lock lock(mLock);
170 android::base::ScopedLockAssertion assumeLocked(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000171 return getAnrTokenLockedInterruptible(timeout, mAnrWindowTokens, lock);
172 }
173
174 sp<IBinder> getResponsiveWindowToken() {
175 std::unique_lock lock(mLock);
176 android::base::ScopedLockAssertion assumeLocked(mLock);
177 return getAnrTokenLockedInterruptible(0s, mResponsiveWindowTokens, lock);
178 }
179
180 int32_t getUnresponsiveMonitorPid(std::chrono::nanoseconds timeout) {
181 std::unique_lock lock(mLock);
182 android::base::ScopedLockAssertion assumeLocked(mLock);
183 return getAnrTokenLockedInterruptible(timeout, mAnrMonitorPids, lock);
184 }
185
186 int32_t getResponsiveMonitorPid() {
187 std::unique_lock lock(mLock);
188 android::base::ScopedLockAssertion assumeLocked(mLock);
189 return getAnrTokenLockedInterruptible(0s, mResponsiveMonitorPids, lock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500190 }
191
192 // All three ANR-related callbacks behave the same way, so we use this generic function to wait
193 // for a specific container to become non-empty. When the container is non-empty, return the
194 // first entry from the container and erase it.
195 template <class T>
196 T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage,
197 std::unique_lock<std::mutex>& lock) REQUIRES(mLock) {
198 const std::chrono::time_point start = std::chrono::steady_clock::now();
199 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700200
201 // If there is an ANR, Dispatcher won't be idle because there are still events
202 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
203 // before checking if ANR was called.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500204 // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need
205 // to provide it some time to act. 100ms seems reasonable.
206 mNotifyAnr.wait_for(lock, timeToWait,
207 [&storage]() REQUIRES(mLock) { return !storage.empty(); });
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700208 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500209 if (storage.empty()) {
210 ADD_FAILURE() << "Did not receive the ANR callback";
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000211 return {};
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700212 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700213 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
214 // the dispatcher started counting before this function was called
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700215 if (std::chrono::abs(timeout - waited) > 100ms) {
216 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
217 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
218 << "ms, but waited "
219 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
220 << "ms instead";
221 }
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500222 T token = storage.front();
223 storage.pop();
224 return token;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700225 }
226
227 void assertNotifyAnrWasNotCalled() {
228 std::scoped_lock lock(mLock);
229 ASSERT_TRUE(mAnrApplications.empty());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000230 ASSERT_TRUE(mAnrWindowTokens.empty());
231 ASSERT_TRUE(mAnrMonitorPids.empty());
232 ASSERT_TRUE(mResponsiveWindowTokens.empty())
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500233 << "ANR was not called, but please also consume the 'connection is responsive' "
234 "signal";
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000235 ASSERT_TRUE(mResponsiveMonitorPids.empty())
236 << "Monitor ANR was not called, but please also consume the 'monitor is responsive'"
237 " signal";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700238 }
239
Garfield Tan1c7bc862020-01-28 13:24:04 -0800240 void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) {
241 mConfig.keyRepeatTimeout = timeout;
242 mConfig.keyRepeatDelay = delay;
243 }
244
Prabir Pradhan99987712020-11-10 18:43:05 -0800245 void waitForSetPointerCapture(bool enabled) {
246 std::unique_lock lock(mLock);
247 base::ScopedLockAssertion assumeLocked(mLock);
248
249 if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms,
250 [this, enabled]() REQUIRES(mLock) {
251 return mPointerCaptureEnabled &&
252 *mPointerCaptureEnabled ==
253 enabled;
254 })) {
255 FAIL() << "Timed out waiting for setPointerCapture(" << enabled << ") to be called.";
256 }
257 mPointerCaptureEnabled.reset();
258 }
259
260 void assertSetPointerCaptureNotCalled() {
261 std::unique_lock lock(mLock);
262 base::ScopedLockAssertion assumeLocked(mLock);
263
264 if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) {
265 FAIL() << "Expected setPointerCapture(enabled) to not be called, but was called. "
266 "enabled = "
267 << *mPointerCaptureEnabled;
268 }
269 mPointerCaptureEnabled.reset();
270 }
271
arthurhungf452d0b2021-01-06 00:19:52 +0800272 void assertDropTargetEquals(const sp<IBinder>& targetToken) {
273 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800274 ASSERT_TRUE(mNotifyDropWindowWasCalled);
arthurhungf452d0b2021-01-06 00:19:52 +0800275 ASSERT_EQ(targetToken, mDropTargetWindowToken);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800276 mNotifyDropWindowWasCalled = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800277 }
278
Michael Wrightd02c5b62014-02-10 15:10:22 -0800279private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700280 std::mutex mLock;
281 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
282 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
283 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
284 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800285
Prabir Pradhan99987712020-11-10 18:43:05 -0800286 std::condition_variable mPointerCaptureChangedCondition;
287 std::optional<bool> mPointerCaptureEnabled GUARDED_BY(mLock);
288
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700289 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700290 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000291 std::queue<sp<IBinder>> mAnrWindowTokens GUARDED_BY(mLock);
292 std::queue<sp<IBinder>> mResponsiveWindowTokens GUARDED_BY(mLock);
293 std::queue<int32_t> mAnrMonitorPids GUARDED_BY(mLock);
294 std::queue<int32_t> mResponsiveMonitorPids GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700295 std::condition_variable mNotifyAnr;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700296
arthurhungf452d0b2021-01-06 00:19:52 +0800297 sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800298 bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800299
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600300 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700301 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800302 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800303 }
304
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000305 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700306 std::scoped_lock lock(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000307 mAnrWindowTokens.push(connectionToken);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700308 mNotifyAnr.notify_all();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500309 }
310
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000311 void notifyMonitorUnresponsive(int32_t pid, const std::string&) override {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500312 std::scoped_lock lock(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000313 mAnrMonitorPids.push(pid);
314 mNotifyAnr.notify_all();
315 }
316
317 void notifyWindowResponsive(const sp<IBinder>& connectionToken) override {
318 std::scoped_lock lock(mLock);
319 mResponsiveWindowTokens.push(connectionToken);
320 mNotifyAnr.notify_all();
321 }
322
323 void notifyMonitorResponsive(int32_t pid) override {
324 std::scoped_lock lock(mLock);
325 mResponsiveMonitorPids.push(pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500326 mNotifyAnr.notify_all();
327 }
328
329 void notifyNoFocusedWindowAnr(
330 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
331 std::scoped_lock lock(mLock);
332 mAnrApplications.push(applicationHandle);
333 mNotifyAnr.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800334 }
335
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600336 void notifyInputChannelBroken(const sp<IBinder>&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800337
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600338 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700339
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600340 void notifyUntrustedTouch(const std::string& obscuringPackage) override {}
Chris Yef59a2f42020-10-16 12:55:26 -0700341 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
342 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
343 const std::vector<float>& values) override {}
344
345 void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType,
346 InputDeviceSensorAccuracy accuracy) override {}
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000347
Chris Yefb552902021-02-03 17:18:37 -0800348 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
349
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600350 void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351 *outConfig = mConfig;
352 }
353
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600354 bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700355 std::scoped_lock lock(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800356 switch (inputEvent->getType()) {
357 case AINPUT_EVENT_TYPE_KEY: {
358 const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800359 mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800360 break;
361 }
362
363 case AINPUT_EVENT_TYPE_MOTION: {
364 const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800365 mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800366 break;
367 }
368 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800369 return true;
370 }
371
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600372 void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800373
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600374 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800375
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600376 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800377 return 0;
378 }
379
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600380 bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800381 return false;
382 }
383
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600384 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
385 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700386 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800387 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
388 * essentially a passthrough for notifySwitch.
389 */
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800390 mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800391 }
392
Sean Stoutb4e0a592021-02-23 07:34:53 -0800393 void pokeUserActivity(nsecs_t, int32_t, int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800394
Prabir Pradhan93f342c2021-03-11 15:05:30 -0800395 bool checkInjectEventsPermissionNonReentrant(int32_t pid, int32_t uid) override {
396 return pid == INJECTOR_PID && uid == INJECTOR_UID;
397 }
Jackal Guof9696682018-10-05 12:23:23 +0800398
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600399 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700400 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700401 mOnPointerDownToken = newToken;
402 }
403
Prabir Pradhan99987712020-11-10 18:43:05 -0800404 void setPointerCapture(bool enabled) override {
405 std::scoped_lock lock(mLock);
406 mPointerCaptureEnabled = {enabled};
407 mPointerCaptureChangedCondition.notify_all();
408 }
409
arthurhungf452d0b2021-01-06 00:19:52 +0800410 void notifyDropWindow(const sp<IBinder>& token, float x, float y) override {
411 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800412 mNotifyDropWindowWasCalled = true;
arthurhungf452d0b2021-01-06 00:19:52 +0800413 mDropTargetWindowToken = token;
414 }
415
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800416 void assertFilterInputEventWasCalled(int type, nsecs_t eventTime, int32_t action,
417 int32_t displayId) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700418 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800419 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
420 ASSERT_EQ(mFilteredEvent->getType(), type);
421
422 if (type == AINPUT_EVENT_TYPE_KEY) {
423 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*mFilteredEvent);
424 EXPECT_EQ(keyEvent.getEventTime(), eventTime);
425 EXPECT_EQ(keyEvent.getAction(), action);
426 EXPECT_EQ(keyEvent.getDisplayId(), displayId);
427 } else if (type == AINPUT_EVENT_TYPE_MOTION) {
428 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*mFilteredEvent);
429 EXPECT_EQ(motionEvent.getEventTime(), eventTime);
430 EXPECT_EQ(motionEvent.getAction(), action);
431 EXPECT_EQ(motionEvent.getDisplayId(), displayId);
432 } else {
433 FAIL() << "Unknown type: " << type;
434 }
435
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800436 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800437 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800438};
439
Michael Wrightd02c5b62014-02-10 15:10:22 -0800440// --- InputDispatcherTest ---
441
442class InputDispatcherTest : public testing::Test {
443protected:
444 sp<FakeInputDispatcherPolicy> mFakePolicy;
445 sp<InputDispatcher> mDispatcher;
446
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000447 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800448 mFakePolicy = new FakeInputDispatcherPolicy();
449 mDispatcher = new InputDispatcher(mFakePolicy);
Arthur Hungb92218b2018-08-14 12:00:21 +0800450 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000451 // Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700452 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800453 }
454
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000455 void TearDown() override {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700456 ASSERT_EQ(OK, mDispatcher->stop());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800457 mFakePolicy.clear();
458 mDispatcher.clear();
459 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700460
461 /**
462 * Used for debugging when writing the test
463 */
464 void dumpDispatcherState() {
465 std::string dump;
466 mDispatcher->dump(dump);
467 std::stringstream ss(dump);
468 std::string to;
469
470 while (std::getline(ss, to, '\n')) {
471 ALOGE("%s", to.c_str());
472 }
473 }
Vishnu Nair958da932020-08-21 17:12:37 -0700474
chaviw3277faf2021-05-19 16:45:23 -0500475 void setFocusedWindow(const sp<WindowInfoHandle>& window,
476 const sp<WindowInfoHandle>& focusedWindow = nullptr) {
Vishnu Nair958da932020-08-21 17:12:37 -0700477 FocusRequest request;
478 request.token = window->getToken();
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +0000479 request.windowName = window->getName();
Vishnu Nair958da932020-08-21 17:12:37 -0700480 if (focusedWindow) {
481 request.focusedToken = focusedWindow->getToken();
482 }
483 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
484 request.displayId = window->getInfo()->displayId;
485 mDispatcher->setFocusedWindow(request);
486 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800487};
488
Michael Wrightd02c5b62014-02-10 15:10:22 -0800489TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
490 KeyEvent event;
491
492 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800493 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
494 INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600495 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
496 ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800497 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700498 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800499 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800500 << "Should reject key events with undefined action.";
501
502 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800503 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
504 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600505 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800506 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700507 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800508 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800509 << "Should reject key events with ACTION_MULTIPLE.";
510}
511
512TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
513 MotionEvent event;
514 PointerProperties pointerProperties[MAX_POINTERS + 1];
515 PointerCoords pointerCoords[MAX_POINTERS + 1];
516 for (int i = 0; i <= MAX_POINTERS; i++) {
517 pointerProperties[i].clear();
518 pointerProperties[i].id = i;
519 pointerCoords[i].clear();
520 }
521
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800522 // Some constants commonly used below
523 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
524 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
525 constexpr int32_t metaState = AMETA_NONE;
526 constexpr MotionClassification classification = MotionClassification::NONE;
527
chaviw9eaa22c2020-07-01 16:21:27 -0700528 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800529 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800530 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
chaviw9eaa22c2020-07-01 16:21:27 -0700531 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification,
532 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700533 AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0,
534 INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700535 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800536 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700537 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800538 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800539 << "Should reject motion events with undefined action.";
540
541 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800542 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700543 AMOTION_EVENT_ACTION_POINTER_DOWN |
544 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700545 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
546 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700547 ui::Transform::ROT_0, INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE,
548 ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500549 /*pointerCount*/ 1, pointerProperties, 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 large.";
554
Garfield Tanfbe732e2020-01-24 11:26:14 -0800555 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700556 AMOTION_EVENT_ACTION_POINTER_DOWN |
557 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700558 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
559 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700560 ui::Transform::ROT_0, INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE,
561 ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500562 /*pointerCount*/ 1, pointerProperties, 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 down index too small.";
567
568 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800569 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700570 AMOTION_EVENT_ACTION_POINTER_UP |
571 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700572 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
573 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700574 ui::Transform::ROT_0, INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE,
575 ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500576 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800577 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700578 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800579 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800580 << "Should reject motion events with pointer up index too large.";
581
Garfield Tanfbe732e2020-01-24 11:26:14 -0800582 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700583 AMOTION_EVENT_ACTION_POINTER_UP |
584 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700585 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
586 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700587 ui::Transform::ROT_0, INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE,
588 ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500589 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800590 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700591 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800592 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800593 << "Should reject motion events with pointer up index too small.";
594
595 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800596 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
597 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700598 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700599 AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0,
600 INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700601 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800602 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700603 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800604 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800605 << "Should reject motion events with 0 pointers.";
606
Garfield Tanfbe732e2020-01-24 11:26:14 -0800607 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
608 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700609 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700610 AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0,
611 INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700612 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800613 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700614 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800615 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800616 << "Should reject motion events with more than MAX_POINTERS pointers.";
617
618 // Rejects motion events with invalid pointer ids.
619 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800620 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
621 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700622 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700623 AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0,
624 INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700625 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800626 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700627 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800628 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800629 << "Should reject motion events with pointer ids less than 0.";
630
631 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800632 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
633 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700634 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700635 AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0,
636 INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700637 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800638 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700639 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800640 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800641 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
642
643 // Rejects motion events with duplicate pointer ids.
644 pointerProperties[0].id = 1;
645 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800646 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
647 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700648 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700649 AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0,
650 INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700651 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800652 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700653 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800654 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800655 << "Should reject motion events with duplicate pointer ids.";
656}
657
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800658/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
659
660TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
661 constexpr nsecs_t eventTime = 20;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800662 NotifyConfigurationChangedArgs args(10 /*id*/, eventTime);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800663 mDispatcher->notifyConfigurationChanged(&args);
664 ASSERT_TRUE(mDispatcher->waitForIdle());
665
666 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
667}
668
669TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800670 NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/,
671 2 /*switchMask*/);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800672 mDispatcher->notifySwitch(&args);
673
674 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
675 args.policyFlags |= POLICY_FLAG_TRUSTED;
676 mFakePolicy->assertNotifySwitchWasCalled(args);
677}
678
Arthur Hungb92218b2018-08-14 12:00:21 +0800679// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700680static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700681static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s;
Arthur Hungb92218b2018-08-14 12:00:21 +0800682
683class FakeApplicationHandle : public InputApplicationHandle {
684public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700685 FakeApplicationHandle() {
686 mInfo.name = "Fake Application";
687 mInfo.token = new BBinder();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500688 mInfo.dispatchingTimeoutMillis =
689 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700690 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800691 virtual ~FakeApplicationHandle() {}
692
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000693 virtual bool updateInfo() override { return true; }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700694
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500695 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
696 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700697 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800698};
699
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800700class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800701public:
Garfield Tan15601662020-09-22 15:32:38 -0700702 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800703 : mName(name) {
Garfield Tan15601662020-09-22 15:32:38 -0700704 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800705 }
706
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800707 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700708 InputEvent* event;
709 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
710 if (!consumeSeq) {
711 return nullptr;
712 }
713 finishEvent(*consumeSeq);
714 return event;
715 }
716
717 /**
718 * Receive an event without acknowledging it.
719 * Return the sequence number that could later be used to send finished signal.
720 */
721 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800722 uint32_t consumeSeq;
723 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800724
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800725 std::chrono::time_point start = std::chrono::steady_clock::now();
726 status_t status = WOULD_BLOCK;
727 while (status == WOULD_BLOCK) {
chaviw81e2bb92019-12-18 15:03:51 -0800728 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800729 &event);
730 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
731 if (elapsed > 100ms) {
732 break;
733 }
734 }
735
736 if (status == WOULD_BLOCK) {
737 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700738 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800739 }
740
741 if (status != OK) {
742 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700743 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800744 }
745 if (event == nullptr) {
746 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700747 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800748 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700749 if (outEvent != nullptr) {
750 *outEvent = event;
751 }
752 return consumeSeq;
753 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800754
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700755 /**
756 * To be used together with "receiveEvent" to complete the consumption of an event.
757 */
758 void finishEvent(uint32_t consumeSeq) {
759 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
760 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800761 }
762
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000763 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
764 const status_t status = mConsumer->sendTimeline(inputEventId, timeline);
765 ASSERT_EQ(OK, status);
766 }
767
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000768 void consumeEvent(int32_t expectedEventType, int32_t expectedAction,
769 std::optional<int32_t> expectedDisplayId,
770 std::optional<int32_t> expectedFlags) {
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800771 InputEvent* event = consume();
772
773 ASSERT_NE(nullptr, event) << mName.c_str()
774 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800775 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700776 << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType)
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800777 << " event, got " << inputEventTypeToString(event->getType()) << " event";
Arthur Hungb92218b2018-08-14 12:00:21 +0800778
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000779 if (expectedDisplayId.has_value()) {
780 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
781 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800782
Tiger Huang8664f8c2018-10-11 19:14:35 +0800783 switch (expectedEventType) {
784 case AINPUT_EVENT_TYPE_KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800785 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
786 EXPECT_EQ(expectedAction, keyEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000787 if (expectedFlags.has_value()) {
788 EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags());
789 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800790 break;
791 }
792 case AINPUT_EVENT_TYPE_MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800793 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
794 EXPECT_EQ(expectedAction, motionEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000795 if (expectedFlags.has_value()) {
796 EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags());
797 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800798 break;
799 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100800 case AINPUT_EVENT_TYPE_FOCUS: {
801 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
802 }
Prabir Pradhan99987712020-11-10 18:43:05 -0800803 case AINPUT_EVENT_TYPE_CAPTURE: {
804 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
805 }
arthurhungb89ccb02020-12-30 16:19:01 +0800806 case AINPUT_EVENT_TYPE_DRAG: {
807 FAIL() << "Use 'consumeDragEvent' for DRAG events";
808 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800809 default: {
810 FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType;
811 }
812 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800813 }
814
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100815 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
816 InputEvent* event = consume();
817 ASSERT_NE(nullptr, event) << mName.c_str()
818 << ": consumer should have returned non-NULL event.";
819 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
820 << "Got " << inputEventTypeToString(event->getType())
821 << " event instead of FOCUS event";
822
823 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
824 << mName.c_str() << ": event displayId should always be NONE.";
825
826 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
827 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
828 EXPECT_EQ(inTouchMode, focusEvent->getInTouchMode());
829 }
830
Prabir Pradhan99987712020-11-10 18:43:05 -0800831 void consumeCaptureEvent(bool hasCapture) {
832 const InputEvent* event = consume();
833 ASSERT_NE(nullptr, event) << mName.c_str()
834 << ": consumer should have returned non-NULL event.";
835 ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType())
836 << "Got " << inputEventTypeToString(event->getType())
837 << " event instead of CAPTURE event";
838
839 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
840 << mName.c_str() << ": event displayId should always be NONE.";
841
842 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
843 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
844 }
845
arthurhungb89ccb02020-12-30 16:19:01 +0800846 void consumeDragEvent(bool isExiting, float x, float y) {
847 const InputEvent* event = consume();
848 ASSERT_NE(nullptr, event) << mName.c_str()
849 << ": consumer should have returned non-NULL event.";
850 ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType())
851 << "Got " << inputEventTypeToString(event->getType())
852 << " event instead of DRAG event";
853
854 EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
855 << mName.c_str() << ": event displayId should always be NONE.";
856
857 const auto& dragEvent = static_cast<const DragEvent&>(*event);
858 EXPECT_EQ(isExiting, dragEvent.isExiting());
859 EXPECT_EQ(x, dragEvent.getX());
860 EXPECT_EQ(y, dragEvent.getY());
861 }
862
chaviwd1c23182019-12-20 18:44:56 -0800863 void assertNoEvents() {
864 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700865 if (event == nullptr) {
866 return;
867 }
868 if (event->getType() == AINPUT_EVENT_TYPE_KEY) {
869 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
870 ADD_FAILURE() << "Received key event "
871 << KeyEvent::actionToString(keyEvent.getAction());
872 } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) {
873 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
874 ADD_FAILURE() << "Received motion event "
875 << MotionEvent::actionToString(motionEvent.getAction());
876 } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) {
877 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
878 ADD_FAILURE() << "Received focus event, hasFocus = "
879 << (focusEvent.getHasFocus() ? "true" : "false");
Prabir Pradhan99987712020-11-10 18:43:05 -0800880 } else if (event->getType() == AINPUT_EVENT_TYPE_CAPTURE) {
881 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
882 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
883 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700884 }
885 FAIL() << mName.c_str()
886 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -0800887 }
888
889 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
890
891protected:
892 std::unique_ptr<InputConsumer> mConsumer;
893 PreallocatedInputEventFactory mEventFactory;
894
895 std::string mName;
896};
897
chaviw3277faf2021-05-19 16:45:23 -0500898class FakeWindowHandle : public WindowInfoHandle {
chaviwd1c23182019-12-20 18:44:56 -0800899public:
900 static const int32_t WIDTH = 600;
901 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -0800902
Chris Yea209fde2020-07-22 13:54:51 -0700903 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
chaviwd1c23182019-12-20 18:44:56 -0800904 const sp<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500905 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -0800906 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500907 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -0700908 base::Result<std::unique_ptr<InputChannel>> channel =
909 dispatcher->createInputChannel(name);
910 token = (*channel)->getConnectionToken();
911 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -0800912 }
913
914 inputApplicationHandle->updateInfo();
915 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
916
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500917 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700918 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -0800919 mInfo.name = name;
chaviw3277faf2021-05-19 16:45:23 -0500920 mInfo.type = WindowInfo::Type::APPLICATION;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500921 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000922 mInfo.alpha = 1.0;
chaviwd1c23182019-12-20 18:44:56 -0800923 mInfo.frameLeft = 0;
924 mInfo.frameTop = 0;
925 mInfo.frameRight = WIDTH;
926 mInfo.frameBottom = HEIGHT;
chaviw1ff3d1e2020-07-01 15:53:47 -0700927 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -0800928 mInfo.globalScaleFactor = 1.0;
929 mInfo.touchableRegion.clear();
930 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
931 mInfo.visible = true;
Vishnu Nair47074b82020-08-14 11:54:47 -0700932 mInfo.focusable = false;
chaviwd1c23182019-12-20 18:44:56 -0800933 mInfo.hasWallpaper = false;
934 mInfo.paused = false;
chaviwd1c23182019-12-20 18:44:56 -0800935 mInfo.ownerPid = INJECTOR_PID;
936 mInfo.ownerUid = INJECTOR_UID;
chaviwd1c23182019-12-20 18:44:56 -0800937 mInfo.displayId = displayId;
938 }
939
Vishnu Nair47074b82020-08-14 11:54:47 -0700940 void setFocusable(bool focusable) { mInfo.focusable = focusable; }
chaviwd1c23182019-12-20 18:44:56 -0800941
Vishnu Nair958da932020-08-21 17:12:37 -0700942 void setVisible(bool visible) { mInfo.visible = visible; }
943
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700944 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500945 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700946 }
947
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700948 void setPaused(bool paused) { mInfo.paused = paused; }
949
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000950 void setAlpha(float alpha) { mInfo.alpha = alpha; }
951
chaviw3277faf2021-05-19 16:45:23 -0500952 void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; }
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000953
Bernardo Rufino7393d172021-02-26 13:56:11 +0000954 void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
955
chaviwd1c23182019-12-20 18:44:56 -0800956 void setFrame(const Rect& frame) {
957 mInfo.frameLeft = frame.left;
958 mInfo.frameTop = frame.top;
959 mInfo.frameRight = frame.right;
960 mInfo.frameBottom = frame.bottom;
arthurhungb89ccb02020-12-30 16:19:01 +0800961 mInfo.transform.set(-frame.left, -frame.top);
chaviwd1c23182019-12-20 18:44:56 -0800962 mInfo.touchableRegion.clear();
963 mInfo.addTouchableRegion(frame);
964 }
965
chaviw3277faf2021-05-19 16:45:23 -0500966 void addFlags(Flags<WindowInfo::Flag> flags) { mInfo.flags |= flags; }
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +0000967
chaviw3277faf2021-05-19 16:45:23 -0500968 void setFlags(Flags<WindowInfo::Flag> flags) { mInfo.flags = flags; }
chaviwd1c23182019-12-20 18:44:56 -0800969
chaviw3277faf2021-05-19 16:45:23 -0500970 void setInputFeatures(WindowInfo::Feature features) { mInfo.inputFeatures = features; }
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500971
chaviw9eaa22c2020-07-01 16:21:27 -0700972 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
973 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
974 }
975
976 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -0700977
yunho.shinf4a80b82020-11-16 21:13:57 +0900978 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
979
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800980 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
981 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
982 expectedFlags);
983 }
984
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700985 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
986 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
987 }
988
Svet Ganov5d3bc372020-01-26 23:11:07 -0800989 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000990 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800991 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId,
992 expectedFlags);
993 }
994
995 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000996 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800997 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId,
998 expectedFlags);
999 }
1000
1001 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001002 int32_t expectedFlags = 0) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001003 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
1004 }
1005
1006 void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
1007 std::optional<int32_t> expectedFlags = std::nullopt) {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001008 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
1009 expectedFlags);
1010 }
1011
Svet Ganov5d3bc372020-01-26 23:11:07 -08001012 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001013 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1014 int32_t expectedFlags = 0) {
1015 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
1016 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001017 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
1018 }
1019
1020 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001021 int32_t expectedFlags = 0) {
1022 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
1023 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001024 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
1025 }
1026
1027 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001028 int32_t expectedFlags = 0) {
Michael Wright3a240c42019-12-10 20:53:41 +00001029 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
1030 expectedFlags);
1031 }
1032
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001033 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1034 int32_t expectedFlags = 0) {
1035 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
1036 expectedFlags);
1037 }
1038
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001039 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
1040 ASSERT_NE(mInputReceiver, nullptr)
1041 << "Cannot consume events from a window with no receiver";
1042 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
1043 }
1044
Prabir Pradhan99987712020-11-10 18:43:05 -08001045 void consumeCaptureEvent(bool hasCapture) {
1046 ASSERT_NE(mInputReceiver, nullptr)
1047 << "Cannot consume events from a window with no receiver";
1048 mInputReceiver->consumeCaptureEvent(hasCapture);
1049 }
1050
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001051 void consumeEvent(int32_t expectedEventType, int32_t expectedAction,
1052 std::optional<int32_t> expectedDisplayId,
1053 std::optional<int32_t> expectedFlags) {
chaviwd1c23182019-12-20 18:44:56 -08001054 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
1055 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
1056 expectedFlags);
1057 }
1058
arthurhungb89ccb02020-12-30 16:19:01 +08001059 void consumeDragEvent(bool isExiting, float x, float y) {
1060 mInputReceiver->consumeDragEvent(isExiting, x, y);
1061 }
1062
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001063 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001064 if (mInputReceiver == nullptr) {
1065 ADD_FAILURE() << "Invalid receive event on window with no receiver";
1066 return std::nullopt;
1067 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001068 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001069 }
1070
1071 void finishEvent(uint32_t sequenceNum) {
1072 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1073 mInputReceiver->finishEvent(sequenceNum);
1074 }
1075
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001076 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
1077 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1078 mInputReceiver->sendTimeline(inputEventId, timeline);
1079 }
1080
chaviwaf87b3e2019-10-01 16:59:28 -07001081 InputEvent* consume() {
1082 if (mInputReceiver == nullptr) {
1083 return nullptr;
1084 }
1085 return mInputReceiver->consume();
1086 }
1087
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001088 MotionEvent* consumeMotion() {
1089 InputEvent* event = consume();
1090 if (event == nullptr) {
1091 ADD_FAILURE() << "Consume failed : no event";
1092 return nullptr;
1093 }
1094 if (event->getType() != AINPUT_EVENT_TYPE_MOTION) {
1095 ADD_FAILURE() << "Instead of motion event, got "
1096 << inputEventTypeToString(event->getType());
1097 return nullptr;
1098 }
1099 return static_cast<MotionEvent*>(event);
1100 }
1101
Arthur Hungb92218b2018-08-14 12:00:21 +08001102 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001103 if (mInputReceiver == nullptr &&
chaviw3277faf2021-05-19 16:45:23 -05001104 mInfo.inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL)) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001105 return; // Can't receive events if the window does not have input channel
1106 }
1107 ASSERT_NE(nullptr, mInputReceiver)
1108 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -08001109 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +08001110 }
1111
chaviwaf87b3e2019-10-01 16:59:28 -07001112 sp<IBinder> getToken() { return mInfo.token; }
1113
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001114 const std::string& getName() { return mName; }
1115
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001116 void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) {
1117 mInfo.ownerPid = ownerPid;
1118 mInfo.ownerUid = ownerUid;
1119 }
1120
chaviwd1c23182019-12-20 18:44:56 -08001121private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001122 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -08001123 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001124 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001125};
1126
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001127std::atomic<int32_t> FakeWindowHandle::sId{1};
1128
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001129static InputEventInjectionResult injectKey(
1130 const sp<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
1131 int32_t displayId = ADISPLAY_ID_NONE,
1132 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001133 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
1134 bool allowKeyRepeat = true) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001135 KeyEvent event;
1136 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1137
1138 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001139 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001140 INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE,
1141 repeatCount, currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001142
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001143 int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
1144 if (!allowKeyRepeat) {
1145 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
1146 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001147 // Inject event until dispatch out.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001148 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, syncMode,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001149 injectionTimeout, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001150}
1151
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001152static InputEventInjectionResult injectKeyDown(const sp<InputDispatcher>& dispatcher,
1153 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001154 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId);
1155}
1156
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001157// Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without
1158// sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it
1159// has to be woken up to process the repeating key.
1160static InputEventInjectionResult injectKeyDownNoRepeat(const sp<InputDispatcher>& dispatcher,
1161 int32_t displayId = ADISPLAY_ID_NONE) {
1162 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId,
1163 InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT,
1164 /* allowKeyRepeat */ false);
1165}
1166
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001167static InputEventInjectionResult injectKeyUp(const sp<InputDispatcher>& dispatcher,
1168 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001169 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId);
1170}
1171
Garfield Tandf26e862020-07-01 20:18:19 -07001172class PointerBuilder {
1173public:
1174 PointerBuilder(int32_t id, int32_t toolType) {
1175 mProperties.clear();
1176 mProperties.id = id;
1177 mProperties.toolType = toolType;
1178 mCoords.clear();
1179 }
1180
1181 PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); }
1182
1183 PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); }
1184
1185 PointerBuilder& axis(int32_t axis, float value) {
1186 mCoords.setAxisValue(axis, value);
1187 return *this;
1188 }
1189
1190 PointerProperties buildProperties() const { return mProperties; }
1191
1192 PointerCoords buildCoords() const { return mCoords; }
1193
1194private:
1195 PointerProperties mProperties;
1196 PointerCoords mCoords;
1197};
1198
1199class MotionEventBuilder {
1200public:
1201 MotionEventBuilder(int32_t action, int32_t source) {
1202 mAction = action;
1203 mSource = source;
1204 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1205 }
1206
1207 MotionEventBuilder& eventTime(nsecs_t eventTime) {
1208 mEventTime = eventTime;
1209 return *this;
1210 }
1211
1212 MotionEventBuilder& displayId(int32_t displayId) {
1213 mDisplayId = displayId;
1214 return *this;
1215 }
1216
1217 MotionEventBuilder& actionButton(int32_t actionButton) {
1218 mActionButton = actionButton;
1219 return *this;
1220 }
1221
arthurhung6d4bed92021-03-17 11:59:33 +08001222 MotionEventBuilder& buttonState(int32_t buttonState) {
1223 mButtonState = buttonState;
Garfield Tandf26e862020-07-01 20:18:19 -07001224 return *this;
1225 }
1226
1227 MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) {
1228 mRawXCursorPosition = rawXCursorPosition;
1229 return *this;
1230 }
1231
1232 MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) {
1233 mRawYCursorPosition = rawYCursorPosition;
1234 return *this;
1235 }
1236
1237 MotionEventBuilder& pointer(PointerBuilder pointer) {
1238 mPointers.push_back(pointer);
1239 return *this;
1240 }
1241
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001242 MotionEventBuilder& addFlag(uint32_t flags) {
1243 mFlags |= flags;
1244 return *this;
1245 }
1246
Garfield Tandf26e862020-07-01 20:18:19 -07001247 MotionEvent build() {
1248 std::vector<PointerProperties> pointerProperties;
1249 std::vector<PointerCoords> pointerCoords;
1250 for (const PointerBuilder& pointer : mPointers) {
1251 pointerProperties.push_back(pointer.buildProperties());
1252 pointerCoords.push_back(pointer.buildCoords());
1253 }
1254
1255 // Set mouse cursor position for the most common cases to avoid boilerplate.
1256 if (mSource == AINPUT_SOURCE_MOUSE &&
1257 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) &&
1258 mPointers.size() == 1) {
1259 mRawXCursorPosition = pointerCoords[0].getX();
1260 mRawYCursorPosition = pointerCoords[0].getY();
1261 }
1262
1263 MotionEvent event;
chaviw9eaa22c2020-07-01 16:21:27 -07001264 ui::Transform identityTransform;
Garfield Tandf26e862020-07-01 20:18:19 -07001265 event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001266 mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE,
chaviw9eaa22c2020-07-01 16:21:27 -07001267 mButtonState, MotionClassification::NONE, identityTransform,
1268 /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition,
Evan Rosky09576692021-07-01 12:22:09 -07001269 mRawYCursorPosition, mDisplayOrientation, mDisplayWidth, mDisplayHeight,
1270 mEventTime, mEventTime, mPointers.size(), pointerProperties.data(),
1271 pointerCoords.data());
Garfield Tandf26e862020-07-01 20:18:19 -07001272
1273 return event;
1274 }
1275
1276private:
1277 int32_t mAction;
1278 int32_t mSource;
1279 nsecs_t mEventTime;
1280 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1281 int32_t mActionButton{0};
1282 int32_t mButtonState{0};
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001283 int32_t mFlags{0};
Garfield Tandf26e862020-07-01 20:18:19 -07001284 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1285 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
Evan Rosky09576692021-07-01 12:22:09 -07001286 uint32_t mDisplayOrientation{ui::Transform::ROT_0};
chaviw3277faf2021-05-19 16:45:23 -05001287 int32_t mDisplayWidth{INVALID_DISPLAY_SIZE};
1288 int32_t mDisplayHeight{INVALID_DISPLAY_SIZE};
Garfield Tandf26e862020-07-01 20:18:19 -07001289
1290 std::vector<PointerBuilder> mPointers;
1291};
1292
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001293static InputEventInjectionResult injectMotionEvent(
Garfield Tandf26e862020-07-01 20:18:19 -07001294 const sp<InputDispatcher>& dispatcher, const MotionEvent& event,
1295 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001296 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT) {
Garfield Tandf26e862020-07-01 20:18:19 -07001297 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, injectionMode,
1298 injectionTimeout,
1299 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
1300}
1301
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001302static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001303 const sp<InputDispatcher>& dispatcher, int32_t action, int32_t source, int32_t displayId,
1304 const PointF& position,
1305 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001306 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1307 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001308 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001309 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC)) {
Garfield Tandf26e862020-07-01 20:18:19 -07001310 MotionEvent event = MotionEventBuilder(action, source)
1311 .displayId(displayId)
1312 .eventTime(eventTime)
1313 .rawXCursorPosition(cursorPosition.x)
1314 .rawYCursorPosition(cursorPosition.y)
1315 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1316 .x(position.x)
1317 .y(position.y))
1318 .build();
Arthur Hungb92218b2018-08-14 12:00:21 +08001319
1320 // Inject event until dispatch out.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001321 return injectMotionEvent(dispatcher, event, injectionTimeout, injectionMode);
Arthur Hungb92218b2018-08-14 12:00:21 +08001322}
1323
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001324static InputEventInjectionResult injectMotionDown(const sp<InputDispatcher>& dispatcher,
1325 int32_t source, int32_t displayId,
1326 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001327 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001328}
1329
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001330static InputEventInjectionResult injectMotionUp(const sp<InputDispatcher>& dispatcher,
1331 int32_t source, int32_t displayId,
1332 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001333 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001334}
1335
Jackal Guof9696682018-10-05 12:23:23 +08001336static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1337 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1338 // Define a valid key event.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001339 NotifyKeyArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1340 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A,
1341 KEY_A, AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001342
1343 return args;
1344}
1345
chaviwd1c23182019-12-20 18:44:56 -08001346static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId,
1347 const std::vector<PointF>& points) {
1348 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001349 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1350 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1351 }
1352
chaviwd1c23182019-12-20 18:44:56 -08001353 PointerProperties pointerProperties[pointerCount];
1354 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001355
chaviwd1c23182019-12-20 18:44:56 -08001356 for (size_t i = 0; i < pointerCount; i++) {
1357 pointerProperties[i].clear();
1358 pointerProperties[i].id = i;
1359 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001360
chaviwd1c23182019-12-20 18:44:56 -08001361 pointerCoords[i].clear();
1362 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1363 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1364 }
Jackal Guof9696682018-10-05 12:23:23 +08001365
1366 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1367 // Define a valid motion event.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001368 NotifyMotionArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001369 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1370 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001371 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1372 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001373 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1374 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001375
1376 return args;
1377}
1378
chaviwd1c23182019-12-20 18:44:56 -08001379static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1380 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1381}
1382
Prabir Pradhan99987712020-11-10 18:43:05 -08001383static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(bool enabled) {
1384 return NotifyPointerCaptureChangedArgs(/* id */ 0, systemTime(SYSTEM_TIME_MONOTONIC), enabled);
1385}
1386
Arthur Hungb92218b2018-08-14 12:00:21 +08001387TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001388 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001389 sp<FakeWindowHandle> window =
1390 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001391
Arthur Hung72d8dc32020-03-28 00:48:39 +00001392 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001393 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1394 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1395 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001396
1397 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001398 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001399}
1400
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001401/**
1402 * Calling setInputWindows once with FLAG_NOT_TOUCH_MODAL should not cause any issues.
1403 * To ensure that window receives only events that were directly inside of it, add
1404 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1405 * when finding touched windows.
1406 * This test serves as a sanity check for the next test, where setInputWindows is
1407 * called twice.
1408 */
1409TEST_F(InputDispatcherTest, SetInputWindowOnce_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001410 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001411 sp<FakeWindowHandle> window =
1412 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1413 window->setFrame(Rect(0, 0, 100, 100));
chaviw3277faf2021-05-19 16:45:23 -05001414 window->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001415
1416 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001417 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001418 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1419 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001420 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001421
1422 // Window should receive motion event.
1423 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1424}
1425
1426/**
1427 * Calling setInputWindows twice, with the same info, should not cause any issues.
1428 * To ensure that window receives only events that were directly inside of it, add
1429 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1430 * when finding touched windows.
1431 */
1432TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001433 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001434 sp<FakeWindowHandle> window =
1435 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1436 window->setFrame(Rect(0, 0, 100, 100));
chaviw3277faf2021-05-19 16:45:23 -05001437 window->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001438
1439 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1440 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001441 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001442 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1443 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001444 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001445
1446 // Window should receive motion event.
1447 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1448}
1449
Arthur Hungb92218b2018-08-14 12:00:21 +08001450// The foreground window should receive the first touch down event.
1451TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001452 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001453 sp<FakeWindowHandle> windowTop =
1454 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
1455 sp<FakeWindowHandle> windowSecond =
1456 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001457
Arthur Hung72d8dc32020-03-28 00:48:39 +00001458 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001459 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1460 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1461 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001462
1463 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001464 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001465 windowSecond->assertNoEvents();
1466}
1467
Garfield Tandf26e862020-07-01 20:18:19 -07001468TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07001469 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07001470 sp<FakeWindowHandle> windowLeft =
1471 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1472 windowLeft->setFrame(Rect(0, 0, 600, 800));
chaviw3277faf2021-05-19 16:45:23 -05001473 windowLeft->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001474 sp<FakeWindowHandle> windowRight =
1475 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1476 windowRight->setFrame(Rect(600, 0, 1200, 800));
chaviw3277faf2021-05-19 16:45:23 -05001477 windowRight->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001478
1479 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1480
1481 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
1482
1483 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001484 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001485 injectMotionEvent(mDispatcher,
1486 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1487 AINPUT_SOURCE_MOUSE)
1488 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1489 .x(900)
1490 .y(400))
1491 .build()));
1492 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1493 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1494 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1495 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1496
1497 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001498 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001499 injectMotionEvent(mDispatcher,
1500 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1501 AINPUT_SOURCE_MOUSE)
1502 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1503 .x(300)
1504 .y(400))
1505 .build()));
1506 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1507 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1508 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1509 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1510 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1511 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1512
1513 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001514 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001515 injectMotionEvent(mDispatcher,
1516 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1517 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1518 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1519 .x(300)
1520 .y(400))
1521 .build()));
1522 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1523
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001524 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001525 injectMotionEvent(mDispatcher,
1526 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
1527 AINPUT_SOURCE_MOUSE)
1528 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1529 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1530 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1531 .x(300)
1532 .y(400))
1533 .build()));
1534 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1535 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1536
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001537 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001538 injectMotionEvent(mDispatcher,
1539 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1540 AINPUT_SOURCE_MOUSE)
1541 .buttonState(0)
1542 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1543 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1544 .x(300)
1545 .y(400))
1546 .build()));
1547 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1548 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1549
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001550 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001551 injectMotionEvent(mDispatcher,
1552 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
1553 .buttonState(0)
1554 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1555 .x(300)
1556 .y(400))
1557 .build()));
1558 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1559
1560 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001561 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001562 injectMotionEvent(mDispatcher,
1563 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1564 AINPUT_SOURCE_MOUSE)
1565 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1566 .x(900)
1567 .y(400))
1568 .build()));
1569 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1570 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1571 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1572 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1573 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1574 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1575}
1576
1577// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
1578// directly in this test.
1579TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07001580 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07001581 sp<FakeWindowHandle> window =
1582 new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1583 window->setFrame(Rect(0, 0, 1200, 800));
chaviw3277faf2021-05-19 16:45:23 -05001584 window->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001585
1586 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1587
1588 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1589
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001590 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001591 injectMotionEvent(mDispatcher,
1592 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
1593 AINPUT_SOURCE_MOUSE)
1594 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1595 .x(300)
1596 .y(400))
1597 .build()));
1598 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1599 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1600
1601 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001602 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001603 injectMotionEvent(mDispatcher,
1604 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1605 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1606 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1607 .x(300)
1608 .y(400))
1609 .build()));
1610 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1611
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001612 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001613 injectMotionEvent(mDispatcher,
1614 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
1615 AINPUT_SOURCE_MOUSE)
1616 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1617 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1618 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1619 .x(300)
1620 .y(400))
1621 .build()));
1622 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1623 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1624
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001625 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001626 injectMotionEvent(mDispatcher,
1627 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1628 AINPUT_SOURCE_MOUSE)
1629 .buttonState(0)
1630 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1631 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1632 .x(300)
1633 .y(400))
1634 .build()));
1635 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1636 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1637
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001638 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001639 injectMotionEvent(mDispatcher,
1640 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
1641 .buttonState(0)
1642 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1643 .x(300)
1644 .y(400))
1645 .build()));
1646 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1647
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001648 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001649 injectMotionEvent(mDispatcher,
1650 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
1651 AINPUT_SOURCE_MOUSE)
1652 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1653 .x(300)
1654 .y(400))
1655 .build()));
1656 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1657 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1658}
1659
Garfield Tan00f511d2019-06-12 16:55:40 -07001660TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07001661 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07001662
1663 sp<FakeWindowHandle> windowLeft =
1664 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1665 windowLeft->setFrame(Rect(0, 0, 600, 800));
chaviw3277faf2021-05-19 16:45:23 -05001666 windowLeft->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07001667 sp<FakeWindowHandle> windowRight =
1668 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1669 windowRight->setFrame(Rect(600, 0, 1200, 800));
chaviw3277faf2021-05-19 16:45:23 -05001670 windowRight->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07001671
1672 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1673
Arthur Hung72d8dc32020-03-28 00:48:39 +00001674 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07001675
1676 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
1677 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001678 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07001679 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001680 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001681 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07001682 windowRight->assertNoEvents();
1683}
1684
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001685TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001686 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001687 sp<FakeWindowHandle> window =
1688 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07001689 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001690
Arthur Hung72d8dc32020-03-28 00:48:39 +00001691 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001692 setFocusedWindow(window);
1693
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001694 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001695
1696 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1697 mDispatcher->notifyKey(&keyArgs);
1698
1699 // Window should receive key down event.
1700 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1701
1702 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
1703 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001704 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001705 mDispatcher->notifyDeviceReset(&args);
1706 window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
1707 AKEY_EVENT_FLAG_CANCELED);
1708}
1709
1710TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001711 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001712 sp<FakeWindowHandle> window =
1713 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1714
Arthur Hung72d8dc32020-03-28 00:48:39 +00001715 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001716
1717 NotifyMotionArgs motionArgs =
1718 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1719 ADISPLAY_ID_DEFAULT);
1720 mDispatcher->notifyMotion(&motionArgs);
1721
1722 // Window should receive motion down event.
1723 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1724
1725 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
1726 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001727 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001728 mDispatcher->notifyDeviceReset(&args);
1729 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT,
1730 0 /*expectedFlags*/);
1731}
1732
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001733using TransferFunction =
1734 std::function<bool(sp<InputDispatcher> dispatcher, sp<IBinder>, sp<IBinder>)>;
1735
1736class TransferTouchFixture : public InputDispatcherTest,
1737 public ::testing::WithParamInterface<TransferFunction> {};
1738
1739TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07001740 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001741
1742 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001743 sp<FakeWindowHandle> firstWindow =
1744 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1745 sp<FakeWindowHandle> secondWindow =
1746 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001747
1748 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001749 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001750
1751 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001752 NotifyMotionArgs downMotionArgs =
1753 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1754 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001755 mDispatcher->notifyMotion(&downMotionArgs);
1756 // Only the first window should get the down event
1757 firstWindow->consumeMotionDown();
1758 secondWindow->assertNoEvents();
1759
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001760 // Transfer touch to the second window
1761 TransferFunction f = GetParam();
1762 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
1763 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001764 // The first window gets cancel and the second gets down
1765 firstWindow->consumeMotionCancel();
1766 secondWindow->consumeMotionDown();
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 no events and the second gets up
1774 firstWindow->assertNoEvents();
1775 secondWindow->consumeMotionUp();
1776}
1777
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001778TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
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 PointF touchPoint = {10, 10};
1782
1783 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001784 sp<FakeWindowHandle> firstWindow =
1785 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1786 sp<FakeWindowHandle> secondWindow =
1787 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001788
1789 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001790 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001791
1792 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001793 NotifyMotionArgs downMotionArgs =
1794 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1795 ADISPLAY_ID_DEFAULT, {touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001796 mDispatcher->notifyMotion(&downMotionArgs);
1797 // Only the first window should get the down event
1798 firstWindow->consumeMotionDown();
1799 secondWindow->assertNoEvents();
1800
1801 // Send pointer down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001802 NotifyMotionArgs pointerDownMotionArgs =
1803 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1804 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1805 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1806 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001807 mDispatcher->notifyMotion(&pointerDownMotionArgs);
1808 // Only the first window should get the pointer down event
1809 firstWindow->consumeMotionPointerDown(1);
1810 secondWindow->assertNoEvents();
1811
1812 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001813 TransferFunction f = GetParam();
1814 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
1815 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001816 // The first window gets cancel and the second gets down and pointer down
1817 firstWindow->consumeMotionCancel();
1818 secondWindow->consumeMotionDown();
1819 secondWindow->consumeMotionPointerDown(1);
1820
1821 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001822 NotifyMotionArgs pointerUpMotionArgs =
1823 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1824 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1825 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1826 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001827 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1828 // The first window gets nothing and the second gets pointer up
1829 firstWindow->assertNoEvents();
1830 secondWindow->consumeMotionPointerUp(1);
1831
1832 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001833 NotifyMotionArgs upMotionArgs =
1834 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1835 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001836 mDispatcher->notifyMotion(&upMotionArgs);
1837 // The first window gets nothing and the second gets up
1838 firstWindow->assertNoEvents();
1839 secondWindow->consumeMotionUp();
1840}
1841
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001842// For the cases of single pointer touch and two pointers non-split touch, the api's
1843// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
1844// for the case where there are multiple pointers split across several windows.
1845INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
1846 ::testing::Values(
1847 [&](sp<InputDispatcher> dispatcher, sp<IBinder> /*ignored*/,
1848 sp<IBinder> destChannelToken) {
1849 return dispatcher->transferTouch(destChannelToken);
1850 },
1851 [&](sp<InputDispatcher> dispatcher, sp<IBinder> from,
1852 sp<IBinder> to) {
1853 return dispatcher->transferTouchFocus(from, to,
1854 false /*isDragAndDrop*/);
1855 }));
1856
Svet Ganov5d3bc372020-01-26 23:11:07 -08001857TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001858 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001859
1860 // Create a non touch modal window that supports split touch
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001861 sp<FakeWindowHandle> firstWindow =
1862 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001863 firstWindow->setFrame(Rect(0, 0, 600, 400));
chaviw3277faf2021-05-19 16:45:23 -05001864 firstWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001865
1866 // Create a non touch modal window that supports split touch
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001867 sp<FakeWindowHandle> secondWindow =
1868 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001869 secondWindow->setFrame(Rect(0, 400, 600, 800));
chaviw3277faf2021-05-19 16:45:23 -05001870 secondWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001871
1872 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001873 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001874
1875 PointF pointInFirst = {300, 200};
1876 PointF pointInSecond = {300, 600};
1877
1878 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001879 NotifyMotionArgs firstDownMotionArgs =
1880 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1881 ADISPLAY_ID_DEFAULT, {pointInFirst});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001882 mDispatcher->notifyMotion(&firstDownMotionArgs);
1883 // Only the first window should get the down event
1884 firstWindow->consumeMotionDown();
1885 secondWindow->assertNoEvents();
1886
1887 // Send down to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001888 NotifyMotionArgs secondDownMotionArgs =
1889 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1890 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1891 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1892 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001893 mDispatcher->notifyMotion(&secondDownMotionArgs);
1894 // The first window gets a move and the second a down
1895 firstWindow->consumeMotionMove();
1896 secondWindow->consumeMotionDown();
1897
1898 // Transfer touch focus to the second window
1899 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1900 // The first window gets cancel and the new gets pointer down (it already saw down)
1901 firstWindow->consumeMotionCancel();
1902 secondWindow->consumeMotionPointerDown(1);
1903
1904 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001905 NotifyMotionArgs pointerUpMotionArgs =
1906 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1907 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1908 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1909 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001910 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1911 // The first window gets nothing and the second gets pointer up
1912 firstWindow->assertNoEvents();
1913 secondWindow->consumeMotionPointerUp(1);
1914
1915 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001916 NotifyMotionArgs upMotionArgs =
1917 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1918 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001919 mDispatcher->notifyMotion(&upMotionArgs);
1920 // The first window gets nothing and the second gets up
1921 firstWindow->assertNoEvents();
1922 secondWindow->consumeMotionUp();
1923}
1924
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001925// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
1926// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
1927// touch is not supported, so the touch should continue on those windows and the transferred-to
1928// window should get nothing.
1929TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
1930 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1931
1932 // Create a non touch modal window that supports split touch
1933 sp<FakeWindowHandle> firstWindow =
1934 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1935 firstWindow->setFrame(Rect(0, 0, 600, 400));
chaviw3277faf2021-05-19 16:45:23 -05001936 firstWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001937
1938 // Create a non touch modal window that supports split touch
1939 sp<FakeWindowHandle> secondWindow =
1940 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
1941 secondWindow->setFrame(Rect(0, 400, 600, 800));
chaviw3277faf2021-05-19 16:45:23 -05001942 secondWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001943
1944 // Add the windows to the dispatcher
1945 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
1946
1947 PointF pointInFirst = {300, 200};
1948 PointF pointInSecond = {300, 600};
1949
1950 // Send down to the first window
1951 NotifyMotionArgs firstDownMotionArgs =
1952 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1953 ADISPLAY_ID_DEFAULT, {pointInFirst});
1954 mDispatcher->notifyMotion(&firstDownMotionArgs);
1955 // Only the first window should get the down event
1956 firstWindow->consumeMotionDown();
1957 secondWindow->assertNoEvents();
1958
1959 // Send down to the second window
1960 NotifyMotionArgs secondDownMotionArgs =
1961 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1962 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1963 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1964 {pointInFirst, pointInSecond});
1965 mDispatcher->notifyMotion(&secondDownMotionArgs);
1966 // The first window gets a move and the second a down
1967 firstWindow->consumeMotionMove();
1968 secondWindow->consumeMotionDown();
1969
1970 // Transfer touch focus to the second window
1971 const bool transferred = mDispatcher->transferTouch(secondWindow->getToken());
1972 // The 'transferTouch' call should not succeed, because there are 2 touched windows
1973 ASSERT_FALSE(transferred);
1974 firstWindow->assertNoEvents();
1975 secondWindow->assertNoEvents();
1976
1977 // The rest of the dispatch should proceed as normal
1978 // Send pointer up to the second window
1979 NotifyMotionArgs pointerUpMotionArgs =
1980 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1981 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1982 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1983 {pointInFirst, pointInSecond});
1984 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1985 // The first window gets MOVE and the second gets pointer up
1986 firstWindow->consumeMotionMove();
1987 secondWindow->consumeMotionUp();
1988
1989 // Send up event to the first window
1990 NotifyMotionArgs upMotionArgs =
1991 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1992 ADISPLAY_ID_DEFAULT);
1993 mDispatcher->notifyMotion(&upMotionArgs);
1994 // The first window gets nothing and the second gets up
1995 firstWindow->consumeMotionUp();
1996 secondWindow->assertNoEvents();
1997}
1998
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001999TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002000 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002001 sp<FakeWindowHandle> window =
2002 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2003
Vishnu Nair47074b82020-08-14 11:54:47 -07002004 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002005 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002006 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002007
2008 window->consumeFocusEvent(true);
2009
2010 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2011 mDispatcher->notifyKey(&keyArgs);
2012
2013 // Window should receive key down event.
2014 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2015}
2016
2017TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002018 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002019 sp<FakeWindowHandle> window =
2020 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2021
Arthur Hung72d8dc32020-03-28 00:48:39 +00002022 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002023
2024 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2025 mDispatcher->notifyKey(&keyArgs);
2026 mDispatcher->waitForIdle();
2027
2028 window->assertNoEvents();
2029}
2030
2031// If a window is touchable, but does not have focus, it should receive motion events, but not keys
2032TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07002033 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002034 sp<FakeWindowHandle> window =
2035 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2036
Arthur Hung72d8dc32020-03-28 00:48:39 +00002037 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002038
2039 // Send key
2040 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2041 mDispatcher->notifyKey(&keyArgs);
2042 // Send motion
2043 NotifyMotionArgs motionArgs =
2044 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2045 ADISPLAY_ID_DEFAULT);
2046 mDispatcher->notifyMotion(&motionArgs);
2047
2048 // Window should receive only the motion event
2049 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2050 window->assertNoEvents(); // Key event or focus event will not be received
2051}
2052
arthurhungea3f4fc2020-12-21 23:18:53 +08002053TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
2054 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2055
2056 // Create first non touch modal window that supports split touch
2057 sp<FakeWindowHandle> firstWindow =
2058 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
2059 firstWindow->setFrame(Rect(0, 0, 600, 400));
chaviw3277faf2021-05-19 16:45:23 -05002060 firstWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
arthurhungea3f4fc2020-12-21 23:18:53 +08002061
2062 // Create second non touch modal window that supports split touch
2063 sp<FakeWindowHandle> secondWindow =
2064 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
2065 secondWindow->setFrame(Rect(0, 400, 600, 800));
chaviw3277faf2021-05-19 16:45:23 -05002066 secondWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
arthurhungea3f4fc2020-12-21 23:18:53 +08002067
2068 // Add the windows to the dispatcher
2069 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
2070
2071 PointF pointInFirst = {300, 200};
2072 PointF pointInSecond = {300, 600};
2073
2074 // Send down to the first window
2075 NotifyMotionArgs firstDownMotionArgs =
2076 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2077 ADISPLAY_ID_DEFAULT, {pointInFirst});
2078 mDispatcher->notifyMotion(&firstDownMotionArgs);
2079 // Only the first window should get the down event
2080 firstWindow->consumeMotionDown();
2081 secondWindow->assertNoEvents();
2082
2083 // Send down to the second window
2084 NotifyMotionArgs secondDownMotionArgs =
2085 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
2086 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2087 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2088 {pointInFirst, pointInSecond});
2089 mDispatcher->notifyMotion(&secondDownMotionArgs);
2090 // The first window gets a move and the second a down
2091 firstWindow->consumeMotionMove();
2092 secondWindow->consumeMotionDown();
2093
2094 // Send pointer cancel to the second window
2095 NotifyMotionArgs pointerUpMotionArgs =
2096 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
2097 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2098 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2099 {pointInFirst, pointInSecond});
2100 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
2101 mDispatcher->notifyMotion(&pointerUpMotionArgs);
2102 // The first window gets move and the second gets cancel.
2103 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
2104 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
2105
2106 // Send up event.
2107 NotifyMotionArgs upMotionArgs =
2108 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2109 ADISPLAY_ID_DEFAULT);
2110 mDispatcher->notifyMotion(&upMotionArgs);
2111 // The first window gets up and the second gets nothing.
2112 firstWindow->consumeMotionUp();
2113 secondWindow->assertNoEvents();
2114}
2115
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00002116TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
2117 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2118
2119 sp<FakeWindowHandle> window =
2120 new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2121 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2122 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
2123 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
2124 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
2125
2126 window->sendTimeline(1 /*inputEventId*/, graphicsTimeline);
2127 window->assertNoEvents();
2128 mDispatcher->waitForIdle();
2129}
2130
chaviwd1c23182019-12-20 18:44:56 -08002131class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00002132public:
2133 FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name,
chaviwd1c23182019-12-20 18:44:56 -08002134 int32_t displayId, bool isGestureMonitor = false) {
Garfield Tan15601662020-09-22 15:32:38 -07002135 base::Result<std::unique_ptr<InputChannel>> channel =
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +00002136 dispatcher->createInputMonitor(displayId, isGestureMonitor, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07002137 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00002138 }
2139
chaviwd1c23182019-12-20 18:44:56 -08002140 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
2141
2142 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
2143 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN,
2144 expectedDisplayId, expectedFlags);
2145 }
2146
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002147 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
2148
2149 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
2150
chaviwd1c23182019-12-20 18:44:56 -08002151 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
2152 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
2153 expectedDisplayId, expectedFlags);
2154 }
2155
2156 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
2157 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP,
2158 expectedDisplayId, expectedFlags);
2159 }
2160
Evan Rosky84f07f02021-04-16 10:42:42 -07002161 MotionEvent* consumeMotion() {
2162 InputEvent* event = mInputReceiver->consume();
2163 if (!event) {
2164 ADD_FAILURE() << "No event was produced";
2165 return nullptr;
2166 }
2167 if (event->getType() != AINPUT_EVENT_TYPE_MOTION) {
2168 ADD_FAILURE() << "Received event of type " << event->getType() << " instead of motion";
2169 return nullptr;
2170 }
2171 return static_cast<MotionEvent*>(event);
2172 }
2173
chaviwd1c23182019-12-20 18:44:56 -08002174 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
2175
2176private:
2177 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00002178};
2179
2180// Tests for gesture monitors
2181TEST_F(InputDispatcherTest, GestureMonitor_ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07002182 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00002183 sp<FakeWindowHandle> window =
2184 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002185 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00002186
chaviwd1c23182019-12-20 18:44:56 -08002187 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
2188 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00002189
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002190 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00002191 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002192 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00002193 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002194 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00002195}
2196
2197TEST_F(InputDispatcherTest, GestureMonitor_DoesNotReceiveKeyEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07002198 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00002199 sp<FakeWindowHandle> window =
2200 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2201
2202 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002203 window->setFocusable(true);
Michael Wright3a240c42019-12-10 20:53:41 +00002204
Arthur Hung72d8dc32020-03-28 00:48:39 +00002205 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002206 setFocusedWindow(window);
2207
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002208 window->consumeFocusEvent(true);
Michael Wright3a240c42019-12-10 20:53:41 +00002209
chaviwd1c23182019-12-20 18:44:56 -08002210 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
2211 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00002212
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002213 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2214 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00002215 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002216 monitor.assertNoEvents();
Michael Wright3a240c42019-12-10 20:53:41 +00002217}
2218
2219TEST_F(InputDispatcherTest, GestureMonitor_CanPilferAfterWindowIsRemovedMidStream) {
Chris Yea209fde2020-07-22 13:54:51 -07002220 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00002221 sp<FakeWindowHandle> window =
2222 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002223 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00002224
chaviwd1c23182019-12-20 18:44:56 -08002225 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
2226 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00002227
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002228 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00002229 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002230 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00002231 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002232 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00002233
2234 window->releaseChannel();
2235
chaviwd1c23182019-12-20 18:44:56 -08002236 mDispatcher->pilferPointers(monitor.getToken());
Michael Wright3a240c42019-12-10 20:53:41 +00002237
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002238 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00002239 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002240 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08002241 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00002242}
2243
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002244TEST_F(InputDispatcherTest, UnresponsiveGestureMonitor_GetsAnr) {
2245 FakeMonitorReceiver monitor =
2246 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
2247 true /*isGestureMonitor*/);
2248
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002249 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002250 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
2251 std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
2252 ASSERT_TRUE(consumeSeq);
2253
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00002254 mFakePolicy->assertNotifyMonitorUnresponsiveWasCalled(DISPATCHING_TIMEOUT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002255 monitor.finishEvent(*consumeSeq);
2256 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00002257 mFakePolicy->assertNotifyMonitorResponsiveWasCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002258}
2259
Evan Rosky84f07f02021-04-16 10:42:42 -07002260// Tests for gesture monitors
2261TEST_F(InputDispatcherTest, GestureMonitor_NoWindowTransform) {
2262 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2263 sp<FakeWindowHandle> window =
2264 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2265 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2266 window->setWindowOffset(20, 40);
2267 window->setWindowTransform(0, 1, -1, 0);
2268
2269 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
2270 true /*isGestureMonitor*/);
2271
2272 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2273 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2274 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2275 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2276 MotionEvent* event = monitor.consumeMotion();
2277 // Even though window has transform, gesture monitor must not.
2278 ASSERT_EQ(ui::Transform(), event->getTransform());
2279}
2280
chaviw81e2bb92019-12-18 15:03:51 -08002281TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002282 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
chaviw81e2bb92019-12-18 15:03:51 -08002283 sp<FakeWindowHandle> window =
2284 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2285
Arthur Hung72d8dc32020-03-28 00:48:39 +00002286 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08002287
2288 NotifyMotionArgs motionArgs =
2289 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2290 ADISPLAY_ID_DEFAULT);
2291
2292 mDispatcher->notifyMotion(&motionArgs);
2293 // Window should receive motion down event.
2294 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2295
2296 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002297 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08002298 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
2299 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
2300 motionArgs.pointerCoords[0].getX() - 10);
2301
2302 mDispatcher->notifyMotion(&motionArgs);
2303 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
2304 0 /*expectedFlags*/);
2305}
2306
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002307/**
2308 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
2309 * the device default right away. In the test scenario, we check both the default value,
2310 * and the action of enabling / disabling.
2311 */
2312TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07002313 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002314 sp<FakeWindowHandle> window =
2315 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2316
2317 // Set focused application.
2318 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002319 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002320
2321 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00002322 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002323 setFocusedWindow(window);
2324
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002325 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2326
2327 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07002328 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002329 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002330 window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/);
2331
2332 SCOPED_TRACE("Disable touch mode");
2333 mDispatcher->setInTouchMode(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07002334 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002335 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002336 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002337 window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/);
2338
2339 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07002340 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002341 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002342 window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/);
2343
2344 SCOPED_TRACE("Enable touch mode again");
2345 mDispatcher->setInTouchMode(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07002346 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002347 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002348 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002349 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2350
2351 window->assertNoEvents();
2352}
2353
Gang Wange9087892020-01-07 12:17:14 -05002354TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002355 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Gang Wange9087892020-01-07 12:17:14 -05002356 sp<FakeWindowHandle> window =
2357 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2358
2359 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002360 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05002361
Arthur Hung72d8dc32020-03-28 00:48:39 +00002362 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002363 setFocusedWindow(window);
2364
Gang Wange9087892020-01-07 12:17:14 -05002365 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2366
2367 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
2368 mDispatcher->notifyKey(&keyArgs);
2369
2370 InputEvent* event = window->consume();
2371 ASSERT_NE(event, nullptr);
2372
2373 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
2374 ASSERT_NE(verified, nullptr);
2375 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
2376
2377 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
2378 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
2379 ASSERT_EQ(keyArgs.source, verified->source);
2380 ASSERT_EQ(keyArgs.displayId, verified->displayId);
2381
2382 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
2383
2384 ASSERT_EQ(keyArgs.action, verifiedKey.action);
2385 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05002386 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
2387 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
2388 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
2389 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
2390 ASSERT_EQ(0, verifiedKey.repeatCount);
2391}
2392
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002393TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002394 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002395 sp<FakeWindowHandle> window =
2396 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2397
2398 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2399
Arthur Hung72d8dc32020-03-28 00:48:39 +00002400 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002401
2402 NotifyMotionArgs motionArgs =
2403 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2404 ADISPLAY_ID_DEFAULT);
2405 mDispatcher->notifyMotion(&motionArgs);
2406
2407 InputEvent* event = window->consume();
2408 ASSERT_NE(event, nullptr);
2409
2410 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
2411 ASSERT_NE(verified, nullptr);
2412 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
2413
2414 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
2415 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
2416 EXPECT_EQ(motionArgs.source, verified->source);
2417 EXPECT_EQ(motionArgs.displayId, verified->displayId);
2418
2419 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
2420
2421 EXPECT_EQ(motionArgs.pointerCoords[0].getX(), verifiedMotion.rawX);
2422 EXPECT_EQ(motionArgs.pointerCoords[0].getY(), verifiedMotion.rawY);
2423 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
2424 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
2425 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
2426 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
2427 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
2428}
2429
Prabir Pradhan664834b2021-05-20 16:00:42 -07002430TEST_F(InputDispatcherTest, NonPointerMotionEvent_JoystickAndTouchpadNotTransformed) {
yunho.shinf4a80b82020-11-16 21:13:57 +09002431 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2432 sp<FakeWindowHandle> window =
2433 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2434 const std::string name = window->getName();
2435
2436 // Window gets transformed by offset values.
2437 window->setWindowOffset(500.0f, 500.0f);
2438
2439 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2440 window->setFocusable(true);
2441
2442 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2443
2444 // First, we set focused window so that focusedWindowHandle is not null.
2445 setFocusedWindow(window);
2446
2447 // Second, we consume focus event if it is right or wrong according to onFocusChangedLocked.
2448 window->consumeFocusEvent(true);
2449
Prabir Pradhan664834b2021-05-20 16:00:42 -07002450 constexpr const std::array nonTransformedSources = {std::pair(AINPUT_SOURCE_TOUCHPAD,
2451 AMOTION_EVENT_ACTION_DOWN),
2452 std::pair(AINPUT_SOURCE_JOYSTICK,
2453 AMOTION_EVENT_ACTION_MOVE)};
2454 for (const auto& [source, action] : nonTransformedSources) {
2455 const NotifyMotionArgs motionArgs = generateMotionArgs(action, source, ADISPLAY_ID_DEFAULT);
Prabir Pradhanbd527712021-03-09 19:17:09 -08002456 mDispatcher->notifyMotion(&motionArgs);
yunho.shinf4a80b82020-11-16 21:13:57 +09002457
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00002458 MotionEvent* event = window->consumeMotion();
Prabir Pradhanbd527712021-03-09 19:17:09 -08002459 ASSERT_NE(event, nullptr);
yunho.shinf4a80b82020-11-16 21:13:57 +09002460
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00002461 const MotionEvent& motionEvent = *event;
Prabir Pradhan664834b2021-05-20 16:00:42 -07002462 EXPECT_EQ(action, motionEvent.getAction());
Prabir Pradhanbd527712021-03-09 19:17:09 -08002463 EXPECT_EQ(motionArgs.pointerCount, motionEvent.getPointerCount());
yunho.shinf4a80b82020-11-16 21:13:57 +09002464
Prabir Pradhanbd527712021-03-09 19:17:09 -08002465 float expectedX = motionArgs.pointerCoords[0].getX();
2466 float expectedY = motionArgs.pointerCoords[0].getY();
yunho.shinf4a80b82020-11-16 21:13:57 +09002467
Prabir Pradhanbd527712021-03-09 19:17:09 -08002468 // Ensure the axis values from the final motion event are not transformed.
2469 EXPECT_EQ(expectedX, motionEvent.getX(0))
2470 << "expected " << expectedX << " for x coord of " << name.c_str() << ", got "
2471 << motionEvent.getX(0);
2472 EXPECT_EQ(expectedY, motionEvent.getY(0))
2473 << "expected " << expectedY << " for y coord of " << name.c_str() << ", got "
2474 << motionEvent.getY(0);
2475 // Ensure the raw and transformed axis values for the motion event are the same.
2476 EXPECT_EQ(motionEvent.getRawX(0), motionEvent.getX(0))
2477 << "expected raw and transformed X-axis values to be equal";
2478 EXPECT_EQ(motionEvent.getRawY(0), motionEvent.getY(0))
2479 << "expected raw and transformed Y-axis values to be equal";
2480 }
yunho.shinf4a80b82020-11-16 21:13:57 +09002481}
2482
chaviw09c8d2d2020-08-24 15:48:26 -07002483/**
2484 * Ensure that separate calls to sign the same data are generating the same key.
2485 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
2486 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
2487 * tests.
2488 */
2489TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
2490 KeyEvent event = getTestKeyEvent();
2491 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
2492
2493 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
2494 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
2495 ASSERT_EQ(hmac1, hmac2);
2496}
2497
2498/**
2499 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
2500 */
2501TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
2502 KeyEvent event = getTestKeyEvent();
2503 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
2504 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
2505
2506 verifiedEvent.deviceId += 1;
2507 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2508
2509 verifiedEvent.source += 1;
2510 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2511
2512 verifiedEvent.eventTimeNanos += 1;
2513 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2514
2515 verifiedEvent.displayId += 1;
2516 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2517
2518 verifiedEvent.action += 1;
2519 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2520
2521 verifiedEvent.downTimeNanos += 1;
2522 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2523
2524 verifiedEvent.flags += 1;
2525 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2526
2527 verifiedEvent.keyCode += 1;
2528 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2529
2530 verifiedEvent.scanCode += 1;
2531 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2532
2533 verifiedEvent.metaState += 1;
2534 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2535
2536 verifiedEvent.repeatCount += 1;
2537 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2538}
2539
Vishnu Nair958da932020-08-21 17:12:37 -07002540TEST_F(InputDispatcherTest, SetFocusedWindow) {
2541 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2542 sp<FakeWindowHandle> windowTop =
2543 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2544 sp<FakeWindowHandle> windowSecond =
2545 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2546 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2547
2548 // Top window is also focusable but is not granted focus.
2549 windowTop->setFocusable(true);
2550 windowSecond->setFocusable(true);
2551 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2552 setFocusedWindow(windowSecond);
2553
2554 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002555 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2556 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002557
2558 // Focused window should receive event.
2559 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
2560 windowTop->assertNoEvents();
2561}
2562
2563TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
2564 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2565 sp<FakeWindowHandle> window =
2566 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2567 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2568
2569 window->setFocusable(true);
2570 // Release channel for window is no longer valid.
2571 window->releaseChannel();
2572 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2573 setFocusedWindow(window);
2574
2575 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002576 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2577 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002578
2579 // window channel is invalid, so it should not receive any input event.
2580 window->assertNoEvents();
2581}
2582
2583TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
2584 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2585 sp<FakeWindowHandle> window =
2586 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2587 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2588
2589 // Window is not focusable.
2590 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2591 setFocusedWindow(window);
2592
2593 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002594 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2595 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002596
2597 // window is invalid, so it should not receive any input event.
2598 window->assertNoEvents();
2599}
2600
2601TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
2602 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2603 sp<FakeWindowHandle> windowTop =
2604 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2605 sp<FakeWindowHandle> windowSecond =
2606 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2607 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2608
2609 windowTop->setFocusable(true);
2610 windowSecond->setFocusable(true);
2611 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2612 setFocusedWindow(windowTop);
2613 windowTop->consumeFocusEvent(true);
2614
2615 setFocusedWindow(windowSecond, windowTop);
2616 windowSecond->consumeFocusEvent(true);
2617 windowTop->consumeFocusEvent(false);
2618
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002619 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2620 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002621
2622 // Focused window should receive event.
2623 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
2624}
2625
2626TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) {
2627 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2628 sp<FakeWindowHandle> windowTop =
2629 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2630 sp<FakeWindowHandle> windowSecond =
2631 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2632 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2633
2634 windowTop->setFocusable(true);
2635 windowSecond->setFocusable(true);
2636 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2637 setFocusedWindow(windowSecond, windowTop);
2638
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002639 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2640 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002641
2642 // Event should be dropped.
2643 windowTop->assertNoEvents();
2644 windowSecond->assertNoEvents();
2645}
2646
2647TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
2648 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2649 sp<FakeWindowHandle> window =
2650 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2651 sp<FakeWindowHandle> previousFocusedWindow =
2652 new FakeWindowHandle(application, mDispatcher, "previousFocusedWindow",
2653 ADISPLAY_ID_DEFAULT);
2654 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2655
2656 window->setFocusable(true);
2657 previousFocusedWindow->setFocusable(true);
2658 window->setVisible(false);
2659 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
2660 setFocusedWindow(previousFocusedWindow);
2661 previousFocusedWindow->consumeFocusEvent(true);
2662
2663 // Requesting focus on invisible window takes focus from currently focused window.
2664 setFocusedWindow(window);
2665 previousFocusedWindow->consumeFocusEvent(false);
2666
2667 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002668 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07002669 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002670 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07002671
2672 // Window does not get focus event or key down.
2673 window->assertNoEvents();
2674
2675 // Window becomes visible.
2676 window->setVisible(true);
2677 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2678
2679 // Window receives focus event.
2680 window->consumeFocusEvent(true);
2681 // Focused window receives key down.
2682 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2683}
2684
Vishnu Nair599f1412021-06-21 10:39:58 -07002685TEST_F(InputDispatcherTest, DisplayRemoved) {
2686 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2687 sp<FakeWindowHandle> window =
2688 new FakeWindowHandle(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
2689 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2690
2691 // window is granted focus.
2692 window->setFocusable(true);
2693 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2694 setFocusedWindow(window);
2695 window->consumeFocusEvent(true);
2696
2697 // When a display is removed window loses focus.
2698 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
2699 window->consumeFocusEvent(false);
2700}
2701
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002702/**
2703 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
2704 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
2705 * of the 'slipperyEnterWindow'.
2706 *
2707 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
2708 * a way so that the touched location is no longer covered by the top window.
2709 *
2710 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
2711 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
2712 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
2713 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
2714 * with ACTION_DOWN).
2715 * Thus, the touch has been transferred from the top window into the bottom window, because the top
2716 * window moved itself away from the touched location and had Flag::SLIPPERY.
2717 *
2718 * Even though the top window moved away from the touched location, it is still obscuring the bottom
2719 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
2720 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
2721 *
2722 * In this test, we ensure that the event received by the bottom window has
2723 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
2724 */
2725TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
2726 constexpr int32_t SLIPPERY_PID = INJECTOR_PID + 1;
2727 constexpr int32_t SLIPPERY_UID = INJECTOR_UID + 1;
2728
2729 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2730 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2731
2732 sp<FakeWindowHandle> slipperyExitWindow =
2733 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviw3277faf2021-05-19 16:45:23 -05002734 slipperyExitWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SLIPPERY);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002735 // Make sure this one overlaps the bottom window
2736 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
2737 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
2738 // one. Windows with the same owner are not considered to be occluding each other.
2739 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
2740
2741 sp<FakeWindowHandle> slipperyEnterWindow =
2742 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2743 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
2744
2745 mDispatcher->setInputWindows(
2746 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
2747
2748 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
2749 NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2750 ADISPLAY_ID_DEFAULT, {{50, 50}});
2751 mDispatcher->notifyMotion(&args);
2752 slipperyExitWindow->consumeMotionDown();
2753 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
2754 mDispatcher->setInputWindows(
2755 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
2756
2757 args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2758 ADISPLAY_ID_DEFAULT, {{51, 51}});
2759 mDispatcher->notifyMotion(&args);
2760
2761 slipperyExitWindow->consumeMotionCancel();
2762
2763 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
2764 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
2765}
2766
Garfield Tan1c7bc862020-01-28 13:24:04 -08002767class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
2768protected:
2769 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
2770 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
2771
Chris Yea209fde2020-07-22 13:54:51 -07002772 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002773 sp<FakeWindowHandle> mWindow;
2774
2775 virtual void SetUp() override {
2776 mFakePolicy = new FakeInputDispatcherPolicy();
2777 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
2778 mDispatcher = new InputDispatcher(mFakePolicy);
2779 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
2780 ASSERT_EQ(OK, mDispatcher->start());
2781
2782 setUpWindow();
2783 }
2784
2785 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07002786 mApp = std::make_shared<FakeApplicationHandle>();
Garfield Tan1c7bc862020-01-28 13:24:04 -08002787 mWindow = new FakeWindowHandle(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2788
Vishnu Nair47074b82020-08-14 11:54:47 -07002789 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002790 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002791 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002792 mWindow->consumeFocusEvent(true);
2793 }
2794
Chris Ye2ad95392020-09-01 13:44:44 -07002795 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08002796 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07002797 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002798 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
2799 mDispatcher->notifyKey(&keyArgs);
2800
2801 // Window should receive key down event.
2802 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2803 }
2804
2805 void expectKeyRepeatOnce(int32_t repeatCount) {
2806 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
2807 InputEvent* repeatEvent = mWindow->consume();
2808 ASSERT_NE(nullptr, repeatEvent);
2809
2810 uint32_t eventType = repeatEvent->getType();
2811 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType);
2812
2813 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
2814 uint32_t eventAction = repeatKeyEvent->getAction();
2815 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
2816 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
2817 }
2818
Chris Ye2ad95392020-09-01 13:44:44 -07002819 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08002820 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07002821 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002822 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
2823 mDispatcher->notifyKey(&keyArgs);
2824
2825 // Window should receive key down event.
2826 mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
2827 0 /*expectedFlags*/);
2828 }
2829};
2830
2831TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Chris Ye2ad95392020-09-01 13:44:44 -07002832 sendAndConsumeKeyDown(1 /* deviceId */);
2833 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2834 expectKeyRepeatOnce(repeatCount);
2835 }
2836}
2837
2838TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
2839 sendAndConsumeKeyDown(1 /* deviceId */);
2840 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2841 expectKeyRepeatOnce(repeatCount);
2842 }
2843 sendAndConsumeKeyDown(2 /* deviceId */);
2844 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08002845 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2846 expectKeyRepeatOnce(repeatCount);
2847 }
2848}
2849
2850TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Chris Ye2ad95392020-09-01 13:44:44 -07002851 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002852 expectKeyRepeatOnce(1 /*repeatCount*/);
Chris Ye2ad95392020-09-01 13:44:44 -07002853 sendAndConsumeKeyUp(1 /* deviceId */);
2854 mWindow->assertNoEvents();
2855}
2856
2857TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
2858 sendAndConsumeKeyDown(1 /* deviceId */);
2859 expectKeyRepeatOnce(1 /*repeatCount*/);
2860 sendAndConsumeKeyDown(2 /* deviceId */);
2861 expectKeyRepeatOnce(1 /*repeatCount*/);
2862 // Stale key up from device 1.
2863 sendAndConsumeKeyUp(1 /* deviceId */);
2864 // Device 2 is still down, keep repeating
2865 expectKeyRepeatOnce(2 /*repeatCount*/);
2866 expectKeyRepeatOnce(3 /*repeatCount*/);
2867 // Device 2 key up
2868 sendAndConsumeKeyUp(2 /* deviceId */);
2869 mWindow->assertNoEvents();
2870}
2871
2872TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
2873 sendAndConsumeKeyDown(1 /* deviceId */);
2874 expectKeyRepeatOnce(1 /*repeatCount*/);
2875 sendAndConsumeKeyDown(2 /* deviceId */);
2876 expectKeyRepeatOnce(1 /*repeatCount*/);
2877 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
2878 sendAndConsumeKeyUp(2 /* deviceId */);
2879 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08002880 mWindow->assertNoEvents();
2881}
2882
liushenxiang42232912021-05-21 20:24:09 +08002883TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
2884 sendAndConsumeKeyDown(DEVICE_ID);
2885 expectKeyRepeatOnce(1 /*repeatCount*/);
2886 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
2887 mDispatcher->notifyDeviceReset(&args);
2888 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
2889 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
2890 mWindow->assertNoEvents();
2891}
2892
Garfield Tan1c7bc862020-01-28 13:24:04 -08002893TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Chris Ye2ad95392020-09-01 13:44:44 -07002894 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002895 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2896 InputEvent* repeatEvent = mWindow->consume();
2897 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2898 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
2899 IdGenerator::getSource(repeatEvent->getId()));
2900 }
2901}
2902
2903TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Chris Ye2ad95392020-09-01 13:44:44 -07002904 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002905
2906 std::unordered_set<int32_t> idSet;
2907 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2908 InputEvent* repeatEvent = mWindow->consume();
2909 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2910 int32_t id = repeatEvent->getId();
2911 EXPECT_EQ(idSet.end(), idSet.find(id));
2912 idSet.insert(id);
2913 }
2914}
2915
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002916/* Test InputDispatcher for MultiDisplay */
2917class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
2918public:
2919 static constexpr int32_t SECOND_DISPLAY_ID = 1;
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002920 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002921 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08002922
Chris Yea209fde2020-07-22 13:54:51 -07002923 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002924 windowInPrimary =
2925 new FakeWindowHandle(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002926
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002927 // Set focus window for primary display, but focused display would be second one.
2928 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07002929 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002930 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002931 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002932 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08002933
Chris Yea209fde2020-07-22 13:54:51 -07002934 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002935 windowInSecondary =
2936 new FakeWindowHandle(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002937 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002938 // Set focus display to second one.
2939 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
2940 // Set focus window for second display.
2941 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07002942 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002943 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002944 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002945 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002946 }
2947
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002948 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002949 InputDispatcherTest::TearDown();
2950
Chris Yea209fde2020-07-22 13:54:51 -07002951 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002952 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07002953 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002954 windowInSecondary.clear();
2955 }
2956
2957protected:
Chris Yea209fde2020-07-22 13:54:51 -07002958 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002959 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07002960 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002961 sp<FakeWindowHandle> windowInSecondary;
2962};
2963
2964TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
2965 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002966 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2967 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2968 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002969 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08002970 windowInSecondary->assertNoEvents();
2971
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002972 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002973 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2974 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
2975 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08002976 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002977 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08002978}
2979
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002980TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08002981 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08002982 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2983 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002984 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002985 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08002986 windowInSecondary->assertNoEvents();
2987
2988 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08002989 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002990 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08002991 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002992 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08002993
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002994 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00002995 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08002996
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002997 // Old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002998 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
2999 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08003000
3001 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003002 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003003 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08003004 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003005 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08003006 windowInSecondary->assertNoEvents();
3007}
3008
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003009// Test per-display input monitors for motion event.
3010TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08003011 FakeMonitorReceiver monitorInPrimary =
3012 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
3013 FakeMonitorReceiver monitorInSecondary =
3014 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003015
3016 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003017 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3018 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
3019 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003020 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08003021 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003022 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003023 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003024
3025 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003026 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3027 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
3028 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003029 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003030 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003031 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08003032 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003033
3034 // Test inject a non-pointer motion event.
3035 // If specific a display, it will dispatch to the focused window of particular display,
3036 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003037 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3038 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
3039 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003040 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003041 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003042 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08003043 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003044}
3045
3046// Test per-display input monitors for key event.
3047TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003048 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08003049 FakeMonitorReceiver monitorInPrimary =
3050 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
3051 FakeMonitorReceiver monitorInSecondary =
3052 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003053
3054 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003055 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3056 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003057 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003058 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003059 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08003060 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003061}
3062
Vishnu Nair958da932020-08-21 17:12:37 -07003063TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
3064 sp<FakeWindowHandle> secondWindowInPrimary =
3065 new FakeWindowHandle(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
3066 secondWindowInPrimary->setFocusable(true);
3067 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
3068 setFocusedWindow(secondWindowInPrimary);
3069 windowInPrimary->consumeFocusEvent(false);
3070 secondWindowInPrimary->consumeFocusEvent(true);
3071
3072 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003073 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
3074 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003075 windowInPrimary->assertNoEvents();
3076 windowInSecondary->assertNoEvents();
3077 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3078}
3079
Jackal Guof9696682018-10-05 12:23:23 +08003080class InputFilterTest : public InputDispatcherTest {
3081protected:
3082 static constexpr int32_t SECOND_DISPLAY_ID = 1;
3083
3084 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) {
3085 NotifyMotionArgs motionArgs;
3086
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003087 motionArgs =
3088 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08003089 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003090 motionArgs =
3091 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08003092 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003093 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08003094 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08003095 mFakePolicy->assertFilterInputEventWasCalled(motionArgs);
Jackal Guof9696682018-10-05 12:23:23 +08003096 } else {
3097 mFakePolicy->assertFilterInputEventWasNotCalled();
3098 }
3099 }
3100
3101 void testNotifyKey(bool expectToBeFiltered) {
3102 NotifyKeyArgs keyArgs;
3103
3104 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
3105 mDispatcher->notifyKey(&keyArgs);
3106 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
3107 mDispatcher->notifyKey(&keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003108 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08003109
3110 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08003111 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08003112 } else {
3113 mFakePolicy->assertFilterInputEventWasNotCalled();
3114 }
3115 }
3116};
3117
3118// Test InputFilter for MotionEvent
3119TEST_F(InputFilterTest, MotionEvent_InputFilter) {
3120 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
3121 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
3122 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
3123
3124 // Enable InputFilter
3125 mDispatcher->setInputFilterEnabled(true);
3126 // Test touch on both primary and second display, and check if both events are filtered.
3127 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
3128 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
3129
3130 // Disable InputFilter
3131 mDispatcher->setInputFilterEnabled(false);
3132 // Test touch on both primary and second display, and check if both events aren't filtered.
3133 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
3134 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
3135}
3136
3137// Test InputFilter for KeyEvent
3138TEST_F(InputFilterTest, KeyEvent_InputFilter) {
3139 // Since the InputFilter is disabled by default, check if key event aren't filtered.
3140 testNotifyKey(/*expectToBeFiltered*/ false);
3141
3142 // Enable InputFilter
3143 mDispatcher->setInputFilterEnabled(true);
3144 // Send a key event, and check if it is filtered.
3145 testNotifyKey(/*expectToBeFiltered*/ true);
3146
3147 // Disable InputFilter
3148 mDispatcher->setInputFilterEnabled(false);
3149 // Send a key event, and check if it isn't filtered.
3150 testNotifyKey(/*expectToBeFiltered*/ false);
3151}
3152
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003153class InputFilterInjectionPolicyTest : public InputDispatcherTest {
3154protected:
3155 virtual void SetUp() override {
3156 InputDispatcherTest::SetUp();
3157
3158 /**
3159 * We don't need to enable input filter to test the injected event policy, but we enabled it
3160 * here to make the tests more realistic, since this policy only matters when inputfilter is
3161 * on.
3162 */
3163 mDispatcher->setInputFilterEnabled(true);
3164
3165 std::shared_ptr<InputApplicationHandle> application =
3166 std::make_shared<FakeApplicationHandle>();
3167 mWindow =
3168 new FakeWindowHandle(application, mDispatcher, "Test Window", ADISPLAY_ID_DEFAULT);
3169
3170 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3171 mWindow->setFocusable(true);
3172 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3173 setFocusedWindow(mWindow);
3174 mWindow->consumeFocusEvent(true);
3175 }
3176
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003177 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
3178 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003179 KeyEvent event;
3180
3181 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
3182 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
3183 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
3184 KEY_A, AMETA_NONE, 0 /*repeatCount*/, eventTime, eventTime);
3185 const int32_t additionalPolicyFlags =
3186 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
3187 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3188 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
3189 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
3190 policyFlags | additionalPolicyFlags));
3191
3192 InputEvent* received = mWindow->consume();
3193 ASSERT_NE(nullptr, received);
3194 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003195 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_KEY);
3196 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
3197 ASSERT_EQ(flags, keyEvent.getFlags());
3198 }
3199
3200 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
3201 int32_t flags) {
3202 MotionEvent event;
3203 PointerProperties pointerProperties[1];
3204 PointerCoords pointerCoords[1];
3205 pointerProperties[0].clear();
3206 pointerProperties[0].id = 0;
3207 pointerCoords[0].clear();
3208 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
3209 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
3210
3211 ui::Transform identityTransform;
3212 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
3213 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
3214 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
3215 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
3216 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -07003217 AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0,
3218 0 /*INVALID_DISPLAY_SIZE*/, 0 /*INVALID_DISPLAY_SIZE*/, eventTime,
3219 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003220 /*pointerCount*/ 1, pointerProperties, pointerCoords);
3221
3222 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
3223 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3224 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
3225 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
3226 policyFlags | additionalPolicyFlags));
3227
3228 InputEvent* received = mWindow->consume();
3229 ASSERT_NE(nullptr, received);
3230 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
3231 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_MOTION);
3232 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
3233 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003234 }
3235
3236private:
3237 sp<FakeWindowHandle> mWindow;
3238};
3239
3240TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003241 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
3242 // filter. Without it, the event will no different from a regularly injected event, and the
3243 // injected device id will be overwritten.
3244 testInjectedKey(POLICY_FLAG_FILTERED, 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
3245 0 /*flags*/);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003246}
3247
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003248TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003249 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003250 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
3251 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
3252}
3253
3254TEST_F(InputFilterInjectionPolicyTest,
3255 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
3256 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
3257 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
3258 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003259}
3260
3261TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
3262 testInjectedKey(0 /*policyFlags*/, 3 /*injectedDeviceId*/,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003263 VIRTUAL_KEYBOARD_ID /*resolvedDeviceId*/, 0 /*flags*/);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003264}
3265
chaviwfd6d3512019-03-25 13:23:49 -07003266class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07003267 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07003268 InputDispatcherTest::SetUp();
3269
Chris Yea209fde2020-07-22 13:54:51 -07003270 std::shared_ptr<FakeApplicationHandle> application =
3271 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003272 mUnfocusedWindow =
3273 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07003274 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
3275 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3276 // window.
chaviw3277faf2021-05-19 16:45:23 -05003277 mUnfocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07003278
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003279 mFocusedWindow =
3280 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
3281 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviw3277faf2021-05-19 16:45:23 -05003282 mFocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07003283
3284 // Set focused application.
3285 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07003286 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07003287
3288 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00003289 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003290 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003291 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07003292 }
3293
Prabir Pradhan3608aad2019-10-02 17:08:26 -07003294 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07003295 InputDispatcherTest::TearDown();
3296
3297 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003298 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07003299 }
3300
3301protected:
3302 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003303 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003304 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07003305};
3306
3307// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
3308// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
3309// the onPointerDownOutsideFocus callback.
3310TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003311 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003312 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3313 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003314 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003315 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07003316
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003317 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07003318 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
3319}
3320
3321// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
3322// DOWN on the window that doesn't have focus. Ensure no window received the
3323// onPointerDownOutsideFocus callback.
3324TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003325 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003326 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003327 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003328 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07003329
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003330 ASSERT_TRUE(mDispatcher->waitForIdle());
3331 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07003332}
3333
3334// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
3335// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
3336TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003337 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3338 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003339 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003340 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07003341
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003342 ASSERT_TRUE(mDispatcher->waitForIdle());
3343 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07003344}
3345
3346// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
3347// DOWN on the window that already has focus. Ensure no window received the
3348// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003349TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003350 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003351 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003352 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003353 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003354 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07003355
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003356 ASSERT_TRUE(mDispatcher->waitForIdle());
3357 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07003358}
3359
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08003360// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
3361// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
3362TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
3363 const MotionEvent event =
3364 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3365 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
3366 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(20).y(20))
3367 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
3368 .build();
3369 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
3370 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3371 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
3372
3373 ASSERT_TRUE(mDispatcher->waitForIdle());
3374 mFakePolicy->assertOnPointerDownWasNotCalled();
3375 // Ensure that the unfocused window did not receive any FOCUS events.
3376 mUnfocusedWindow->assertNoEvents();
3377}
3378
chaviwaf87b3e2019-10-01 16:59:28 -07003379// These tests ensures we can send touch events to a single client when there are multiple input
3380// windows that point to the same client token.
3381class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
3382 virtual void SetUp() override {
3383 InputDispatcherTest::SetUp();
3384
Chris Yea209fde2020-07-22 13:54:51 -07003385 std::shared_ptr<FakeApplicationHandle> application =
3386 std::make_shared<FakeApplicationHandle>();
chaviwaf87b3e2019-10-01 16:59:28 -07003387 mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1",
3388 ADISPLAY_ID_DEFAULT);
3389 // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window.
3390 // We also need FLAG_SPLIT_TOUCH or we won't be able to get touches for both windows.
chaviw3277faf2021-05-19 16:45:23 -05003391 mWindow1->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07003392 mWindow1->setFrame(Rect(0, 0, 100, 100));
3393
3394 mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2",
3395 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviw3277faf2021-05-19 16:45:23 -05003396 mWindow2->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07003397 mWindow2->setFrame(Rect(100, 100, 200, 200));
3398
Arthur Hung72d8dc32020-03-28 00:48:39 +00003399 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07003400 }
3401
3402protected:
3403 sp<FakeWindowHandle> mWindow1;
3404 sp<FakeWindowHandle> mWindow2;
3405
3406 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05003407 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07003408 vec2 vals = windowInfo->transform.transform(point.x, point.y);
3409 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07003410 }
3411
3412 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
3413 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003414 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07003415 InputEvent* event = window->consume();
3416
3417 ASSERT_NE(nullptr, event) << name.c_str()
3418 << ": consumer should have returned non-NULL event.";
3419
3420 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
3421 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
3422 << " event, got " << inputEventTypeToString(event->getType()) << " event";
3423
3424 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
3425 EXPECT_EQ(expectedAction, motionEvent.getAction());
3426
3427 for (size_t i = 0; i < points.size(); i++) {
3428 float expectedX = points[i].x;
3429 float expectedY = points[i].y;
3430
3431 EXPECT_EQ(expectedX, motionEvent.getX(i))
3432 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
3433 << ", got " << motionEvent.getX(i);
3434 EXPECT_EQ(expectedY, motionEvent.getY(i))
3435 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
3436 << ", got " << motionEvent.getY(i);
3437 }
3438 }
chaviw9eaa22c2020-07-01 16:21:27 -07003439
3440 void touchAndAssertPositions(int32_t action, std::vector<PointF> touchedPoints,
3441 std::vector<PointF> expectedPoints) {
3442 NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
3443 ADISPLAY_ID_DEFAULT, touchedPoints);
3444 mDispatcher->notifyMotion(&motionArgs);
3445
3446 // Always consume from window1 since it's the window that has the InputReceiver
3447 consumeMotionEvent(mWindow1, action, expectedPoints);
3448 }
chaviwaf87b3e2019-10-01 16:59:28 -07003449};
3450
3451TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
3452 // Touch Window 1
3453 PointF touchedPoint = {10, 10};
3454 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003455 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003456
3457 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07003458 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003459
3460 // Touch Window 2
3461 touchedPoint = {150, 150};
3462 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003463 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003464}
3465
chaviw9eaa22c2020-07-01 16:21:27 -07003466TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
3467 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07003468 mWindow2->setWindowScale(0.5f, 0.5f);
3469
3470 // Touch Window 1
3471 PointF touchedPoint = {10, 10};
3472 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003473 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003474 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07003475 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003476
3477 // Touch Window 2
3478 touchedPoint = {150, 150};
3479 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003480 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
3481 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003482
chaviw9eaa22c2020-07-01 16:21:27 -07003483 // Update the transform so rotation is set
3484 mWindow2->setWindowTransform(0, -1, 1, 0);
3485 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
3486 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003487}
3488
chaviw9eaa22c2020-07-01 16:21:27 -07003489TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003490 mWindow2->setWindowScale(0.5f, 0.5f);
3491
3492 // Touch Window 1
3493 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3494 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003495 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003496
3497 // Touch Window 2
3498 int32_t actionPointerDown =
3499 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003500 touchedPoints.push_back(PointF{150, 150});
3501 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3502 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003503
chaviw9eaa22c2020-07-01 16:21:27 -07003504 // Release Window 2
3505 int32_t actionPointerUp =
3506 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3507 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
3508 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003509
chaviw9eaa22c2020-07-01 16:21:27 -07003510 // Update the transform so rotation is set for Window 2
3511 mWindow2->setWindowTransform(0, -1, 1, 0);
3512 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3513 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003514}
3515
chaviw9eaa22c2020-07-01 16:21:27 -07003516TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003517 mWindow2->setWindowScale(0.5f, 0.5f);
3518
3519 // Touch Window 1
3520 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3521 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003522 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003523
3524 // Touch Window 2
3525 int32_t actionPointerDown =
3526 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003527 touchedPoints.push_back(PointF{150, 150});
3528 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003529
chaviw9eaa22c2020-07-01 16:21:27 -07003530 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003531
3532 // Move both windows
3533 touchedPoints = {{20, 20}, {175, 175}};
3534 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3535 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3536
chaviw9eaa22c2020-07-01 16:21:27 -07003537 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003538
chaviw9eaa22c2020-07-01 16:21:27 -07003539 // Release Window 2
3540 int32_t actionPointerUp =
3541 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3542 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
3543 expectedPoints.pop_back();
3544
3545 // Touch Window 2
3546 mWindow2->setWindowTransform(0, -1, 1, 0);
3547 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3548 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
3549
3550 // Move both windows
3551 touchedPoints = {{20, 20}, {175, 175}};
3552 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3553 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3554
3555 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003556}
3557
3558TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
3559 mWindow1->setWindowScale(0.5f, 0.5f);
3560
3561 // Touch Window 1
3562 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3563 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003564 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003565
3566 // Touch Window 2
3567 int32_t actionPointerDown =
3568 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003569 touchedPoints.push_back(PointF{150, 150});
3570 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003571
chaviw9eaa22c2020-07-01 16:21:27 -07003572 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003573
3574 // Move both windows
3575 touchedPoints = {{20, 20}, {175, 175}};
3576 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3577 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3578
chaviw9eaa22c2020-07-01 16:21:27 -07003579 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003580}
3581
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003582class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
3583 virtual void SetUp() override {
3584 InputDispatcherTest::SetUp();
3585
Chris Yea209fde2020-07-22 13:54:51 -07003586 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003587 mApplication->setDispatchingTimeout(20ms);
3588 mWindow =
3589 new FakeWindowHandle(mApplication, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
3590 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05003591 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07003592 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003593 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3594 // window.
chaviw3277faf2021-05-19 16:45:23 -05003595 mWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003596
3597 // Set focused application.
3598 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
3599
3600 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003601 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003602 mWindow->consumeFocusEvent(true);
3603 }
3604
3605 virtual void TearDown() override {
3606 InputDispatcherTest::TearDown();
3607 mWindow.clear();
3608 }
3609
3610protected:
Chris Yea209fde2020-07-22 13:54:51 -07003611 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003612 sp<FakeWindowHandle> mWindow;
3613 static constexpr PointF WINDOW_LOCATION = {20, 20};
3614
3615 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003616 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003617 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3618 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003619 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003620 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3621 WINDOW_LOCATION));
3622 }
3623};
3624
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003625// Send a tap and respond, which should not cause an ANR.
3626TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
3627 tapOnWindow();
3628 mWindow->consumeMotionDown();
3629 mWindow->consumeMotionUp();
3630 ASSERT_TRUE(mDispatcher->waitForIdle());
3631 mFakePolicy->assertNotifyAnrWasNotCalled();
3632}
3633
3634// Send a regular key and respond, which should not cause an ANR.
3635TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003636 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003637 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
3638 ASSERT_TRUE(mDispatcher->waitForIdle());
3639 mFakePolicy->assertNotifyAnrWasNotCalled();
3640}
3641
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003642TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
3643 mWindow->setFocusable(false);
3644 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3645 mWindow->consumeFocusEvent(false);
3646
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003647 InputEventInjectionResult result =
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003648 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003649 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/,
3650 false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003651 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003652 // Key will not go to window because we have no focused window.
3653 // The 'no focused window' ANR timer should start instead.
3654
3655 // Now, the focused application goes away.
3656 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
3657 // The key should get dropped and there should be no ANR.
3658
3659 ASSERT_TRUE(mDispatcher->waitForIdle());
3660 mFakePolicy->assertNotifyAnrWasNotCalled();
3661}
3662
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003663// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003664// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
3665// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003666TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003667 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003668 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3669 WINDOW_LOCATION));
3670
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003671 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
3672 ASSERT_TRUE(sequenceNum);
3673 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003674 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003675
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003676 mWindow->finishEvent(*sequenceNum);
3677 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3678 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003679 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003680 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003681}
3682
3683// Send a key to the app and have the app not respond right away.
3684TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
3685 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003686 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003687 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
3688 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003689 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003690 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003691 ASSERT_TRUE(mDispatcher->waitForIdle());
3692}
3693
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003694// We have a focused application, but no focused window
3695TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003696 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003697 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3698 mWindow->consumeFocusEvent(false);
3699
3700 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003701 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003702 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3703 WINDOW_LOCATION));
3704 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
3705 mDispatcher->waitForIdle();
3706 mFakePolicy->assertNotifyAnrWasNotCalled();
3707
3708 // Once a focused event arrives, we get an ANR for this application
3709 // We specify the injection timeout to be smaller than the application timeout, to ensure that
3710 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003711 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003712 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003713 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003714 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003715 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003716 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003717 ASSERT_TRUE(mDispatcher->waitForIdle());
3718}
3719
3720// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003721// Make sure that we don't notify policy twice about the same ANR.
3722TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003723 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003724 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3725 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003726
3727 // Once a focused event arrives, we get an ANR for this application
3728 // We specify the injection timeout to be smaller than the application timeout, to ensure that
3729 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003730 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003731 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003732 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003733 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003734 const std::chrono::duration appTimeout =
3735 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003736 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003737
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003738 std::this_thread::sleep_for(appTimeout);
3739 // ANR should not be raised again. It is up to policy to do that if it desires.
3740 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003741
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003742 // If we now get a focused window, the ANR should stop, but the policy handles that via
3743 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003744 ASSERT_TRUE(mDispatcher->waitForIdle());
3745}
3746
3747// We have a focused application, but no focused window
3748TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003749 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003750 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3751 mWindow->consumeFocusEvent(false);
3752
3753 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003754 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003755 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003756 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3757 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003758
3759 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003760 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003761
3762 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003763 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003764 ASSERT_TRUE(mDispatcher->waitForIdle());
3765 mWindow->assertNoEvents();
3766}
3767
3768/**
3769 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
3770 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
3771 * If we process 1 of the events, but ANR on the second event with the same timestamp,
3772 * the ANR mechanism should still work.
3773 *
3774 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
3775 * DOWN event, while not responding on the second one.
3776 */
3777TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
3778 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
3779 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3780 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
3781 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
3782 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003783 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003784
3785 // Now send ACTION_UP, with identical timestamp
3786 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3787 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
3788 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
3789 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003790 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003791
3792 // We have now sent down and up. Let's consume first event and then ANR on the second.
3793 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3794 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003795 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003796}
3797
3798// If an app is not responding to a key event, gesture monitors should continue to receive
3799// new motion events
3800TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnKey) {
3801 FakeMonitorReceiver monitor =
3802 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
3803 true /*isGestureMonitor*/);
3804
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003805 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3806 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003807 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003808 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003809
3810 // Stuck on the ACTION_UP
3811 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003812 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003813
3814 // New tap will go to the gesture monitor, but not to the window
3815 tapOnWindow();
3816 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3817 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3818
3819 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
3820 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003821 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003822 mWindow->assertNoEvents();
3823 monitor.assertNoEvents();
3824}
3825
3826// If an app is not responding to a motion event, gesture monitors should continue to receive
3827// new motion events
3828TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnMotion) {
3829 FakeMonitorReceiver monitor =
3830 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
3831 true /*isGestureMonitor*/);
3832
3833 tapOnWindow();
3834 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3835 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3836
3837 mWindow->consumeMotionDown();
3838 // Stuck on the ACTION_UP
3839 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003840 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003841
3842 // New tap will go to the gesture monitor, but not to the window
3843 tapOnWindow();
3844 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3845 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3846
3847 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
3848 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003849 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003850 mWindow->assertNoEvents();
3851 monitor.assertNoEvents();
3852}
3853
3854// If a window is unresponsive, then you get anr. if the window later catches up and starts to
3855// process events, you don't get an anr. When the window later becomes unresponsive again, you
3856// get an ANR again.
3857// 1. tap -> block on ACTION_UP -> receive ANR
3858// 2. consume all pending events (= queue becomes healthy again)
3859// 3. tap again -> block on ACTION_UP again -> receive ANR second time
3860TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
3861 tapOnWindow();
3862
3863 mWindow->consumeMotionDown();
3864 // Block on ACTION_UP
3865 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003866 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003867 mWindow->consumeMotionUp(); // Now the connection should be healthy again
3868 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003869 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003870 mWindow->assertNoEvents();
3871
3872 tapOnWindow();
3873 mWindow->consumeMotionDown();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003874 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003875 mWindow->consumeMotionUp();
3876
3877 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003878 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003879 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003880 mWindow->assertNoEvents();
3881}
3882
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003883// If a connection remains unresponsive for a while, make sure policy is only notified once about
3884// it.
3885TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003886 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003887 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3888 WINDOW_LOCATION));
3889
3890 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003891 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003892 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003893 // 'notifyConnectionUnresponsive' should only be called once per connection
3894 mFakePolicy->assertNotifyAnrWasNotCalled();
3895 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003896 mWindow->consumeMotionDown();
3897 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3898 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3899 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003900 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003901 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003902 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003903}
3904
3905/**
3906 * If a window is processing a motion event, and then a key event comes in, the key event should
3907 * not to to the focused window until the motion is processed.
3908 *
3909 * Warning!!!
3910 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
3911 * and the injection timeout that we specify when injecting the key.
3912 * We must have the injection timeout (10ms) be smaller than
3913 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
3914 *
3915 * If that value changes, this test should also change.
3916 */
3917TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
3918 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3919 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3920
3921 tapOnWindow();
3922 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3923 ASSERT_TRUE(downSequenceNum);
3924 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3925 ASSERT_TRUE(upSequenceNum);
3926 // Don't finish the events yet, and send a key
3927 // Injection will "succeed" because we will eventually give up and send the key to the focused
3928 // window even if motions are still being processed. But because the injection timeout is short,
3929 // we will receive INJECTION_TIMED_OUT as the result.
3930
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003931 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003932 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003933 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3934 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003935 // Key will not be sent to the window, yet, because the window is still processing events
3936 // and the key remains pending, waiting for the touch events to be processed
3937 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3938 ASSERT_FALSE(keySequenceNum);
3939
3940 std::this_thread::sleep_for(500ms);
3941 // if we wait long enough though, dispatcher will give up, and still send the key
3942 // to the focused window, even though we have not yet finished the motion event
3943 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3944 mWindow->finishEvent(*downSequenceNum);
3945 mWindow->finishEvent(*upSequenceNum);
3946}
3947
3948/**
3949 * If a window is processing a motion event, and then a key event comes in, the key event should
3950 * not go to the focused window until the motion is processed.
3951 * If then a new motion comes in, then the pending key event should be going to the currently
3952 * focused window right away.
3953 */
3954TEST_F(InputDispatcherSingleWindowAnr,
3955 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
3956 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3957 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3958
3959 tapOnWindow();
3960 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3961 ASSERT_TRUE(downSequenceNum);
3962 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3963 ASSERT_TRUE(upSequenceNum);
3964 // Don't finish the events yet, and send a key
3965 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003966 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003967 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003968 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003969 // At this point, key is still pending, and should not be sent to the application yet.
3970 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3971 ASSERT_FALSE(keySequenceNum);
3972
3973 // Now tap down again. It should cause the pending key to go to the focused window right away.
3974 tapOnWindow();
3975 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
3976 // the other events yet. We can finish events in any order.
3977 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
3978 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
3979 mWindow->consumeMotionDown();
3980 mWindow->consumeMotionUp();
3981 mWindow->assertNoEvents();
3982}
3983
3984class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
3985 virtual void SetUp() override {
3986 InputDispatcherTest::SetUp();
3987
Chris Yea209fde2020-07-22 13:54:51 -07003988 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003989 mApplication->setDispatchingTimeout(10ms);
3990 mUnfocusedWindow =
3991 new FakeWindowHandle(mApplication, mDispatcher, "Unfocused", ADISPLAY_ID_DEFAULT);
3992 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
3993 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3994 // window.
3995 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
chaviw3277faf2021-05-19 16:45:23 -05003996 mUnfocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL |
3997 WindowInfo::Flag::WATCH_OUTSIDE_TOUCH |
3998 WindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003999
4000 mFocusedWindow =
4001 new FakeWindowHandle(mApplication, mDispatcher, "Focused", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05004002 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004003 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviw3277faf2021-05-19 16:45:23 -05004004 mFocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004005
4006 // Set focused application.
4007 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07004008 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004009
4010 // Expect one focus window exist in display.
4011 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004012 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004013 mFocusedWindow->consumeFocusEvent(true);
4014 }
4015
4016 virtual void TearDown() override {
4017 InputDispatcherTest::TearDown();
4018
4019 mUnfocusedWindow.clear();
4020 mFocusedWindow.clear();
4021 }
4022
4023protected:
Chris Yea209fde2020-07-22 13:54:51 -07004024 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004025 sp<FakeWindowHandle> mUnfocusedWindow;
4026 sp<FakeWindowHandle> mFocusedWindow;
4027 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
4028 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
4029 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
4030
4031 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
4032
4033 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
4034
4035private:
4036 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004037 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004038 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4039 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004040 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004041 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4042 location));
4043 }
4044};
4045
4046// If we have 2 windows that are both unresponsive, the one with the shortest timeout
4047// should be ANR'd first.
4048TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004049 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004050 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4051 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004052 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004053 mFocusedWindow->consumeMotionDown();
4054 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
4055 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
4056 // We consumed all events, so no ANR
4057 ASSERT_TRUE(mDispatcher->waitForIdle());
4058 mFakePolicy->assertNotifyAnrWasNotCalled();
4059
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004060 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004061 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4062 FOCUSED_WINDOW_LOCATION));
4063 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
4064 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004065
4066 const std::chrono::duration timeout =
4067 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004068 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004069 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
4070 // sequence to make it consistent
4071 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004072 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004073 mFocusedWindow->consumeMotionDown();
4074 // This cancel is generated because the connection was unresponsive
4075 mFocusedWindow->consumeMotionCancel();
4076 mFocusedWindow->assertNoEvents();
4077 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004078 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004079 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004080 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004081}
4082
4083// If we have 2 windows with identical timeouts that are both unresponsive,
4084// it doesn't matter which order they should have ANR.
4085// But we should receive ANR for both.
4086TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
4087 // Set the timeout for unfocused window to match the focused window
4088 mUnfocusedWindow->setDispatchingTimeout(10ms);
4089 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
4090
4091 tapOnFocusedWindow();
4092 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004093 sp<IBinder> anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms);
4094 sp<IBinder> anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004095
4096 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004097 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
4098 mFocusedWindow->getToken() == anrConnectionToken2);
4099 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
4100 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004101
4102 ASSERT_TRUE(mDispatcher->waitForIdle());
4103 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004104
4105 mFocusedWindow->consumeMotionDown();
4106 mFocusedWindow->consumeMotionUp();
4107 mUnfocusedWindow->consumeMotionOutside();
4108
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004109 sp<IBinder> responsiveToken1 = mFakePolicy->getResponsiveWindowToken();
4110 sp<IBinder> responsiveToken2 = mFakePolicy->getResponsiveWindowToken();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004111
4112 // Both applications should be marked as responsive, in any order
4113 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
4114 mFocusedWindow->getToken() == responsiveToken2);
4115 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
4116 mUnfocusedWindow->getToken() == responsiveToken2);
4117 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004118}
4119
4120// If a window is already not responding, the second tap on the same window should be ignored.
4121// We should also log an error to account for the dropped event (not tested here).
4122// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
4123TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
4124 tapOnFocusedWindow();
4125 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
4126 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
4127 // Receive the events, but don't respond
4128 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
4129 ASSERT_TRUE(downEventSequenceNum);
4130 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
4131 ASSERT_TRUE(upEventSequenceNum);
4132 const std::chrono::duration timeout =
4133 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004134 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004135
4136 // Tap once again
4137 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004138 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004139 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4140 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004141 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004142 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4143 FOCUSED_WINDOW_LOCATION));
4144 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
4145 // valid touch target
4146 mUnfocusedWindow->assertNoEvents();
4147
4148 // Consume the first tap
4149 mFocusedWindow->finishEvent(*downEventSequenceNum);
4150 mFocusedWindow->finishEvent(*upEventSequenceNum);
4151 ASSERT_TRUE(mDispatcher->waitForIdle());
4152 // The second tap did not go to the focused window
4153 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004154 // Since all events are finished, connection should be deemed healthy again
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004155 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004156 mFakePolicy->assertNotifyAnrWasNotCalled();
4157}
4158
4159// If you tap outside of all windows, there will not be ANR
4160TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004161 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004162 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4163 LOCATION_OUTSIDE_ALL_WINDOWS));
4164 ASSERT_TRUE(mDispatcher->waitForIdle());
4165 mFakePolicy->assertNotifyAnrWasNotCalled();
4166}
4167
4168// Since the focused window is paused, tapping on it should not produce any events
4169TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
4170 mFocusedWindow->setPaused(true);
4171 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
4172
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004173 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004174 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4175 FOCUSED_WINDOW_LOCATION));
4176
4177 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
4178 ASSERT_TRUE(mDispatcher->waitForIdle());
4179 // Should not ANR because the window is paused, and touches shouldn't go to it
4180 mFakePolicy->assertNotifyAnrWasNotCalled();
4181
4182 mFocusedWindow->assertNoEvents();
4183 mUnfocusedWindow->assertNoEvents();
4184}
4185
4186/**
4187 * If a window is processing a motion event, and then a key event comes in, the key event should
4188 * not to to the focused window until the motion is processed.
4189 * If a different window becomes focused at this time, the key should go to that window instead.
4190 *
4191 * Warning!!!
4192 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
4193 * and the injection timeout that we specify when injecting the key.
4194 * We must have the injection timeout (10ms) be smaller than
4195 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
4196 *
4197 * If that value changes, this test should also change.
4198 */
4199TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
4200 // Set a long ANR timeout to prevent it from triggering
4201 mFocusedWindow->setDispatchingTimeout(2s);
4202 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
4203
4204 tapOnUnfocusedWindow();
4205 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
4206 ASSERT_TRUE(downSequenceNum);
4207 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
4208 ASSERT_TRUE(upSequenceNum);
4209 // Don't finish the events yet, and send a key
4210 // Injection will succeed because we will eventually give up and send the key to the focused
4211 // window even if motions are still being processed.
4212
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004213 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004214 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004215 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
4216 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004217 // Key will not be sent to the window, yet, because the window is still processing events
4218 // and the key remains pending, waiting for the touch events to be processed
4219 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
4220 ASSERT_FALSE(keySequenceNum);
4221
4222 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07004223 mFocusedWindow->setFocusable(false);
4224 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004225 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004226 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004227
4228 // Focus events should precede the key events
4229 mUnfocusedWindow->consumeFocusEvent(true);
4230 mFocusedWindow->consumeFocusEvent(false);
4231
4232 // Finish the tap events, which should unblock dispatcher
4233 mUnfocusedWindow->finishEvent(*downSequenceNum);
4234 mUnfocusedWindow->finishEvent(*upSequenceNum);
4235
4236 // Now that all queues are cleared and no backlog in the connections, the key event
4237 // can finally go to the newly focused "mUnfocusedWindow".
4238 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4239 mFocusedWindow->assertNoEvents();
4240 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004241 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004242}
4243
4244// When the touch stream is split across 2 windows, and one of them does not respond,
4245// then ANR should be raised and the touch should be canceled for the unresponsive window.
4246// The other window should not be affected by that.
4247TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
4248 // Touch Window 1
4249 NotifyMotionArgs motionArgs =
4250 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4251 ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION});
4252 mDispatcher->notifyMotion(&motionArgs);
4253 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
4254 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
4255
4256 // Touch Window 2
4257 int32_t actionPointerDown =
4258 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
4259
4260 motionArgs =
4261 generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4262 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION});
4263 mDispatcher->notifyMotion(&motionArgs);
4264
4265 const std::chrono::duration timeout =
4266 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004267 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004268
4269 mUnfocusedWindow->consumeMotionDown();
4270 mFocusedWindow->consumeMotionDown();
4271 // Focused window may or may not receive ACTION_MOVE
4272 // But it should definitely receive ACTION_CANCEL due to the ANR
4273 InputEvent* event;
4274 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
4275 ASSERT_TRUE(moveOrCancelSequenceNum);
4276 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
4277 ASSERT_NE(nullptr, event);
4278 ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION);
4279 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
4280 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
4281 mFocusedWindow->consumeMotionCancel();
4282 } else {
4283 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
4284 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004285 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004286 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004287
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004288 mUnfocusedWindow->assertNoEvents();
4289 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004290 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004291}
4292
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05004293/**
4294 * If we have no focused window, and a key comes in, we start the ANR timer.
4295 * The focused application should add a focused window before the timer runs out to prevent ANR.
4296 *
4297 * If the user touches another application during this time, the key should be dropped.
4298 * Next, if a new focused window comes in, without toggling the focused application,
4299 * then no ANR should occur.
4300 *
4301 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
4302 * but in some cases the policy may not update the focused application.
4303 */
4304TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
4305 std::shared_ptr<FakeApplicationHandle> focusedApplication =
4306 std::make_shared<FakeApplicationHandle>();
4307 focusedApplication->setDispatchingTimeout(60ms);
4308 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
4309 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
4310 mFocusedWindow->setFocusable(false);
4311
4312 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
4313 mFocusedWindow->consumeFocusEvent(false);
4314
4315 // Send a key. The ANR timer should start because there is no focused window.
4316 // 'focusedApplication' will get blamed if this timer completes.
4317 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004318 InputEventInjectionResult result =
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05004319 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004320 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/,
4321 false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004322 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05004323
4324 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
4325 // then the injected touches won't cause the focused event to get dropped.
4326 // The dispatcher only checks for whether the queue should be pruned upon queueing.
4327 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
4328 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
4329 // For this test, it means that the key would get delivered to the window once it becomes
4330 // focused.
4331 std::this_thread::sleep_for(10ms);
4332
4333 // Touch unfocused window. This should force the pending key to get dropped.
4334 NotifyMotionArgs motionArgs =
4335 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4336 ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION});
4337 mDispatcher->notifyMotion(&motionArgs);
4338
4339 // We do not consume the motion right away, because that would require dispatcher to first
4340 // process (== drop) the key event, and by that time, ANR will be raised.
4341 // Set the focused window first.
4342 mFocusedWindow->setFocusable(true);
4343 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
4344 setFocusedWindow(mFocusedWindow);
4345 mFocusedWindow->consumeFocusEvent(true);
4346 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
4347 // to another application. This could be a bug / behaviour in the policy.
4348
4349 mUnfocusedWindow->consumeMotionDown();
4350
4351 ASSERT_TRUE(mDispatcher->waitForIdle());
4352 // Should not ANR because we actually have a focused window. It was just added too slowly.
4353 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
4354}
4355
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05004356// These tests ensure we cannot send touch events to a window that's positioned behind a window
4357// that has feature NO_INPUT_CHANNEL.
4358// Layout:
4359// Top (closest to user)
4360// mNoInputWindow (above all windows)
4361// mBottomWindow
4362// Bottom (furthest from user)
4363class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
4364 virtual void SetUp() override {
4365 InputDispatcherTest::SetUp();
4366
4367 mApplication = std::make_shared<FakeApplicationHandle>();
4368 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
4369 "Window without input channel", ADISPLAY_ID_DEFAULT,
4370 std::make_optional<sp<IBinder>>(nullptr) /*token*/);
4371
chaviw3277faf2021-05-19 16:45:23 -05004372 mNoInputWindow->setInputFeatures(WindowInfo::Feature::NO_INPUT_CHANNEL);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05004373 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
4374 // It's perfectly valid for this window to not have an associated input channel
4375
4376 mBottomWindow = new FakeWindowHandle(mApplication, mDispatcher, "Bottom window",
4377 ADISPLAY_ID_DEFAULT);
4378 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
4379
4380 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
4381 }
4382
4383protected:
4384 std::shared_ptr<FakeApplicationHandle> mApplication;
4385 sp<FakeWindowHandle> mNoInputWindow;
4386 sp<FakeWindowHandle> mBottomWindow;
4387};
4388
4389TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
4390 PointF touchedPoint = {10, 10};
4391
4392 NotifyMotionArgs motionArgs =
4393 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4394 ADISPLAY_ID_DEFAULT, {touchedPoint});
4395 mDispatcher->notifyMotion(&motionArgs);
4396
4397 mNoInputWindow->assertNoEvents();
4398 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
4399 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
4400 // and therefore should prevent mBottomWindow from receiving touches
4401 mBottomWindow->assertNoEvents();
4402}
4403
4404/**
4405 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
4406 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
4407 */
4408TEST_F(InputDispatcherMultiWindowOcclusionTests,
4409 NoInputChannelFeature_DropsTouchesWithValidChannel) {
4410 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
4411 "Window with input channel and NO_INPUT_CHANNEL",
4412 ADISPLAY_ID_DEFAULT);
4413
chaviw3277faf2021-05-19 16:45:23 -05004414 mNoInputWindow->setInputFeatures(WindowInfo::Feature::NO_INPUT_CHANNEL);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05004415 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
4416 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
4417
4418 PointF touchedPoint = {10, 10};
4419
4420 NotifyMotionArgs motionArgs =
4421 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4422 ADISPLAY_ID_DEFAULT, {touchedPoint});
4423 mDispatcher->notifyMotion(&motionArgs);
4424
4425 mNoInputWindow->assertNoEvents();
4426 mBottomWindow->assertNoEvents();
4427}
4428
Vishnu Nair958da932020-08-21 17:12:37 -07004429class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
4430protected:
4431 std::shared_ptr<FakeApplicationHandle> mApp;
4432 sp<FakeWindowHandle> mWindow;
4433 sp<FakeWindowHandle> mMirror;
4434
4435 virtual void SetUp() override {
4436 InputDispatcherTest::SetUp();
4437 mApp = std::make_shared<FakeApplicationHandle>();
4438 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
4439 mMirror = new FakeWindowHandle(mApp, mDispatcher, "TestWindowMirror", ADISPLAY_ID_DEFAULT,
4440 mWindow->getToken());
4441 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
4442 mWindow->setFocusable(true);
4443 mMirror->setFocusable(true);
4444 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4445 }
4446};
4447
4448TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
4449 // Request focus on a mirrored window
4450 setFocusedWindow(mMirror);
4451
4452 // window gets focused
4453 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004454 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4455 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004456 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
4457}
4458
4459// A focused & mirrored window remains focused only if the window and its mirror are both
4460// focusable.
4461TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
4462 setFocusedWindow(mMirror);
4463
4464 // window gets focused
4465 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004466 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4467 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004468 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004469 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4470 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004471 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4472
4473 mMirror->setFocusable(false);
4474 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4475
4476 // window loses focus since one of the windows associated with the token in not focusable
4477 mWindow->consumeFocusEvent(false);
4478
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004479 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4480 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004481 mWindow->assertNoEvents();
4482}
4483
4484// A focused & mirrored window remains focused until the window and its mirror both become
4485// invisible.
4486TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
4487 setFocusedWindow(mMirror);
4488
4489 // window gets focused
4490 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004491 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4492 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004493 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004494 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4495 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004496 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4497
4498 mMirror->setVisible(false);
4499 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4500
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004501 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4502 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004503 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004504 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4505 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004506 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4507
4508 mWindow->setVisible(false);
4509 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4510
4511 // window loses focus only after all windows associated with the token become invisible.
4512 mWindow->consumeFocusEvent(false);
4513
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004514 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4515 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004516 mWindow->assertNoEvents();
4517}
4518
4519// A focused & mirrored window remains focused until both windows are removed.
4520TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
4521 setFocusedWindow(mMirror);
4522
4523 // window gets focused
4524 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004525 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4526 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004527 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004528 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4529 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004530 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4531
4532 // single window is removed but the window token remains focused
4533 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
4534
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004535 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4536 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004537 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004538 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4539 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004540 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4541
4542 // Both windows are removed
4543 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
4544 mWindow->consumeFocusEvent(false);
4545
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004546 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4547 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004548 mWindow->assertNoEvents();
4549}
4550
4551// Focus request can be pending until one window becomes visible.
4552TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
4553 // Request focus on an invisible mirror.
4554 mWindow->setVisible(false);
4555 mMirror->setVisible(false);
4556 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4557 setFocusedWindow(mMirror);
4558
4559 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004560 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07004561 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004562 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07004563
4564 mMirror->setVisible(true);
4565 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4566
4567 // window gets focused
4568 mWindow->consumeFocusEvent(true);
4569 // window gets the pending key event
4570 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4571}
Prabir Pradhan99987712020-11-10 18:43:05 -08004572
4573class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
4574protected:
4575 std::shared_ptr<FakeApplicationHandle> mApp;
4576 sp<FakeWindowHandle> mWindow;
4577 sp<FakeWindowHandle> mSecondWindow;
4578
4579 void SetUp() override {
4580 InputDispatcherTest::SetUp();
4581 mApp = std::make_shared<FakeApplicationHandle>();
4582 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
4583 mWindow->setFocusable(true);
4584 mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
4585 mSecondWindow->setFocusable(true);
4586
4587 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
4588 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
4589
4590 setFocusedWindow(mWindow);
4591 mWindow->consumeFocusEvent(true);
4592 }
4593
4594 void notifyPointerCaptureChanged(bool enabled) {
4595 const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(enabled);
4596 mDispatcher->notifyPointerCaptureChanged(&args);
4597 }
4598
4599 void requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, bool enabled) {
4600 mDispatcher->requestPointerCapture(window->getToken(), enabled);
4601 mFakePolicy->waitForSetPointerCapture(enabled);
4602 notifyPointerCaptureChanged(enabled);
4603 window->consumeCaptureEvent(enabled);
4604 }
4605};
4606
4607TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
4608 // Ensure that capture cannot be obtained for unfocused windows.
4609 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
4610 mFakePolicy->assertSetPointerCaptureNotCalled();
4611 mSecondWindow->assertNoEvents();
4612
4613 // Ensure that capture can be enabled from the focus window.
4614 requestAndVerifyPointerCapture(mWindow, true);
4615
4616 // Ensure that capture cannot be disabled from a window that does not have capture.
4617 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
4618 mFakePolicy->assertSetPointerCaptureNotCalled();
4619
4620 // Ensure that capture can be disabled from the window with capture.
4621 requestAndVerifyPointerCapture(mWindow, false);
4622}
4623
4624TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
4625 requestAndVerifyPointerCapture(mWindow, true);
4626
4627 setFocusedWindow(mSecondWindow);
4628
4629 // Ensure that the capture disabled event was sent first.
4630 mWindow->consumeCaptureEvent(false);
4631 mWindow->consumeFocusEvent(false);
4632 mSecondWindow->consumeFocusEvent(true);
4633 mFakePolicy->waitForSetPointerCapture(false);
4634
4635 // Ensure that additional state changes from InputReader are not sent to the window.
4636 notifyPointerCaptureChanged(false);
4637 notifyPointerCaptureChanged(true);
4638 notifyPointerCaptureChanged(false);
4639 mWindow->assertNoEvents();
4640 mSecondWindow->assertNoEvents();
4641 mFakePolicy->assertSetPointerCaptureNotCalled();
4642}
4643
4644TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
4645 requestAndVerifyPointerCapture(mWindow, true);
4646
4647 // InputReader unexpectedly disables and enables pointer capture.
4648 notifyPointerCaptureChanged(false);
4649 notifyPointerCaptureChanged(true);
4650
4651 // Ensure that Pointer Capture is disabled.
Prabir Pradhan7d030382020-12-21 07:58:35 -08004652 mFakePolicy->waitForSetPointerCapture(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08004653 mWindow->consumeCaptureEvent(false);
4654 mWindow->assertNoEvents();
4655}
4656
Prabir Pradhan167e6d92021-02-04 16:18:17 -08004657TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
4658 requestAndVerifyPointerCapture(mWindow, true);
4659
4660 // The first window loses focus.
4661 setFocusedWindow(mSecondWindow);
4662 mFakePolicy->waitForSetPointerCapture(false);
4663 mWindow->consumeCaptureEvent(false);
4664
4665 // Request Pointer Capture from the second window before the notification from InputReader
4666 // arrives.
4667 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
4668 mFakePolicy->waitForSetPointerCapture(true);
4669
4670 // InputReader notifies Pointer Capture was disabled (because of the focus change).
4671 notifyPointerCaptureChanged(false);
4672
4673 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
4674 notifyPointerCaptureChanged(true);
4675
4676 mSecondWindow->consumeFocusEvent(true);
4677 mSecondWindow->consumeCaptureEvent(true);
4678}
4679
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004680class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
4681protected:
4682 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00004683
4684 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
4685 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
4686
4687 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
4688 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
4689
4690 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
4691 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
4692 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
4693 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
4694 MAXIMUM_OBSCURING_OPACITY);
4695
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004696 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004697 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004698 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004699
4700 sp<FakeWindowHandle> mTouchWindow;
4701
4702 virtual void SetUp() override {
4703 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004704 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004705 mDispatcher->setBlockUntrustedTouchesMode(android::os::BlockUntrustedTouchesMode::BLOCK);
4706 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
4707 }
4708
4709 virtual void TearDown() override {
4710 InputDispatcherTest::TearDown();
4711 mTouchWindow.clear();
4712 }
4713
chaviw3277faf2021-05-19 16:45:23 -05004714 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode,
4715 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004716 sp<FakeWindowHandle> window = getWindow(uid, name);
chaviw3277faf2021-05-19 16:45:23 -05004717 window->setFlags(WindowInfo::Flag::NOT_TOUCHABLE);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004718 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004719 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004720 return window;
4721 }
4722
4723 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
4724 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
4725 sp<FakeWindowHandle> window =
4726 new FakeWindowHandle(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
4727 // Generate an arbitrary PID based on the UID
4728 window->setOwnerInfo(1777 + (uid % 10000), uid);
4729 return window;
4730 }
4731
4732 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
4733 NotifyMotionArgs args =
4734 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4735 ADISPLAY_ID_DEFAULT, points);
4736 mDispatcher->notifyMotion(&args);
4737 }
4738};
4739
4740TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004741 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004742 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004743 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004744
4745 touch();
4746
4747 mTouchWindow->assertNoEvents();
4748}
4749
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004750TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00004751 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
4752 const sp<FakeWindowHandle>& w =
4753 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
4754 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4755
4756 touch();
4757
4758 mTouchWindow->assertNoEvents();
4759}
4760
4761TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004762 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
4763 const sp<FakeWindowHandle>& w =
4764 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
4765 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4766
4767 touch();
4768
4769 w->assertNoEvents();
4770}
4771
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004772TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004773 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
4774 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004775
4776 touch();
4777
4778 mTouchWindow->consumeAnyMotionDown();
4779}
4780
4781TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004782 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004783 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004784 w->setFrame(Rect(0, 0, 50, 50));
4785 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004786
4787 touch({PointF{100, 100}});
4788
4789 mTouchWindow->consumeAnyMotionDown();
4790}
4791
4792TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004793 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004794 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004795 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4796
4797 touch();
4798
4799 mTouchWindow->consumeAnyMotionDown();
4800}
4801
4802TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
4803 const sp<FakeWindowHandle>& w =
4804 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4805 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004806
4807 touch();
4808
4809 mTouchWindow->consumeAnyMotionDown();
4810}
4811
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004812TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
4813 const sp<FakeWindowHandle>& w =
4814 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4815 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4816
4817 touch();
4818
4819 w->assertNoEvents();
4820}
4821
4822/**
4823 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
4824 * inside) while letting them pass-through. Note that even though touch passes through the occluding
4825 * window, the occluding window will still receive ACTION_OUTSIDE event.
4826 */
4827TEST_F(InputDispatcherUntrustedTouchesTest,
4828 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
4829 const sp<FakeWindowHandle>& w =
4830 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
chaviw3277faf2021-05-19 16:45:23 -05004831 w->addFlags(WindowInfo::Flag::WATCH_OUTSIDE_TOUCH);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004832 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4833
4834 touch();
4835
4836 w->consumeMotionOutside();
4837}
4838
4839TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
4840 const sp<FakeWindowHandle>& w =
4841 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
chaviw3277faf2021-05-19 16:45:23 -05004842 w->addFlags(WindowInfo::Flag::WATCH_OUTSIDE_TOUCH);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004843 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4844
4845 touch();
4846
4847 InputEvent* event = w->consume();
4848 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType());
4849 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
4850 EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getX());
4851 EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getY());
4852}
4853
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004854TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004855 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004856 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4857 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004858 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4859
4860 touch();
4861
4862 mTouchWindow->consumeAnyMotionDown();
4863}
4864
4865TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
4866 const sp<FakeWindowHandle>& w =
4867 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4868 MAXIMUM_OBSCURING_OPACITY);
4869 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004870
4871 touch();
4872
4873 mTouchWindow->consumeAnyMotionDown();
4874}
4875
4876TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004877 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004878 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4879 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004880 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4881
4882 touch();
4883
4884 mTouchWindow->assertNoEvents();
4885}
4886
4887TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
4888 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
4889 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004890 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
4891 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004892 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004893 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
4894 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004895 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
4896
4897 touch();
4898
4899 mTouchWindow->assertNoEvents();
4900}
4901
4902TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
4903 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
4904 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004905 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
4906 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004907 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004908 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
4909 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004910 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
4911
4912 touch();
4913
4914 mTouchWindow->consumeAnyMotionDown();
4915}
4916
4917TEST_F(InputDispatcherUntrustedTouchesTest,
4918 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
4919 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004920 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4921 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004922 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004923 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
4924 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004925 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
4926
4927 touch();
4928
4929 mTouchWindow->consumeAnyMotionDown();
4930}
4931
4932TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
4933 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004934 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4935 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004936 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004937 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
4938 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004939 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004940
4941 touch();
4942
4943 mTouchWindow->assertNoEvents();
4944}
4945
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004946TEST_F(InputDispatcherUntrustedTouchesTest,
4947 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
4948 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004949 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
4950 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004951 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004952 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4953 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004954 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
4955
4956 touch();
4957
4958 mTouchWindow->assertNoEvents();
4959}
4960
4961TEST_F(InputDispatcherUntrustedTouchesTest,
4962 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
4963 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004964 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
4965 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004966 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004967 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4968 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004969 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
4970
4971 touch();
4972
4973 mTouchWindow->consumeAnyMotionDown();
4974}
4975
4976TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
4977 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004978 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
4979 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004980 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4981
4982 touch();
4983
4984 mTouchWindow->consumeAnyMotionDown();
4985}
4986
4987TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
4988 const sp<FakeWindowHandle>& w =
4989 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
4990 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4991
4992 touch();
4993
4994 mTouchWindow->consumeAnyMotionDown();
4995}
4996
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00004997TEST_F(InputDispatcherUntrustedTouchesTest,
4998 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
4999 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
5000 const sp<FakeWindowHandle>& w =
5001 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
5002 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5003
5004 touch();
5005
5006 mTouchWindow->assertNoEvents();
5007}
5008
5009TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
5010 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
5011 const sp<FakeWindowHandle>& w =
5012 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
5013 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5014
5015 touch();
5016
5017 mTouchWindow->consumeAnyMotionDown();
5018}
5019
5020TEST_F(InputDispatcherUntrustedTouchesTest,
5021 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
5022 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
5023 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005024 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
5025 OPACITY_ABOVE_THRESHOLD);
5026 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5027
5028 touch();
5029
5030 mTouchWindow->consumeAnyMotionDown();
5031}
5032
5033TEST_F(InputDispatcherUntrustedTouchesTest,
5034 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
5035 const sp<FakeWindowHandle>& w1 =
5036 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
5037 OPACITY_BELOW_THRESHOLD);
5038 const sp<FakeWindowHandle>& w2 =
5039 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
5040 OPACITY_BELOW_THRESHOLD);
5041 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
5042
5043 touch();
5044
5045 mTouchWindow->assertNoEvents();
5046}
5047
5048/**
5049 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
5050 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
5051 * (which alone would result in allowing touches) does not affect the blocking behavior.
5052 */
5053TEST_F(InputDispatcherUntrustedTouchesTest,
5054 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
5055 const sp<FakeWindowHandle>& wB =
5056 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
5057 OPACITY_BELOW_THRESHOLD);
5058 const sp<FakeWindowHandle>& wC =
5059 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
5060 OPACITY_BELOW_THRESHOLD);
5061 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
5062
5063 touch();
5064
5065 mTouchWindow->assertNoEvents();
5066}
5067
5068/**
5069 * This test is testing that a window from a different UID but with same application token doesn't
5070 * block the touch. Apps can share the application token for close UI collaboration for example.
5071 */
5072TEST_F(InputDispatcherUntrustedTouchesTest,
5073 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
5074 const sp<FakeWindowHandle>& w =
5075 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
5076 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00005077 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5078
5079 touch();
5080
5081 mTouchWindow->consumeAnyMotionDown();
5082}
5083
arthurhungb89ccb02020-12-30 16:19:01 +08005084class InputDispatcherDragTests : public InputDispatcherTest {
5085protected:
5086 std::shared_ptr<FakeApplicationHandle> mApp;
5087 sp<FakeWindowHandle> mWindow;
5088 sp<FakeWindowHandle> mSecondWindow;
5089 sp<FakeWindowHandle> mDragWindow;
5090
5091 void SetUp() override {
5092 InputDispatcherTest::SetUp();
5093 mApp = std::make_shared<FakeApplicationHandle>();
5094 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
5095 mWindow->setFrame(Rect(0, 0, 100, 100));
chaviw3277faf2021-05-19 16:45:23 -05005096 mWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
arthurhungb89ccb02020-12-30 16:19:01 +08005097
5098 mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
5099 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
chaviw3277faf2021-05-19 16:45:23 -05005100 mSecondWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
arthurhungb89ccb02020-12-30 16:19:01 +08005101
5102 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
5103 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
5104 }
5105
5106 // Start performing drag, we will create a drag window and transfer touch to it.
5107 void performDrag() {
5108 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5109 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5110 {50, 50}))
5111 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5112
5113 // Window should receive motion event.
5114 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5115
5116 // The drag window covers the entire display
5117 mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
5118 mDispatcher->setInputWindows(
5119 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
5120
5121 // Transfer touch focus to the drag window
5122 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
5123 true /* isDragDrop */);
5124 mWindow->consumeMotionCancel();
5125 mDragWindow->consumeMotionDown();
5126 }
arthurhung6d4bed92021-03-17 11:59:33 +08005127
5128 // Start performing drag, we will create a drag window and transfer touch to it.
5129 void performStylusDrag() {
5130 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5131 injectMotionEvent(mDispatcher,
5132 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
5133 AINPUT_SOURCE_STYLUS)
5134 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
5135 .pointer(PointerBuilder(0,
5136 AMOTION_EVENT_TOOL_TYPE_STYLUS)
5137 .x(50)
5138 .y(50))
5139 .build()));
5140 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5141
5142 // The drag window covers the entire display
5143 mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
5144 mDispatcher->setInputWindows(
5145 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
5146
5147 // Transfer touch focus to the drag window
5148 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
5149 true /* isDragDrop */);
5150 mWindow->consumeMotionCancel();
5151 mDragWindow->consumeMotionDown();
5152 }
arthurhungb89ccb02020-12-30 16:19:01 +08005153};
5154
5155TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
5156 performDrag();
5157
5158 // Move on window.
5159 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5160 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5161 ADISPLAY_ID_DEFAULT, {50, 50}))
5162 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5163 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5164 mWindow->consumeDragEvent(false, 50, 50);
5165 mSecondWindow->assertNoEvents();
5166
5167 // Move to another window.
5168 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5169 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5170 ADISPLAY_ID_DEFAULT, {150, 50}))
5171 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5172 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5173 mWindow->consumeDragEvent(true, 150, 50);
5174 mSecondWindow->consumeDragEvent(false, 50, 50);
5175
5176 // Move back to original window.
5177 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5178 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5179 ADISPLAY_ID_DEFAULT, {50, 50}))
5180 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5181 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5182 mWindow->consumeDragEvent(false, 50, 50);
5183 mSecondWindow->consumeDragEvent(true, -50, 50);
5184
5185 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5186 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
5187 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5188 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5189 mWindow->assertNoEvents();
5190 mSecondWindow->assertNoEvents();
5191}
5192
arthurhungf452d0b2021-01-06 00:19:52 +08005193TEST_F(InputDispatcherDragTests, DragAndDrop) {
5194 performDrag();
5195
5196 // Move on window.
5197 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5198 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5199 ADISPLAY_ID_DEFAULT, {50, 50}))
5200 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5201 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5202 mWindow->consumeDragEvent(false, 50, 50);
5203 mSecondWindow->assertNoEvents();
5204
5205 // Move to another window.
5206 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5207 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5208 ADISPLAY_ID_DEFAULT, {150, 50}))
5209 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5210 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5211 mWindow->consumeDragEvent(true, 150, 50);
5212 mSecondWindow->consumeDragEvent(false, 50, 50);
5213
5214 // drop to another window.
5215 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5216 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5217 {150, 50}))
5218 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5219 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5220 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
5221 mWindow->assertNoEvents();
5222 mSecondWindow->assertNoEvents();
5223}
5224
arthurhung6d4bed92021-03-17 11:59:33 +08005225TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
5226 performStylusDrag();
5227
5228 // Move on window and keep button pressed.
5229 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5230 injectMotionEvent(mDispatcher,
5231 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
5232 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
5233 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
5234 .x(50)
5235 .y(50))
5236 .build()))
5237 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5238 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5239 mWindow->consumeDragEvent(false, 50, 50);
5240 mSecondWindow->assertNoEvents();
5241
5242 // Move to another window and release button, expect to drop item.
5243 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5244 injectMotionEvent(mDispatcher,
5245 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
5246 .buttonState(0)
5247 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
5248 .x(150)
5249 .y(50))
5250 .build()))
5251 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5252 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5253 mWindow->assertNoEvents();
5254 mSecondWindow->assertNoEvents();
5255 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
5256
5257 // nothing to the window.
5258 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5259 injectMotionEvent(mDispatcher,
5260 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
5261 .buttonState(0)
5262 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
5263 .x(150)
5264 .y(50))
5265 .build()))
5266 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5267 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5268 mWindow->assertNoEvents();
5269 mSecondWindow->assertNoEvents();
5270}
5271
Arthur Hung6d0571e2021-04-09 20:18:16 +08005272TEST_F(InputDispatcherDragTests, DragAndDrop_InvalidWindow) {
5273 performDrag();
5274
5275 // Set second window invisible.
5276 mSecondWindow->setVisible(false);
5277 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
5278
5279 // Move on window.
5280 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5281 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5282 ADISPLAY_ID_DEFAULT, {50, 50}))
5283 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5284 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5285 mWindow->consumeDragEvent(false, 50, 50);
5286 mSecondWindow->assertNoEvents();
5287
5288 // Move to another window.
5289 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5290 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5291 ADISPLAY_ID_DEFAULT, {150, 50}))
5292 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5293 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5294 mWindow->consumeDragEvent(true, 150, 50);
5295 mSecondWindow->assertNoEvents();
5296
5297 // drop to another window.
5298 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5299 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5300 {150, 50}))
5301 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5302 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5303 mFakePolicy->assertDropTargetEquals(nullptr);
5304 mWindow->assertNoEvents();
5305 mSecondWindow->assertNoEvents();
5306}
5307
Garfield Tane84e6f92019-08-29 17:28:41 -07005308} // namespace android::inputdispatcher