blob: f03cf8ba0b788cdcc210f165b768762182a5551f [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
liushenxiang42232912021-05-21 20:24:09 +08002885TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
2886 sendAndConsumeKeyDown(DEVICE_ID);
2887 expectKeyRepeatOnce(1 /*repeatCount*/);
2888 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
2889 mDispatcher->notifyDeviceReset(&args);
2890 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
2891 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
2892 mWindow->assertNoEvents();
2893}
2894
Garfield Tan1c7bc862020-01-28 13:24:04 -08002895TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Chris Ye2ad95392020-09-01 13:44:44 -07002896 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002897 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2898 InputEvent* repeatEvent = mWindow->consume();
2899 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2900 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
2901 IdGenerator::getSource(repeatEvent->getId()));
2902 }
2903}
2904
2905TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Chris Ye2ad95392020-09-01 13:44:44 -07002906 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002907
2908 std::unordered_set<int32_t> idSet;
2909 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2910 InputEvent* repeatEvent = mWindow->consume();
2911 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2912 int32_t id = repeatEvent->getId();
2913 EXPECT_EQ(idSet.end(), idSet.find(id));
2914 idSet.insert(id);
2915 }
2916}
2917
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002918/* Test InputDispatcher for MultiDisplay */
2919class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
2920public:
2921 static constexpr int32_t SECOND_DISPLAY_ID = 1;
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002922 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002923 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08002924
Chris Yea209fde2020-07-22 13:54:51 -07002925 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002926 windowInPrimary =
2927 new FakeWindowHandle(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002928
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002929 // Set focus window for primary display, but focused display would be second one.
2930 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07002931 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002932 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002933 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002934 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08002935
Chris Yea209fde2020-07-22 13:54:51 -07002936 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002937 windowInSecondary =
2938 new FakeWindowHandle(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002939 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002940 // Set focus display to second one.
2941 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
2942 // Set focus window for second display.
2943 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07002944 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002945 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002946 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002947 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002948 }
2949
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002950 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002951 InputDispatcherTest::TearDown();
2952
Chris Yea209fde2020-07-22 13:54:51 -07002953 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002954 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07002955 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002956 windowInSecondary.clear();
2957 }
2958
2959protected:
Chris Yea209fde2020-07-22 13:54:51 -07002960 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002961 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07002962 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002963 sp<FakeWindowHandle> windowInSecondary;
2964};
2965
2966TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
2967 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002968 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2969 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2970 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002971 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08002972 windowInSecondary->assertNoEvents();
2973
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002974 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002975 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2976 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
2977 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08002978 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002979 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08002980}
2981
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002982TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08002983 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08002984 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2985 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002986 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002987 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08002988 windowInSecondary->assertNoEvents();
2989
2990 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08002991 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002992 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08002993 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002994 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08002995
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002996 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00002997 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08002998
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002999 // Old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003000 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
3001 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08003002
3003 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003004 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003005 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08003006 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003007 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08003008 windowInSecondary->assertNoEvents();
3009}
3010
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003011// Test per-display input monitors for motion event.
3012TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08003013 FakeMonitorReceiver monitorInPrimary =
3014 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
3015 FakeMonitorReceiver monitorInSecondary =
3016 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003017
3018 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003019 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3020 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
3021 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003022 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08003023 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003024 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003025 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003026
3027 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003028 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3029 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
3030 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003031 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003032 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003033 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08003034 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003035
3036 // Test inject a non-pointer motion event.
3037 // If specific a display, it will dispatch to the focused window of particular display,
3038 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003039 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3040 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
3041 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003042 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003043 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003044 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08003045 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003046}
3047
3048// Test per-display input monitors for key event.
3049TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003050 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08003051 FakeMonitorReceiver monitorInPrimary =
3052 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
3053 FakeMonitorReceiver monitorInSecondary =
3054 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003055
3056 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003057 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3058 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003059 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003060 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003061 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08003062 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003063}
3064
Vishnu Nair958da932020-08-21 17:12:37 -07003065TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
3066 sp<FakeWindowHandle> secondWindowInPrimary =
3067 new FakeWindowHandle(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
3068 secondWindowInPrimary->setFocusable(true);
3069 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
3070 setFocusedWindow(secondWindowInPrimary);
3071 windowInPrimary->consumeFocusEvent(false);
3072 secondWindowInPrimary->consumeFocusEvent(true);
3073
3074 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003075 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
3076 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003077 windowInPrimary->assertNoEvents();
3078 windowInSecondary->assertNoEvents();
3079 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3080}
3081
Jackal Guof9696682018-10-05 12:23:23 +08003082class InputFilterTest : public InputDispatcherTest {
3083protected:
3084 static constexpr int32_t SECOND_DISPLAY_ID = 1;
3085
3086 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) {
3087 NotifyMotionArgs motionArgs;
3088
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003089 motionArgs =
3090 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08003091 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003092 motionArgs =
3093 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08003094 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003095 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08003096 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08003097 mFakePolicy->assertFilterInputEventWasCalled(motionArgs);
Jackal Guof9696682018-10-05 12:23:23 +08003098 } else {
3099 mFakePolicy->assertFilterInputEventWasNotCalled();
3100 }
3101 }
3102
3103 void testNotifyKey(bool expectToBeFiltered) {
3104 NotifyKeyArgs keyArgs;
3105
3106 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
3107 mDispatcher->notifyKey(&keyArgs);
3108 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
3109 mDispatcher->notifyKey(&keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003110 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08003111
3112 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08003113 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08003114 } else {
3115 mFakePolicy->assertFilterInputEventWasNotCalled();
3116 }
3117 }
3118};
3119
3120// Test InputFilter for MotionEvent
3121TEST_F(InputFilterTest, MotionEvent_InputFilter) {
3122 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
3123 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
3124 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
3125
3126 // Enable InputFilter
3127 mDispatcher->setInputFilterEnabled(true);
3128 // Test touch on both primary and second display, and check if both events are filtered.
3129 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
3130 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
3131
3132 // Disable InputFilter
3133 mDispatcher->setInputFilterEnabled(false);
3134 // Test touch on both primary and second display, and check if both events aren't filtered.
3135 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
3136 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
3137}
3138
3139// Test InputFilter for KeyEvent
3140TEST_F(InputFilterTest, KeyEvent_InputFilter) {
3141 // Since the InputFilter is disabled by default, check if key event aren't filtered.
3142 testNotifyKey(/*expectToBeFiltered*/ false);
3143
3144 // Enable InputFilter
3145 mDispatcher->setInputFilterEnabled(true);
3146 // Send a key event, and check if it is filtered.
3147 testNotifyKey(/*expectToBeFiltered*/ true);
3148
3149 // Disable InputFilter
3150 mDispatcher->setInputFilterEnabled(false);
3151 // Send a key event, and check if it isn't filtered.
3152 testNotifyKey(/*expectToBeFiltered*/ false);
3153}
3154
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003155class InputFilterInjectionPolicyTest : public InputDispatcherTest {
3156protected:
3157 virtual void SetUp() override {
3158 InputDispatcherTest::SetUp();
3159
3160 /**
3161 * We don't need to enable input filter to test the injected event policy, but we enabled it
3162 * here to make the tests more realistic, since this policy only matters when inputfilter is
3163 * on.
3164 */
3165 mDispatcher->setInputFilterEnabled(true);
3166
3167 std::shared_ptr<InputApplicationHandle> application =
3168 std::make_shared<FakeApplicationHandle>();
3169 mWindow =
3170 new FakeWindowHandle(application, mDispatcher, "Test Window", ADISPLAY_ID_DEFAULT);
3171
3172 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3173 mWindow->setFocusable(true);
3174 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3175 setFocusedWindow(mWindow);
3176 mWindow->consumeFocusEvent(true);
3177 }
3178
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003179 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
3180 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003181 KeyEvent event;
3182
3183 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
3184 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
3185 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
3186 KEY_A, AMETA_NONE, 0 /*repeatCount*/, eventTime, eventTime);
3187 const int32_t additionalPolicyFlags =
3188 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
3189 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3190 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
3191 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
3192 policyFlags | additionalPolicyFlags));
3193
3194 InputEvent* received = mWindow->consume();
3195 ASSERT_NE(nullptr, received);
3196 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003197 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_KEY);
3198 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
3199 ASSERT_EQ(flags, keyEvent.getFlags());
3200 }
3201
3202 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
3203 int32_t flags) {
3204 MotionEvent event;
3205 PointerProperties pointerProperties[1];
3206 PointerCoords pointerCoords[1];
3207 pointerProperties[0].clear();
3208 pointerProperties[0].id = 0;
3209 pointerCoords[0].clear();
3210 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
3211 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
3212
3213 ui::Transform identityTransform;
3214 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
3215 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
3216 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
3217 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
3218 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -07003219 AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0,
3220 0 /*INVALID_DISPLAY_SIZE*/, 0 /*INVALID_DISPLAY_SIZE*/, eventTime,
3221 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003222 /*pointerCount*/ 1, pointerProperties, pointerCoords);
3223
3224 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
3225 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3226 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
3227 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
3228 policyFlags | additionalPolicyFlags));
3229
3230 InputEvent* received = mWindow->consume();
3231 ASSERT_NE(nullptr, received);
3232 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
3233 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_MOTION);
3234 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
3235 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003236 }
3237
3238private:
3239 sp<FakeWindowHandle> mWindow;
3240};
3241
3242TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003243 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
3244 // filter. Without it, the event will no different from a regularly injected event, and the
3245 // injected device id will be overwritten.
3246 testInjectedKey(POLICY_FLAG_FILTERED, 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
3247 0 /*flags*/);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003248}
3249
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003250TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003251 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003252 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
3253 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
3254}
3255
3256TEST_F(InputFilterInjectionPolicyTest,
3257 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
3258 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
3259 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
3260 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003261}
3262
3263TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
3264 testInjectedKey(0 /*policyFlags*/, 3 /*injectedDeviceId*/,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003265 VIRTUAL_KEYBOARD_ID /*resolvedDeviceId*/, 0 /*flags*/);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003266}
3267
chaviwfd6d3512019-03-25 13:23:49 -07003268class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07003269 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07003270 InputDispatcherTest::SetUp();
3271
Chris Yea209fde2020-07-22 13:54:51 -07003272 std::shared_ptr<FakeApplicationHandle> application =
3273 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003274 mUnfocusedWindow =
3275 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07003276 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
3277 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3278 // window.
chaviw3277faf2021-05-19 16:45:23 -05003279 mUnfocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07003280
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003281 mFocusedWindow =
3282 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
3283 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviw3277faf2021-05-19 16:45:23 -05003284 mFocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07003285
3286 // Set focused application.
3287 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07003288 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07003289
3290 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00003291 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003292 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003293 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07003294 }
3295
Prabir Pradhan3608aad2019-10-02 17:08:26 -07003296 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07003297 InputDispatcherTest::TearDown();
3298
3299 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003300 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07003301 }
3302
3303protected:
3304 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003305 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003306 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07003307};
3308
3309// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
3310// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
3311// the onPointerDownOutsideFocus callback.
3312TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003313 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003314 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3315 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003316 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003317 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07003318
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003319 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07003320 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
3321}
3322
3323// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
3324// DOWN on the window that doesn't have focus. Ensure no window received the
3325// onPointerDownOutsideFocus callback.
3326TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003327 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003328 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003329 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003330 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07003331
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003332 ASSERT_TRUE(mDispatcher->waitForIdle());
3333 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07003334}
3335
3336// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
3337// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
3338TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003339 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3340 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003341 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003342 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07003343
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003344 ASSERT_TRUE(mDispatcher->waitForIdle());
3345 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07003346}
3347
3348// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
3349// DOWN on the window that already has focus. Ensure no window received the
3350// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003351TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003352 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003353 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003354 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003355 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003356 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07003357
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003358 ASSERT_TRUE(mDispatcher->waitForIdle());
3359 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07003360}
3361
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08003362// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
3363// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
3364TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
3365 const MotionEvent event =
3366 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3367 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
3368 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(20).y(20))
3369 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
3370 .build();
3371 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
3372 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3373 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
3374
3375 ASSERT_TRUE(mDispatcher->waitForIdle());
3376 mFakePolicy->assertOnPointerDownWasNotCalled();
3377 // Ensure that the unfocused window did not receive any FOCUS events.
3378 mUnfocusedWindow->assertNoEvents();
3379}
3380
chaviwaf87b3e2019-10-01 16:59:28 -07003381// These tests ensures we can send touch events to a single client when there are multiple input
3382// windows that point to the same client token.
3383class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
3384 virtual void SetUp() override {
3385 InputDispatcherTest::SetUp();
3386
Chris Yea209fde2020-07-22 13:54:51 -07003387 std::shared_ptr<FakeApplicationHandle> application =
3388 std::make_shared<FakeApplicationHandle>();
chaviwaf87b3e2019-10-01 16:59:28 -07003389 mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1",
3390 ADISPLAY_ID_DEFAULT);
3391 // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window.
3392 // We also need FLAG_SPLIT_TOUCH or we won't be able to get touches for both windows.
chaviw3277faf2021-05-19 16:45:23 -05003393 mWindow1->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07003394 mWindow1->setFrame(Rect(0, 0, 100, 100));
3395
3396 mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2",
3397 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviw3277faf2021-05-19 16:45:23 -05003398 mWindow2->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07003399 mWindow2->setFrame(Rect(100, 100, 200, 200));
3400
Arthur Hung72d8dc32020-03-28 00:48:39 +00003401 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07003402 }
3403
3404protected:
3405 sp<FakeWindowHandle> mWindow1;
3406 sp<FakeWindowHandle> mWindow2;
3407
3408 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05003409 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07003410 vec2 vals = windowInfo->transform.transform(point.x, point.y);
3411 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07003412 }
3413
3414 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
3415 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003416 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07003417 InputEvent* event = window->consume();
3418
3419 ASSERT_NE(nullptr, event) << name.c_str()
3420 << ": consumer should have returned non-NULL event.";
3421
3422 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
3423 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
3424 << " event, got " << inputEventTypeToString(event->getType()) << " event";
3425
3426 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
3427 EXPECT_EQ(expectedAction, motionEvent.getAction());
3428
3429 for (size_t i = 0; i < points.size(); i++) {
3430 float expectedX = points[i].x;
3431 float expectedY = points[i].y;
3432
3433 EXPECT_EQ(expectedX, motionEvent.getX(i))
3434 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
3435 << ", got " << motionEvent.getX(i);
3436 EXPECT_EQ(expectedY, motionEvent.getY(i))
3437 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
3438 << ", got " << motionEvent.getY(i);
3439 }
3440 }
chaviw9eaa22c2020-07-01 16:21:27 -07003441
3442 void touchAndAssertPositions(int32_t action, std::vector<PointF> touchedPoints,
3443 std::vector<PointF> expectedPoints) {
3444 NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
3445 ADISPLAY_ID_DEFAULT, touchedPoints);
3446 mDispatcher->notifyMotion(&motionArgs);
3447
3448 // Always consume from window1 since it's the window that has the InputReceiver
3449 consumeMotionEvent(mWindow1, action, expectedPoints);
3450 }
chaviwaf87b3e2019-10-01 16:59:28 -07003451};
3452
3453TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
3454 // Touch Window 1
3455 PointF touchedPoint = {10, 10};
3456 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003457 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003458
3459 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07003460 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003461
3462 // Touch Window 2
3463 touchedPoint = {150, 150};
3464 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003465 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003466}
3467
chaviw9eaa22c2020-07-01 16:21:27 -07003468TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
3469 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07003470 mWindow2->setWindowScale(0.5f, 0.5f);
3471
3472 // Touch Window 1
3473 PointF touchedPoint = {10, 10};
3474 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003475 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003476 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07003477 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003478
3479 // Touch Window 2
3480 touchedPoint = {150, 150};
3481 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003482 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
3483 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003484
chaviw9eaa22c2020-07-01 16:21:27 -07003485 // Update the transform so rotation is set
3486 mWindow2->setWindowTransform(0, -1, 1, 0);
3487 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
3488 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003489}
3490
chaviw9eaa22c2020-07-01 16:21:27 -07003491TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003492 mWindow2->setWindowScale(0.5f, 0.5f);
3493
3494 // Touch Window 1
3495 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3496 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003497 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003498
3499 // Touch Window 2
3500 int32_t actionPointerDown =
3501 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003502 touchedPoints.push_back(PointF{150, 150});
3503 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3504 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003505
chaviw9eaa22c2020-07-01 16:21:27 -07003506 // Release Window 2
3507 int32_t actionPointerUp =
3508 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3509 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
3510 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003511
chaviw9eaa22c2020-07-01 16:21:27 -07003512 // Update the transform so rotation is set for Window 2
3513 mWindow2->setWindowTransform(0, -1, 1, 0);
3514 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3515 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003516}
3517
chaviw9eaa22c2020-07-01 16:21:27 -07003518TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003519 mWindow2->setWindowScale(0.5f, 0.5f);
3520
3521 // Touch Window 1
3522 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3523 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003524 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003525
3526 // Touch Window 2
3527 int32_t actionPointerDown =
3528 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003529 touchedPoints.push_back(PointF{150, 150});
3530 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003531
chaviw9eaa22c2020-07-01 16:21:27 -07003532 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003533
3534 // Move both windows
3535 touchedPoints = {{20, 20}, {175, 175}};
3536 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3537 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3538
chaviw9eaa22c2020-07-01 16:21:27 -07003539 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003540
chaviw9eaa22c2020-07-01 16:21:27 -07003541 // Release Window 2
3542 int32_t actionPointerUp =
3543 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3544 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
3545 expectedPoints.pop_back();
3546
3547 // Touch Window 2
3548 mWindow2->setWindowTransform(0, -1, 1, 0);
3549 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3550 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
3551
3552 // Move both windows
3553 touchedPoints = {{20, 20}, {175, 175}};
3554 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3555 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3556
3557 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003558}
3559
3560TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
3561 mWindow1->setWindowScale(0.5f, 0.5f);
3562
3563 // Touch Window 1
3564 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3565 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003566 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003567
3568 // Touch Window 2
3569 int32_t actionPointerDown =
3570 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003571 touchedPoints.push_back(PointF{150, 150});
3572 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003573
chaviw9eaa22c2020-07-01 16:21:27 -07003574 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003575
3576 // Move both windows
3577 touchedPoints = {{20, 20}, {175, 175}};
3578 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3579 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3580
chaviw9eaa22c2020-07-01 16:21:27 -07003581 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003582}
3583
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003584class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
3585 virtual void SetUp() override {
3586 InputDispatcherTest::SetUp();
3587
Chris Yea209fde2020-07-22 13:54:51 -07003588 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003589 mApplication->setDispatchingTimeout(20ms);
3590 mWindow =
3591 new FakeWindowHandle(mApplication, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
3592 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05003593 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07003594 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003595 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3596 // window.
chaviw3277faf2021-05-19 16:45:23 -05003597 mWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003598
3599 // Set focused application.
3600 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
3601
3602 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003603 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003604 mWindow->consumeFocusEvent(true);
3605 }
3606
3607 virtual void TearDown() override {
3608 InputDispatcherTest::TearDown();
3609 mWindow.clear();
3610 }
3611
3612protected:
Chris Yea209fde2020-07-22 13:54:51 -07003613 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003614 sp<FakeWindowHandle> mWindow;
3615 static constexpr PointF WINDOW_LOCATION = {20, 20};
3616
3617 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003618 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003619 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3620 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003621 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003622 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3623 WINDOW_LOCATION));
3624 }
3625};
3626
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003627// Send a tap and respond, which should not cause an ANR.
3628TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
3629 tapOnWindow();
3630 mWindow->consumeMotionDown();
3631 mWindow->consumeMotionUp();
3632 ASSERT_TRUE(mDispatcher->waitForIdle());
3633 mFakePolicy->assertNotifyAnrWasNotCalled();
3634}
3635
3636// Send a regular key and respond, which should not cause an ANR.
3637TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003638 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003639 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
3640 ASSERT_TRUE(mDispatcher->waitForIdle());
3641 mFakePolicy->assertNotifyAnrWasNotCalled();
3642}
3643
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003644TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
3645 mWindow->setFocusable(false);
3646 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3647 mWindow->consumeFocusEvent(false);
3648
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003649 InputEventInjectionResult result =
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003650 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003651 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/,
3652 false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003653 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003654 // Key will not go to window because we have no focused window.
3655 // The 'no focused window' ANR timer should start instead.
3656
3657 // Now, the focused application goes away.
3658 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
3659 // The key should get dropped and there should be no ANR.
3660
3661 ASSERT_TRUE(mDispatcher->waitForIdle());
3662 mFakePolicy->assertNotifyAnrWasNotCalled();
3663}
3664
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003665// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003666// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
3667// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003668TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003669 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003670 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3671 WINDOW_LOCATION));
3672
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003673 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
3674 ASSERT_TRUE(sequenceNum);
3675 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003676 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003677
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003678 mWindow->finishEvent(*sequenceNum);
3679 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3680 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003681 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003682 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003683}
3684
3685// Send a key to the app and have the app not respond right away.
3686TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
3687 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003688 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003689 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
3690 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003691 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003692 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003693 ASSERT_TRUE(mDispatcher->waitForIdle());
3694}
3695
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003696// We have a focused application, but no focused window
3697TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003698 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003699 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3700 mWindow->consumeFocusEvent(false);
3701
3702 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003703 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003704 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3705 WINDOW_LOCATION));
3706 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
3707 mDispatcher->waitForIdle();
3708 mFakePolicy->assertNotifyAnrWasNotCalled();
3709
3710 // Once a focused event arrives, we get an ANR for this application
3711 // We specify the injection timeout to be smaller than the application timeout, to ensure that
3712 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003713 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003714 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003715 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003716 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003717 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003718 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003719 ASSERT_TRUE(mDispatcher->waitForIdle());
3720}
3721
3722// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003723// Make sure that we don't notify policy twice about the same ANR.
3724TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003725 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003726 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3727 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003728
3729 // Once a focused event arrives, we get an ANR for this application
3730 // We specify the injection timeout to be smaller than the application timeout, to ensure that
3731 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003732 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003733 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003734 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003735 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003736 const std::chrono::duration appTimeout =
3737 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003738 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003739
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003740 std::this_thread::sleep_for(appTimeout);
3741 // ANR should not be raised again. It is up to policy to do that if it desires.
3742 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003743
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003744 // If we now get a focused window, the ANR should stop, but the policy handles that via
3745 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003746 ASSERT_TRUE(mDispatcher->waitForIdle());
3747}
3748
3749// We have a focused application, but no focused window
3750TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003751 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003752 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3753 mWindow->consumeFocusEvent(false);
3754
3755 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003756 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003757 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003758 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3759 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003760
3761 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003762 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003763
3764 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003765 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003766 ASSERT_TRUE(mDispatcher->waitForIdle());
3767 mWindow->assertNoEvents();
3768}
3769
3770/**
3771 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
3772 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
3773 * If we process 1 of the events, but ANR on the second event with the same timestamp,
3774 * the ANR mechanism should still work.
3775 *
3776 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
3777 * DOWN event, while not responding on the second one.
3778 */
3779TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
3780 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
3781 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3782 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
3783 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
3784 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003785 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003786
3787 // Now send ACTION_UP, with identical timestamp
3788 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3789 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
3790 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
3791 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003792 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003793
3794 // We have now sent down and up. Let's consume first event and then ANR on the second.
3795 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3796 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003797 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003798}
3799
3800// If an app is not responding to a key event, gesture monitors should continue to receive
3801// new motion events
3802TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnKey) {
3803 FakeMonitorReceiver monitor =
3804 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
3805 true /*isGestureMonitor*/);
3806
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003807 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3808 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003809 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003810 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003811
3812 // Stuck on the ACTION_UP
3813 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003814 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003815
3816 // New tap will go to the gesture monitor, but not to the window
3817 tapOnWindow();
3818 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3819 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3820
3821 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
3822 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003823 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003824 mWindow->assertNoEvents();
3825 monitor.assertNoEvents();
3826}
3827
3828// If an app is not responding to a motion event, gesture monitors should continue to receive
3829// new motion events
3830TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnMotion) {
3831 FakeMonitorReceiver monitor =
3832 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
3833 true /*isGestureMonitor*/);
3834
3835 tapOnWindow();
3836 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3837 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3838
3839 mWindow->consumeMotionDown();
3840 // Stuck on the ACTION_UP
3841 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003842 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003843
3844 // New tap will go to the gesture monitor, but not to the window
3845 tapOnWindow();
3846 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3847 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3848
3849 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
3850 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003851 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003852 mWindow->assertNoEvents();
3853 monitor.assertNoEvents();
3854}
3855
3856// If a window is unresponsive, then you get anr. if the window later catches up and starts to
3857// process events, you don't get an anr. When the window later becomes unresponsive again, you
3858// get an ANR again.
3859// 1. tap -> block on ACTION_UP -> receive ANR
3860// 2. consume all pending events (= queue becomes healthy again)
3861// 3. tap again -> block on ACTION_UP again -> receive ANR second time
3862TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
3863 tapOnWindow();
3864
3865 mWindow->consumeMotionDown();
3866 // Block on ACTION_UP
3867 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003868 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003869 mWindow->consumeMotionUp(); // Now the connection should be healthy again
3870 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003871 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003872 mWindow->assertNoEvents();
3873
3874 tapOnWindow();
3875 mWindow->consumeMotionDown();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003876 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003877 mWindow->consumeMotionUp();
3878
3879 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003880 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003881 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003882 mWindow->assertNoEvents();
3883}
3884
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003885// If a connection remains unresponsive for a while, make sure policy is only notified once about
3886// it.
3887TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003888 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003889 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3890 WINDOW_LOCATION));
3891
3892 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003893 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003894 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003895 // 'notifyConnectionUnresponsive' should only be called once per connection
3896 mFakePolicy->assertNotifyAnrWasNotCalled();
3897 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003898 mWindow->consumeMotionDown();
3899 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3900 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3901 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003902 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003903 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003904 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003905}
3906
3907/**
3908 * If a window is processing a motion event, and then a key event comes in, the key event should
3909 * not to to the focused window until the motion is processed.
3910 *
3911 * Warning!!!
3912 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
3913 * and the injection timeout that we specify when injecting the key.
3914 * We must have the injection timeout (10ms) be smaller than
3915 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
3916 *
3917 * If that value changes, this test should also change.
3918 */
3919TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
3920 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3921 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3922
3923 tapOnWindow();
3924 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3925 ASSERT_TRUE(downSequenceNum);
3926 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3927 ASSERT_TRUE(upSequenceNum);
3928 // Don't finish the events yet, and send a key
3929 // Injection will "succeed" because we will eventually give up and send the key to the focused
3930 // window even if motions are still being processed. But because the injection timeout is short,
3931 // we will receive INJECTION_TIMED_OUT as the result.
3932
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003933 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003934 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003935 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3936 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003937 // Key will not be sent to the window, yet, because the window is still processing events
3938 // and the key remains pending, waiting for the touch events to be processed
3939 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3940 ASSERT_FALSE(keySequenceNum);
3941
3942 std::this_thread::sleep_for(500ms);
3943 // if we wait long enough though, dispatcher will give up, and still send the key
3944 // to the focused window, even though we have not yet finished the motion event
3945 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3946 mWindow->finishEvent(*downSequenceNum);
3947 mWindow->finishEvent(*upSequenceNum);
3948}
3949
3950/**
3951 * If a window is processing a motion event, and then a key event comes in, the key event should
3952 * not go to the focused window until the motion is processed.
3953 * If then a new motion comes in, then the pending key event should be going to the currently
3954 * focused window right away.
3955 */
3956TEST_F(InputDispatcherSingleWindowAnr,
3957 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
3958 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3959 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3960
3961 tapOnWindow();
3962 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3963 ASSERT_TRUE(downSequenceNum);
3964 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3965 ASSERT_TRUE(upSequenceNum);
3966 // Don't finish the events yet, and send a key
3967 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003968 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003969 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003970 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003971 // At this point, key is still pending, and should not be sent to the application yet.
3972 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3973 ASSERT_FALSE(keySequenceNum);
3974
3975 // Now tap down again. It should cause the pending key to go to the focused window right away.
3976 tapOnWindow();
3977 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
3978 // the other events yet. We can finish events in any order.
3979 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
3980 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
3981 mWindow->consumeMotionDown();
3982 mWindow->consumeMotionUp();
3983 mWindow->assertNoEvents();
3984}
3985
3986class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
3987 virtual void SetUp() override {
3988 InputDispatcherTest::SetUp();
3989
Chris Yea209fde2020-07-22 13:54:51 -07003990 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003991 mApplication->setDispatchingTimeout(10ms);
3992 mUnfocusedWindow =
3993 new FakeWindowHandle(mApplication, mDispatcher, "Unfocused", ADISPLAY_ID_DEFAULT);
3994 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
3995 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3996 // window.
3997 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
chaviw3277faf2021-05-19 16:45:23 -05003998 mUnfocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL |
3999 WindowInfo::Flag::WATCH_OUTSIDE_TOUCH |
4000 WindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004001
4002 mFocusedWindow =
4003 new FakeWindowHandle(mApplication, mDispatcher, "Focused", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05004004 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004005 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviw3277faf2021-05-19 16:45:23 -05004006 mFocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004007
4008 // Set focused application.
4009 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07004010 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004011
4012 // Expect one focus window exist in display.
4013 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004014 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004015 mFocusedWindow->consumeFocusEvent(true);
4016 }
4017
4018 virtual void TearDown() override {
4019 InputDispatcherTest::TearDown();
4020
4021 mUnfocusedWindow.clear();
4022 mFocusedWindow.clear();
4023 }
4024
4025protected:
Chris Yea209fde2020-07-22 13:54:51 -07004026 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004027 sp<FakeWindowHandle> mUnfocusedWindow;
4028 sp<FakeWindowHandle> mFocusedWindow;
4029 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
4030 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
4031 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
4032
4033 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
4034
4035 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
4036
4037private:
4038 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004039 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004040 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4041 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004042 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004043 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4044 location));
4045 }
4046};
4047
4048// If we have 2 windows that are both unresponsive, the one with the shortest timeout
4049// should be ANR'd first.
4050TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004051 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004052 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4053 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004054 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004055 mFocusedWindow->consumeMotionDown();
4056 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
4057 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
4058 // We consumed all events, so no ANR
4059 ASSERT_TRUE(mDispatcher->waitForIdle());
4060 mFakePolicy->assertNotifyAnrWasNotCalled();
4061
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004062 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004063 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4064 FOCUSED_WINDOW_LOCATION));
4065 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
4066 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004067
4068 const std::chrono::duration timeout =
4069 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004070 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004071 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
4072 // sequence to make it consistent
4073 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004074 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004075 mFocusedWindow->consumeMotionDown();
4076 // This cancel is generated because the connection was unresponsive
4077 mFocusedWindow->consumeMotionCancel();
4078 mFocusedWindow->assertNoEvents();
4079 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004080 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004081 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004082 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004083}
4084
4085// If we have 2 windows with identical timeouts that are both unresponsive,
4086// it doesn't matter which order they should have ANR.
4087// But we should receive ANR for both.
4088TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
4089 // Set the timeout for unfocused window to match the focused window
4090 mUnfocusedWindow->setDispatchingTimeout(10ms);
4091 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
4092
4093 tapOnFocusedWindow();
4094 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004095 sp<IBinder> anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms);
4096 sp<IBinder> anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004097
4098 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004099 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
4100 mFocusedWindow->getToken() == anrConnectionToken2);
4101 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
4102 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004103
4104 ASSERT_TRUE(mDispatcher->waitForIdle());
4105 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004106
4107 mFocusedWindow->consumeMotionDown();
4108 mFocusedWindow->consumeMotionUp();
4109 mUnfocusedWindow->consumeMotionOutside();
4110
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004111 sp<IBinder> responsiveToken1 = mFakePolicy->getResponsiveWindowToken();
4112 sp<IBinder> responsiveToken2 = mFakePolicy->getResponsiveWindowToken();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004113
4114 // Both applications should be marked as responsive, in any order
4115 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
4116 mFocusedWindow->getToken() == responsiveToken2);
4117 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
4118 mUnfocusedWindow->getToken() == responsiveToken2);
4119 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004120}
4121
4122// If a window is already not responding, the second tap on the same window should be ignored.
4123// We should also log an error to account for the dropped event (not tested here).
4124// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
4125TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
4126 tapOnFocusedWindow();
4127 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
4128 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
4129 // Receive the events, but don't respond
4130 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
4131 ASSERT_TRUE(downEventSequenceNum);
4132 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
4133 ASSERT_TRUE(upEventSequenceNum);
4134 const std::chrono::duration timeout =
4135 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004136 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004137
4138 // Tap once again
4139 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004140 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004141 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4142 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004143 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004144 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4145 FOCUSED_WINDOW_LOCATION));
4146 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
4147 // valid touch target
4148 mUnfocusedWindow->assertNoEvents();
4149
4150 // Consume the first tap
4151 mFocusedWindow->finishEvent(*downEventSequenceNum);
4152 mFocusedWindow->finishEvent(*upEventSequenceNum);
4153 ASSERT_TRUE(mDispatcher->waitForIdle());
4154 // The second tap did not go to the focused window
4155 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004156 // Since all events are finished, connection should be deemed healthy again
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004157 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004158 mFakePolicy->assertNotifyAnrWasNotCalled();
4159}
4160
4161// If you tap outside of all windows, there will not be ANR
4162TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004163 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004164 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4165 LOCATION_OUTSIDE_ALL_WINDOWS));
4166 ASSERT_TRUE(mDispatcher->waitForIdle());
4167 mFakePolicy->assertNotifyAnrWasNotCalled();
4168}
4169
4170// Since the focused window is paused, tapping on it should not produce any events
4171TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
4172 mFocusedWindow->setPaused(true);
4173 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
4174
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004175 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004176 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4177 FOCUSED_WINDOW_LOCATION));
4178
4179 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
4180 ASSERT_TRUE(mDispatcher->waitForIdle());
4181 // Should not ANR because the window is paused, and touches shouldn't go to it
4182 mFakePolicy->assertNotifyAnrWasNotCalled();
4183
4184 mFocusedWindow->assertNoEvents();
4185 mUnfocusedWindow->assertNoEvents();
4186}
4187
4188/**
4189 * If a window is processing a motion event, and then a key event comes in, the key event should
4190 * not to to the focused window until the motion is processed.
4191 * If a different window becomes focused at this time, the key should go to that window instead.
4192 *
4193 * Warning!!!
4194 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
4195 * and the injection timeout that we specify when injecting the key.
4196 * We must have the injection timeout (10ms) be smaller than
4197 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
4198 *
4199 * If that value changes, this test should also change.
4200 */
4201TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
4202 // Set a long ANR timeout to prevent it from triggering
4203 mFocusedWindow->setDispatchingTimeout(2s);
4204 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
4205
4206 tapOnUnfocusedWindow();
4207 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
4208 ASSERT_TRUE(downSequenceNum);
4209 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
4210 ASSERT_TRUE(upSequenceNum);
4211 // Don't finish the events yet, and send a key
4212 // Injection will succeed because we will eventually give up and send the key to the focused
4213 // window even if motions are still being processed.
4214
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004215 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004216 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004217 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
4218 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004219 // Key will not be sent to the window, yet, because the window is still processing events
4220 // and the key remains pending, waiting for the touch events to be processed
4221 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
4222 ASSERT_FALSE(keySequenceNum);
4223
4224 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07004225 mFocusedWindow->setFocusable(false);
4226 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004227 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004228 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004229
4230 // Focus events should precede the key events
4231 mUnfocusedWindow->consumeFocusEvent(true);
4232 mFocusedWindow->consumeFocusEvent(false);
4233
4234 // Finish the tap events, which should unblock dispatcher
4235 mUnfocusedWindow->finishEvent(*downSequenceNum);
4236 mUnfocusedWindow->finishEvent(*upSequenceNum);
4237
4238 // Now that all queues are cleared and no backlog in the connections, the key event
4239 // can finally go to the newly focused "mUnfocusedWindow".
4240 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4241 mFocusedWindow->assertNoEvents();
4242 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004243 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004244}
4245
4246// When the touch stream is split across 2 windows, and one of them does not respond,
4247// then ANR should be raised and the touch should be canceled for the unresponsive window.
4248// The other window should not be affected by that.
4249TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
4250 // Touch Window 1
4251 NotifyMotionArgs motionArgs =
4252 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4253 ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION});
4254 mDispatcher->notifyMotion(&motionArgs);
4255 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
4256 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
4257
4258 // Touch Window 2
4259 int32_t actionPointerDown =
4260 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
4261
4262 motionArgs =
4263 generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4264 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION});
4265 mDispatcher->notifyMotion(&motionArgs);
4266
4267 const std::chrono::duration timeout =
4268 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004269 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004270
4271 mUnfocusedWindow->consumeMotionDown();
4272 mFocusedWindow->consumeMotionDown();
4273 // Focused window may or may not receive ACTION_MOVE
4274 // But it should definitely receive ACTION_CANCEL due to the ANR
4275 InputEvent* event;
4276 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
4277 ASSERT_TRUE(moveOrCancelSequenceNum);
4278 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
4279 ASSERT_NE(nullptr, event);
4280 ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION);
4281 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
4282 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
4283 mFocusedWindow->consumeMotionCancel();
4284 } else {
4285 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
4286 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004287 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004288 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004289
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004290 mUnfocusedWindow->assertNoEvents();
4291 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004292 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004293}
4294
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05004295/**
4296 * If we have no focused window, and a key comes in, we start the ANR timer.
4297 * The focused application should add a focused window before the timer runs out to prevent ANR.
4298 *
4299 * If the user touches another application during this time, the key should be dropped.
4300 * Next, if a new focused window comes in, without toggling the focused application,
4301 * then no ANR should occur.
4302 *
4303 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
4304 * but in some cases the policy may not update the focused application.
4305 */
4306TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
4307 std::shared_ptr<FakeApplicationHandle> focusedApplication =
4308 std::make_shared<FakeApplicationHandle>();
4309 focusedApplication->setDispatchingTimeout(60ms);
4310 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
4311 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
4312 mFocusedWindow->setFocusable(false);
4313
4314 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
4315 mFocusedWindow->consumeFocusEvent(false);
4316
4317 // Send a key. The ANR timer should start because there is no focused window.
4318 // 'focusedApplication' will get blamed if this timer completes.
4319 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004320 InputEventInjectionResult result =
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05004321 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004322 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/,
4323 false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004324 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05004325
4326 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
4327 // then the injected touches won't cause the focused event to get dropped.
4328 // The dispatcher only checks for whether the queue should be pruned upon queueing.
4329 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
4330 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
4331 // For this test, it means that the key would get delivered to the window once it becomes
4332 // focused.
4333 std::this_thread::sleep_for(10ms);
4334
4335 // Touch unfocused window. This should force the pending key to get dropped.
4336 NotifyMotionArgs motionArgs =
4337 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4338 ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION});
4339 mDispatcher->notifyMotion(&motionArgs);
4340
4341 // We do not consume the motion right away, because that would require dispatcher to first
4342 // process (== drop) the key event, and by that time, ANR will be raised.
4343 // Set the focused window first.
4344 mFocusedWindow->setFocusable(true);
4345 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
4346 setFocusedWindow(mFocusedWindow);
4347 mFocusedWindow->consumeFocusEvent(true);
4348 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
4349 // to another application. This could be a bug / behaviour in the policy.
4350
4351 mUnfocusedWindow->consumeMotionDown();
4352
4353 ASSERT_TRUE(mDispatcher->waitForIdle());
4354 // Should not ANR because we actually have a focused window. It was just added too slowly.
4355 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
4356}
4357
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05004358// These tests ensure we cannot send touch events to a window that's positioned behind a window
4359// that has feature NO_INPUT_CHANNEL.
4360// Layout:
4361// Top (closest to user)
4362// mNoInputWindow (above all windows)
4363// mBottomWindow
4364// Bottom (furthest from user)
4365class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
4366 virtual void SetUp() override {
4367 InputDispatcherTest::SetUp();
4368
4369 mApplication = std::make_shared<FakeApplicationHandle>();
4370 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
4371 "Window without input channel", ADISPLAY_ID_DEFAULT,
4372 std::make_optional<sp<IBinder>>(nullptr) /*token*/);
4373
chaviw3277faf2021-05-19 16:45:23 -05004374 mNoInputWindow->setInputFeatures(WindowInfo::Feature::NO_INPUT_CHANNEL);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05004375 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
4376 // It's perfectly valid for this window to not have an associated input channel
4377
4378 mBottomWindow = new FakeWindowHandle(mApplication, mDispatcher, "Bottom window",
4379 ADISPLAY_ID_DEFAULT);
4380 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
4381
4382 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
4383 }
4384
4385protected:
4386 std::shared_ptr<FakeApplicationHandle> mApplication;
4387 sp<FakeWindowHandle> mNoInputWindow;
4388 sp<FakeWindowHandle> mBottomWindow;
4389};
4390
4391TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
4392 PointF touchedPoint = {10, 10};
4393
4394 NotifyMotionArgs motionArgs =
4395 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4396 ADISPLAY_ID_DEFAULT, {touchedPoint});
4397 mDispatcher->notifyMotion(&motionArgs);
4398
4399 mNoInputWindow->assertNoEvents();
4400 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
4401 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
4402 // and therefore should prevent mBottomWindow from receiving touches
4403 mBottomWindow->assertNoEvents();
4404}
4405
4406/**
4407 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
4408 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
4409 */
4410TEST_F(InputDispatcherMultiWindowOcclusionTests,
4411 NoInputChannelFeature_DropsTouchesWithValidChannel) {
4412 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
4413 "Window with input channel and NO_INPUT_CHANNEL",
4414 ADISPLAY_ID_DEFAULT);
4415
chaviw3277faf2021-05-19 16:45:23 -05004416 mNoInputWindow->setInputFeatures(WindowInfo::Feature::NO_INPUT_CHANNEL);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05004417 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
4418 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
4419
4420 PointF touchedPoint = {10, 10};
4421
4422 NotifyMotionArgs motionArgs =
4423 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4424 ADISPLAY_ID_DEFAULT, {touchedPoint});
4425 mDispatcher->notifyMotion(&motionArgs);
4426
4427 mNoInputWindow->assertNoEvents();
4428 mBottomWindow->assertNoEvents();
4429}
4430
Vishnu Nair958da932020-08-21 17:12:37 -07004431class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
4432protected:
4433 std::shared_ptr<FakeApplicationHandle> mApp;
4434 sp<FakeWindowHandle> mWindow;
4435 sp<FakeWindowHandle> mMirror;
4436
4437 virtual void SetUp() override {
4438 InputDispatcherTest::SetUp();
4439 mApp = std::make_shared<FakeApplicationHandle>();
4440 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
4441 mMirror = new FakeWindowHandle(mApp, mDispatcher, "TestWindowMirror", ADISPLAY_ID_DEFAULT,
4442 mWindow->getToken());
4443 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
4444 mWindow->setFocusable(true);
4445 mMirror->setFocusable(true);
4446 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4447 }
4448};
4449
4450TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
4451 // Request focus on a mirrored window
4452 setFocusedWindow(mMirror);
4453
4454 // window gets focused
4455 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004456 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4457 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004458 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
4459}
4460
4461// A focused & mirrored window remains focused only if the window and its mirror are both
4462// focusable.
4463TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
4464 setFocusedWindow(mMirror);
4465
4466 // window gets focused
4467 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004468 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4469 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004470 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004471 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4472 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004473 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4474
4475 mMirror->setFocusable(false);
4476 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4477
4478 // window loses focus since one of the windows associated with the token in not focusable
4479 mWindow->consumeFocusEvent(false);
4480
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004481 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4482 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004483 mWindow->assertNoEvents();
4484}
4485
4486// A focused & mirrored window remains focused until the window and its mirror both become
4487// invisible.
4488TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
4489 setFocusedWindow(mMirror);
4490
4491 // window gets focused
4492 mWindow->consumeFocusEvent(true);
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 mMirror->setVisible(false);
4501 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4502
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004503 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4504 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004505 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004506 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4507 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004508 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4509
4510 mWindow->setVisible(false);
4511 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4512
4513 // window loses focus only after all windows associated with the token become invisible.
4514 mWindow->consumeFocusEvent(false);
4515
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004516 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4517 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004518 mWindow->assertNoEvents();
4519}
4520
4521// A focused & mirrored window remains focused until both windows are removed.
4522TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
4523 setFocusedWindow(mMirror);
4524
4525 // window gets focused
4526 mWindow->consumeFocusEvent(true);
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 // single window is removed but the window token remains focused
4535 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
4536
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004537 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4538 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004539 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004540 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4541 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004542 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4543
4544 // Both windows are removed
4545 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
4546 mWindow->consumeFocusEvent(false);
4547
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004548 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4549 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004550 mWindow->assertNoEvents();
4551}
4552
4553// Focus request can be pending until one window becomes visible.
4554TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
4555 // Request focus on an invisible mirror.
4556 mWindow->setVisible(false);
4557 mMirror->setVisible(false);
4558 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4559 setFocusedWindow(mMirror);
4560
4561 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004562 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07004563 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004564 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07004565
4566 mMirror->setVisible(true);
4567 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4568
4569 // window gets focused
4570 mWindow->consumeFocusEvent(true);
4571 // window gets the pending key event
4572 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4573}
Prabir Pradhan99987712020-11-10 18:43:05 -08004574
4575class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
4576protected:
4577 std::shared_ptr<FakeApplicationHandle> mApp;
4578 sp<FakeWindowHandle> mWindow;
4579 sp<FakeWindowHandle> mSecondWindow;
4580
4581 void SetUp() override {
4582 InputDispatcherTest::SetUp();
4583 mApp = std::make_shared<FakeApplicationHandle>();
4584 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
4585 mWindow->setFocusable(true);
4586 mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
4587 mSecondWindow->setFocusable(true);
4588
4589 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
4590 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
4591
4592 setFocusedWindow(mWindow);
4593 mWindow->consumeFocusEvent(true);
4594 }
4595
4596 void notifyPointerCaptureChanged(bool enabled) {
4597 const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(enabled);
4598 mDispatcher->notifyPointerCaptureChanged(&args);
4599 }
4600
4601 void requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, bool enabled) {
4602 mDispatcher->requestPointerCapture(window->getToken(), enabled);
4603 mFakePolicy->waitForSetPointerCapture(enabled);
4604 notifyPointerCaptureChanged(enabled);
4605 window->consumeCaptureEvent(enabled);
4606 }
4607};
4608
4609TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
4610 // Ensure that capture cannot be obtained for unfocused windows.
4611 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
4612 mFakePolicy->assertSetPointerCaptureNotCalled();
4613 mSecondWindow->assertNoEvents();
4614
4615 // Ensure that capture can be enabled from the focus window.
4616 requestAndVerifyPointerCapture(mWindow, true);
4617
4618 // Ensure that capture cannot be disabled from a window that does not have capture.
4619 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
4620 mFakePolicy->assertSetPointerCaptureNotCalled();
4621
4622 // Ensure that capture can be disabled from the window with capture.
4623 requestAndVerifyPointerCapture(mWindow, false);
4624}
4625
4626TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
4627 requestAndVerifyPointerCapture(mWindow, true);
4628
4629 setFocusedWindow(mSecondWindow);
4630
4631 // Ensure that the capture disabled event was sent first.
4632 mWindow->consumeCaptureEvent(false);
4633 mWindow->consumeFocusEvent(false);
4634 mSecondWindow->consumeFocusEvent(true);
4635 mFakePolicy->waitForSetPointerCapture(false);
4636
4637 // Ensure that additional state changes from InputReader are not sent to the window.
4638 notifyPointerCaptureChanged(false);
4639 notifyPointerCaptureChanged(true);
4640 notifyPointerCaptureChanged(false);
4641 mWindow->assertNoEvents();
4642 mSecondWindow->assertNoEvents();
4643 mFakePolicy->assertSetPointerCaptureNotCalled();
4644}
4645
4646TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
4647 requestAndVerifyPointerCapture(mWindow, true);
4648
4649 // InputReader unexpectedly disables and enables pointer capture.
4650 notifyPointerCaptureChanged(false);
4651 notifyPointerCaptureChanged(true);
4652
4653 // Ensure that Pointer Capture is disabled.
Prabir Pradhan7d030382020-12-21 07:58:35 -08004654 mFakePolicy->waitForSetPointerCapture(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08004655 mWindow->consumeCaptureEvent(false);
4656 mWindow->assertNoEvents();
4657}
4658
Prabir Pradhan167e6d92021-02-04 16:18:17 -08004659TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
4660 requestAndVerifyPointerCapture(mWindow, true);
4661
4662 // The first window loses focus.
4663 setFocusedWindow(mSecondWindow);
4664 mFakePolicy->waitForSetPointerCapture(false);
4665 mWindow->consumeCaptureEvent(false);
4666
4667 // Request Pointer Capture from the second window before the notification from InputReader
4668 // arrives.
4669 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
4670 mFakePolicy->waitForSetPointerCapture(true);
4671
4672 // InputReader notifies Pointer Capture was disabled (because of the focus change).
4673 notifyPointerCaptureChanged(false);
4674
4675 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
4676 notifyPointerCaptureChanged(true);
4677
4678 mSecondWindow->consumeFocusEvent(true);
4679 mSecondWindow->consumeCaptureEvent(true);
4680}
4681
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004682class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
4683protected:
4684 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00004685
4686 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
4687 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
4688
4689 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
4690 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
4691
4692 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
4693 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
4694 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
4695 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
4696 MAXIMUM_OBSCURING_OPACITY);
4697
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004698 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004699 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004700 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004701
4702 sp<FakeWindowHandle> mTouchWindow;
4703
4704 virtual void SetUp() override {
4705 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004706 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004707 mDispatcher->setBlockUntrustedTouchesMode(android::os::BlockUntrustedTouchesMode::BLOCK);
4708 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
4709 }
4710
4711 virtual void TearDown() override {
4712 InputDispatcherTest::TearDown();
4713 mTouchWindow.clear();
4714 }
4715
chaviw3277faf2021-05-19 16:45:23 -05004716 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode,
4717 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004718 sp<FakeWindowHandle> window = getWindow(uid, name);
chaviw3277faf2021-05-19 16:45:23 -05004719 window->setFlags(WindowInfo::Flag::NOT_TOUCHABLE);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004720 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004721 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004722 return window;
4723 }
4724
4725 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
4726 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
4727 sp<FakeWindowHandle> window =
4728 new FakeWindowHandle(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
4729 // Generate an arbitrary PID based on the UID
4730 window->setOwnerInfo(1777 + (uid % 10000), uid);
4731 return window;
4732 }
4733
4734 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
4735 NotifyMotionArgs args =
4736 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4737 ADISPLAY_ID_DEFAULT, points);
4738 mDispatcher->notifyMotion(&args);
4739 }
4740};
4741
4742TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004743 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004744 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004745 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004746
4747 touch();
4748
4749 mTouchWindow->assertNoEvents();
4750}
4751
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004752TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00004753 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
4754 const sp<FakeWindowHandle>& w =
4755 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
4756 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4757
4758 touch();
4759
4760 mTouchWindow->assertNoEvents();
4761}
4762
4763TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004764 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
4765 const sp<FakeWindowHandle>& w =
4766 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
4767 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4768
4769 touch();
4770
4771 w->assertNoEvents();
4772}
4773
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004774TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004775 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
4776 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004777
4778 touch();
4779
4780 mTouchWindow->consumeAnyMotionDown();
4781}
4782
4783TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004784 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004785 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004786 w->setFrame(Rect(0, 0, 50, 50));
4787 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004788
4789 touch({PointF{100, 100}});
4790
4791 mTouchWindow->consumeAnyMotionDown();
4792}
4793
4794TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004795 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004796 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004797 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4798
4799 touch();
4800
4801 mTouchWindow->consumeAnyMotionDown();
4802}
4803
4804TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
4805 const sp<FakeWindowHandle>& w =
4806 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4807 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004808
4809 touch();
4810
4811 mTouchWindow->consumeAnyMotionDown();
4812}
4813
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004814TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
4815 const sp<FakeWindowHandle>& w =
4816 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4817 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4818
4819 touch();
4820
4821 w->assertNoEvents();
4822}
4823
4824/**
4825 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
4826 * inside) while letting them pass-through. Note that even though touch passes through the occluding
4827 * window, the occluding window will still receive ACTION_OUTSIDE event.
4828 */
4829TEST_F(InputDispatcherUntrustedTouchesTest,
4830 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
4831 const sp<FakeWindowHandle>& w =
4832 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
chaviw3277faf2021-05-19 16:45:23 -05004833 w->addFlags(WindowInfo::Flag::WATCH_OUTSIDE_TOUCH);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004834 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4835
4836 touch();
4837
4838 w->consumeMotionOutside();
4839}
4840
4841TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
4842 const sp<FakeWindowHandle>& w =
4843 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
chaviw3277faf2021-05-19 16:45:23 -05004844 w->addFlags(WindowInfo::Flag::WATCH_OUTSIDE_TOUCH);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004845 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4846
4847 touch();
4848
4849 InputEvent* event = w->consume();
4850 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType());
4851 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
4852 EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getX());
4853 EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getY());
4854}
4855
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004856TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004857 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004858 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4859 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004860 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4861
4862 touch();
4863
4864 mTouchWindow->consumeAnyMotionDown();
4865}
4866
4867TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
4868 const sp<FakeWindowHandle>& w =
4869 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4870 MAXIMUM_OBSCURING_OPACITY);
4871 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004872
4873 touch();
4874
4875 mTouchWindow->consumeAnyMotionDown();
4876}
4877
4878TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004879 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004880 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4881 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004882 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4883
4884 touch();
4885
4886 mTouchWindow->assertNoEvents();
4887}
4888
4889TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
4890 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
4891 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004892 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
4893 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004894 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004895 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
4896 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004897 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
4898
4899 touch();
4900
4901 mTouchWindow->assertNoEvents();
4902}
4903
4904TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
4905 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
4906 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004907 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
4908 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004909 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004910 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
4911 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004912 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
4913
4914 touch();
4915
4916 mTouchWindow->consumeAnyMotionDown();
4917}
4918
4919TEST_F(InputDispatcherUntrustedTouchesTest,
4920 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
4921 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004922 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4923 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004924 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004925 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
4926 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004927 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
4928
4929 touch();
4930
4931 mTouchWindow->consumeAnyMotionDown();
4932}
4933
4934TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
4935 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004936 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4937 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004938 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004939 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
4940 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004941 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004942
4943 touch();
4944
4945 mTouchWindow->assertNoEvents();
4946}
4947
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004948TEST_F(InputDispatcherUntrustedTouchesTest,
4949 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
4950 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004951 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
4952 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004953 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004954 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4955 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004956 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
4957
4958 touch();
4959
4960 mTouchWindow->assertNoEvents();
4961}
4962
4963TEST_F(InputDispatcherUntrustedTouchesTest,
4964 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
4965 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004966 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
4967 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004968 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004969 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4970 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004971 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
4972
4973 touch();
4974
4975 mTouchWindow->consumeAnyMotionDown();
4976}
4977
4978TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
4979 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004980 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
4981 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004982 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4983
4984 touch();
4985
4986 mTouchWindow->consumeAnyMotionDown();
4987}
4988
4989TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
4990 const sp<FakeWindowHandle>& w =
4991 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
4992 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4993
4994 touch();
4995
4996 mTouchWindow->consumeAnyMotionDown();
4997}
4998
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00004999TEST_F(InputDispatcherUntrustedTouchesTest,
5000 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
5001 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
5002 const sp<FakeWindowHandle>& w =
5003 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
5004 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5005
5006 touch();
5007
5008 mTouchWindow->assertNoEvents();
5009}
5010
5011TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
5012 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
5013 const sp<FakeWindowHandle>& w =
5014 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
5015 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5016
5017 touch();
5018
5019 mTouchWindow->consumeAnyMotionDown();
5020}
5021
5022TEST_F(InputDispatcherUntrustedTouchesTest,
5023 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
5024 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
5025 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005026 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
5027 OPACITY_ABOVE_THRESHOLD);
5028 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5029
5030 touch();
5031
5032 mTouchWindow->consumeAnyMotionDown();
5033}
5034
5035TEST_F(InputDispatcherUntrustedTouchesTest,
5036 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
5037 const sp<FakeWindowHandle>& w1 =
5038 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
5039 OPACITY_BELOW_THRESHOLD);
5040 const sp<FakeWindowHandle>& w2 =
5041 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
5042 OPACITY_BELOW_THRESHOLD);
5043 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
5044
5045 touch();
5046
5047 mTouchWindow->assertNoEvents();
5048}
5049
5050/**
5051 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
5052 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
5053 * (which alone would result in allowing touches) does not affect the blocking behavior.
5054 */
5055TEST_F(InputDispatcherUntrustedTouchesTest,
5056 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
5057 const sp<FakeWindowHandle>& wB =
5058 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
5059 OPACITY_BELOW_THRESHOLD);
5060 const sp<FakeWindowHandle>& wC =
5061 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
5062 OPACITY_BELOW_THRESHOLD);
5063 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
5064
5065 touch();
5066
5067 mTouchWindow->assertNoEvents();
5068}
5069
5070/**
5071 * This test is testing that a window from a different UID but with same application token doesn't
5072 * block the touch. Apps can share the application token for close UI collaboration for example.
5073 */
5074TEST_F(InputDispatcherUntrustedTouchesTest,
5075 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
5076 const sp<FakeWindowHandle>& w =
5077 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
5078 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00005079 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5080
5081 touch();
5082
5083 mTouchWindow->consumeAnyMotionDown();
5084}
5085
arthurhungb89ccb02020-12-30 16:19:01 +08005086class InputDispatcherDragTests : public InputDispatcherTest {
5087protected:
5088 std::shared_ptr<FakeApplicationHandle> mApp;
5089 sp<FakeWindowHandle> mWindow;
5090 sp<FakeWindowHandle> mSecondWindow;
5091 sp<FakeWindowHandle> mDragWindow;
5092
5093 void SetUp() override {
5094 InputDispatcherTest::SetUp();
5095 mApp = std::make_shared<FakeApplicationHandle>();
5096 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
5097 mWindow->setFrame(Rect(0, 0, 100, 100));
chaviw3277faf2021-05-19 16:45:23 -05005098 mWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
arthurhungb89ccb02020-12-30 16:19:01 +08005099
5100 mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
5101 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
chaviw3277faf2021-05-19 16:45:23 -05005102 mSecondWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL);
arthurhungb89ccb02020-12-30 16:19:01 +08005103
5104 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
5105 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
5106 }
5107
5108 // Start performing drag, we will create a drag window and transfer touch to it.
5109 void performDrag() {
5110 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5111 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5112 {50, 50}))
5113 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5114
5115 // Window should receive motion event.
5116 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5117
5118 // The drag window covers the entire display
5119 mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
5120 mDispatcher->setInputWindows(
5121 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
5122
5123 // Transfer touch focus to the drag window
5124 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
5125 true /* isDragDrop */);
5126 mWindow->consumeMotionCancel();
5127 mDragWindow->consumeMotionDown();
5128 }
arthurhung6d4bed92021-03-17 11:59:33 +08005129
5130 // Start performing drag, we will create a drag window and transfer touch to it.
5131 void performStylusDrag() {
5132 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5133 injectMotionEvent(mDispatcher,
5134 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
5135 AINPUT_SOURCE_STYLUS)
5136 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
5137 .pointer(PointerBuilder(0,
5138 AMOTION_EVENT_TOOL_TYPE_STYLUS)
5139 .x(50)
5140 .y(50))
5141 .build()));
5142 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5143
5144 // The drag window covers the entire display
5145 mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
5146 mDispatcher->setInputWindows(
5147 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
5148
5149 // Transfer touch focus to the drag window
5150 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
5151 true /* isDragDrop */);
5152 mWindow->consumeMotionCancel();
5153 mDragWindow->consumeMotionDown();
5154 }
arthurhungb89ccb02020-12-30 16:19:01 +08005155};
5156
5157TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
5158 performDrag();
5159
5160 // Move on window.
5161 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5162 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5163 ADISPLAY_ID_DEFAULT, {50, 50}))
5164 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5165 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5166 mWindow->consumeDragEvent(false, 50, 50);
5167 mSecondWindow->assertNoEvents();
5168
5169 // Move to another window.
5170 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5171 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5172 ADISPLAY_ID_DEFAULT, {150, 50}))
5173 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5174 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5175 mWindow->consumeDragEvent(true, 150, 50);
5176 mSecondWindow->consumeDragEvent(false, 50, 50);
5177
5178 // Move back to original window.
5179 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5180 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5181 ADISPLAY_ID_DEFAULT, {50, 50}))
5182 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5183 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5184 mWindow->consumeDragEvent(false, 50, 50);
5185 mSecondWindow->consumeDragEvent(true, -50, 50);
5186
5187 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5188 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
5189 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5190 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5191 mWindow->assertNoEvents();
5192 mSecondWindow->assertNoEvents();
5193}
5194
arthurhungf452d0b2021-01-06 00:19:52 +08005195TEST_F(InputDispatcherDragTests, DragAndDrop) {
5196 performDrag();
5197
5198 // Move on window.
5199 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5200 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5201 ADISPLAY_ID_DEFAULT, {50, 50}))
5202 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5203 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5204 mWindow->consumeDragEvent(false, 50, 50);
5205 mSecondWindow->assertNoEvents();
5206
5207 // Move to another window.
5208 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5209 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5210 ADISPLAY_ID_DEFAULT, {150, 50}))
5211 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5212 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5213 mWindow->consumeDragEvent(true, 150, 50);
5214 mSecondWindow->consumeDragEvent(false, 50, 50);
5215
5216 // drop to another window.
5217 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5218 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5219 {150, 50}))
5220 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5221 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5222 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
5223 mWindow->assertNoEvents();
5224 mSecondWindow->assertNoEvents();
5225}
5226
arthurhung6d4bed92021-03-17 11:59:33 +08005227TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
5228 performStylusDrag();
5229
5230 // Move on window and keep button pressed.
5231 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5232 injectMotionEvent(mDispatcher,
5233 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
5234 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
5235 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
5236 .x(50)
5237 .y(50))
5238 .build()))
5239 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5240 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5241 mWindow->consumeDragEvent(false, 50, 50);
5242 mSecondWindow->assertNoEvents();
5243
5244 // Move to another window and release button, expect to drop item.
5245 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5246 injectMotionEvent(mDispatcher,
5247 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
5248 .buttonState(0)
5249 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
5250 .x(150)
5251 .y(50))
5252 .build()))
5253 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5254 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5255 mWindow->assertNoEvents();
5256 mSecondWindow->assertNoEvents();
5257 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
5258
5259 // nothing to the window.
5260 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5261 injectMotionEvent(mDispatcher,
5262 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
5263 .buttonState(0)
5264 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
5265 .x(150)
5266 .y(50))
5267 .build()))
5268 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5269 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5270 mWindow->assertNoEvents();
5271 mSecondWindow->assertNoEvents();
5272}
5273
Arthur Hung6d0571e2021-04-09 20:18:16 +08005274TEST_F(InputDispatcherDragTests, DragAndDrop_InvalidWindow) {
5275 performDrag();
5276
5277 // Set second window invisible.
5278 mSecondWindow->setVisible(false);
5279 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
5280
5281 // Move on window.
5282 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5283 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5284 ADISPLAY_ID_DEFAULT, {50, 50}))
5285 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5286 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5287 mWindow->consumeDragEvent(false, 50, 50);
5288 mSecondWindow->assertNoEvents();
5289
5290 // Move to another window.
5291 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5292 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5293 ADISPLAY_ID_DEFAULT, {150, 50}))
5294 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5295 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5296 mWindow->consumeDragEvent(true, 150, 50);
5297 mSecondWindow->assertNoEvents();
5298
5299 // drop to another window.
5300 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5301 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5302 {150, 50}))
5303 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5304 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5305 mFakePolicy->assertDropTargetEquals(nullptr);
5306 mWindow->assertNoEvents();
5307 mSecondWindow->assertNoEvents();
5308}
5309
Garfield Tane84e6f92019-08-29 17:28:41 -07005310} // namespace android::inputdispatcher