blob: a5682200e4493f15699fa08ec5b3c75015c6611f [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
940 virtual bool updateInfo() { return true; }
941
Vishnu Nair47074b82020-08-14 11:54:47 -0700942 void setFocusable(bool focusable) { mInfo.focusable = focusable; }
chaviwd1c23182019-12-20 18:44:56 -0800943
Vishnu Nair958da932020-08-21 17:12:37 -0700944 void setVisible(bool visible) { mInfo.visible = visible; }
945
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700946 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500947 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700948 }
949
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700950 void setPaused(bool paused) { mInfo.paused = paused; }
951
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000952 void setAlpha(float alpha) { mInfo.alpha = alpha; }
953
chaviw3277faf2021-05-19 16:45:23 -0500954 void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; }
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000955
Bernardo Rufino7393d172021-02-26 13:56:11 +0000956 void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
957
chaviwd1c23182019-12-20 18:44:56 -0800958 void setFrame(const Rect& frame) {
959 mInfo.frameLeft = frame.left;
960 mInfo.frameTop = frame.top;
961 mInfo.frameRight = frame.right;
962 mInfo.frameBottom = frame.bottom;
arthurhungb89ccb02020-12-30 16:19:01 +0800963 mInfo.transform.set(-frame.left, -frame.top);
chaviwd1c23182019-12-20 18:44:56 -0800964 mInfo.touchableRegion.clear();
965 mInfo.addTouchableRegion(frame);
966 }
967
chaviw3277faf2021-05-19 16:45:23 -0500968 void addFlags(Flags<WindowInfo::Flag> flags) { mInfo.flags |= flags; }
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +0000969
chaviw3277faf2021-05-19 16:45:23 -0500970 void setFlags(Flags<WindowInfo::Flag> flags) { mInfo.flags = flags; }
chaviwd1c23182019-12-20 18:44:56 -0800971
chaviw3277faf2021-05-19 16:45:23 -0500972 void setInputFeatures(WindowInfo::Feature features) { mInfo.inputFeatures = features; }
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500973
chaviw9eaa22c2020-07-01 16:21:27 -0700974 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
975 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
976 }
977
978 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -0700979
yunho.shinf4a80b82020-11-16 21:13:57 +0900980 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
981
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800982 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
983 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
984 expectedFlags);
985 }
986
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700987 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
988 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
989 }
990
Svet Ganov5d3bc372020-01-26 23:11:07 -0800991 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000992 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800993 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId,
994 expectedFlags);
995 }
996
997 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000998 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800999 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId,
1000 expectedFlags);
1001 }
1002
1003 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001004 int32_t expectedFlags = 0) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001005 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
1006 }
1007
1008 void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
1009 std::optional<int32_t> expectedFlags = std::nullopt) {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001010 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
1011 expectedFlags);
1012 }
1013
Svet Ganov5d3bc372020-01-26 23:11:07 -08001014 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001015 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1016 int32_t expectedFlags = 0) {
1017 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
1018 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001019 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
1020 }
1021
1022 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001023 int32_t expectedFlags = 0) {
1024 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
1025 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001026 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
1027 }
1028
1029 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001030 int32_t expectedFlags = 0) {
Michael Wright3a240c42019-12-10 20:53:41 +00001031 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
1032 expectedFlags);
1033 }
1034
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001035 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1036 int32_t expectedFlags = 0) {
1037 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
1038 expectedFlags);
1039 }
1040
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001041 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
1042 ASSERT_NE(mInputReceiver, nullptr)
1043 << "Cannot consume events from a window with no receiver";
1044 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
1045 }
1046
Prabir Pradhan99987712020-11-10 18:43:05 -08001047 void consumeCaptureEvent(bool hasCapture) {
1048 ASSERT_NE(mInputReceiver, nullptr)
1049 << "Cannot consume events from a window with no receiver";
1050 mInputReceiver->consumeCaptureEvent(hasCapture);
1051 }
1052
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001053 void consumeEvent(int32_t expectedEventType, int32_t expectedAction,
1054 std::optional<int32_t> expectedDisplayId,
1055 std::optional<int32_t> expectedFlags) {
chaviwd1c23182019-12-20 18:44:56 -08001056 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
1057 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
1058 expectedFlags);
1059 }
1060
arthurhungb89ccb02020-12-30 16:19:01 +08001061 void consumeDragEvent(bool isExiting, float x, float y) {
1062 mInputReceiver->consumeDragEvent(isExiting, x, y);
1063 }
1064
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001065 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001066 if (mInputReceiver == nullptr) {
1067 ADD_FAILURE() << "Invalid receive event on window with no receiver";
1068 return std::nullopt;
1069 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001070 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001071 }
1072
1073 void finishEvent(uint32_t sequenceNum) {
1074 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1075 mInputReceiver->finishEvent(sequenceNum);
1076 }
1077
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001078 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
1079 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1080 mInputReceiver->sendTimeline(inputEventId, timeline);
1081 }
1082
chaviwaf87b3e2019-10-01 16:59:28 -07001083 InputEvent* consume() {
1084 if (mInputReceiver == nullptr) {
1085 return nullptr;
1086 }
1087 return mInputReceiver->consume();
1088 }
1089
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001090 MotionEvent* consumeMotion() {
1091 InputEvent* event = consume();
1092 if (event == nullptr) {
1093 ADD_FAILURE() << "Consume failed : no event";
1094 return nullptr;
1095 }
1096 if (event->getType() != AINPUT_EVENT_TYPE_MOTION) {
1097 ADD_FAILURE() << "Instead of motion event, got "
1098 << inputEventTypeToString(event->getType());
1099 return nullptr;
1100 }
1101 return static_cast<MotionEvent*>(event);
1102 }
1103
Arthur Hungb92218b2018-08-14 12:00:21 +08001104 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001105 if (mInputReceiver == nullptr &&
chaviw3277faf2021-05-19 16:45:23 -05001106 mInfo.inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL)) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001107 return; // Can't receive events if the window does not have input channel
1108 }
1109 ASSERT_NE(nullptr, mInputReceiver)
1110 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -08001111 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +08001112 }
1113
chaviwaf87b3e2019-10-01 16:59:28 -07001114 sp<IBinder> getToken() { return mInfo.token; }
1115
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001116 const std::string& getName() { return mName; }
1117
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001118 void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) {
1119 mInfo.ownerPid = ownerPid;
1120 mInfo.ownerUid = ownerUid;
1121 }
1122
chaviwd1c23182019-12-20 18:44:56 -08001123private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001124 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -08001125 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001126 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001127};
1128
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001129std::atomic<int32_t> FakeWindowHandle::sId{1};
1130
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001131static InputEventInjectionResult injectKey(
1132 const sp<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
1133 int32_t displayId = ADISPLAY_ID_NONE,
1134 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001135 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
1136 bool allowKeyRepeat = true) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001137 KeyEvent event;
1138 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1139
1140 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001141 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001142 INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE,
1143 repeatCount, currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001144
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001145 int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
1146 if (!allowKeyRepeat) {
1147 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
1148 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001149 // Inject event until dispatch out.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001150 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, syncMode,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001151 injectionTimeout, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001152}
1153
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001154static InputEventInjectionResult injectKeyDown(const sp<InputDispatcher>& dispatcher,
1155 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001156 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId);
1157}
1158
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001159// Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without
1160// sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it
1161// has to be woken up to process the repeating key.
1162static InputEventInjectionResult injectKeyDownNoRepeat(const sp<InputDispatcher>& dispatcher,
1163 int32_t displayId = ADISPLAY_ID_NONE) {
1164 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId,
1165 InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT,
1166 /* allowKeyRepeat */ false);
1167}
1168
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001169static InputEventInjectionResult injectKeyUp(const sp<InputDispatcher>& dispatcher,
1170 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001171 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId);
1172}
1173
Garfield Tandf26e862020-07-01 20:18:19 -07001174class PointerBuilder {
1175public:
1176 PointerBuilder(int32_t id, int32_t toolType) {
1177 mProperties.clear();
1178 mProperties.id = id;
1179 mProperties.toolType = toolType;
1180 mCoords.clear();
1181 }
1182
1183 PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); }
1184
1185 PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); }
1186
1187 PointerBuilder& axis(int32_t axis, float value) {
1188 mCoords.setAxisValue(axis, value);
1189 return *this;
1190 }
1191
1192 PointerProperties buildProperties() const { return mProperties; }
1193
1194 PointerCoords buildCoords() const { return mCoords; }
1195
1196private:
1197 PointerProperties mProperties;
1198 PointerCoords mCoords;
1199};
1200
1201class MotionEventBuilder {
1202public:
1203 MotionEventBuilder(int32_t action, int32_t source) {
1204 mAction = action;
1205 mSource = source;
1206 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1207 }
1208
1209 MotionEventBuilder& eventTime(nsecs_t eventTime) {
1210 mEventTime = eventTime;
1211 return *this;
1212 }
1213
1214 MotionEventBuilder& displayId(int32_t displayId) {
1215 mDisplayId = displayId;
1216 return *this;
1217 }
1218
1219 MotionEventBuilder& actionButton(int32_t actionButton) {
1220 mActionButton = actionButton;
1221 return *this;
1222 }
1223
arthurhung6d4bed92021-03-17 11:59:33 +08001224 MotionEventBuilder& buttonState(int32_t buttonState) {
1225 mButtonState = buttonState;
Garfield Tandf26e862020-07-01 20:18:19 -07001226 return *this;
1227 }
1228
1229 MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) {
1230 mRawXCursorPosition = rawXCursorPosition;
1231 return *this;
1232 }
1233
1234 MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) {
1235 mRawYCursorPosition = rawYCursorPosition;
1236 return *this;
1237 }
1238
1239 MotionEventBuilder& pointer(PointerBuilder pointer) {
1240 mPointers.push_back(pointer);
1241 return *this;
1242 }
1243
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001244 MotionEventBuilder& addFlag(uint32_t flags) {
1245 mFlags |= flags;
1246 return *this;
1247 }
1248
Garfield Tandf26e862020-07-01 20:18:19 -07001249 MotionEvent build() {
1250 std::vector<PointerProperties> pointerProperties;
1251 std::vector<PointerCoords> pointerCoords;
1252 for (const PointerBuilder& pointer : mPointers) {
1253 pointerProperties.push_back(pointer.buildProperties());
1254 pointerCoords.push_back(pointer.buildCoords());
1255 }
1256
1257 // Set mouse cursor position for the most common cases to avoid boilerplate.
1258 if (mSource == AINPUT_SOURCE_MOUSE &&
1259 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) &&
1260 mPointers.size() == 1) {
1261 mRawXCursorPosition = pointerCoords[0].getX();
1262 mRawYCursorPosition = pointerCoords[0].getY();
1263 }
1264
1265 MotionEvent event;
chaviw9eaa22c2020-07-01 16:21:27 -07001266 ui::Transform identityTransform;
Garfield Tandf26e862020-07-01 20:18:19 -07001267 event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001268 mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE,
chaviw9eaa22c2020-07-01 16:21:27 -07001269 mButtonState, MotionClassification::NONE, identityTransform,
1270 /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition,
Evan Rosky09576692021-07-01 12:22:09 -07001271 mRawYCursorPosition, mDisplayOrientation, mDisplayWidth, mDisplayHeight,
1272 mEventTime, mEventTime, mPointers.size(), pointerProperties.data(),
1273 pointerCoords.data());
Garfield Tandf26e862020-07-01 20:18:19 -07001274
1275 return event;
1276 }
1277
1278private:
1279 int32_t mAction;
1280 int32_t mSource;
1281 nsecs_t mEventTime;
1282 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1283 int32_t mActionButton{0};
1284 int32_t mButtonState{0};
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001285 int32_t mFlags{0};
Garfield Tandf26e862020-07-01 20:18:19 -07001286 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1287 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
Evan Rosky09576692021-07-01 12:22:09 -07001288 uint32_t mDisplayOrientation{ui::Transform::ROT_0};
chaviw3277faf2021-05-19 16:45:23 -05001289 int32_t mDisplayWidth{INVALID_DISPLAY_SIZE};
1290 int32_t mDisplayHeight{INVALID_DISPLAY_SIZE};
Garfield Tandf26e862020-07-01 20:18:19 -07001291
1292 std::vector<PointerBuilder> mPointers;
1293};
1294
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001295static InputEventInjectionResult injectMotionEvent(
Garfield Tandf26e862020-07-01 20:18:19 -07001296 const sp<InputDispatcher>& dispatcher, const MotionEvent& event,
1297 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001298 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT) {
Garfield Tandf26e862020-07-01 20:18:19 -07001299 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, injectionMode,
1300 injectionTimeout,
1301 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
1302}
1303
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001304static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001305 const sp<InputDispatcher>& dispatcher, int32_t action, int32_t source, int32_t displayId,
1306 const PointF& position,
1307 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001308 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1309 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001310 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001311 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC)) {
Garfield Tandf26e862020-07-01 20:18:19 -07001312 MotionEvent event = MotionEventBuilder(action, source)
1313 .displayId(displayId)
1314 .eventTime(eventTime)
1315 .rawXCursorPosition(cursorPosition.x)
1316 .rawYCursorPosition(cursorPosition.y)
1317 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1318 .x(position.x)
1319 .y(position.y))
1320 .build();
Arthur Hungb92218b2018-08-14 12:00:21 +08001321
1322 // Inject event until dispatch out.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001323 return injectMotionEvent(dispatcher, event, injectionTimeout, injectionMode);
Arthur Hungb92218b2018-08-14 12:00:21 +08001324}
1325
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001326static InputEventInjectionResult injectMotionDown(const sp<InputDispatcher>& dispatcher,
1327 int32_t source, int32_t displayId,
1328 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001329 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001330}
1331
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001332static InputEventInjectionResult injectMotionUp(const sp<InputDispatcher>& dispatcher,
1333 int32_t source, int32_t displayId,
1334 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001335 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001336}
1337
Jackal Guof9696682018-10-05 12:23:23 +08001338static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1339 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1340 // Define a valid key event.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001341 NotifyKeyArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1342 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A,
1343 KEY_A, AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001344
1345 return args;
1346}
1347
chaviwd1c23182019-12-20 18:44:56 -08001348static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId,
1349 const std::vector<PointF>& points) {
1350 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001351 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1352 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1353 }
1354
chaviwd1c23182019-12-20 18:44:56 -08001355 PointerProperties pointerProperties[pointerCount];
1356 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001357
chaviwd1c23182019-12-20 18:44:56 -08001358 for (size_t i = 0; i < pointerCount; i++) {
1359 pointerProperties[i].clear();
1360 pointerProperties[i].id = i;
1361 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001362
chaviwd1c23182019-12-20 18:44:56 -08001363 pointerCoords[i].clear();
1364 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1365 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1366 }
Jackal Guof9696682018-10-05 12:23:23 +08001367
1368 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1369 // Define a valid motion event.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001370 NotifyMotionArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001371 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1372 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001373 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1374 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001375 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1376 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001377
1378 return args;
1379}
1380
chaviwd1c23182019-12-20 18:44:56 -08001381static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1382 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1383}
1384
Prabir Pradhan99987712020-11-10 18:43:05 -08001385static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(bool enabled) {
1386 return NotifyPointerCaptureChangedArgs(/* id */ 0, systemTime(SYSTEM_TIME_MONOTONIC), enabled);
1387}
1388
Arthur Hungb92218b2018-08-14 12:00:21 +08001389TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001390 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001391 sp<FakeWindowHandle> window =
1392 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001393
Arthur Hung72d8dc32020-03-28 00:48:39 +00001394 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001395 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1396 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1397 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001398
1399 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001400 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001401}
1402
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001403/**
1404 * Calling setInputWindows once with FLAG_NOT_TOUCH_MODAL should not cause any issues.
1405 * To ensure that window receives only events that were directly inside of it, add
1406 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1407 * when finding touched windows.
1408 * This test serves as a sanity check for the next test, where setInputWindows is
1409 * called twice.
1410 */
1411TEST_F(InputDispatcherTest, SetInputWindowOnce_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001412 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001413 sp<FakeWindowHandle> window =
1414 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1415 window->setFrame(Rect(0, 0, 100, 100));
chaviw3277faf2021-05-19 16:45:23 -05001416 window->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001417
1418 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001419 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001420 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1421 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001422 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001423
1424 // Window should receive motion event.
1425 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1426}
1427
1428/**
1429 * Calling setInputWindows twice, with the same info, should not cause any issues.
1430 * To ensure that window receives only events that were directly inside of it, add
1431 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1432 * when finding touched windows.
1433 */
1434TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001435 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001436 sp<FakeWindowHandle> window =
1437 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1438 window->setFrame(Rect(0, 0, 100, 100));
chaviw3277faf2021-05-19 16:45:23 -05001439 window->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001440
1441 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1442 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001443 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001444 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1445 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001446 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001447
1448 // Window should receive motion event.
1449 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1450}
1451
Arthur Hungb92218b2018-08-14 12:00:21 +08001452// The foreground window should receive the first touch down event.
1453TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001454 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001455 sp<FakeWindowHandle> windowTop =
1456 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
1457 sp<FakeWindowHandle> windowSecond =
1458 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001459
Arthur Hung72d8dc32020-03-28 00:48:39 +00001460 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001461 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1462 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1463 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001464
1465 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001466 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001467 windowSecond->assertNoEvents();
1468}
1469
Garfield Tandf26e862020-07-01 20:18:19 -07001470TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07001471 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07001472 sp<FakeWindowHandle> windowLeft =
1473 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1474 windowLeft->setFrame(Rect(0, 0, 600, 800));
chaviw3277faf2021-05-19 16:45:23 -05001475 windowLeft->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001476 sp<FakeWindowHandle> windowRight =
1477 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1478 windowRight->setFrame(Rect(600, 0, 1200, 800));
chaviw3277faf2021-05-19 16:45:23 -05001479 windowRight->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001480
1481 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1482
1483 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
1484
1485 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001486 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001487 injectMotionEvent(mDispatcher,
1488 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1489 AINPUT_SOURCE_MOUSE)
1490 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1491 .x(900)
1492 .y(400))
1493 .build()));
1494 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1495 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1496 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1497 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1498
1499 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001500 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001501 injectMotionEvent(mDispatcher,
1502 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1503 AINPUT_SOURCE_MOUSE)
1504 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1505 .x(300)
1506 .y(400))
1507 .build()));
1508 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1509 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1510 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1511 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1512 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1513 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1514
1515 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001516 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001517 injectMotionEvent(mDispatcher,
1518 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1519 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1520 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1521 .x(300)
1522 .y(400))
1523 .build()));
1524 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1525
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001526 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001527 injectMotionEvent(mDispatcher,
1528 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
1529 AINPUT_SOURCE_MOUSE)
1530 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1531 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1532 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1533 .x(300)
1534 .y(400))
1535 .build()));
1536 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1537 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1538
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001539 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001540 injectMotionEvent(mDispatcher,
1541 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1542 AINPUT_SOURCE_MOUSE)
1543 .buttonState(0)
1544 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1545 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1546 .x(300)
1547 .y(400))
1548 .build()));
1549 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1550 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1551
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001552 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001553 injectMotionEvent(mDispatcher,
1554 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
1555 .buttonState(0)
1556 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1557 .x(300)
1558 .y(400))
1559 .build()));
1560 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1561
1562 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001563 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001564 injectMotionEvent(mDispatcher,
1565 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1566 AINPUT_SOURCE_MOUSE)
1567 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1568 .x(900)
1569 .y(400))
1570 .build()));
1571 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1572 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1573 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1574 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1575 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1576 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1577}
1578
1579// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
1580// directly in this test.
1581TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07001582 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07001583 sp<FakeWindowHandle> window =
1584 new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1585 window->setFrame(Rect(0, 0, 1200, 800));
chaviw3277faf2021-05-19 16:45:23 -05001586 window->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001587
1588 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1589
1590 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1591
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001592 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001593 injectMotionEvent(mDispatcher,
1594 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
1595 AINPUT_SOURCE_MOUSE)
1596 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1597 .x(300)
1598 .y(400))
1599 .build()));
1600 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1601 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1602
1603 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001604 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001605 injectMotionEvent(mDispatcher,
1606 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1607 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1608 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1609 .x(300)
1610 .y(400))
1611 .build()));
1612 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1613
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001614 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001615 injectMotionEvent(mDispatcher,
1616 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
1617 AINPUT_SOURCE_MOUSE)
1618 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1619 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1620 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1621 .x(300)
1622 .y(400))
1623 .build()));
1624 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1625 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1626
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001627 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001628 injectMotionEvent(mDispatcher,
1629 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1630 AINPUT_SOURCE_MOUSE)
1631 .buttonState(0)
1632 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1633 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1634 .x(300)
1635 .y(400))
1636 .build()));
1637 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1638 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1639
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001640 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001641 injectMotionEvent(mDispatcher,
1642 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
1643 .buttonState(0)
1644 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1645 .x(300)
1646 .y(400))
1647 .build()));
1648 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1649
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001650 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001651 injectMotionEvent(mDispatcher,
1652 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
1653 AINPUT_SOURCE_MOUSE)
1654 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1655 .x(300)
1656 .y(400))
1657 .build()));
1658 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1659 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1660}
1661
Garfield Tan00f511d2019-06-12 16:55:40 -07001662TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07001663 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07001664
1665 sp<FakeWindowHandle> windowLeft =
1666 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1667 windowLeft->setFrame(Rect(0, 0, 600, 800));
chaviw3277faf2021-05-19 16:45:23 -05001668 windowLeft->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07001669 sp<FakeWindowHandle> windowRight =
1670 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1671 windowRight->setFrame(Rect(600, 0, 1200, 800));
chaviw3277faf2021-05-19 16:45:23 -05001672 windowRight->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07001673
1674 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1675
Arthur Hung72d8dc32020-03-28 00:48:39 +00001676 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07001677
1678 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
1679 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001680 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07001681 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001682 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001683 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07001684 windowRight->assertNoEvents();
1685}
1686
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001687TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001688 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001689 sp<FakeWindowHandle> window =
1690 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07001691 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001692
Arthur Hung72d8dc32020-03-28 00:48:39 +00001693 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001694 setFocusedWindow(window);
1695
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001696 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001697
1698 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1699 mDispatcher->notifyKey(&keyArgs);
1700
1701 // Window should receive key down event.
1702 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1703
1704 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
1705 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001706 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001707 mDispatcher->notifyDeviceReset(&args);
1708 window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
1709 AKEY_EVENT_FLAG_CANCELED);
1710}
1711
1712TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001713 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001714 sp<FakeWindowHandle> window =
1715 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1716
Arthur Hung72d8dc32020-03-28 00:48:39 +00001717 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001718
1719 NotifyMotionArgs motionArgs =
1720 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1721 ADISPLAY_ID_DEFAULT);
1722 mDispatcher->notifyMotion(&motionArgs);
1723
1724 // Window should receive motion down event.
1725 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1726
1727 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
1728 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001729 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001730 mDispatcher->notifyDeviceReset(&args);
1731 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT,
1732 0 /*expectedFlags*/);
1733}
1734
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001735using TransferFunction =
1736 std::function<bool(sp<InputDispatcher> dispatcher, sp<IBinder>, sp<IBinder>)>;
1737
1738class TransferTouchFixture : public InputDispatcherTest,
1739 public ::testing::WithParamInterface<TransferFunction> {};
1740
1741TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07001742 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001743
1744 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001745 sp<FakeWindowHandle> firstWindow =
1746 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1747 sp<FakeWindowHandle> secondWindow =
1748 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001749
1750 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001751 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001752
1753 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001754 NotifyMotionArgs downMotionArgs =
1755 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1756 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001757 mDispatcher->notifyMotion(&downMotionArgs);
1758 // Only the first window should get the down event
1759 firstWindow->consumeMotionDown();
1760 secondWindow->assertNoEvents();
1761
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001762 // Transfer touch to the second window
1763 TransferFunction f = GetParam();
1764 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
1765 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001766 // The first window gets cancel and the second gets down
1767 firstWindow->consumeMotionCancel();
1768 secondWindow->consumeMotionDown();
1769
1770 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001771 NotifyMotionArgs upMotionArgs =
1772 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1773 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001774 mDispatcher->notifyMotion(&upMotionArgs);
1775 // The first window gets no events and the second gets up
1776 firstWindow->assertNoEvents();
1777 secondWindow->consumeMotionUp();
1778}
1779
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001780TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001781 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001782
1783 PointF touchPoint = {10, 10};
1784
1785 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001786 sp<FakeWindowHandle> firstWindow =
1787 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1788 sp<FakeWindowHandle> secondWindow =
1789 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001790
1791 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001792 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001793
1794 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001795 NotifyMotionArgs downMotionArgs =
1796 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1797 ADISPLAY_ID_DEFAULT, {touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001798 mDispatcher->notifyMotion(&downMotionArgs);
1799 // Only the first window should get the down event
1800 firstWindow->consumeMotionDown();
1801 secondWindow->assertNoEvents();
1802
1803 // Send pointer down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001804 NotifyMotionArgs pointerDownMotionArgs =
1805 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1806 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1807 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1808 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001809 mDispatcher->notifyMotion(&pointerDownMotionArgs);
1810 // Only the first window should get the pointer down event
1811 firstWindow->consumeMotionPointerDown(1);
1812 secondWindow->assertNoEvents();
1813
1814 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001815 TransferFunction f = GetParam();
1816 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
1817 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001818 // The first window gets cancel and the second gets down and pointer down
1819 firstWindow->consumeMotionCancel();
1820 secondWindow->consumeMotionDown();
1821 secondWindow->consumeMotionPointerDown(1);
1822
1823 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001824 NotifyMotionArgs pointerUpMotionArgs =
1825 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1826 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1827 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1828 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001829 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1830 // The first window gets nothing and the second gets pointer up
1831 firstWindow->assertNoEvents();
1832 secondWindow->consumeMotionPointerUp(1);
1833
1834 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001835 NotifyMotionArgs upMotionArgs =
1836 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1837 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001838 mDispatcher->notifyMotion(&upMotionArgs);
1839 // The first window gets nothing and the second gets up
1840 firstWindow->assertNoEvents();
1841 secondWindow->consumeMotionUp();
1842}
1843
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001844// For the cases of single pointer touch and two pointers non-split touch, the api's
1845// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
1846// for the case where there are multiple pointers split across several windows.
1847INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
1848 ::testing::Values(
1849 [&](sp<InputDispatcher> dispatcher, sp<IBinder> /*ignored*/,
1850 sp<IBinder> destChannelToken) {
1851 return dispatcher->transferTouch(destChannelToken);
1852 },
1853 [&](sp<InputDispatcher> dispatcher, sp<IBinder> from,
1854 sp<IBinder> to) {
1855 return dispatcher->transferTouchFocus(from, to,
1856 false /*isDragAndDrop*/);
1857 }));
1858
Svet Ganov5d3bc372020-01-26 23:11:07 -08001859TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001860 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001861
1862 // Create a non touch modal window that supports split touch
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001863 sp<FakeWindowHandle> firstWindow =
1864 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001865 firstWindow->setFrame(Rect(0, 0, 600, 400));
chaviw3277faf2021-05-19 16:45:23 -05001866 firstWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001867
1868 // Create a non touch modal window that supports split touch
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001869 sp<FakeWindowHandle> secondWindow =
1870 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001871 secondWindow->setFrame(Rect(0, 400, 600, 800));
chaviw3277faf2021-05-19 16:45:23 -05001872 secondWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001873
1874 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001875 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001876
1877 PointF pointInFirst = {300, 200};
1878 PointF pointInSecond = {300, 600};
1879
1880 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001881 NotifyMotionArgs firstDownMotionArgs =
1882 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1883 ADISPLAY_ID_DEFAULT, {pointInFirst});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001884 mDispatcher->notifyMotion(&firstDownMotionArgs);
1885 // Only the first window should get the down event
1886 firstWindow->consumeMotionDown();
1887 secondWindow->assertNoEvents();
1888
1889 // Send down to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001890 NotifyMotionArgs secondDownMotionArgs =
1891 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1892 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1893 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1894 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001895 mDispatcher->notifyMotion(&secondDownMotionArgs);
1896 // The first window gets a move and the second a down
1897 firstWindow->consumeMotionMove();
1898 secondWindow->consumeMotionDown();
1899
1900 // Transfer touch focus to the second window
1901 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1902 // The first window gets cancel and the new gets pointer down (it already saw down)
1903 firstWindow->consumeMotionCancel();
1904 secondWindow->consumeMotionPointerDown(1);
1905
1906 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001907 NotifyMotionArgs pointerUpMotionArgs =
1908 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1909 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1910 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1911 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001912 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1913 // The first window gets nothing and the second gets pointer up
1914 firstWindow->assertNoEvents();
1915 secondWindow->consumeMotionPointerUp(1);
1916
1917 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001918 NotifyMotionArgs upMotionArgs =
1919 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1920 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001921 mDispatcher->notifyMotion(&upMotionArgs);
1922 // The first window gets nothing and the second gets up
1923 firstWindow->assertNoEvents();
1924 secondWindow->consumeMotionUp();
1925}
1926
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001927// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
1928// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
1929// touch is not supported, so the touch should continue on those windows and the transferred-to
1930// window should get nothing.
1931TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
1932 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1933
1934 // Create a non touch modal window that supports split touch
1935 sp<FakeWindowHandle> firstWindow =
1936 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1937 firstWindow->setFrame(Rect(0, 0, 600, 400));
chaviw3277faf2021-05-19 16:45:23 -05001938 firstWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001939
1940 // Create a non touch modal window that supports split touch
1941 sp<FakeWindowHandle> secondWindow =
1942 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
1943 secondWindow->setFrame(Rect(0, 400, 600, 800));
chaviw3277faf2021-05-19 16:45:23 -05001944 secondWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001945
1946 // Add the windows to the dispatcher
1947 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
1948
1949 PointF pointInFirst = {300, 200};
1950 PointF pointInSecond = {300, 600};
1951
1952 // Send down to the first window
1953 NotifyMotionArgs firstDownMotionArgs =
1954 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1955 ADISPLAY_ID_DEFAULT, {pointInFirst});
1956 mDispatcher->notifyMotion(&firstDownMotionArgs);
1957 // Only the first window should get the down event
1958 firstWindow->consumeMotionDown();
1959 secondWindow->assertNoEvents();
1960
1961 // Send down to the second window
1962 NotifyMotionArgs secondDownMotionArgs =
1963 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1964 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1965 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1966 {pointInFirst, pointInSecond});
1967 mDispatcher->notifyMotion(&secondDownMotionArgs);
1968 // The first window gets a move and the second a down
1969 firstWindow->consumeMotionMove();
1970 secondWindow->consumeMotionDown();
1971
1972 // Transfer touch focus to the second window
1973 const bool transferred = mDispatcher->transferTouch(secondWindow->getToken());
1974 // The 'transferTouch' call should not succeed, because there are 2 touched windows
1975 ASSERT_FALSE(transferred);
1976 firstWindow->assertNoEvents();
1977 secondWindow->assertNoEvents();
1978
1979 // The rest of the dispatch should proceed as normal
1980 // Send pointer up to the second window
1981 NotifyMotionArgs pointerUpMotionArgs =
1982 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1983 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1984 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1985 {pointInFirst, pointInSecond});
1986 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1987 // The first window gets MOVE and the second gets pointer up
1988 firstWindow->consumeMotionMove();
1989 secondWindow->consumeMotionUp();
1990
1991 // Send up event to the first window
1992 NotifyMotionArgs upMotionArgs =
1993 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1994 ADISPLAY_ID_DEFAULT);
1995 mDispatcher->notifyMotion(&upMotionArgs);
1996 // The first window gets nothing and the second gets up
1997 firstWindow->consumeMotionUp();
1998 secondWindow->assertNoEvents();
1999}
2000
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002001TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002002 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002003 sp<FakeWindowHandle> window =
2004 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2005
Vishnu Nair47074b82020-08-14 11:54:47 -07002006 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002007 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002008 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002009
2010 window->consumeFocusEvent(true);
2011
2012 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2013 mDispatcher->notifyKey(&keyArgs);
2014
2015 // Window should receive key down event.
2016 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2017}
2018
2019TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002020 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002021 sp<FakeWindowHandle> window =
2022 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2023
Arthur Hung72d8dc32020-03-28 00:48:39 +00002024 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002025
2026 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2027 mDispatcher->notifyKey(&keyArgs);
2028 mDispatcher->waitForIdle();
2029
2030 window->assertNoEvents();
2031}
2032
2033// If a window is touchable, but does not have focus, it should receive motion events, but not keys
2034TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07002035 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002036 sp<FakeWindowHandle> window =
2037 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2038
Arthur Hung72d8dc32020-03-28 00:48:39 +00002039 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002040
2041 // Send key
2042 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2043 mDispatcher->notifyKey(&keyArgs);
2044 // Send motion
2045 NotifyMotionArgs motionArgs =
2046 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2047 ADISPLAY_ID_DEFAULT);
2048 mDispatcher->notifyMotion(&motionArgs);
2049
2050 // Window should receive only the motion event
2051 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2052 window->assertNoEvents(); // Key event or focus event will not be received
2053}
2054
arthurhungea3f4fc2020-12-21 23:18:53 +08002055TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
2056 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2057
2058 // Create first non touch modal window that supports split touch
2059 sp<FakeWindowHandle> firstWindow =
2060 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
2061 firstWindow->setFrame(Rect(0, 0, 600, 400));
chaviw3277faf2021-05-19 16:45:23 -05002062 firstWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
arthurhungea3f4fc2020-12-21 23:18:53 +08002063
2064 // Create second non touch modal window that supports split touch
2065 sp<FakeWindowHandle> secondWindow =
2066 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
2067 secondWindow->setFrame(Rect(0, 400, 600, 800));
chaviw3277faf2021-05-19 16:45:23 -05002068 secondWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
arthurhungea3f4fc2020-12-21 23:18:53 +08002069
2070 // Add the windows to the dispatcher
2071 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
2072
2073 PointF pointInFirst = {300, 200};
2074 PointF pointInSecond = {300, 600};
2075
2076 // Send down to the first window
2077 NotifyMotionArgs firstDownMotionArgs =
2078 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2079 ADISPLAY_ID_DEFAULT, {pointInFirst});
2080 mDispatcher->notifyMotion(&firstDownMotionArgs);
2081 // Only the first window should get the down event
2082 firstWindow->consumeMotionDown();
2083 secondWindow->assertNoEvents();
2084
2085 // Send down to the second window
2086 NotifyMotionArgs secondDownMotionArgs =
2087 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
2088 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2089 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2090 {pointInFirst, pointInSecond});
2091 mDispatcher->notifyMotion(&secondDownMotionArgs);
2092 // The first window gets a move and the second a down
2093 firstWindow->consumeMotionMove();
2094 secondWindow->consumeMotionDown();
2095
2096 // Send pointer cancel to the second window
2097 NotifyMotionArgs pointerUpMotionArgs =
2098 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
2099 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2100 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2101 {pointInFirst, pointInSecond});
2102 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
2103 mDispatcher->notifyMotion(&pointerUpMotionArgs);
2104 // The first window gets move and the second gets cancel.
2105 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
2106 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
2107
2108 // Send up event.
2109 NotifyMotionArgs upMotionArgs =
2110 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2111 ADISPLAY_ID_DEFAULT);
2112 mDispatcher->notifyMotion(&upMotionArgs);
2113 // The first window gets up and the second gets nothing.
2114 firstWindow->consumeMotionUp();
2115 secondWindow->assertNoEvents();
2116}
2117
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00002118TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
2119 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2120
2121 sp<FakeWindowHandle> window =
2122 new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2123 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2124 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
2125 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
2126 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
2127
2128 window->sendTimeline(1 /*inputEventId*/, graphicsTimeline);
2129 window->assertNoEvents();
2130 mDispatcher->waitForIdle();
2131}
2132
chaviwd1c23182019-12-20 18:44:56 -08002133class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00002134public:
2135 FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name,
chaviwd1c23182019-12-20 18:44:56 -08002136 int32_t displayId, bool isGestureMonitor = false) {
Garfield Tan15601662020-09-22 15:32:38 -07002137 base::Result<std::unique_ptr<InputChannel>> channel =
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +00002138 dispatcher->createInputMonitor(displayId, isGestureMonitor, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07002139 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00002140 }
2141
chaviwd1c23182019-12-20 18:44:56 -08002142 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
2143
2144 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
2145 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN,
2146 expectedDisplayId, expectedFlags);
2147 }
2148
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002149 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
2150
2151 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
2152
chaviwd1c23182019-12-20 18:44:56 -08002153 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
2154 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
2155 expectedDisplayId, expectedFlags);
2156 }
2157
2158 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
2159 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP,
2160 expectedDisplayId, expectedFlags);
2161 }
2162
Evan Rosky84f07f02021-04-16 10:42:42 -07002163 MotionEvent* consumeMotion() {
2164 InputEvent* event = mInputReceiver->consume();
2165 if (!event) {
2166 ADD_FAILURE() << "No event was produced";
2167 return nullptr;
2168 }
2169 if (event->getType() != AINPUT_EVENT_TYPE_MOTION) {
2170 ADD_FAILURE() << "Received event of type " << event->getType() << " instead of motion";
2171 return nullptr;
2172 }
2173 return static_cast<MotionEvent*>(event);
2174 }
2175
chaviwd1c23182019-12-20 18:44:56 -08002176 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
2177
2178private:
2179 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00002180};
2181
2182// Tests for gesture monitors
2183TEST_F(InputDispatcherTest, GestureMonitor_ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07002184 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00002185 sp<FakeWindowHandle> window =
2186 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002187 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00002188
chaviwd1c23182019-12-20 18:44:56 -08002189 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
2190 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00002191
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002192 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00002193 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002194 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00002195 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002196 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00002197}
2198
2199TEST_F(InputDispatcherTest, GestureMonitor_DoesNotReceiveKeyEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07002200 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00002201 sp<FakeWindowHandle> window =
2202 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2203
2204 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002205 window->setFocusable(true);
Michael Wright3a240c42019-12-10 20:53:41 +00002206
Arthur Hung72d8dc32020-03-28 00:48:39 +00002207 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002208 setFocusedWindow(window);
2209
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002210 window->consumeFocusEvent(true);
Michael Wright3a240c42019-12-10 20:53:41 +00002211
chaviwd1c23182019-12-20 18:44:56 -08002212 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
2213 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00002214
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002215 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2216 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00002217 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002218 monitor.assertNoEvents();
Michael Wright3a240c42019-12-10 20:53:41 +00002219}
2220
2221TEST_F(InputDispatcherTest, GestureMonitor_CanPilferAfterWindowIsRemovedMidStream) {
Chris Yea209fde2020-07-22 13:54:51 -07002222 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00002223 sp<FakeWindowHandle> window =
2224 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002225 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00002226
chaviwd1c23182019-12-20 18:44:56 -08002227 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
2228 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00002229
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002230 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00002231 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002232 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00002233 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002234 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00002235
2236 window->releaseChannel();
2237
chaviwd1c23182019-12-20 18:44:56 -08002238 mDispatcher->pilferPointers(monitor.getToken());
Michael Wright3a240c42019-12-10 20:53:41 +00002239
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002240 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00002241 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002242 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08002243 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00002244}
2245
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002246TEST_F(InputDispatcherTest, UnresponsiveGestureMonitor_GetsAnr) {
2247 FakeMonitorReceiver monitor =
2248 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
2249 true /*isGestureMonitor*/);
2250
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002251 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002252 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
2253 std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
2254 ASSERT_TRUE(consumeSeq);
2255
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00002256 mFakePolicy->assertNotifyMonitorUnresponsiveWasCalled(DISPATCHING_TIMEOUT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002257 monitor.finishEvent(*consumeSeq);
2258 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00002259 mFakePolicy->assertNotifyMonitorResponsiveWasCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002260}
2261
Evan Rosky84f07f02021-04-16 10:42:42 -07002262// Tests for gesture monitors
2263TEST_F(InputDispatcherTest, GestureMonitor_NoWindowTransform) {
2264 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2265 sp<FakeWindowHandle> window =
2266 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2267 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2268 window->setWindowOffset(20, 40);
2269 window->setWindowTransform(0, 1, -1, 0);
2270
2271 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
2272 true /*isGestureMonitor*/);
2273
2274 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2275 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2276 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2277 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2278 MotionEvent* event = monitor.consumeMotion();
2279 // Even though window has transform, gesture monitor must not.
2280 ASSERT_EQ(ui::Transform(), event->getTransform());
2281}
2282
chaviw81e2bb92019-12-18 15:03:51 -08002283TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002284 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
chaviw81e2bb92019-12-18 15:03:51 -08002285 sp<FakeWindowHandle> window =
2286 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2287
Arthur Hung72d8dc32020-03-28 00:48:39 +00002288 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08002289
2290 NotifyMotionArgs motionArgs =
2291 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2292 ADISPLAY_ID_DEFAULT);
2293
2294 mDispatcher->notifyMotion(&motionArgs);
2295 // Window should receive motion down event.
2296 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2297
2298 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002299 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08002300 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
2301 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
2302 motionArgs.pointerCoords[0].getX() - 10);
2303
2304 mDispatcher->notifyMotion(&motionArgs);
2305 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
2306 0 /*expectedFlags*/);
2307}
2308
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002309/**
2310 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
2311 * the device default right away. In the test scenario, we check both the default value,
2312 * and the action of enabling / disabling.
2313 */
2314TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07002315 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002316 sp<FakeWindowHandle> window =
2317 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2318
2319 // Set focused application.
2320 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002321 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002322
2323 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00002324 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002325 setFocusedWindow(window);
2326
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002327 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2328
2329 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07002330 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002331 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002332 window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/);
2333
2334 SCOPED_TRACE("Disable touch mode");
2335 mDispatcher->setInTouchMode(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07002336 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002337 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002338 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002339 window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/);
2340
2341 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07002342 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002343 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002344 window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/);
2345
2346 SCOPED_TRACE("Enable touch mode again");
2347 mDispatcher->setInTouchMode(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07002348 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002349 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002350 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002351 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2352
2353 window->assertNoEvents();
2354}
2355
Gang Wange9087892020-01-07 12:17:14 -05002356TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002357 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Gang Wange9087892020-01-07 12:17:14 -05002358 sp<FakeWindowHandle> window =
2359 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2360
2361 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002362 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05002363
Arthur Hung72d8dc32020-03-28 00:48:39 +00002364 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002365 setFocusedWindow(window);
2366
Gang Wange9087892020-01-07 12:17:14 -05002367 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2368
2369 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
2370 mDispatcher->notifyKey(&keyArgs);
2371
2372 InputEvent* event = window->consume();
2373 ASSERT_NE(event, nullptr);
2374
2375 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
2376 ASSERT_NE(verified, nullptr);
2377 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
2378
2379 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
2380 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
2381 ASSERT_EQ(keyArgs.source, verified->source);
2382 ASSERT_EQ(keyArgs.displayId, verified->displayId);
2383
2384 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
2385
2386 ASSERT_EQ(keyArgs.action, verifiedKey.action);
2387 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05002388 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
2389 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
2390 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
2391 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
2392 ASSERT_EQ(0, verifiedKey.repeatCount);
2393}
2394
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002395TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002396 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002397 sp<FakeWindowHandle> window =
2398 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2399
2400 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2401
Arthur Hung72d8dc32020-03-28 00:48:39 +00002402 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002403
2404 NotifyMotionArgs motionArgs =
2405 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2406 ADISPLAY_ID_DEFAULT);
2407 mDispatcher->notifyMotion(&motionArgs);
2408
2409 InputEvent* event = window->consume();
2410 ASSERT_NE(event, nullptr);
2411
2412 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
2413 ASSERT_NE(verified, nullptr);
2414 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
2415
2416 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
2417 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
2418 EXPECT_EQ(motionArgs.source, verified->source);
2419 EXPECT_EQ(motionArgs.displayId, verified->displayId);
2420
2421 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
2422
2423 EXPECT_EQ(motionArgs.pointerCoords[0].getX(), verifiedMotion.rawX);
2424 EXPECT_EQ(motionArgs.pointerCoords[0].getY(), verifiedMotion.rawY);
2425 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
2426 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
2427 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
2428 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
2429 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
2430}
2431
Prabir Pradhan664834b2021-05-20 16:00:42 -07002432TEST_F(InputDispatcherTest, NonPointerMotionEvent_JoystickAndTouchpadNotTransformed) {
yunho.shinf4a80b82020-11-16 21:13:57 +09002433 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2434 sp<FakeWindowHandle> window =
2435 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2436 const std::string name = window->getName();
2437
2438 // Window gets transformed by offset values.
2439 window->setWindowOffset(500.0f, 500.0f);
2440
2441 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2442 window->setFocusable(true);
2443
2444 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2445
2446 // First, we set focused window so that focusedWindowHandle is not null.
2447 setFocusedWindow(window);
2448
2449 // Second, we consume focus event if it is right or wrong according to onFocusChangedLocked.
2450 window->consumeFocusEvent(true);
2451
Prabir Pradhan664834b2021-05-20 16:00:42 -07002452 constexpr const std::array nonTransformedSources = {std::pair(AINPUT_SOURCE_TOUCHPAD,
2453 AMOTION_EVENT_ACTION_DOWN),
2454 std::pair(AINPUT_SOURCE_JOYSTICK,
2455 AMOTION_EVENT_ACTION_MOVE)};
2456 for (const auto& [source, action] : nonTransformedSources) {
2457 const NotifyMotionArgs motionArgs = generateMotionArgs(action, source, ADISPLAY_ID_DEFAULT);
Prabir Pradhanbd527712021-03-09 19:17:09 -08002458 mDispatcher->notifyMotion(&motionArgs);
yunho.shinf4a80b82020-11-16 21:13:57 +09002459
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00002460 MotionEvent* event = window->consumeMotion();
Prabir Pradhanbd527712021-03-09 19:17:09 -08002461 ASSERT_NE(event, nullptr);
yunho.shinf4a80b82020-11-16 21:13:57 +09002462
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00002463 const MotionEvent& motionEvent = *event;
Prabir Pradhan664834b2021-05-20 16:00:42 -07002464 EXPECT_EQ(action, motionEvent.getAction());
Prabir Pradhanbd527712021-03-09 19:17:09 -08002465 EXPECT_EQ(motionArgs.pointerCount, motionEvent.getPointerCount());
yunho.shinf4a80b82020-11-16 21:13:57 +09002466
Prabir Pradhanbd527712021-03-09 19:17:09 -08002467 float expectedX = motionArgs.pointerCoords[0].getX();
2468 float expectedY = motionArgs.pointerCoords[0].getY();
yunho.shinf4a80b82020-11-16 21:13:57 +09002469
Prabir Pradhanbd527712021-03-09 19:17:09 -08002470 // Ensure the axis values from the final motion event are not transformed.
2471 EXPECT_EQ(expectedX, motionEvent.getX(0))
2472 << "expected " << expectedX << " for x coord of " << name.c_str() << ", got "
2473 << motionEvent.getX(0);
2474 EXPECT_EQ(expectedY, motionEvent.getY(0))
2475 << "expected " << expectedY << " for y coord of " << name.c_str() << ", got "
2476 << motionEvent.getY(0);
2477 // Ensure the raw and transformed axis values for the motion event are the same.
2478 EXPECT_EQ(motionEvent.getRawX(0), motionEvent.getX(0))
2479 << "expected raw and transformed X-axis values to be equal";
2480 EXPECT_EQ(motionEvent.getRawY(0), motionEvent.getY(0))
2481 << "expected raw and transformed Y-axis values to be equal";
2482 }
yunho.shinf4a80b82020-11-16 21:13:57 +09002483}
2484
chaviw09c8d2d2020-08-24 15:48:26 -07002485/**
2486 * Ensure that separate calls to sign the same data are generating the same key.
2487 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
2488 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
2489 * tests.
2490 */
2491TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
2492 KeyEvent event = getTestKeyEvent();
2493 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
2494
2495 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
2496 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
2497 ASSERT_EQ(hmac1, hmac2);
2498}
2499
2500/**
2501 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
2502 */
2503TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
2504 KeyEvent event = getTestKeyEvent();
2505 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
2506 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
2507
2508 verifiedEvent.deviceId += 1;
2509 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2510
2511 verifiedEvent.source += 1;
2512 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2513
2514 verifiedEvent.eventTimeNanos += 1;
2515 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2516
2517 verifiedEvent.displayId += 1;
2518 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2519
2520 verifiedEvent.action += 1;
2521 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2522
2523 verifiedEvent.downTimeNanos += 1;
2524 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2525
2526 verifiedEvent.flags += 1;
2527 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2528
2529 verifiedEvent.keyCode += 1;
2530 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2531
2532 verifiedEvent.scanCode += 1;
2533 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2534
2535 verifiedEvent.metaState += 1;
2536 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2537
2538 verifiedEvent.repeatCount += 1;
2539 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2540}
2541
Vishnu Nair958da932020-08-21 17:12:37 -07002542TEST_F(InputDispatcherTest, SetFocusedWindow) {
2543 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2544 sp<FakeWindowHandle> windowTop =
2545 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2546 sp<FakeWindowHandle> windowSecond =
2547 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2548 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2549
2550 // Top window is also focusable but is not granted focus.
2551 windowTop->setFocusable(true);
2552 windowSecond->setFocusable(true);
2553 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2554 setFocusedWindow(windowSecond);
2555
2556 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002557 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2558 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002559
2560 // Focused window should receive event.
2561 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
2562 windowTop->assertNoEvents();
2563}
2564
2565TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
2566 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2567 sp<FakeWindowHandle> window =
2568 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2569 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2570
2571 window->setFocusable(true);
2572 // Release channel for window is no longer valid.
2573 window->releaseChannel();
2574 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2575 setFocusedWindow(window);
2576
2577 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002578 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2579 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002580
2581 // window channel is invalid, so it should not receive any input event.
2582 window->assertNoEvents();
2583}
2584
2585TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
2586 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2587 sp<FakeWindowHandle> window =
2588 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2589 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2590
2591 // Window is not focusable.
2592 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2593 setFocusedWindow(window);
2594
2595 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002596 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2597 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002598
2599 // window is invalid, so it should not receive any input event.
2600 window->assertNoEvents();
2601}
2602
2603TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
2604 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2605 sp<FakeWindowHandle> windowTop =
2606 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2607 sp<FakeWindowHandle> windowSecond =
2608 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2609 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2610
2611 windowTop->setFocusable(true);
2612 windowSecond->setFocusable(true);
2613 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2614 setFocusedWindow(windowTop);
2615 windowTop->consumeFocusEvent(true);
2616
2617 setFocusedWindow(windowSecond, windowTop);
2618 windowSecond->consumeFocusEvent(true);
2619 windowTop->consumeFocusEvent(false);
2620
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002621 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2622 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002623
2624 // Focused window should receive event.
2625 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
2626}
2627
2628TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) {
2629 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2630 sp<FakeWindowHandle> windowTop =
2631 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2632 sp<FakeWindowHandle> windowSecond =
2633 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2634 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2635
2636 windowTop->setFocusable(true);
2637 windowSecond->setFocusable(true);
2638 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2639 setFocusedWindow(windowSecond, windowTop);
2640
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002641 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2642 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002643
2644 // Event should be dropped.
2645 windowTop->assertNoEvents();
2646 windowSecond->assertNoEvents();
2647}
2648
2649TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
2650 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2651 sp<FakeWindowHandle> window =
2652 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2653 sp<FakeWindowHandle> previousFocusedWindow =
2654 new FakeWindowHandle(application, mDispatcher, "previousFocusedWindow",
2655 ADISPLAY_ID_DEFAULT);
2656 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2657
2658 window->setFocusable(true);
2659 previousFocusedWindow->setFocusable(true);
2660 window->setVisible(false);
2661 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
2662 setFocusedWindow(previousFocusedWindow);
2663 previousFocusedWindow->consumeFocusEvent(true);
2664
2665 // Requesting focus on invisible window takes focus from currently focused window.
2666 setFocusedWindow(window);
2667 previousFocusedWindow->consumeFocusEvent(false);
2668
2669 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002670 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07002671 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002672 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07002673
2674 // Window does not get focus event or key down.
2675 window->assertNoEvents();
2676
2677 // Window becomes visible.
2678 window->setVisible(true);
2679 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2680
2681 // Window receives focus event.
2682 window->consumeFocusEvent(true);
2683 // Focused window receives key down.
2684 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2685}
2686
Vishnu Nair599f1412021-06-21 10:39:58 -07002687TEST_F(InputDispatcherTest, DisplayRemoved) {
2688 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2689 sp<FakeWindowHandle> window =
2690 new FakeWindowHandle(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
2691 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2692
2693 // window is granted focus.
2694 window->setFocusable(true);
2695 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2696 setFocusedWindow(window);
2697 window->consumeFocusEvent(true);
2698
2699 // When a display is removed window loses focus.
2700 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
2701 window->consumeFocusEvent(false);
2702}
2703
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002704/**
2705 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
2706 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
2707 * of the 'slipperyEnterWindow'.
2708 *
2709 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
2710 * a way so that the touched location is no longer covered by the top window.
2711 *
2712 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
2713 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
2714 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
2715 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
2716 * with ACTION_DOWN).
2717 * Thus, the touch has been transferred from the top window into the bottom window, because the top
2718 * window moved itself away from the touched location and had Flag::SLIPPERY.
2719 *
2720 * Even though the top window moved away from the touched location, it is still obscuring the bottom
2721 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
2722 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
2723 *
2724 * In this test, we ensure that the event received by the bottom window has
2725 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
2726 */
2727TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
2728 constexpr int32_t SLIPPERY_PID = INJECTOR_PID + 1;
2729 constexpr int32_t SLIPPERY_UID = INJECTOR_UID + 1;
2730
2731 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2732 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2733
2734 sp<FakeWindowHandle> slipperyExitWindow =
2735 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviw3277faf2021-05-19 16:45:23 -05002736 slipperyExitWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SLIPPERY);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002737 // Make sure this one overlaps the bottom window
2738 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
2739 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
2740 // one. Windows with the same owner are not considered to be occluding each other.
2741 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
2742
2743 sp<FakeWindowHandle> slipperyEnterWindow =
2744 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2745 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
2746
2747 mDispatcher->setInputWindows(
2748 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
2749
2750 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
2751 NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2752 ADISPLAY_ID_DEFAULT, {{50, 50}});
2753 mDispatcher->notifyMotion(&args);
2754 slipperyExitWindow->consumeMotionDown();
2755 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
2756 mDispatcher->setInputWindows(
2757 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
2758
2759 args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2760 ADISPLAY_ID_DEFAULT, {{51, 51}});
2761 mDispatcher->notifyMotion(&args);
2762
2763 slipperyExitWindow->consumeMotionCancel();
2764
2765 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
2766 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
2767}
2768
Garfield Tan1c7bc862020-01-28 13:24:04 -08002769class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
2770protected:
2771 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
2772 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
2773
Chris Yea209fde2020-07-22 13:54:51 -07002774 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002775 sp<FakeWindowHandle> mWindow;
2776
2777 virtual void SetUp() override {
2778 mFakePolicy = new FakeInputDispatcherPolicy();
2779 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
2780 mDispatcher = new InputDispatcher(mFakePolicy);
2781 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
2782 ASSERT_EQ(OK, mDispatcher->start());
2783
2784 setUpWindow();
2785 }
2786
2787 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07002788 mApp = std::make_shared<FakeApplicationHandle>();
Garfield Tan1c7bc862020-01-28 13:24:04 -08002789 mWindow = new FakeWindowHandle(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2790
Vishnu Nair47074b82020-08-14 11:54:47 -07002791 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002792 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002793 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002794 mWindow->consumeFocusEvent(true);
2795 }
2796
Chris Ye2ad95392020-09-01 13:44:44 -07002797 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08002798 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07002799 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002800 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
2801 mDispatcher->notifyKey(&keyArgs);
2802
2803 // Window should receive key down event.
2804 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2805 }
2806
2807 void expectKeyRepeatOnce(int32_t repeatCount) {
2808 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
2809 InputEvent* repeatEvent = mWindow->consume();
2810 ASSERT_NE(nullptr, repeatEvent);
2811
2812 uint32_t eventType = repeatEvent->getType();
2813 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType);
2814
2815 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
2816 uint32_t eventAction = repeatKeyEvent->getAction();
2817 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
2818 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
2819 }
2820
Chris Ye2ad95392020-09-01 13:44:44 -07002821 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08002822 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07002823 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002824 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
2825 mDispatcher->notifyKey(&keyArgs);
2826
2827 // Window should receive key down event.
2828 mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
2829 0 /*expectedFlags*/);
2830 }
2831};
2832
2833TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Chris Ye2ad95392020-09-01 13:44:44 -07002834 sendAndConsumeKeyDown(1 /* deviceId */);
2835 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2836 expectKeyRepeatOnce(repeatCount);
2837 }
2838}
2839
2840TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
2841 sendAndConsumeKeyDown(1 /* deviceId */);
2842 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2843 expectKeyRepeatOnce(repeatCount);
2844 }
2845 sendAndConsumeKeyDown(2 /* deviceId */);
2846 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08002847 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2848 expectKeyRepeatOnce(repeatCount);
2849 }
2850}
2851
2852TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Chris Ye2ad95392020-09-01 13:44:44 -07002853 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002854 expectKeyRepeatOnce(1 /*repeatCount*/);
Chris Ye2ad95392020-09-01 13:44:44 -07002855 sendAndConsumeKeyUp(1 /* deviceId */);
2856 mWindow->assertNoEvents();
2857}
2858
2859TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
2860 sendAndConsumeKeyDown(1 /* deviceId */);
2861 expectKeyRepeatOnce(1 /*repeatCount*/);
2862 sendAndConsumeKeyDown(2 /* deviceId */);
2863 expectKeyRepeatOnce(1 /*repeatCount*/);
2864 // Stale key up from device 1.
2865 sendAndConsumeKeyUp(1 /* deviceId */);
2866 // Device 2 is still down, keep repeating
2867 expectKeyRepeatOnce(2 /*repeatCount*/);
2868 expectKeyRepeatOnce(3 /*repeatCount*/);
2869 // Device 2 key up
2870 sendAndConsumeKeyUp(2 /* deviceId */);
2871 mWindow->assertNoEvents();
2872}
2873
2874TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
2875 sendAndConsumeKeyDown(1 /* deviceId */);
2876 expectKeyRepeatOnce(1 /*repeatCount*/);
2877 sendAndConsumeKeyDown(2 /* deviceId */);
2878 expectKeyRepeatOnce(1 /*repeatCount*/);
2879 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
2880 sendAndConsumeKeyUp(2 /* deviceId */);
2881 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08002882 mWindow->assertNoEvents();
2883}
2884
2885TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Chris Ye2ad95392020-09-01 13:44:44 -07002886 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002887 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2888 InputEvent* repeatEvent = mWindow->consume();
2889 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2890 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
2891 IdGenerator::getSource(repeatEvent->getId()));
2892 }
2893}
2894
2895TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Chris Ye2ad95392020-09-01 13:44:44 -07002896 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002897
2898 std::unordered_set<int32_t> idSet;
2899 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2900 InputEvent* repeatEvent = mWindow->consume();
2901 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2902 int32_t id = repeatEvent->getId();
2903 EXPECT_EQ(idSet.end(), idSet.find(id));
2904 idSet.insert(id);
2905 }
2906}
2907
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002908/* Test InputDispatcher for MultiDisplay */
2909class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
2910public:
2911 static constexpr int32_t SECOND_DISPLAY_ID = 1;
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002912 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002913 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08002914
Chris Yea209fde2020-07-22 13:54:51 -07002915 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002916 windowInPrimary =
2917 new FakeWindowHandle(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002918
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002919 // Set focus window for primary display, but focused display would be second one.
2920 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07002921 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002922 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002923 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002924 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08002925
Chris Yea209fde2020-07-22 13:54:51 -07002926 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002927 windowInSecondary =
2928 new FakeWindowHandle(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002929 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002930 // Set focus display to second one.
2931 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
2932 // Set focus window for second display.
2933 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07002934 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002935 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002936 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002937 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002938 }
2939
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002940 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002941 InputDispatcherTest::TearDown();
2942
Chris Yea209fde2020-07-22 13:54:51 -07002943 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002944 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07002945 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002946 windowInSecondary.clear();
2947 }
2948
2949protected:
Chris Yea209fde2020-07-22 13:54:51 -07002950 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002951 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07002952 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002953 sp<FakeWindowHandle> windowInSecondary;
2954};
2955
2956TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
2957 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002958 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2959 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2960 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002961 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08002962 windowInSecondary->assertNoEvents();
2963
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002964 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002965 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2966 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
2967 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08002968 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002969 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08002970}
2971
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002972TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08002973 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08002974 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2975 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002976 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002977 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08002978 windowInSecondary->assertNoEvents();
2979
2980 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08002981 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002982 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08002983 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002984 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08002985
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002986 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00002987 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08002988
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002989 // Old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002990 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
2991 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08002992
2993 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08002994 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002995 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08002996 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002997 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08002998 windowInSecondary->assertNoEvents();
2999}
3000
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003001// Test per-display input monitors for motion event.
3002TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08003003 FakeMonitorReceiver monitorInPrimary =
3004 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
3005 FakeMonitorReceiver monitorInSecondary =
3006 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003007
3008 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003009 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3010 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
3011 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003012 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08003013 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003014 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003015 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003016
3017 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003018 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3019 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
3020 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003021 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003022 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003023 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08003024 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003025
3026 // Test inject a non-pointer motion event.
3027 // If specific a display, it will dispatch to the focused window of particular display,
3028 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003029 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3030 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
3031 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003032 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003033 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003034 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08003035 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003036}
3037
3038// Test per-display input monitors for key event.
3039TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003040 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08003041 FakeMonitorReceiver monitorInPrimary =
3042 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
3043 FakeMonitorReceiver monitorInSecondary =
3044 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003045
3046 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003047 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3048 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003049 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003050 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003051 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08003052 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003053}
3054
Vishnu Nair958da932020-08-21 17:12:37 -07003055TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
3056 sp<FakeWindowHandle> secondWindowInPrimary =
3057 new FakeWindowHandle(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
3058 secondWindowInPrimary->setFocusable(true);
3059 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
3060 setFocusedWindow(secondWindowInPrimary);
3061 windowInPrimary->consumeFocusEvent(false);
3062 secondWindowInPrimary->consumeFocusEvent(true);
3063
3064 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003065 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
3066 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003067 windowInPrimary->assertNoEvents();
3068 windowInSecondary->assertNoEvents();
3069 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3070}
3071
Jackal Guof9696682018-10-05 12:23:23 +08003072class InputFilterTest : public InputDispatcherTest {
3073protected:
3074 static constexpr int32_t SECOND_DISPLAY_ID = 1;
3075
3076 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) {
3077 NotifyMotionArgs motionArgs;
3078
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003079 motionArgs =
3080 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08003081 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003082 motionArgs =
3083 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08003084 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003085 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08003086 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08003087 mFakePolicy->assertFilterInputEventWasCalled(motionArgs);
Jackal Guof9696682018-10-05 12:23:23 +08003088 } else {
3089 mFakePolicy->assertFilterInputEventWasNotCalled();
3090 }
3091 }
3092
3093 void testNotifyKey(bool expectToBeFiltered) {
3094 NotifyKeyArgs keyArgs;
3095
3096 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
3097 mDispatcher->notifyKey(&keyArgs);
3098 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
3099 mDispatcher->notifyKey(&keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003100 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08003101
3102 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08003103 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08003104 } else {
3105 mFakePolicy->assertFilterInputEventWasNotCalled();
3106 }
3107 }
3108};
3109
3110// Test InputFilter for MotionEvent
3111TEST_F(InputFilterTest, MotionEvent_InputFilter) {
3112 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
3113 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
3114 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
3115
3116 // Enable InputFilter
3117 mDispatcher->setInputFilterEnabled(true);
3118 // Test touch on both primary and second display, and check if both events are filtered.
3119 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
3120 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
3121
3122 // Disable InputFilter
3123 mDispatcher->setInputFilterEnabled(false);
3124 // Test touch on both primary and second display, and check if both events aren't filtered.
3125 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
3126 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
3127}
3128
3129// Test InputFilter for KeyEvent
3130TEST_F(InputFilterTest, KeyEvent_InputFilter) {
3131 // Since the InputFilter is disabled by default, check if key event aren't filtered.
3132 testNotifyKey(/*expectToBeFiltered*/ false);
3133
3134 // Enable InputFilter
3135 mDispatcher->setInputFilterEnabled(true);
3136 // Send a key event, and check if it is filtered.
3137 testNotifyKey(/*expectToBeFiltered*/ true);
3138
3139 // Disable InputFilter
3140 mDispatcher->setInputFilterEnabled(false);
3141 // Send a key event, and check if it isn't filtered.
3142 testNotifyKey(/*expectToBeFiltered*/ false);
3143}
3144
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003145class InputFilterInjectionPolicyTest : public InputDispatcherTest {
3146protected:
3147 virtual void SetUp() override {
3148 InputDispatcherTest::SetUp();
3149
3150 /**
3151 * We don't need to enable input filter to test the injected event policy, but we enabled it
3152 * here to make the tests more realistic, since this policy only matters when inputfilter is
3153 * on.
3154 */
3155 mDispatcher->setInputFilterEnabled(true);
3156
3157 std::shared_ptr<InputApplicationHandle> application =
3158 std::make_shared<FakeApplicationHandle>();
3159 mWindow =
3160 new FakeWindowHandle(application, mDispatcher, "Test Window", ADISPLAY_ID_DEFAULT);
3161
3162 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3163 mWindow->setFocusable(true);
3164 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3165 setFocusedWindow(mWindow);
3166 mWindow->consumeFocusEvent(true);
3167 }
3168
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003169 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
3170 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003171 KeyEvent event;
3172
3173 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
3174 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
3175 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
3176 KEY_A, AMETA_NONE, 0 /*repeatCount*/, eventTime, eventTime);
3177 const int32_t additionalPolicyFlags =
3178 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
3179 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3180 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
3181 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
3182 policyFlags | additionalPolicyFlags));
3183
3184 InputEvent* received = mWindow->consume();
3185 ASSERT_NE(nullptr, received);
3186 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003187 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_KEY);
3188 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
3189 ASSERT_EQ(flags, keyEvent.getFlags());
3190 }
3191
3192 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
3193 int32_t flags) {
3194 MotionEvent event;
3195 PointerProperties pointerProperties[1];
3196 PointerCoords pointerCoords[1];
3197 pointerProperties[0].clear();
3198 pointerProperties[0].id = 0;
3199 pointerCoords[0].clear();
3200 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
3201 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
3202
3203 ui::Transform identityTransform;
3204 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
3205 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
3206 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
3207 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
3208 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -07003209 AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0,
3210 0 /*INVALID_DISPLAY_SIZE*/, 0 /*INVALID_DISPLAY_SIZE*/, eventTime,
3211 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003212 /*pointerCount*/ 1, pointerProperties, pointerCoords);
3213
3214 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
3215 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3216 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
3217 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
3218 policyFlags | additionalPolicyFlags));
3219
3220 InputEvent* received = mWindow->consume();
3221 ASSERT_NE(nullptr, received);
3222 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
3223 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_MOTION);
3224 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
3225 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003226 }
3227
3228private:
3229 sp<FakeWindowHandle> mWindow;
3230};
3231
3232TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003233 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
3234 // filter. Without it, the event will no different from a regularly injected event, and the
3235 // injected device id will be overwritten.
3236 testInjectedKey(POLICY_FLAG_FILTERED, 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
3237 0 /*flags*/);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003238}
3239
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003240TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003241 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003242 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
3243 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
3244}
3245
3246TEST_F(InputFilterInjectionPolicyTest,
3247 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
3248 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
3249 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
3250 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003251}
3252
3253TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
3254 testInjectedKey(0 /*policyFlags*/, 3 /*injectedDeviceId*/,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003255 VIRTUAL_KEYBOARD_ID /*resolvedDeviceId*/, 0 /*flags*/);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003256}
3257
chaviwfd6d3512019-03-25 13:23:49 -07003258class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07003259 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07003260 InputDispatcherTest::SetUp();
3261
Chris Yea209fde2020-07-22 13:54:51 -07003262 std::shared_ptr<FakeApplicationHandle> application =
3263 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003264 mUnfocusedWindow =
3265 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07003266 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
3267 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3268 // window.
chaviw3277faf2021-05-19 16:45:23 -05003269 mUnfocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07003270
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003271 mFocusedWindow =
3272 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
3273 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviw3277faf2021-05-19 16:45:23 -05003274 mFocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07003275
3276 // Set focused application.
3277 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07003278 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07003279
3280 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00003281 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003282 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003283 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07003284 }
3285
Prabir Pradhan3608aad2019-10-02 17:08:26 -07003286 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07003287 InputDispatcherTest::TearDown();
3288
3289 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003290 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07003291 }
3292
3293protected:
3294 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003295 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003296 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07003297};
3298
3299// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
3300// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
3301// the onPointerDownOutsideFocus callback.
3302TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003303 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003304 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3305 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003306 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003307 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07003308
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003309 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07003310 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
3311}
3312
3313// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
3314// DOWN on the window that doesn't have focus. Ensure no window received the
3315// onPointerDownOutsideFocus callback.
3316TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003317 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003318 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003319 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003320 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07003321
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003322 ASSERT_TRUE(mDispatcher->waitForIdle());
3323 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07003324}
3325
3326// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
3327// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
3328TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003329 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3330 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003331 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003332 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07003333
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003334 ASSERT_TRUE(mDispatcher->waitForIdle());
3335 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07003336}
3337
3338// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
3339// DOWN on the window that already has focus. Ensure no window received the
3340// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003341TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003342 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003343 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003344 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003345 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003346 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07003347
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003348 ASSERT_TRUE(mDispatcher->waitForIdle());
3349 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07003350}
3351
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08003352// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
3353// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
3354TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
3355 const MotionEvent event =
3356 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3357 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
3358 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(20).y(20))
3359 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
3360 .build();
3361 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
3362 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3363 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
3364
3365 ASSERT_TRUE(mDispatcher->waitForIdle());
3366 mFakePolicy->assertOnPointerDownWasNotCalled();
3367 // Ensure that the unfocused window did not receive any FOCUS events.
3368 mUnfocusedWindow->assertNoEvents();
3369}
3370
chaviwaf87b3e2019-10-01 16:59:28 -07003371// These tests ensures we can send touch events to a single client when there are multiple input
3372// windows that point to the same client token.
3373class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
3374 virtual void SetUp() override {
3375 InputDispatcherTest::SetUp();
3376
Chris Yea209fde2020-07-22 13:54:51 -07003377 std::shared_ptr<FakeApplicationHandle> application =
3378 std::make_shared<FakeApplicationHandle>();
chaviwaf87b3e2019-10-01 16:59:28 -07003379 mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1",
3380 ADISPLAY_ID_DEFAULT);
3381 // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window.
3382 // We also need FLAG_SPLIT_TOUCH or we won't be able to get touches for both windows.
chaviw3277faf2021-05-19 16:45:23 -05003383 mWindow1->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07003384 mWindow1->setFrame(Rect(0, 0, 100, 100));
3385
3386 mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2",
3387 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviw3277faf2021-05-19 16:45:23 -05003388 mWindow2->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07003389 mWindow2->setFrame(Rect(100, 100, 200, 200));
3390
Arthur Hung72d8dc32020-03-28 00:48:39 +00003391 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07003392 }
3393
3394protected:
3395 sp<FakeWindowHandle> mWindow1;
3396 sp<FakeWindowHandle> mWindow2;
3397
3398 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05003399 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07003400 vec2 vals = windowInfo->transform.transform(point.x, point.y);
3401 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07003402 }
3403
3404 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
3405 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003406 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07003407 InputEvent* event = window->consume();
3408
3409 ASSERT_NE(nullptr, event) << name.c_str()
3410 << ": consumer should have returned non-NULL event.";
3411
3412 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
3413 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
3414 << " event, got " << inputEventTypeToString(event->getType()) << " event";
3415
3416 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
3417 EXPECT_EQ(expectedAction, motionEvent.getAction());
3418
3419 for (size_t i = 0; i < points.size(); i++) {
3420 float expectedX = points[i].x;
3421 float expectedY = points[i].y;
3422
3423 EXPECT_EQ(expectedX, motionEvent.getX(i))
3424 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
3425 << ", got " << motionEvent.getX(i);
3426 EXPECT_EQ(expectedY, motionEvent.getY(i))
3427 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
3428 << ", got " << motionEvent.getY(i);
3429 }
3430 }
chaviw9eaa22c2020-07-01 16:21:27 -07003431
3432 void touchAndAssertPositions(int32_t action, std::vector<PointF> touchedPoints,
3433 std::vector<PointF> expectedPoints) {
3434 NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
3435 ADISPLAY_ID_DEFAULT, touchedPoints);
3436 mDispatcher->notifyMotion(&motionArgs);
3437
3438 // Always consume from window1 since it's the window that has the InputReceiver
3439 consumeMotionEvent(mWindow1, action, expectedPoints);
3440 }
chaviwaf87b3e2019-10-01 16:59:28 -07003441};
3442
3443TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
3444 // Touch Window 1
3445 PointF touchedPoint = {10, 10};
3446 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003447 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003448
3449 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07003450 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003451
3452 // Touch Window 2
3453 touchedPoint = {150, 150};
3454 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003455 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003456}
3457
chaviw9eaa22c2020-07-01 16:21:27 -07003458TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
3459 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07003460 mWindow2->setWindowScale(0.5f, 0.5f);
3461
3462 // Touch Window 1
3463 PointF touchedPoint = {10, 10};
3464 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003465 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003466 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07003467 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003468
3469 // Touch Window 2
3470 touchedPoint = {150, 150};
3471 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003472 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
3473 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003474
chaviw9eaa22c2020-07-01 16:21:27 -07003475 // Update the transform so rotation is set
3476 mWindow2->setWindowTransform(0, -1, 1, 0);
3477 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
3478 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003479}
3480
chaviw9eaa22c2020-07-01 16:21:27 -07003481TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003482 mWindow2->setWindowScale(0.5f, 0.5f);
3483
3484 // Touch Window 1
3485 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3486 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003487 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003488
3489 // Touch Window 2
3490 int32_t actionPointerDown =
3491 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003492 touchedPoints.push_back(PointF{150, 150});
3493 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3494 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003495
chaviw9eaa22c2020-07-01 16:21:27 -07003496 // Release Window 2
3497 int32_t actionPointerUp =
3498 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3499 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
3500 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003501
chaviw9eaa22c2020-07-01 16:21:27 -07003502 // Update the transform so rotation is set for Window 2
3503 mWindow2->setWindowTransform(0, -1, 1, 0);
3504 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3505 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003506}
3507
chaviw9eaa22c2020-07-01 16:21:27 -07003508TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003509 mWindow2->setWindowScale(0.5f, 0.5f);
3510
3511 // Touch Window 1
3512 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3513 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003514 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003515
3516 // Touch Window 2
3517 int32_t actionPointerDown =
3518 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003519 touchedPoints.push_back(PointF{150, 150});
3520 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003521
chaviw9eaa22c2020-07-01 16:21:27 -07003522 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003523
3524 // Move both windows
3525 touchedPoints = {{20, 20}, {175, 175}};
3526 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3527 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3528
chaviw9eaa22c2020-07-01 16:21:27 -07003529 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003530
chaviw9eaa22c2020-07-01 16:21:27 -07003531 // Release Window 2
3532 int32_t actionPointerUp =
3533 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3534 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
3535 expectedPoints.pop_back();
3536
3537 // Touch Window 2
3538 mWindow2->setWindowTransform(0, -1, 1, 0);
3539 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3540 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
3541
3542 // Move both windows
3543 touchedPoints = {{20, 20}, {175, 175}};
3544 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3545 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3546
3547 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003548}
3549
3550TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
3551 mWindow1->setWindowScale(0.5f, 0.5f);
3552
3553 // Touch Window 1
3554 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3555 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003556 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003557
3558 // Touch Window 2
3559 int32_t actionPointerDown =
3560 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003561 touchedPoints.push_back(PointF{150, 150});
3562 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003563
chaviw9eaa22c2020-07-01 16:21:27 -07003564 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003565
3566 // Move both windows
3567 touchedPoints = {{20, 20}, {175, 175}};
3568 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3569 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3570
chaviw9eaa22c2020-07-01 16:21:27 -07003571 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003572}
3573
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003574class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
3575 virtual void SetUp() override {
3576 InputDispatcherTest::SetUp();
3577
Chris Yea209fde2020-07-22 13:54:51 -07003578 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003579 mApplication->setDispatchingTimeout(20ms);
3580 mWindow =
3581 new FakeWindowHandle(mApplication, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
3582 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05003583 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07003584 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003585 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3586 // window.
chaviw3277faf2021-05-19 16:45:23 -05003587 mWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003588
3589 // Set focused application.
3590 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
3591
3592 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003593 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003594 mWindow->consumeFocusEvent(true);
3595 }
3596
3597 virtual void TearDown() override {
3598 InputDispatcherTest::TearDown();
3599 mWindow.clear();
3600 }
3601
3602protected:
Chris Yea209fde2020-07-22 13:54:51 -07003603 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003604 sp<FakeWindowHandle> mWindow;
3605 static constexpr PointF WINDOW_LOCATION = {20, 20};
3606
3607 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003608 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003609 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3610 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003611 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003612 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3613 WINDOW_LOCATION));
3614 }
3615};
3616
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003617// Send a tap and respond, which should not cause an ANR.
3618TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
3619 tapOnWindow();
3620 mWindow->consumeMotionDown();
3621 mWindow->consumeMotionUp();
3622 ASSERT_TRUE(mDispatcher->waitForIdle());
3623 mFakePolicy->assertNotifyAnrWasNotCalled();
3624}
3625
3626// Send a regular key and respond, which should not cause an ANR.
3627TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003628 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003629 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
3630 ASSERT_TRUE(mDispatcher->waitForIdle());
3631 mFakePolicy->assertNotifyAnrWasNotCalled();
3632}
3633
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003634TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
3635 mWindow->setFocusable(false);
3636 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3637 mWindow->consumeFocusEvent(false);
3638
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003639 InputEventInjectionResult result =
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003640 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003641 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/,
3642 false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003643 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003644 // Key will not go to window because we have no focused window.
3645 // The 'no focused window' ANR timer should start instead.
3646
3647 // Now, the focused application goes away.
3648 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
3649 // The key should get dropped and there should be no ANR.
3650
3651 ASSERT_TRUE(mDispatcher->waitForIdle());
3652 mFakePolicy->assertNotifyAnrWasNotCalled();
3653}
3654
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003655// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003656// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
3657// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003658TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003659 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003660 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3661 WINDOW_LOCATION));
3662
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003663 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
3664 ASSERT_TRUE(sequenceNum);
3665 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003666 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003667
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003668 mWindow->finishEvent(*sequenceNum);
3669 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3670 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003671 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003672 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003673}
3674
3675// Send a key to the app and have the app not respond right away.
3676TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
3677 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003678 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003679 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
3680 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003681 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003682 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003683 ASSERT_TRUE(mDispatcher->waitForIdle());
3684}
3685
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003686// We have a focused application, but no focused window
3687TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003688 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003689 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3690 mWindow->consumeFocusEvent(false);
3691
3692 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003693 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003694 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3695 WINDOW_LOCATION));
3696 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
3697 mDispatcher->waitForIdle();
3698 mFakePolicy->assertNotifyAnrWasNotCalled();
3699
3700 // Once a focused event arrives, we get an ANR for this application
3701 // We specify the injection timeout to be smaller than the application timeout, to ensure that
3702 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003703 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003704 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003705 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003706 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003707 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003708 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003709 ASSERT_TRUE(mDispatcher->waitForIdle());
3710}
3711
3712// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003713// Make sure that we don't notify policy twice about the same ANR.
3714TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003715 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003716 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3717 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003718
3719 // Once a focused event arrives, we get an ANR for this application
3720 // We specify the injection timeout to be smaller than the application timeout, to ensure that
3721 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003722 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003723 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003724 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003725 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003726 const std::chrono::duration appTimeout =
3727 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003728 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003729
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003730 std::this_thread::sleep_for(appTimeout);
3731 // ANR should not be raised again. It is up to policy to do that if it desires.
3732 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003733
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003734 // If we now get a focused window, the ANR should stop, but the policy handles that via
3735 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003736 ASSERT_TRUE(mDispatcher->waitForIdle());
3737}
3738
3739// We have a focused application, but no focused window
3740TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003741 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003742 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3743 mWindow->consumeFocusEvent(false);
3744
3745 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003746 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003747 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003748 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3749 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003750
3751 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003752 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003753
3754 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003755 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003756 ASSERT_TRUE(mDispatcher->waitForIdle());
3757 mWindow->assertNoEvents();
3758}
3759
3760/**
3761 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
3762 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
3763 * If we process 1 of the events, but ANR on the second event with the same timestamp,
3764 * the ANR mechanism should still work.
3765 *
3766 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
3767 * DOWN event, while not responding on the second one.
3768 */
3769TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
3770 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
3771 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3772 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
3773 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
3774 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003775 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003776
3777 // Now send ACTION_UP, with identical timestamp
3778 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3779 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
3780 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
3781 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003782 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003783
3784 // We have now sent down and up. Let's consume first event and then ANR on the second.
3785 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3786 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003787 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003788}
3789
3790// If an app is not responding to a key event, gesture monitors should continue to receive
3791// new motion events
3792TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnKey) {
3793 FakeMonitorReceiver monitor =
3794 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
3795 true /*isGestureMonitor*/);
3796
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003797 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3798 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003799 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003800 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003801
3802 // Stuck on the ACTION_UP
3803 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003804 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003805
3806 // New tap will go to the gesture monitor, but not to the window
3807 tapOnWindow();
3808 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3809 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3810
3811 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
3812 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003813 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003814 mWindow->assertNoEvents();
3815 monitor.assertNoEvents();
3816}
3817
3818// If an app is not responding to a motion event, gesture monitors should continue to receive
3819// new motion events
3820TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnMotion) {
3821 FakeMonitorReceiver monitor =
3822 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
3823 true /*isGestureMonitor*/);
3824
3825 tapOnWindow();
3826 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3827 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3828
3829 mWindow->consumeMotionDown();
3830 // Stuck on the ACTION_UP
3831 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003832 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003833
3834 // New tap will go to the gesture monitor, but not to the window
3835 tapOnWindow();
3836 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3837 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3838
3839 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
3840 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003841 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003842 mWindow->assertNoEvents();
3843 monitor.assertNoEvents();
3844}
3845
3846// If a window is unresponsive, then you get anr. if the window later catches up and starts to
3847// process events, you don't get an anr. When the window later becomes unresponsive again, you
3848// get an ANR again.
3849// 1. tap -> block on ACTION_UP -> receive ANR
3850// 2. consume all pending events (= queue becomes healthy again)
3851// 3. tap again -> block on ACTION_UP again -> receive ANR second time
3852TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
3853 tapOnWindow();
3854
3855 mWindow->consumeMotionDown();
3856 // Block on ACTION_UP
3857 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003858 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003859 mWindow->consumeMotionUp(); // Now the connection should be healthy again
3860 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003861 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003862 mWindow->assertNoEvents();
3863
3864 tapOnWindow();
3865 mWindow->consumeMotionDown();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003866 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003867 mWindow->consumeMotionUp();
3868
3869 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003870 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003871 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003872 mWindow->assertNoEvents();
3873}
3874
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003875// If a connection remains unresponsive for a while, make sure policy is only notified once about
3876// it.
3877TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003878 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003879 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3880 WINDOW_LOCATION));
3881
3882 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003883 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003884 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003885 // 'notifyConnectionUnresponsive' should only be called once per connection
3886 mFakePolicy->assertNotifyAnrWasNotCalled();
3887 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003888 mWindow->consumeMotionDown();
3889 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3890 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3891 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003892 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003893 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003894 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003895}
3896
3897/**
3898 * If a window is processing a motion event, and then a key event comes in, the key event should
3899 * not to to the focused window until the motion is processed.
3900 *
3901 * Warning!!!
3902 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
3903 * and the injection timeout that we specify when injecting the key.
3904 * We must have the injection timeout (10ms) be smaller than
3905 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
3906 *
3907 * If that value changes, this test should also change.
3908 */
3909TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
3910 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3911 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3912
3913 tapOnWindow();
3914 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3915 ASSERT_TRUE(downSequenceNum);
3916 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3917 ASSERT_TRUE(upSequenceNum);
3918 // Don't finish the events yet, and send a key
3919 // Injection will "succeed" because we will eventually give up and send the key to the focused
3920 // window even if motions are still being processed. But because the injection timeout is short,
3921 // we will receive INJECTION_TIMED_OUT as the result.
3922
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003923 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003924 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003925 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3926 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003927 // Key will not be sent to the window, yet, because the window is still processing events
3928 // and the key remains pending, waiting for the touch events to be processed
3929 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3930 ASSERT_FALSE(keySequenceNum);
3931
3932 std::this_thread::sleep_for(500ms);
3933 // if we wait long enough though, dispatcher will give up, and still send the key
3934 // to the focused window, even though we have not yet finished the motion event
3935 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3936 mWindow->finishEvent(*downSequenceNum);
3937 mWindow->finishEvent(*upSequenceNum);
3938}
3939
3940/**
3941 * If a window is processing a motion event, and then a key event comes in, the key event should
3942 * not go to the focused window until the motion is processed.
3943 * If then a new motion comes in, then the pending key event should be going to the currently
3944 * focused window right away.
3945 */
3946TEST_F(InputDispatcherSingleWindowAnr,
3947 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
3948 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3949 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3950
3951 tapOnWindow();
3952 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3953 ASSERT_TRUE(downSequenceNum);
3954 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3955 ASSERT_TRUE(upSequenceNum);
3956 // Don't finish the events yet, and send a key
3957 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003958 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003959 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003960 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003961 // At this point, key is still pending, and should not be sent to the application yet.
3962 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3963 ASSERT_FALSE(keySequenceNum);
3964
3965 // Now tap down again. It should cause the pending key to go to the focused window right away.
3966 tapOnWindow();
3967 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
3968 // the other events yet. We can finish events in any order.
3969 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
3970 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
3971 mWindow->consumeMotionDown();
3972 mWindow->consumeMotionUp();
3973 mWindow->assertNoEvents();
3974}
3975
3976class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
3977 virtual void SetUp() override {
3978 InputDispatcherTest::SetUp();
3979
Chris Yea209fde2020-07-22 13:54:51 -07003980 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003981 mApplication->setDispatchingTimeout(10ms);
3982 mUnfocusedWindow =
3983 new FakeWindowHandle(mApplication, mDispatcher, "Unfocused", ADISPLAY_ID_DEFAULT);
3984 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
3985 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3986 // window.
3987 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
chaviw3277faf2021-05-19 16:45:23 -05003988 mUnfocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL |
3989 WindowInfo::Flag::WATCH_OUTSIDE_TOUCH |
3990 WindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003991
3992 mFocusedWindow =
3993 new FakeWindowHandle(mApplication, mDispatcher, "Focused", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05003994 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003995 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviw3277faf2021-05-19 16:45:23 -05003996 mFocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003997
3998 // Set focused application.
3999 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07004000 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004001
4002 // Expect one focus window exist in display.
4003 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004004 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004005 mFocusedWindow->consumeFocusEvent(true);
4006 }
4007
4008 virtual void TearDown() override {
4009 InputDispatcherTest::TearDown();
4010
4011 mUnfocusedWindow.clear();
4012 mFocusedWindow.clear();
4013 }
4014
4015protected:
Chris Yea209fde2020-07-22 13:54:51 -07004016 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004017 sp<FakeWindowHandle> mUnfocusedWindow;
4018 sp<FakeWindowHandle> mFocusedWindow;
4019 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
4020 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
4021 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
4022
4023 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
4024
4025 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
4026
4027private:
4028 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004029 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004030 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4031 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004032 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004033 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4034 location));
4035 }
4036};
4037
4038// If we have 2 windows that are both unresponsive, the one with the shortest timeout
4039// should be ANR'd first.
4040TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004041 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004042 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4043 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004044 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004045 mFocusedWindow->consumeMotionDown();
4046 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
4047 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
4048 // We consumed all events, so no ANR
4049 ASSERT_TRUE(mDispatcher->waitForIdle());
4050 mFakePolicy->assertNotifyAnrWasNotCalled();
4051
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004052 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004053 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4054 FOCUSED_WINDOW_LOCATION));
4055 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
4056 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004057
4058 const std::chrono::duration timeout =
4059 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004060 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004061 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
4062 // sequence to make it consistent
4063 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004064 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004065 mFocusedWindow->consumeMotionDown();
4066 // This cancel is generated because the connection was unresponsive
4067 mFocusedWindow->consumeMotionCancel();
4068 mFocusedWindow->assertNoEvents();
4069 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004070 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004071 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004072 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004073}
4074
4075// If we have 2 windows with identical timeouts that are both unresponsive,
4076// it doesn't matter which order they should have ANR.
4077// But we should receive ANR for both.
4078TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
4079 // Set the timeout for unfocused window to match the focused window
4080 mUnfocusedWindow->setDispatchingTimeout(10ms);
4081 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
4082
4083 tapOnFocusedWindow();
4084 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004085 sp<IBinder> anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms);
4086 sp<IBinder> anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004087
4088 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004089 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
4090 mFocusedWindow->getToken() == anrConnectionToken2);
4091 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
4092 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004093
4094 ASSERT_TRUE(mDispatcher->waitForIdle());
4095 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004096
4097 mFocusedWindow->consumeMotionDown();
4098 mFocusedWindow->consumeMotionUp();
4099 mUnfocusedWindow->consumeMotionOutside();
4100
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004101 sp<IBinder> responsiveToken1 = mFakePolicy->getResponsiveWindowToken();
4102 sp<IBinder> responsiveToken2 = mFakePolicy->getResponsiveWindowToken();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004103
4104 // Both applications should be marked as responsive, in any order
4105 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
4106 mFocusedWindow->getToken() == responsiveToken2);
4107 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
4108 mUnfocusedWindow->getToken() == responsiveToken2);
4109 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004110}
4111
4112// If a window is already not responding, the second tap on the same window should be ignored.
4113// We should also log an error to account for the dropped event (not tested here).
4114// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
4115TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
4116 tapOnFocusedWindow();
4117 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
4118 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
4119 // Receive the events, but don't respond
4120 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
4121 ASSERT_TRUE(downEventSequenceNum);
4122 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
4123 ASSERT_TRUE(upEventSequenceNum);
4124 const std::chrono::duration timeout =
4125 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004126 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004127
4128 // Tap once again
4129 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004130 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004131 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4132 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004133 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004134 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4135 FOCUSED_WINDOW_LOCATION));
4136 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
4137 // valid touch target
4138 mUnfocusedWindow->assertNoEvents();
4139
4140 // Consume the first tap
4141 mFocusedWindow->finishEvent(*downEventSequenceNum);
4142 mFocusedWindow->finishEvent(*upEventSequenceNum);
4143 ASSERT_TRUE(mDispatcher->waitForIdle());
4144 // The second tap did not go to the focused window
4145 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004146 // Since all events are finished, connection should be deemed healthy again
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004147 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004148 mFakePolicy->assertNotifyAnrWasNotCalled();
4149}
4150
4151// If you tap outside of all windows, there will not be ANR
4152TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004153 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004154 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4155 LOCATION_OUTSIDE_ALL_WINDOWS));
4156 ASSERT_TRUE(mDispatcher->waitForIdle());
4157 mFakePolicy->assertNotifyAnrWasNotCalled();
4158}
4159
4160// Since the focused window is paused, tapping on it should not produce any events
4161TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
4162 mFocusedWindow->setPaused(true);
4163 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
4164
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004165 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004166 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4167 FOCUSED_WINDOW_LOCATION));
4168
4169 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
4170 ASSERT_TRUE(mDispatcher->waitForIdle());
4171 // Should not ANR because the window is paused, and touches shouldn't go to it
4172 mFakePolicy->assertNotifyAnrWasNotCalled();
4173
4174 mFocusedWindow->assertNoEvents();
4175 mUnfocusedWindow->assertNoEvents();
4176}
4177
4178/**
4179 * If a window is processing a motion event, and then a key event comes in, the key event should
4180 * not to to the focused window until the motion is processed.
4181 * If a different window becomes focused at this time, the key should go to that window instead.
4182 *
4183 * Warning!!!
4184 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
4185 * and the injection timeout that we specify when injecting the key.
4186 * We must have the injection timeout (10ms) be smaller than
4187 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
4188 *
4189 * If that value changes, this test should also change.
4190 */
4191TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
4192 // Set a long ANR timeout to prevent it from triggering
4193 mFocusedWindow->setDispatchingTimeout(2s);
4194 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
4195
4196 tapOnUnfocusedWindow();
4197 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
4198 ASSERT_TRUE(downSequenceNum);
4199 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
4200 ASSERT_TRUE(upSequenceNum);
4201 // Don't finish the events yet, and send a key
4202 // Injection will succeed because we will eventually give up and send the key to the focused
4203 // window even if motions are still being processed.
4204
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004205 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004206 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004207 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
4208 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004209 // Key will not be sent to the window, yet, because the window is still processing events
4210 // and the key remains pending, waiting for the touch events to be processed
4211 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
4212 ASSERT_FALSE(keySequenceNum);
4213
4214 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07004215 mFocusedWindow->setFocusable(false);
4216 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004217 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004218 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004219
4220 // Focus events should precede the key events
4221 mUnfocusedWindow->consumeFocusEvent(true);
4222 mFocusedWindow->consumeFocusEvent(false);
4223
4224 // Finish the tap events, which should unblock dispatcher
4225 mUnfocusedWindow->finishEvent(*downSequenceNum);
4226 mUnfocusedWindow->finishEvent(*upSequenceNum);
4227
4228 // Now that all queues are cleared and no backlog in the connections, the key event
4229 // can finally go to the newly focused "mUnfocusedWindow".
4230 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4231 mFocusedWindow->assertNoEvents();
4232 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004233 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004234}
4235
4236// When the touch stream is split across 2 windows, and one of them does not respond,
4237// then ANR should be raised and the touch should be canceled for the unresponsive window.
4238// The other window should not be affected by that.
4239TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
4240 // Touch Window 1
4241 NotifyMotionArgs motionArgs =
4242 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4243 ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION});
4244 mDispatcher->notifyMotion(&motionArgs);
4245 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
4246 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
4247
4248 // Touch Window 2
4249 int32_t actionPointerDown =
4250 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
4251
4252 motionArgs =
4253 generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4254 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION});
4255 mDispatcher->notifyMotion(&motionArgs);
4256
4257 const std::chrono::duration timeout =
4258 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004259 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004260
4261 mUnfocusedWindow->consumeMotionDown();
4262 mFocusedWindow->consumeMotionDown();
4263 // Focused window may or may not receive ACTION_MOVE
4264 // But it should definitely receive ACTION_CANCEL due to the ANR
4265 InputEvent* event;
4266 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
4267 ASSERT_TRUE(moveOrCancelSequenceNum);
4268 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
4269 ASSERT_NE(nullptr, event);
4270 ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION);
4271 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
4272 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
4273 mFocusedWindow->consumeMotionCancel();
4274 } else {
4275 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
4276 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004277 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004278 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004279
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004280 mUnfocusedWindow->assertNoEvents();
4281 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004282 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004283}
4284
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05004285/**
4286 * If we have no focused window, and a key comes in, we start the ANR timer.
4287 * The focused application should add a focused window before the timer runs out to prevent ANR.
4288 *
4289 * If the user touches another application during this time, the key should be dropped.
4290 * Next, if a new focused window comes in, without toggling the focused application,
4291 * then no ANR should occur.
4292 *
4293 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
4294 * but in some cases the policy may not update the focused application.
4295 */
4296TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
4297 std::shared_ptr<FakeApplicationHandle> focusedApplication =
4298 std::make_shared<FakeApplicationHandle>();
4299 focusedApplication->setDispatchingTimeout(60ms);
4300 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
4301 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
4302 mFocusedWindow->setFocusable(false);
4303
4304 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
4305 mFocusedWindow->consumeFocusEvent(false);
4306
4307 // Send a key. The ANR timer should start because there is no focused window.
4308 // 'focusedApplication' will get blamed if this timer completes.
4309 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004310 InputEventInjectionResult result =
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05004311 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004312 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/,
4313 false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004314 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05004315
4316 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
4317 // then the injected touches won't cause the focused event to get dropped.
4318 // The dispatcher only checks for whether the queue should be pruned upon queueing.
4319 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
4320 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
4321 // For this test, it means that the key would get delivered to the window once it becomes
4322 // focused.
4323 std::this_thread::sleep_for(10ms);
4324
4325 // Touch unfocused window. This should force the pending key to get dropped.
4326 NotifyMotionArgs motionArgs =
4327 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4328 ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION});
4329 mDispatcher->notifyMotion(&motionArgs);
4330
4331 // We do not consume the motion right away, because that would require dispatcher to first
4332 // process (== drop) the key event, and by that time, ANR will be raised.
4333 // Set the focused window first.
4334 mFocusedWindow->setFocusable(true);
4335 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
4336 setFocusedWindow(mFocusedWindow);
4337 mFocusedWindow->consumeFocusEvent(true);
4338 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
4339 // to another application. This could be a bug / behaviour in the policy.
4340
4341 mUnfocusedWindow->consumeMotionDown();
4342
4343 ASSERT_TRUE(mDispatcher->waitForIdle());
4344 // Should not ANR because we actually have a focused window. It was just added too slowly.
4345 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
4346}
4347
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05004348// These tests ensure we cannot send touch events to a window that's positioned behind a window
4349// that has feature NO_INPUT_CHANNEL.
4350// Layout:
4351// Top (closest to user)
4352// mNoInputWindow (above all windows)
4353// mBottomWindow
4354// Bottom (furthest from user)
4355class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
4356 virtual void SetUp() override {
4357 InputDispatcherTest::SetUp();
4358
4359 mApplication = std::make_shared<FakeApplicationHandle>();
4360 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
4361 "Window without input channel", ADISPLAY_ID_DEFAULT,
4362 std::make_optional<sp<IBinder>>(nullptr) /*token*/);
4363
chaviw3277faf2021-05-19 16:45:23 -05004364 mNoInputWindow->setInputFeatures(WindowInfo::Feature::NO_INPUT_CHANNEL);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05004365 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
4366 // It's perfectly valid for this window to not have an associated input channel
4367
4368 mBottomWindow = new FakeWindowHandle(mApplication, mDispatcher, "Bottom window",
4369 ADISPLAY_ID_DEFAULT);
4370 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
4371
4372 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
4373 }
4374
4375protected:
4376 std::shared_ptr<FakeApplicationHandle> mApplication;
4377 sp<FakeWindowHandle> mNoInputWindow;
4378 sp<FakeWindowHandle> mBottomWindow;
4379};
4380
4381TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
4382 PointF touchedPoint = {10, 10};
4383
4384 NotifyMotionArgs motionArgs =
4385 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4386 ADISPLAY_ID_DEFAULT, {touchedPoint});
4387 mDispatcher->notifyMotion(&motionArgs);
4388
4389 mNoInputWindow->assertNoEvents();
4390 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
4391 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
4392 // and therefore should prevent mBottomWindow from receiving touches
4393 mBottomWindow->assertNoEvents();
4394}
4395
4396/**
4397 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
4398 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
4399 */
4400TEST_F(InputDispatcherMultiWindowOcclusionTests,
4401 NoInputChannelFeature_DropsTouchesWithValidChannel) {
4402 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
4403 "Window with input channel and NO_INPUT_CHANNEL",
4404 ADISPLAY_ID_DEFAULT);
4405
chaviw3277faf2021-05-19 16:45:23 -05004406 mNoInputWindow->setInputFeatures(WindowInfo::Feature::NO_INPUT_CHANNEL);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05004407 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
4408 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
4409
4410 PointF touchedPoint = {10, 10};
4411
4412 NotifyMotionArgs motionArgs =
4413 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4414 ADISPLAY_ID_DEFAULT, {touchedPoint});
4415 mDispatcher->notifyMotion(&motionArgs);
4416
4417 mNoInputWindow->assertNoEvents();
4418 mBottomWindow->assertNoEvents();
4419}
4420
Vishnu Nair958da932020-08-21 17:12:37 -07004421class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
4422protected:
4423 std::shared_ptr<FakeApplicationHandle> mApp;
4424 sp<FakeWindowHandle> mWindow;
4425 sp<FakeWindowHandle> mMirror;
4426
4427 virtual void SetUp() override {
4428 InputDispatcherTest::SetUp();
4429 mApp = std::make_shared<FakeApplicationHandle>();
4430 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
4431 mMirror = new FakeWindowHandle(mApp, mDispatcher, "TestWindowMirror", ADISPLAY_ID_DEFAULT,
4432 mWindow->getToken());
4433 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
4434 mWindow->setFocusable(true);
4435 mMirror->setFocusable(true);
4436 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4437 }
4438};
4439
4440TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
4441 // Request focus on a mirrored window
4442 setFocusedWindow(mMirror);
4443
4444 // window gets focused
4445 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004446 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4447 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004448 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
4449}
4450
4451// A focused & mirrored window remains focused only if the window and its mirror are both
4452// focusable.
4453TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
4454 setFocusedWindow(mMirror);
4455
4456 // window gets focused
4457 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004458 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4459 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004460 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004461 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4462 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004463 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4464
4465 mMirror->setFocusable(false);
4466 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4467
4468 // window loses focus since one of the windows associated with the token in not focusable
4469 mWindow->consumeFocusEvent(false);
4470
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004471 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4472 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004473 mWindow->assertNoEvents();
4474}
4475
4476// A focused & mirrored window remains focused until the window and its mirror both become
4477// invisible.
4478TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
4479 setFocusedWindow(mMirror);
4480
4481 // window gets focused
4482 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004483 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4484 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004485 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004486 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4487 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004488 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4489
4490 mMirror->setVisible(false);
4491 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4492
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004493 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4494 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004495 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004496 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4497 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004498 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4499
4500 mWindow->setVisible(false);
4501 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4502
4503 // window loses focus only after all windows associated with the token become invisible.
4504 mWindow->consumeFocusEvent(false);
4505
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004506 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4507 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004508 mWindow->assertNoEvents();
4509}
4510
4511// A focused & mirrored window remains focused until both windows are removed.
4512TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
4513 setFocusedWindow(mMirror);
4514
4515 // window gets focused
4516 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004517 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4518 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004519 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004520 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4521 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004522 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4523
4524 // single window is removed but the window token remains focused
4525 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
4526
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004527 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4528 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004529 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004530 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4531 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004532 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4533
4534 // Both windows are removed
4535 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
4536 mWindow->consumeFocusEvent(false);
4537
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004538 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4539 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004540 mWindow->assertNoEvents();
4541}
4542
4543// Focus request can be pending until one window becomes visible.
4544TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
4545 // Request focus on an invisible mirror.
4546 mWindow->setVisible(false);
4547 mMirror->setVisible(false);
4548 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4549 setFocusedWindow(mMirror);
4550
4551 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004552 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07004553 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004554 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07004555
4556 mMirror->setVisible(true);
4557 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4558
4559 // window gets focused
4560 mWindow->consumeFocusEvent(true);
4561 // window gets the pending key event
4562 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4563}
Prabir Pradhan99987712020-11-10 18:43:05 -08004564
4565class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
4566protected:
4567 std::shared_ptr<FakeApplicationHandle> mApp;
4568 sp<FakeWindowHandle> mWindow;
4569 sp<FakeWindowHandle> mSecondWindow;
4570
4571 void SetUp() override {
4572 InputDispatcherTest::SetUp();
4573 mApp = std::make_shared<FakeApplicationHandle>();
4574 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
4575 mWindow->setFocusable(true);
4576 mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
4577 mSecondWindow->setFocusable(true);
4578
4579 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
4580 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
4581
4582 setFocusedWindow(mWindow);
4583 mWindow->consumeFocusEvent(true);
4584 }
4585
4586 void notifyPointerCaptureChanged(bool enabled) {
4587 const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(enabled);
4588 mDispatcher->notifyPointerCaptureChanged(&args);
4589 }
4590
4591 void requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, bool enabled) {
4592 mDispatcher->requestPointerCapture(window->getToken(), enabled);
4593 mFakePolicy->waitForSetPointerCapture(enabled);
4594 notifyPointerCaptureChanged(enabled);
4595 window->consumeCaptureEvent(enabled);
4596 }
4597};
4598
4599TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
4600 // Ensure that capture cannot be obtained for unfocused windows.
4601 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
4602 mFakePolicy->assertSetPointerCaptureNotCalled();
4603 mSecondWindow->assertNoEvents();
4604
4605 // Ensure that capture can be enabled from the focus window.
4606 requestAndVerifyPointerCapture(mWindow, true);
4607
4608 // Ensure that capture cannot be disabled from a window that does not have capture.
4609 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
4610 mFakePolicy->assertSetPointerCaptureNotCalled();
4611
4612 // Ensure that capture can be disabled from the window with capture.
4613 requestAndVerifyPointerCapture(mWindow, false);
4614}
4615
4616TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
4617 requestAndVerifyPointerCapture(mWindow, true);
4618
4619 setFocusedWindow(mSecondWindow);
4620
4621 // Ensure that the capture disabled event was sent first.
4622 mWindow->consumeCaptureEvent(false);
4623 mWindow->consumeFocusEvent(false);
4624 mSecondWindow->consumeFocusEvent(true);
4625 mFakePolicy->waitForSetPointerCapture(false);
4626
4627 // Ensure that additional state changes from InputReader are not sent to the window.
4628 notifyPointerCaptureChanged(false);
4629 notifyPointerCaptureChanged(true);
4630 notifyPointerCaptureChanged(false);
4631 mWindow->assertNoEvents();
4632 mSecondWindow->assertNoEvents();
4633 mFakePolicy->assertSetPointerCaptureNotCalled();
4634}
4635
4636TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
4637 requestAndVerifyPointerCapture(mWindow, true);
4638
4639 // InputReader unexpectedly disables and enables pointer capture.
4640 notifyPointerCaptureChanged(false);
4641 notifyPointerCaptureChanged(true);
4642
4643 // Ensure that Pointer Capture is disabled.
Prabir Pradhan7d030382020-12-21 07:58:35 -08004644 mFakePolicy->waitForSetPointerCapture(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08004645 mWindow->consumeCaptureEvent(false);
4646 mWindow->assertNoEvents();
4647}
4648
Prabir Pradhan167e6d92021-02-04 16:18:17 -08004649TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
4650 requestAndVerifyPointerCapture(mWindow, true);
4651
4652 // The first window loses focus.
4653 setFocusedWindow(mSecondWindow);
4654 mFakePolicy->waitForSetPointerCapture(false);
4655 mWindow->consumeCaptureEvent(false);
4656
4657 // Request Pointer Capture from the second window before the notification from InputReader
4658 // arrives.
4659 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
4660 mFakePolicy->waitForSetPointerCapture(true);
4661
4662 // InputReader notifies Pointer Capture was disabled (because of the focus change).
4663 notifyPointerCaptureChanged(false);
4664
4665 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
4666 notifyPointerCaptureChanged(true);
4667
4668 mSecondWindow->consumeFocusEvent(true);
4669 mSecondWindow->consumeCaptureEvent(true);
4670}
4671
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004672class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
4673protected:
4674 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00004675
4676 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
4677 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
4678
4679 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
4680 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
4681
4682 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
4683 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
4684 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
4685 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
4686 MAXIMUM_OBSCURING_OPACITY);
4687
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004688 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004689 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004690 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004691
4692 sp<FakeWindowHandle> mTouchWindow;
4693
4694 virtual void SetUp() override {
4695 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004696 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004697 mDispatcher->setBlockUntrustedTouchesMode(android::os::BlockUntrustedTouchesMode::BLOCK);
4698 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
4699 }
4700
4701 virtual void TearDown() override {
4702 InputDispatcherTest::TearDown();
4703 mTouchWindow.clear();
4704 }
4705
chaviw3277faf2021-05-19 16:45:23 -05004706 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode,
4707 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004708 sp<FakeWindowHandle> window = getWindow(uid, name);
chaviw3277faf2021-05-19 16:45:23 -05004709 window->setFlags(WindowInfo::Flag::NOT_TOUCHABLE);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004710 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004711 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004712 return window;
4713 }
4714
4715 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
4716 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
4717 sp<FakeWindowHandle> window =
4718 new FakeWindowHandle(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
4719 // Generate an arbitrary PID based on the UID
4720 window->setOwnerInfo(1777 + (uid % 10000), uid);
4721 return window;
4722 }
4723
4724 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
4725 NotifyMotionArgs args =
4726 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4727 ADISPLAY_ID_DEFAULT, points);
4728 mDispatcher->notifyMotion(&args);
4729 }
4730};
4731
4732TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004733 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004734 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004735 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004736
4737 touch();
4738
4739 mTouchWindow->assertNoEvents();
4740}
4741
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004742TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00004743 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
4744 const sp<FakeWindowHandle>& w =
4745 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
4746 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4747
4748 touch();
4749
4750 mTouchWindow->assertNoEvents();
4751}
4752
4753TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004754 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
4755 const sp<FakeWindowHandle>& w =
4756 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
4757 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4758
4759 touch();
4760
4761 w->assertNoEvents();
4762}
4763
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004764TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004765 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
4766 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004767
4768 touch();
4769
4770 mTouchWindow->consumeAnyMotionDown();
4771}
4772
4773TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004774 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004775 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004776 w->setFrame(Rect(0, 0, 50, 50));
4777 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004778
4779 touch({PointF{100, 100}});
4780
4781 mTouchWindow->consumeAnyMotionDown();
4782}
4783
4784TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004785 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004786 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004787 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4788
4789 touch();
4790
4791 mTouchWindow->consumeAnyMotionDown();
4792}
4793
4794TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
4795 const sp<FakeWindowHandle>& w =
4796 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4797 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004798
4799 touch();
4800
4801 mTouchWindow->consumeAnyMotionDown();
4802}
4803
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004804TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
4805 const sp<FakeWindowHandle>& w =
4806 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4807 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4808
4809 touch();
4810
4811 w->assertNoEvents();
4812}
4813
4814/**
4815 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
4816 * inside) while letting them pass-through. Note that even though touch passes through the occluding
4817 * window, the occluding window will still receive ACTION_OUTSIDE event.
4818 */
4819TEST_F(InputDispatcherUntrustedTouchesTest,
4820 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
4821 const sp<FakeWindowHandle>& w =
4822 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
chaviw3277faf2021-05-19 16:45:23 -05004823 w->addFlags(WindowInfo::Flag::WATCH_OUTSIDE_TOUCH);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004824 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4825
4826 touch();
4827
4828 w->consumeMotionOutside();
4829}
4830
4831TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
4832 const sp<FakeWindowHandle>& w =
4833 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
chaviw3277faf2021-05-19 16:45:23 -05004834 w->addFlags(WindowInfo::Flag::WATCH_OUTSIDE_TOUCH);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004835 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4836
4837 touch();
4838
4839 InputEvent* event = w->consume();
4840 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType());
4841 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
4842 EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getX());
4843 EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getY());
4844}
4845
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004846TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004847 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004848 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4849 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004850 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4851
4852 touch();
4853
4854 mTouchWindow->consumeAnyMotionDown();
4855}
4856
4857TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
4858 const sp<FakeWindowHandle>& w =
4859 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4860 MAXIMUM_OBSCURING_OPACITY);
4861 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004862
4863 touch();
4864
4865 mTouchWindow->consumeAnyMotionDown();
4866}
4867
4868TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004869 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004870 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4871 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004872 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4873
4874 touch();
4875
4876 mTouchWindow->assertNoEvents();
4877}
4878
4879TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
4880 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
4881 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004882 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
4883 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004884 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004885 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
4886 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004887 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
4888
4889 touch();
4890
4891 mTouchWindow->assertNoEvents();
4892}
4893
4894TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
4895 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
4896 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004897 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
4898 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004899 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004900 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
4901 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004902 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
4903
4904 touch();
4905
4906 mTouchWindow->consumeAnyMotionDown();
4907}
4908
4909TEST_F(InputDispatcherUntrustedTouchesTest,
4910 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
4911 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004912 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4913 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004914 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004915 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
4916 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004917 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
4918
4919 touch();
4920
4921 mTouchWindow->consumeAnyMotionDown();
4922}
4923
4924TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
4925 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004926 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4927 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004928 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004929 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
4930 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004931 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004932
4933 touch();
4934
4935 mTouchWindow->assertNoEvents();
4936}
4937
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004938TEST_F(InputDispatcherUntrustedTouchesTest,
4939 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
4940 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004941 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
4942 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004943 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004944 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4945 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004946 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
4947
4948 touch();
4949
4950 mTouchWindow->assertNoEvents();
4951}
4952
4953TEST_F(InputDispatcherUntrustedTouchesTest,
4954 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
4955 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004956 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
4957 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004958 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004959 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4960 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004961 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
4962
4963 touch();
4964
4965 mTouchWindow->consumeAnyMotionDown();
4966}
4967
4968TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
4969 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004970 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
4971 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004972 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4973
4974 touch();
4975
4976 mTouchWindow->consumeAnyMotionDown();
4977}
4978
4979TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
4980 const sp<FakeWindowHandle>& w =
4981 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
4982 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4983
4984 touch();
4985
4986 mTouchWindow->consumeAnyMotionDown();
4987}
4988
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00004989TEST_F(InputDispatcherUntrustedTouchesTest,
4990 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
4991 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
4992 const sp<FakeWindowHandle>& w =
4993 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
4994 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4995
4996 touch();
4997
4998 mTouchWindow->assertNoEvents();
4999}
5000
5001TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
5002 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
5003 const sp<FakeWindowHandle>& w =
5004 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
5005 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5006
5007 touch();
5008
5009 mTouchWindow->consumeAnyMotionDown();
5010}
5011
5012TEST_F(InputDispatcherUntrustedTouchesTest,
5013 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
5014 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
5015 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005016 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
5017 OPACITY_ABOVE_THRESHOLD);
5018 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5019
5020 touch();
5021
5022 mTouchWindow->consumeAnyMotionDown();
5023}
5024
5025TEST_F(InputDispatcherUntrustedTouchesTest,
5026 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
5027 const sp<FakeWindowHandle>& w1 =
5028 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
5029 OPACITY_BELOW_THRESHOLD);
5030 const sp<FakeWindowHandle>& w2 =
5031 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
5032 OPACITY_BELOW_THRESHOLD);
5033 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
5034
5035 touch();
5036
5037 mTouchWindow->assertNoEvents();
5038}
5039
5040/**
5041 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
5042 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
5043 * (which alone would result in allowing touches) does not affect the blocking behavior.
5044 */
5045TEST_F(InputDispatcherUntrustedTouchesTest,
5046 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
5047 const sp<FakeWindowHandle>& wB =
5048 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
5049 OPACITY_BELOW_THRESHOLD);
5050 const sp<FakeWindowHandle>& wC =
5051 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
5052 OPACITY_BELOW_THRESHOLD);
5053 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
5054
5055 touch();
5056
5057 mTouchWindow->assertNoEvents();
5058}
5059
5060/**
5061 * This test is testing that a window from a different UID but with same application token doesn't
5062 * block the touch. Apps can share the application token for close UI collaboration for example.
5063 */
5064TEST_F(InputDispatcherUntrustedTouchesTest,
5065 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
5066 const sp<FakeWindowHandle>& w =
5067 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
5068 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00005069 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5070
5071 touch();
5072
5073 mTouchWindow->consumeAnyMotionDown();
5074}
5075
arthurhungb89ccb02020-12-30 16:19:01 +08005076class InputDispatcherDragTests : public InputDispatcherTest {
5077protected:
5078 std::shared_ptr<FakeApplicationHandle> mApp;
5079 sp<FakeWindowHandle> mWindow;
5080 sp<FakeWindowHandle> mSecondWindow;
5081 sp<FakeWindowHandle> mDragWindow;
5082
5083 void SetUp() override {
5084 InputDispatcherTest::SetUp();
5085 mApp = std::make_shared<FakeApplicationHandle>();
5086 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
5087 mWindow->setFrame(Rect(0, 0, 100, 100));
chaviw3277faf2021-05-19 16:45:23 -05005088 mWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
arthurhungb89ccb02020-12-30 16:19:01 +08005089
5090 mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
5091 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
chaviw3277faf2021-05-19 16:45:23 -05005092 mSecondWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
arthurhungb89ccb02020-12-30 16:19:01 +08005093
5094 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
5095 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
5096 }
5097
5098 // Start performing drag, we will create a drag window and transfer touch to it.
5099 void performDrag() {
5100 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5101 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5102 {50, 50}))
5103 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5104
5105 // Window should receive motion event.
5106 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5107
5108 // The drag window covers the entire display
5109 mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
5110 mDispatcher->setInputWindows(
5111 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
5112
5113 // Transfer touch focus to the drag window
5114 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
5115 true /* isDragDrop */);
5116 mWindow->consumeMotionCancel();
5117 mDragWindow->consumeMotionDown();
5118 }
arthurhung6d4bed92021-03-17 11:59:33 +08005119
5120 // Start performing drag, we will create a drag window and transfer touch to it.
5121 void performStylusDrag() {
5122 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5123 injectMotionEvent(mDispatcher,
5124 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
5125 AINPUT_SOURCE_STYLUS)
5126 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
5127 .pointer(PointerBuilder(0,
5128 AMOTION_EVENT_TOOL_TYPE_STYLUS)
5129 .x(50)
5130 .y(50))
5131 .build()));
5132 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5133
5134 // The drag window covers the entire display
5135 mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
5136 mDispatcher->setInputWindows(
5137 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
5138
5139 // Transfer touch focus to the drag window
5140 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
5141 true /* isDragDrop */);
5142 mWindow->consumeMotionCancel();
5143 mDragWindow->consumeMotionDown();
5144 }
arthurhungb89ccb02020-12-30 16:19:01 +08005145};
5146
5147TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
5148 performDrag();
5149
5150 // Move on window.
5151 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5152 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5153 ADISPLAY_ID_DEFAULT, {50, 50}))
5154 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5155 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5156 mWindow->consumeDragEvent(false, 50, 50);
5157 mSecondWindow->assertNoEvents();
5158
5159 // Move to another window.
5160 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5161 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5162 ADISPLAY_ID_DEFAULT, {150, 50}))
5163 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5164 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5165 mWindow->consumeDragEvent(true, 150, 50);
5166 mSecondWindow->consumeDragEvent(false, 50, 50);
5167
5168 // Move back to original window.
5169 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5170 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5171 ADISPLAY_ID_DEFAULT, {50, 50}))
5172 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5173 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5174 mWindow->consumeDragEvent(false, 50, 50);
5175 mSecondWindow->consumeDragEvent(true, -50, 50);
5176
5177 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5178 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
5179 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5180 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5181 mWindow->assertNoEvents();
5182 mSecondWindow->assertNoEvents();
5183}
5184
arthurhungf452d0b2021-01-06 00:19:52 +08005185TEST_F(InputDispatcherDragTests, DragAndDrop) {
5186 performDrag();
5187
5188 // Move on window.
5189 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5190 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5191 ADISPLAY_ID_DEFAULT, {50, 50}))
5192 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5193 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5194 mWindow->consumeDragEvent(false, 50, 50);
5195 mSecondWindow->assertNoEvents();
5196
5197 // Move to another window.
5198 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5199 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5200 ADISPLAY_ID_DEFAULT, {150, 50}))
5201 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5202 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5203 mWindow->consumeDragEvent(true, 150, 50);
5204 mSecondWindow->consumeDragEvent(false, 50, 50);
5205
5206 // drop to another window.
5207 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5208 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5209 {150, 50}))
5210 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5211 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5212 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
5213 mWindow->assertNoEvents();
5214 mSecondWindow->assertNoEvents();
5215}
5216
arthurhung6d4bed92021-03-17 11:59:33 +08005217TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
5218 performStylusDrag();
5219
5220 // Move on window and keep button pressed.
5221 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5222 injectMotionEvent(mDispatcher,
5223 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
5224 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
5225 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
5226 .x(50)
5227 .y(50))
5228 .build()))
5229 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5230 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5231 mWindow->consumeDragEvent(false, 50, 50);
5232 mSecondWindow->assertNoEvents();
5233
5234 // Move to another window and release button, expect to drop item.
5235 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5236 injectMotionEvent(mDispatcher,
5237 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
5238 .buttonState(0)
5239 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
5240 .x(150)
5241 .y(50))
5242 .build()))
5243 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5244 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5245 mWindow->assertNoEvents();
5246 mSecondWindow->assertNoEvents();
5247 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
5248
5249 // nothing to the window.
5250 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5251 injectMotionEvent(mDispatcher,
5252 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
5253 .buttonState(0)
5254 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
5255 .x(150)
5256 .y(50))
5257 .build()))
5258 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5259 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5260 mWindow->assertNoEvents();
5261 mSecondWindow->assertNoEvents();
5262}
5263
Arthur Hung6d0571e2021-04-09 20:18:16 +08005264TEST_F(InputDispatcherDragTests, DragAndDrop_InvalidWindow) {
5265 performDrag();
5266
5267 // Set second window invisible.
5268 mSecondWindow->setVisible(false);
5269 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
5270
5271 // Move on window.
5272 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5273 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5274 ADISPLAY_ID_DEFAULT, {50, 50}))
5275 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5276 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5277 mWindow->consumeDragEvent(false, 50, 50);
5278 mSecondWindow->assertNoEvents();
5279
5280 // Move to another window.
5281 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5282 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5283 ADISPLAY_ID_DEFAULT, {150, 50}))
5284 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5285 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5286 mWindow->consumeDragEvent(true, 150, 50);
5287 mSecondWindow->assertNoEvents();
5288
5289 // drop to another window.
5290 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5291 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5292 {150, 50}))
5293 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5294 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5295 mFakePolicy->assertDropTargetEquals(nullptr);
5296 mWindow->assertNoEvents();
5297 mSecondWindow->assertNoEvents();
5298}
5299
Garfield Tane84e6f92019-08-29 17:28:41 -07005300} // namespace android::inputdispatcher