blob: 31d6900ef225d70b08c0fd607e3e36da9b950c76 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Garfield Tan0fc2fa72019-08-29 17:22:15 -070017#include "../dispatcher/InputDispatcher.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080018
Garfield Tan1c7bc862020-01-28 13:24:04 -080019#include <android-base/stringprintf.h>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070020#include <android-base/thread_annotations.h>
Robert Carr803535b2018-08-02 16:38:15 -070021#include <binder/Binder.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080022#include <gtest/gtest.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100023#include <input/Input.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080024#include <linux/input.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100025
Garfield Tan1c7bc862020-01-28 13:24:04 -080026#include <cinttypes>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070027#include <thread>
Garfield Tan1c7bc862020-01-28 13:24:04 -080028#include <unordered_set>
chaviwd1c23182019-12-20 18:44:56 -080029#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080030
Garfield Tan1c7bc862020-01-28 13:24:04 -080031using android::base::StringPrintf;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080032using android::os::InputEventInjectionResult;
33using android::os::InputEventInjectionSync;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000034using android::os::TouchOcclusionMode;
Michael Wright44753b12020-07-08 13:48:11 +010035using namespace android::flag_operators;
Garfield Tan1c7bc862020-01-28 13:24:04 -080036
Garfield Tane84e6f92019-08-29 17:28:41 -070037namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080038
39// An arbitrary time value.
40static const nsecs_t ARBITRARY_TIME = 1234;
41
42// An arbitrary device id.
43static const int32_t DEVICE_ID = 1;
44
Jeff Brownf086ddb2014-02-11 14:28:48 -080045// An arbitrary display id.
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -080046static const int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
Jeff Brownf086ddb2014-02-11 14:28:48 -080047
Michael Wrightd02c5b62014-02-10 15:10:22 -080048// An arbitrary injector pid / uid pair that has permission to inject events.
49static const int32_t INJECTOR_PID = 999;
50static const int32_t INJECTOR_UID = 1001;
51
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +000052// An arbitrary pid of the gesture monitor window
53static constexpr int32_t MONITOR_PID = 2001;
54
chaviwd1c23182019-12-20 18:44:56 -080055struct PointF {
56 float x;
57 float y;
58};
Michael Wrightd02c5b62014-02-10 15:10:22 -080059
Gang Wang342c9272020-01-13 13:15:04 -050060/**
61 * Return a DOWN key event with KEYCODE_A.
62 */
63static KeyEvent getTestKeyEvent() {
64 KeyEvent event;
65
Garfield Tanfbe732e2020-01-24 11:26:14 -080066 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
67 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
68 ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -050069 return event;
70}
71
Michael Wrightd02c5b62014-02-10 15:10:22 -080072// --- FakeInputDispatcherPolicy ---
73
74class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
75 InputDispatcherConfiguration mConfig;
76
77protected:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100078 virtual ~FakeInputDispatcherPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -080079
80public:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100081 FakeInputDispatcherPolicy() {}
Jackal Guof9696682018-10-05 12:23:23 +080082
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080083 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080084 assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_KEY, args.eventTime, args.action,
85 args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080086 }
87
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080088 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args) {
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080089 assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_MOTION, args.eventTime, args.action,
90 args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080091 }
92
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -070093 void assertFilterInputEventWasNotCalled() {
94 std::scoped_lock lock(mLock);
95 ASSERT_EQ(nullptr, mFilteredEvent);
96 }
Michael Wrightd02c5b62014-02-10 15:10:22 -080097
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -080098 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -070099 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800100 ASSERT_TRUE(mConfigurationChangedTime)
101 << "Timed out waiting for configuration changed call";
102 ASSERT_EQ(*mConfigurationChangedTime, when);
103 mConfigurationChangedTime = std::nullopt;
104 }
105
106 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700107 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800108 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800109 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800110 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
111 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
112 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
113 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
114 mLastNotifySwitch = std::nullopt;
115 }
116
chaviwfd6d3512019-03-25 13:23:49 -0700117 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700118 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800119 ASSERT_EQ(touchedToken, mOnPointerDownToken);
120 mOnPointerDownToken.clear();
121 }
122
123 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700124 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800125 ASSERT_TRUE(mOnPointerDownToken == nullptr)
126 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700127 }
128
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700129 // This function must be called soon after the expected ANR timer starts,
130 // because we are also checking how much time has passed.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500131 void assertNotifyNoFocusedWindowAnrWasCalled(
Chris Yea209fde2020-07-22 13:54:51 -0700132 std::chrono::nanoseconds timeout,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500133 const std::shared_ptr<InputApplicationHandle>& expectedApplication) {
134 std::shared_ptr<InputApplicationHandle> application;
135 { // acquire lock
136 std::unique_lock lock(mLock);
137 android::base::ScopedLockAssertion assumeLocked(mLock);
138 ASSERT_NO_FATAL_FAILURE(
139 application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock));
140 } // release lock
141 ASSERT_EQ(expectedApplication, application);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700142 }
143
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000144 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
145 const sp<IBinder>& expectedConnectionToken) {
146 sp<IBinder> connectionToken = getUnresponsiveWindowToken(timeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500147 ASSERT_EQ(expectedConnectionToken, connectionToken);
148 }
149
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000150 void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedConnectionToken) {
151 sp<IBinder> connectionToken = getResponsiveWindowToken();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500152 ASSERT_EQ(expectedConnectionToken, connectionToken);
153 }
154
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000155 void assertNotifyMonitorUnresponsiveWasCalled(std::chrono::nanoseconds timeout) {
156 int32_t pid = getUnresponsiveMonitorPid(timeout);
157 ASSERT_EQ(MONITOR_PID, pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500158 }
159
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000160 void assertNotifyMonitorResponsiveWasCalled() {
161 int32_t pid = getResponsiveMonitorPid();
162 ASSERT_EQ(MONITOR_PID, pid);
163 }
164
165 sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500166 std::unique_lock lock(mLock);
167 android::base::ScopedLockAssertion assumeLocked(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000168 return getAnrTokenLockedInterruptible(timeout, mAnrWindowTokens, lock);
169 }
170
171 sp<IBinder> getResponsiveWindowToken() {
172 std::unique_lock lock(mLock);
173 android::base::ScopedLockAssertion assumeLocked(mLock);
174 return getAnrTokenLockedInterruptible(0s, mResponsiveWindowTokens, lock);
175 }
176
177 int32_t getUnresponsiveMonitorPid(std::chrono::nanoseconds timeout) {
178 std::unique_lock lock(mLock);
179 android::base::ScopedLockAssertion assumeLocked(mLock);
180 return getAnrTokenLockedInterruptible(timeout, mAnrMonitorPids, lock);
181 }
182
183 int32_t getResponsiveMonitorPid() {
184 std::unique_lock lock(mLock);
185 android::base::ScopedLockAssertion assumeLocked(mLock);
186 return getAnrTokenLockedInterruptible(0s, mResponsiveMonitorPids, lock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500187 }
188
189 // All three ANR-related callbacks behave the same way, so we use this generic function to wait
190 // for a specific container to become non-empty. When the container is non-empty, return the
191 // first entry from the container and erase it.
192 template <class T>
193 T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage,
194 std::unique_lock<std::mutex>& lock) REQUIRES(mLock) {
195 const std::chrono::time_point start = std::chrono::steady_clock::now();
196 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700197
198 // If there is an ANR, Dispatcher won't be idle because there are still events
199 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
200 // before checking if ANR was called.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500201 // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need
202 // to provide it some time to act. 100ms seems reasonable.
203 mNotifyAnr.wait_for(lock, timeToWait,
204 [&storage]() REQUIRES(mLock) { return !storage.empty(); });
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700205 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500206 if (storage.empty()) {
207 ADD_FAILURE() << "Did not receive the ANR callback";
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000208 return {};
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700209 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700210 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
211 // the dispatcher started counting before this function was called
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700212 if (std::chrono::abs(timeout - waited) > 100ms) {
213 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
214 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
215 << "ms, but waited "
216 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
217 << "ms instead";
218 }
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500219 T token = storage.front();
220 storage.pop();
221 return token;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700222 }
223
224 void assertNotifyAnrWasNotCalled() {
225 std::scoped_lock lock(mLock);
226 ASSERT_TRUE(mAnrApplications.empty());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000227 ASSERT_TRUE(mAnrWindowTokens.empty());
228 ASSERT_TRUE(mAnrMonitorPids.empty());
229 ASSERT_TRUE(mResponsiveWindowTokens.empty())
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500230 << "ANR was not called, but please also consume the 'connection is responsive' "
231 "signal";
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000232 ASSERT_TRUE(mResponsiveMonitorPids.empty())
233 << "Monitor ANR was not called, but please also consume the 'monitor is responsive'"
234 " signal";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700235 }
236
Garfield Tan1c7bc862020-01-28 13:24:04 -0800237 void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) {
238 mConfig.keyRepeatTimeout = timeout;
239 mConfig.keyRepeatDelay = delay;
240 }
241
Prabir Pradhan99987712020-11-10 18:43:05 -0800242 void waitForSetPointerCapture(bool enabled) {
243 std::unique_lock lock(mLock);
244 base::ScopedLockAssertion assumeLocked(mLock);
245
246 if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms,
247 [this, enabled]() REQUIRES(mLock) {
248 return mPointerCaptureEnabled &&
249 *mPointerCaptureEnabled ==
250 enabled;
251 })) {
252 FAIL() << "Timed out waiting for setPointerCapture(" << enabled << ") to be called.";
253 }
254 mPointerCaptureEnabled.reset();
255 }
256
257 void assertSetPointerCaptureNotCalled() {
258 std::unique_lock lock(mLock);
259 base::ScopedLockAssertion assumeLocked(mLock);
260
261 if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) {
262 FAIL() << "Expected setPointerCapture(enabled) to not be called, but was called. "
263 "enabled = "
264 << *mPointerCaptureEnabled;
265 }
266 mPointerCaptureEnabled.reset();
267 }
268
arthurhungf452d0b2021-01-06 00:19:52 +0800269 void assertDropTargetEquals(const sp<IBinder>& targetToken) {
270 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800271 ASSERT_TRUE(mNotifyDropWindowWasCalled);
arthurhungf452d0b2021-01-06 00:19:52 +0800272 ASSERT_EQ(targetToken, mDropTargetWindowToken);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800273 mNotifyDropWindowWasCalled = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800274 }
275
Michael Wrightd02c5b62014-02-10 15:10:22 -0800276private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700277 std::mutex mLock;
278 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
279 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
280 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
281 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800282
Prabir Pradhan99987712020-11-10 18:43:05 -0800283 std::condition_variable mPointerCaptureChangedCondition;
284 std::optional<bool> mPointerCaptureEnabled GUARDED_BY(mLock);
285
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700286 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700287 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000288 std::queue<sp<IBinder>> mAnrWindowTokens GUARDED_BY(mLock);
289 std::queue<sp<IBinder>> mResponsiveWindowTokens GUARDED_BY(mLock);
290 std::queue<int32_t> mAnrMonitorPids GUARDED_BY(mLock);
291 std::queue<int32_t> mResponsiveMonitorPids GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700292 std::condition_variable mNotifyAnr;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700293
arthurhungf452d0b2021-01-06 00:19:52 +0800294 sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800295 bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800296
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600297 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700298 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800299 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800300 }
301
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000302 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700303 std::scoped_lock lock(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000304 mAnrWindowTokens.push(connectionToken);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700305 mNotifyAnr.notify_all();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500306 }
307
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000308 void notifyMonitorUnresponsive(int32_t pid, const std::string&) override {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500309 std::scoped_lock lock(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000310 mAnrMonitorPids.push(pid);
311 mNotifyAnr.notify_all();
312 }
313
314 void notifyWindowResponsive(const sp<IBinder>& connectionToken) override {
315 std::scoped_lock lock(mLock);
316 mResponsiveWindowTokens.push(connectionToken);
317 mNotifyAnr.notify_all();
318 }
319
320 void notifyMonitorResponsive(int32_t pid) override {
321 std::scoped_lock lock(mLock);
322 mResponsiveMonitorPids.push(pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500323 mNotifyAnr.notify_all();
324 }
325
326 void notifyNoFocusedWindowAnr(
327 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
328 std::scoped_lock lock(mLock);
329 mAnrApplications.push(applicationHandle);
330 mNotifyAnr.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800331 }
332
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600333 void notifyInputChannelBroken(const sp<IBinder>&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800334
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600335 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700336
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600337 void notifyUntrustedTouch(const std::string& obscuringPackage) override {}
Chris Yef59a2f42020-10-16 12:55:26 -0700338 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
339 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
340 const std::vector<float>& values) override {}
341
342 void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType,
343 InputDeviceSensorAccuracy accuracy) override {}
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000344
Chris Yefb552902021-02-03 17:18:37 -0800345 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
346
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600347 void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800348 *outConfig = mConfig;
349 }
350
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600351 bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700352 std::scoped_lock lock(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800353 switch (inputEvent->getType()) {
354 case AINPUT_EVENT_TYPE_KEY: {
355 const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800356 mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800357 break;
358 }
359
360 case AINPUT_EVENT_TYPE_MOTION: {
361 const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800362 mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800363 break;
364 }
365 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800366 return true;
367 }
368
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600369 void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800370
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600371 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800372
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600373 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800374 return 0;
375 }
376
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600377 bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800378 return false;
379 }
380
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600381 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
382 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700383 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800384 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
385 * essentially a passthrough for notifySwitch.
386 */
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800387 mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800388 }
389
Sean Stoutb4e0a592021-02-23 07:34:53 -0800390 void pokeUserActivity(nsecs_t, int32_t, int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800391
Prabir Pradhan93f342c2021-03-11 15:05:30 -0800392 bool checkInjectEventsPermissionNonReentrant(int32_t pid, int32_t uid) override {
393 return pid == INJECTOR_PID && uid == INJECTOR_UID;
394 }
Jackal Guof9696682018-10-05 12:23:23 +0800395
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600396 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700397 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700398 mOnPointerDownToken = newToken;
399 }
400
Prabir Pradhan99987712020-11-10 18:43:05 -0800401 void setPointerCapture(bool enabled) override {
402 std::scoped_lock lock(mLock);
403 mPointerCaptureEnabled = {enabled};
404 mPointerCaptureChangedCondition.notify_all();
405 }
406
arthurhungf452d0b2021-01-06 00:19:52 +0800407 void notifyDropWindow(const sp<IBinder>& token, float x, float y) override {
408 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800409 mNotifyDropWindowWasCalled = true;
arthurhungf452d0b2021-01-06 00:19:52 +0800410 mDropTargetWindowToken = token;
411 }
412
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800413 void assertFilterInputEventWasCalled(int type, nsecs_t eventTime, int32_t action,
414 int32_t displayId) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700415 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800416 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
417 ASSERT_EQ(mFilteredEvent->getType(), type);
418
419 if (type == AINPUT_EVENT_TYPE_KEY) {
420 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*mFilteredEvent);
421 EXPECT_EQ(keyEvent.getEventTime(), eventTime);
422 EXPECT_EQ(keyEvent.getAction(), action);
423 EXPECT_EQ(keyEvent.getDisplayId(), displayId);
424 } else if (type == AINPUT_EVENT_TYPE_MOTION) {
425 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*mFilteredEvent);
426 EXPECT_EQ(motionEvent.getEventTime(), eventTime);
427 EXPECT_EQ(motionEvent.getAction(), action);
428 EXPECT_EQ(motionEvent.getDisplayId(), displayId);
429 } else {
430 FAIL() << "Unknown type: " << type;
431 }
432
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800433 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800434 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800435};
436
Michael Wrightd02c5b62014-02-10 15:10:22 -0800437// --- InputDispatcherTest ---
438
439class InputDispatcherTest : public testing::Test {
440protected:
441 sp<FakeInputDispatcherPolicy> mFakePolicy;
442 sp<InputDispatcher> mDispatcher;
443
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700444 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800445 mFakePolicy = new FakeInputDispatcherPolicy();
446 mDispatcher = new InputDispatcher(mFakePolicy);
Arthur Hungb92218b2018-08-14 12:00:21 +0800447 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000448 // Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700449 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800450 }
451
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700452 virtual void TearDown() override {
453 ASSERT_EQ(OK, mDispatcher->stop());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800454 mFakePolicy.clear();
455 mDispatcher.clear();
456 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700457
458 /**
459 * Used for debugging when writing the test
460 */
461 void dumpDispatcherState() {
462 std::string dump;
463 mDispatcher->dump(dump);
464 std::stringstream ss(dump);
465 std::string to;
466
467 while (std::getline(ss, to, '\n')) {
468 ALOGE("%s", to.c_str());
469 }
470 }
Vishnu Nair958da932020-08-21 17:12:37 -0700471
472 void setFocusedWindow(const sp<InputWindowHandle>& window,
473 const sp<InputWindowHandle>& focusedWindow = nullptr) {
474 FocusRequest request;
475 request.token = window->getToken();
476 if (focusedWindow) {
477 request.focusedToken = focusedWindow->getToken();
478 }
479 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
480 request.displayId = window->getInfo()->displayId;
481 mDispatcher->setFocusedWindow(request);
482 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800483};
484
Michael Wrightd02c5b62014-02-10 15:10:22 -0800485TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
486 KeyEvent event;
487
488 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800489 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
490 INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600491 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
492 ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800493 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700494 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800495 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800496 << "Should reject key events with undefined action.";
497
498 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800499 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
500 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600501 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800502 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700503 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800504 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800505 << "Should reject key events with ACTION_MULTIPLE.";
506}
507
508TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
509 MotionEvent event;
510 PointerProperties pointerProperties[MAX_POINTERS + 1];
511 PointerCoords pointerCoords[MAX_POINTERS + 1];
512 for (int i = 0; i <= MAX_POINTERS; i++) {
513 pointerProperties[i].clear();
514 pointerProperties[i].id = i;
515 pointerCoords[i].clear();
516 }
517
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800518 // Some constants commonly used below
519 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
520 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
521 constexpr int32_t metaState = AMETA_NONE;
522 constexpr MotionClassification classification = MotionClassification::NONE;
523
chaviw9eaa22c2020-07-01 16:21:27 -0700524 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800525 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800526 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
chaviw9eaa22c2020-07-01 16:21:27 -0700527 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification,
528 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600529 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700530 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800531 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700532 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800533 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800534 << "Should reject motion events with undefined action.";
535
536 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800537 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700538 AMOTION_EVENT_ACTION_POINTER_DOWN |
539 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700540 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
541 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
542 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
543 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800544 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700545 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800546 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800547 << "Should reject motion events with pointer down index too large.";
548
Garfield Tanfbe732e2020-01-24 11:26:14 -0800549 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700550 AMOTION_EVENT_ACTION_POINTER_DOWN |
551 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700552 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
553 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
554 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
555 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800556 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700557 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800558 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800559 << "Should reject motion events with pointer down index too small.";
560
561 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800562 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700563 AMOTION_EVENT_ACTION_POINTER_UP |
564 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700565 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
566 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
567 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
568 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800569 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700570 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800571 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800572 << "Should reject motion events with pointer up index too large.";
573
Garfield Tanfbe732e2020-01-24 11:26:14 -0800574 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700575 AMOTION_EVENT_ACTION_POINTER_UP |
576 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700577 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
578 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
579 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
580 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800581 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700582 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800583 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800584 << "Should reject motion events with pointer up index too small.";
585
586 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800587 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
588 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700589 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
590 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700591 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800592 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700593 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800594 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800595 << "Should reject motion events with 0 pointers.";
596
Garfield Tanfbe732e2020-01-24 11:26:14 -0800597 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
598 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700599 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
600 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700601 /*pointerCount*/ MAX_POINTERS + 1, 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 more than MAX_POINTERS pointers.";
606
607 // Rejects motion events with invalid pointer ids.
608 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800609 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
610 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700611 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
612 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700613 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800614 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700615 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800616 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800617 << "Should reject motion events with pointer ids less than 0.";
618
619 pointerProperties[0].id = MAX_POINTER_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,
623 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700624 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800625 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700626 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800627 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800628 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
629
630 // Rejects motion events with duplicate pointer ids.
631 pointerProperties[0].id = 1;
632 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800633 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
634 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700635 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
636 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700637 /*pointerCount*/ 2, 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 duplicate pointer ids.";
642}
643
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800644/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
645
646TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
647 constexpr nsecs_t eventTime = 20;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800648 NotifyConfigurationChangedArgs args(10 /*id*/, eventTime);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800649 mDispatcher->notifyConfigurationChanged(&args);
650 ASSERT_TRUE(mDispatcher->waitForIdle());
651
652 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
653}
654
655TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800656 NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/,
657 2 /*switchMask*/);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800658 mDispatcher->notifySwitch(&args);
659
660 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
661 args.policyFlags |= POLICY_FLAG_TRUSTED;
662 mFakePolicy->assertNotifySwitchWasCalled(args);
663}
664
Arthur Hungb92218b2018-08-14 12:00:21 +0800665// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700666static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700667static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s;
Arthur Hungb92218b2018-08-14 12:00:21 +0800668
669class FakeApplicationHandle : public InputApplicationHandle {
670public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700671 FakeApplicationHandle() {
672 mInfo.name = "Fake Application";
673 mInfo.token = new BBinder();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500674 mInfo.dispatchingTimeoutMillis =
675 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700676 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800677 virtual ~FakeApplicationHandle() {}
678
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000679 virtual bool updateInfo() override { return true; }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700680
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500681 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
682 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700683 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800684};
685
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800686class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800687public:
Garfield Tan15601662020-09-22 15:32:38 -0700688 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800689 : mName(name) {
Garfield Tan15601662020-09-22 15:32:38 -0700690 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800691 }
692
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800693 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700694 InputEvent* event;
695 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
696 if (!consumeSeq) {
697 return nullptr;
698 }
699 finishEvent(*consumeSeq);
700 return event;
701 }
702
703 /**
704 * Receive an event without acknowledging it.
705 * Return the sequence number that could later be used to send finished signal.
706 */
707 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800708 uint32_t consumeSeq;
709 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800710
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800711 std::chrono::time_point start = std::chrono::steady_clock::now();
712 status_t status = WOULD_BLOCK;
713 while (status == WOULD_BLOCK) {
chaviw81e2bb92019-12-18 15:03:51 -0800714 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800715 &event);
716 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
717 if (elapsed > 100ms) {
718 break;
719 }
720 }
721
722 if (status == WOULD_BLOCK) {
723 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700724 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800725 }
726
727 if (status != OK) {
728 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700729 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800730 }
731 if (event == nullptr) {
732 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700733 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800734 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700735 if (outEvent != nullptr) {
736 *outEvent = event;
737 }
738 return consumeSeq;
739 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800740
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700741 /**
742 * To be used together with "receiveEvent" to complete the consumption of an event.
743 */
744 void finishEvent(uint32_t consumeSeq) {
745 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
746 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800747 }
748
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000749 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
750 const status_t status = mConsumer->sendTimeline(inputEventId, timeline);
751 ASSERT_EQ(OK, status);
752 }
753
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000754 void consumeEvent(int32_t expectedEventType, int32_t expectedAction,
755 std::optional<int32_t> expectedDisplayId,
756 std::optional<int32_t> expectedFlags) {
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800757 InputEvent* event = consume();
758
759 ASSERT_NE(nullptr, event) << mName.c_str()
760 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800761 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700762 << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType)
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800763 << " event, got " << inputEventTypeToString(event->getType()) << " event";
Arthur Hungb92218b2018-08-14 12:00:21 +0800764
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000765 if (expectedDisplayId.has_value()) {
766 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
767 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800768
Tiger Huang8664f8c2018-10-11 19:14:35 +0800769 switch (expectedEventType) {
770 case AINPUT_EVENT_TYPE_KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800771 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
772 EXPECT_EQ(expectedAction, keyEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000773 if (expectedFlags.has_value()) {
774 EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags());
775 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800776 break;
777 }
778 case AINPUT_EVENT_TYPE_MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800779 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
780 EXPECT_EQ(expectedAction, motionEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000781 if (expectedFlags.has_value()) {
782 EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags());
783 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800784 break;
785 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100786 case AINPUT_EVENT_TYPE_FOCUS: {
787 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
788 }
Prabir Pradhan99987712020-11-10 18:43:05 -0800789 case AINPUT_EVENT_TYPE_CAPTURE: {
790 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
791 }
arthurhungb89ccb02020-12-30 16:19:01 +0800792 case AINPUT_EVENT_TYPE_DRAG: {
793 FAIL() << "Use 'consumeDragEvent' for DRAG events";
794 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800795 default: {
796 FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType;
797 }
798 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800799 }
800
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100801 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
802 InputEvent* event = consume();
803 ASSERT_NE(nullptr, event) << mName.c_str()
804 << ": consumer should have returned non-NULL event.";
805 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
806 << "Got " << inputEventTypeToString(event->getType())
807 << " event instead of FOCUS event";
808
809 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
810 << mName.c_str() << ": event displayId should always be NONE.";
811
812 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
813 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
814 EXPECT_EQ(inTouchMode, focusEvent->getInTouchMode());
815 }
816
Prabir Pradhan99987712020-11-10 18:43:05 -0800817 void consumeCaptureEvent(bool hasCapture) {
818 const InputEvent* event = consume();
819 ASSERT_NE(nullptr, event) << mName.c_str()
820 << ": consumer should have returned non-NULL event.";
821 ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType())
822 << "Got " << inputEventTypeToString(event->getType())
823 << " event instead of CAPTURE event";
824
825 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
826 << mName.c_str() << ": event displayId should always be NONE.";
827
828 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
829 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
830 }
831
arthurhungb89ccb02020-12-30 16:19:01 +0800832 void consumeDragEvent(bool isExiting, float x, float y) {
833 const InputEvent* event = consume();
834 ASSERT_NE(nullptr, event) << mName.c_str()
835 << ": consumer should have returned non-NULL event.";
836 ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType())
837 << "Got " << inputEventTypeToString(event->getType())
838 << " event instead of DRAG event";
839
840 EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
841 << mName.c_str() << ": event displayId should always be NONE.";
842
843 const auto& dragEvent = static_cast<const DragEvent&>(*event);
844 EXPECT_EQ(isExiting, dragEvent.isExiting());
845 EXPECT_EQ(x, dragEvent.getX());
846 EXPECT_EQ(y, dragEvent.getY());
847 }
848
chaviwd1c23182019-12-20 18:44:56 -0800849 void assertNoEvents() {
850 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700851 if (event == nullptr) {
852 return;
853 }
854 if (event->getType() == AINPUT_EVENT_TYPE_KEY) {
855 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
856 ADD_FAILURE() << "Received key event "
857 << KeyEvent::actionToString(keyEvent.getAction());
858 } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) {
859 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
860 ADD_FAILURE() << "Received motion event "
861 << MotionEvent::actionToString(motionEvent.getAction());
862 } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) {
863 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
864 ADD_FAILURE() << "Received focus event, hasFocus = "
865 << (focusEvent.getHasFocus() ? "true" : "false");
Prabir Pradhan99987712020-11-10 18:43:05 -0800866 } else if (event->getType() == AINPUT_EVENT_TYPE_CAPTURE) {
867 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
868 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
869 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700870 }
871 FAIL() << mName.c_str()
872 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -0800873 }
874
875 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
876
877protected:
878 std::unique_ptr<InputConsumer> mConsumer;
879 PreallocatedInputEventFactory mEventFactory;
880
881 std::string mName;
882};
883
884class FakeWindowHandle : public InputWindowHandle {
885public:
886 static const int32_t WIDTH = 600;
887 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -0800888
Chris Yea209fde2020-07-22 13:54:51 -0700889 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
chaviwd1c23182019-12-20 18:44:56 -0800890 const sp<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500891 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -0800892 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500893 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -0700894 base::Result<std::unique_ptr<InputChannel>> channel =
895 dispatcher->createInputChannel(name);
896 token = (*channel)->getConnectionToken();
897 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -0800898 }
899
900 inputApplicationHandle->updateInfo();
901 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
902
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500903 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700904 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -0800905 mInfo.name = name;
Michael Wright44753b12020-07-08 13:48:11 +0100906 mInfo.type = InputWindowInfo::Type::APPLICATION;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500907 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000908 mInfo.alpha = 1.0;
chaviwd1c23182019-12-20 18:44:56 -0800909 mInfo.frameLeft = 0;
910 mInfo.frameTop = 0;
911 mInfo.frameRight = WIDTH;
912 mInfo.frameBottom = HEIGHT;
chaviw1ff3d1e2020-07-01 15:53:47 -0700913 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -0800914 mInfo.globalScaleFactor = 1.0;
915 mInfo.touchableRegion.clear();
916 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
917 mInfo.visible = true;
Vishnu Nair47074b82020-08-14 11:54:47 -0700918 mInfo.focusable = false;
chaviwd1c23182019-12-20 18:44:56 -0800919 mInfo.hasWallpaper = false;
920 mInfo.paused = false;
chaviwd1c23182019-12-20 18:44:56 -0800921 mInfo.ownerPid = INJECTOR_PID;
922 mInfo.ownerUid = INJECTOR_UID;
chaviwd1c23182019-12-20 18:44:56 -0800923 mInfo.displayId = displayId;
924 }
925
926 virtual bool updateInfo() { return true; }
927
Vishnu Nair47074b82020-08-14 11:54:47 -0700928 void setFocusable(bool focusable) { mInfo.focusable = focusable; }
chaviwd1c23182019-12-20 18:44:56 -0800929
Vishnu Nair958da932020-08-21 17:12:37 -0700930 void setVisible(bool visible) { mInfo.visible = visible; }
931
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700932 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500933 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700934 }
935
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700936 void setPaused(bool paused) { mInfo.paused = paused; }
937
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000938 void setAlpha(float alpha) { mInfo.alpha = alpha; }
939
940 void setTouchOcclusionMode(android::os::TouchOcclusionMode mode) {
941 mInfo.touchOcclusionMode = mode;
942 }
943
Bernardo Rufino7393d172021-02-26 13:56:11 +0000944 void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
945
chaviwd1c23182019-12-20 18:44:56 -0800946 void setFrame(const Rect& frame) {
947 mInfo.frameLeft = frame.left;
948 mInfo.frameTop = frame.top;
949 mInfo.frameRight = frame.right;
950 mInfo.frameBottom = frame.bottom;
arthurhungb89ccb02020-12-30 16:19:01 +0800951 mInfo.transform.set(-frame.left, -frame.top);
chaviwd1c23182019-12-20 18:44:56 -0800952 mInfo.touchableRegion.clear();
953 mInfo.addTouchableRegion(frame);
954 }
955
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +0000956 void addFlags(Flags<InputWindowInfo::Flag> flags) { mInfo.flags |= flags; }
957
Michael Wright44753b12020-07-08 13:48:11 +0100958 void setFlags(Flags<InputWindowInfo::Flag> flags) { mInfo.flags = flags; }
chaviwd1c23182019-12-20 18:44:56 -0800959
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500960 void setInputFeatures(InputWindowInfo::Feature features) { mInfo.inputFeatures = features; }
961
chaviw9eaa22c2020-07-01 16:21:27 -0700962 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
963 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
964 }
965
966 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -0700967
yunho.shinf4a80b82020-11-16 21:13:57 +0900968 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
969
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800970 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
971 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
972 expectedFlags);
973 }
974
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700975 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
976 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
977 }
978
Svet Ganov5d3bc372020-01-26 23:11:07 -0800979 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000980 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800981 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId,
982 expectedFlags);
983 }
984
985 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000986 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800987 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId,
988 expectedFlags);
989 }
990
991 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000992 int32_t expectedFlags = 0) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000993 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
994 }
995
996 void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
997 std::optional<int32_t> expectedFlags = std::nullopt) {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800998 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
999 expectedFlags);
1000 }
1001
Svet Ganov5d3bc372020-01-26 23:11:07 -08001002 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001003 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1004 int32_t expectedFlags = 0) {
1005 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
1006 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001007 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
1008 }
1009
1010 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001011 int32_t expectedFlags = 0) {
1012 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
1013 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001014 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
1015 }
1016
1017 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001018 int32_t expectedFlags = 0) {
Michael Wright3a240c42019-12-10 20:53:41 +00001019 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
1020 expectedFlags);
1021 }
1022
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001023 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1024 int32_t expectedFlags = 0) {
1025 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
1026 expectedFlags);
1027 }
1028
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001029 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
1030 ASSERT_NE(mInputReceiver, nullptr)
1031 << "Cannot consume events from a window with no receiver";
1032 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
1033 }
1034
Prabir Pradhan99987712020-11-10 18:43:05 -08001035 void consumeCaptureEvent(bool hasCapture) {
1036 ASSERT_NE(mInputReceiver, nullptr)
1037 << "Cannot consume events from a window with no receiver";
1038 mInputReceiver->consumeCaptureEvent(hasCapture);
1039 }
1040
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001041 void consumeEvent(int32_t expectedEventType, int32_t expectedAction,
1042 std::optional<int32_t> expectedDisplayId,
1043 std::optional<int32_t> expectedFlags) {
chaviwd1c23182019-12-20 18:44:56 -08001044 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
1045 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
1046 expectedFlags);
1047 }
1048
arthurhungb89ccb02020-12-30 16:19:01 +08001049 void consumeDragEvent(bool isExiting, float x, float y) {
1050 mInputReceiver->consumeDragEvent(isExiting, x, y);
1051 }
1052
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001053 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001054 if (mInputReceiver == nullptr) {
1055 ADD_FAILURE() << "Invalid receive event on window with no receiver";
1056 return std::nullopt;
1057 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001058 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001059 }
1060
1061 void finishEvent(uint32_t sequenceNum) {
1062 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1063 mInputReceiver->finishEvent(sequenceNum);
1064 }
1065
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001066 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
1067 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1068 mInputReceiver->sendTimeline(inputEventId, timeline);
1069 }
1070
chaviwaf87b3e2019-10-01 16:59:28 -07001071 InputEvent* consume() {
1072 if (mInputReceiver == nullptr) {
1073 return nullptr;
1074 }
1075 return mInputReceiver->consume();
1076 }
1077
Arthur Hungb92218b2018-08-14 12:00:21 +08001078 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001079 if (mInputReceiver == nullptr &&
1080 mInfo.inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL)) {
1081 return; // Can't receive events if the window does not have input channel
1082 }
1083 ASSERT_NE(nullptr, mInputReceiver)
1084 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -08001085 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +08001086 }
1087
chaviwaf87b3e2019-10-01 16:59:28 -07001088 sp<IBinder> getToken() { return mInfo.token; }
1089
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001090 const std::string& getName() { return mName; }
1091
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001092 void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) {
1093 mInfo.ownerPid = ownerPid;
1094 mInfo.ownerUid = ownerUid;
1095 }
1096
chaviwd1c23182019-12-20 18:44:56 -08001097private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001098 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -08001099 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001100 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001101};
1102
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001103std::atomic<int32_t> FakeWindowHandle::sId{1};
1104
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001105static InputEventInjectionResult injectKey(
1106 const sp<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
1107 int32_t displayId = ADISPLAY_ID_NONE,
1108 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001109 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
1110 bool allowKeyRepeat = true) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001111 KeyEvent event;
1112 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1113
1114 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001115 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001116 INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE,
1117 repeatCount, currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001118
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001119 int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
1120 if (!allowKeyRepeat) {
1121 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
1122 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001123 // Inject event until dispatch out.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001124 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, syncMode,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001125 injectionTimeout, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001126}
1127
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001128static InputEventInjectionResult injectKeyDown(const sp<InputDispatcher>& dispatcher,
1129 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001130 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId);
1131}
1132
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001133// Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without
1134// sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it
1135// has to be woken up to process the repeating key.
1136static InputEventInjectionResult injectKeyDownNoRepeat(const sp<InputDispatcher>& dispatcher,
1137 int32_t displayId = ADISPLAY_ID_NONE) {
1138 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId,
1139 InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT,
1140 /* allowKeyRepeat */ false);
1141}
1142
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001143static InputEventInjectionResult injectKeyUp(const sp<InputDispatcher>& dispatcher,
1144 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001145 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId);
1146}
1147
Garfield Tandf26e862020-07-01 20:18:19 -07001148class PointerBuilder {
1149public:
1150 PointerBuilder(int32_t id, int32_t toolType) {
1151 mProperties.clear();
1152 mProperties.id = id;
1153 mProperties.toolType = toolType;
1154 mCoords.clear();
1155 }
1156
1157 PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); }
1158
1159 PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); }
1160
1161 PointerBuilder& axis(int32_t axis, float value) {
1162 mCoords.setAxisValue(axis, value);
1163 return *this;
1164 }
1165
1166 PointerProperties buildProperties() const { return mProperties; }
1167
1168 PointerCoords buildCoords() const { return mCoords; }
1169
1170private:
1171 PointerProperties mProperties;
1172 PointerCoords mCoords;
1173};
1174
1175class MotionEventBuilder {
1176public:
1177 MotionEventBuilder(int32_t action, int32_t source) {
1178 mAction = action;
1179 mSource = source;
1180 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1181 }
1182
1183 MotionEventBuilder& eventTime(nsecs_t eventTime) {
1184 mEventTime = eventTime;
1185 return *this;
1186 }
1187
1188 MotionEventBuilder& displayId(int32_t displayId) {
1189 mDisplayId = displayId;
1190 return *this;
1191 }
1192
1193 MotionEventBuilder& actionButton(int32_t actionButton) {
1194 mActionButton = actionButton;
1195 return *this;
1196 }
1197
arthurhung6d4bed92021-03-17 11:59:33 +08001198 MotionEventBuilder& buttonState(int32_t buttonState) {
1199 mButtonState = buttonState;
Garfield Tandf26e862020-07-01 20:18:19 -07001200 return *this;
1201 }
1202
1203 MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) {
1204 mRawXCursorPosition = rawXCursorPosition;
1205 return *this;
1206 }
1207
1208 MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) {
1209 mRawYCursorPosition = rawYCursorPosition;
1210 return *this;
1211 }
1212
1213 MotionEventBuilder& pointer(PointerBuilder pointer) {
1214 mPointers.push_back(pointer);
1215 return *this;
1216 }
1217
1218 MotionEvent build() {
1219 std::vector<PointerProperties> pointerProperties;
1220 std::vector<PointerCoords> pointerCoords;
1221 for (const PointerBuilder& pointer : mPointers) {
1222 pointerProperties.push_back(pointer.buildProperties());
1223 pointerCoords.push_back(pointer.buildCoords());
1224 }
1225
1226 // Set mouse cursor position for the most common cases to avoid boilerplate.
1227 if (mSource == AINPUT_SOURCE_MOUSE &&
1228 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) &&
1229 mPointers.size() == 1) {
1230 mRawXCursorPosition = pointerCoords[0].getX();
1231 mRawYCursorPosition = pointerCoords[0].getY();
1232 }
1233
1234 MotionEvent event;
chaviw9eaa22c2020-07-01 16:21:27 -07001235 ui::Transform identityTransform;
Garfield Tandf26e862020-07-01 20:18:19 -07001236 event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC,
1237 mAction, mActionButton, /* flags */ 0, /* edgeFlags */ 0, AMETA_NONE,
chaviw9eaa22c2020-07-01 16:21:27 -07001238 mButtonState, MotionClassification::NONE, identityTransform,
1239 /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition,
1240 mRawYCursorPosition, mEventTime, mEventTime, mPointers.size(),
1241 pointerProperties.data(), pointerCoords.data());
Garfield Tandf26e862020-07-01 20:18:19 -07001242
1243 return event;
1244 }
1245
1246private:
1247 int32_t mAction;
1248 int32_t mSource;
1249 nsecs_t mEventTime;
1250 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1251 int32_t mActionButton{0};
1252 int32_t mButtonState{0};
1253 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1254 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1255
1256 std::vector<PointerBuilder> mPointers;
1257};
1258
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001259static InputEventInjectionResult injectMotionEvent(
Garfield Tandf26e862020-07-01 20:18:19 -07001260 const sp<InputDispatcher>& dispatcher, const MotionEvent& event,
1261 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001262 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT) {
Garfield Tandf26e862020-07-01 20:18:19 -07001263 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, injectionMode,
1264 injectionTimeout,
1265 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
1266}
1267
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001268static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001269 const sp<InputDispatcher>& dispatcher, int32_t action, int32_t source, int32_t displayId,
1270 const PointF& position,
1271 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001272 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1273 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001274 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001275 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC)) {
Garfield Tandf26e862020-07-01 20:18:19 -07001276 MotionEvent event = MotionEventBuilder(action, source)
1277 .displayId(displayId)
1278 .eventTime(eventTime)
1279 .rawXCursorPosition(cursorPosition.x)
1280 .rawYCursorPosition(cursorPosition.y)
1281 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1282 .x(position.x)
1283 .y(position.y))
1284 .build();
Arthur Hungb92218b2018-08-14 12:00:21 +08001285
1286 // Inject event until dispatch out.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001287 return injectMotionEvent(dispatcher, event, injectionTimeout, injectionMode);
Arthur Hungb92218b2018-08-14 12:00:21 +08001288}
1289
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001290static InputEventInjectionResult injectMotionDown(const sp<InputDispatcher>& dispatcher,
1291 int32_t source, int32_t displayId,
1292 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001293 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001294}
1295
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001296static InputEventInjectionResult injectMotionUp(const sp<InputDispatcher>& dispatcher,
1297 int32_t source, int32_t displayId,
1298 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001299 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001300}
1301
Jackal Guof9696682018-10-05 12:23:23 +08001302static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1303 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1304 // Define a valid key event.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001305 NotifyKeyArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1306 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A,
1307 KEY_A, AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001308
1309 return args;
1310}
1311
chaviwd1c23182019-12-20 18:44:56 -08001312static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId,
1313 const std::vector<PointF>& points) {
1314 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001315 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1316 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1317 }
1318
chaviwd1c23182019-12-20 18:44:56 -08001319 PointerProperties pointerProperties[pointerCount];
1320 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001321
chaviwd1c23182019-12-20 18:44:56 -08001322 for (size_t i = 0; i < pointerCount; i++) {
1323 pointerProperties[i].clear();
1324 pointerProperties[i].id = i;
1325 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001326
chaviwd1c23182019-12-20 18:44:56 -08001327 pointerCoords[i].clear();
1328 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1329 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1330 }
Jackal Guof9696682018-10-05 12:23:23 +08001331
1332 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1333 // Define a valid motion event.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001334 NotifyMotionArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001335 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1336 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001337 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1338 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001339 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1340 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001341
1342 return args;
1343}
1344
chaviwd1c23182019-12-20 18:44:56 -08001345static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1346 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1347}
1348
Prabir Pradhan99987712020-11-10 18:43:05 -08001349static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(bool enabled) {
1350 return NotifyPointerCaptureChangedArgs(/* id */ 0, systemTime(SYSTEM_TIME_MONOTONIC), enabled);
1351}
1352
Arthur Hungb92218b2018-08-14 12:00:21 +08001353TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001354 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001355 sp<FakeWindowHandle> window =
1356 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001357
Arthur Hung72d8dc32020-03-28 00:48:39 +00001358 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001359 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1360 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1361 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001362
1363 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001364 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001365}
1366
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001367/**
1368 * Calling setInputWindows once with FLAG_NOT_TOUCH_MODAL should not cause any issues.
1369 * To ensure that window receives only events that were directly inside of it, add
1370 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1371 * when finding touched windows.
1372 * This test serves as a sanity check for the next test, where setInputWindows is
1373 * called twice.
1374 */
1375TEST_F(InputDispatcherTest, SetInputWindowOnce_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001376 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001377 sp<FakeWindowHandle> window =
1378 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1379 window->setFrame(Rect(0, 0, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01001380 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001381
1382 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001383 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001384 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1385 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001386 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001387
1388 // Window should receive motion event.
1389 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1390}
1391
1392/**
1393 * Calling setInputWindows twice, with the same info, should not cause any issues.
1394 * To ensure that window receives only events that were directly inside of it, add
1395 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1396 * when finding touched windows.
1397 */
1398TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001399 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001400 sp<FakeWindowHandle> window =
1401 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1402 window->setFrame(Rect(0, 0, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01001403 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001404
1405 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1406 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001407 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001408 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1409 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001410 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001411
1412 // Window should receive motion event.
1413 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1414}
1415
Arthur Hungb92218b2018-08-14 12:00:21 +08001416// The foreground window should receive the first touch down event.
1417TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001418 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001419 sp<FakeWindowHandle> windowTop =
1420 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
1421 sp<FakeWindowHandle> windowSecond =
1422 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001423
Arthur Hung72d8dc32020-03-28 00:48:39 +00001424 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001425 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1426 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1427 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001428
1429 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001430 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001431 windowSecond->assertNoEvents();
1432}
1433
Garfield Tandf26e862020-07-01 20:18:19 -07001434TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07001435 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07001436 sp<FakeWindowHandle> windowLeft =
1437 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1438 windowLeft->setFrame(Rect(0, 0, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001439 windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001440 sp<FakeWindowHandle> windowRight =
1441 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1442 windowRight->setFrame(Rect(600, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001443 windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001444
1445 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1446
1447 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
1448
1449 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001450 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001451 injectMotionEvent(mDispatcher,
1452 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1453 AINPUT_SOURCE_MOUSE)
1454 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1455 .x(900)
1456 .y(400))
1457 .build()));
1458 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1459 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1460 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1461 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1462
1463 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001464 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001465 injectMotionEvent(mDispatcher,
1466 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1467 AINPUT_SOURCE_MOUSE)
1468 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1469 .x(300)
1470 .y(400))
1471 .build()));
1472 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1473 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1474 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1475 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1476 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1477 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1478
1479 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001480 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001481 injectMotionEvent(mDispatcher,
1482 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1483 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1484 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1485 .x(300)
1486 .y(400))
1487 .build()));
1488 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1489
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001490 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001491 injectMotionEvent(mDispatcher,
1492 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
1493 AINPUT_SOURCE_MOUSE)
1494 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1495 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1496 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1497 .x(300)
1498 .y(400))
1499 .build()));
1500 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1501 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1502
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001503 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001504 injectMotionEvent(mDispatcher,
1505 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1506 AINPUT_SOURCE_MOUSE)
1507 .buttonState(0)
1508 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1509 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1510 .x(300)
1511 .y(400))
1512 .build()));
1513 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1514 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1515
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_UP, AINPUT_SOURCE_MOUSE)
1519 .buttonState(0)
1520 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1521 .x(300)
1522 .y(400))
1523 .build()));
1524 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1525
1526 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001527 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001528 injectMotionEvent(mDispatcher,
1529 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1530 AINPUT_SOURCE_MOUSE)
1531 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1532 .x(900)
1533 .y(400))
1534 .build()));
1535 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1536 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1537 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1538 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1539 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1540 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1541}
1542
1543// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
1544// directly in this test.
1545TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07001546 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07001547 sp<FakeWindowHandle> window =
1548 new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1549 window->setFrame(Rect(0, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001550 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001551
1552 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1553
1554 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1555
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001556 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001557 injectMotionEvent(mDispatcher,
1558 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
1559 AINPUT_SOURCE_MOUSE)
1560 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1561 .x(300)
1562 .y(400))
1563 .build()));
1564 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1565 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1566
1567 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001568 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001569 injectMotionEvent(mDispatcher,
1570 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1571 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1572 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1573 .x(300)
1574 .y(400))
1575 .build()));
1576 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1577
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001578 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001579 injectMotionEvent(mDispatcher,
1580 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
1581 AINPUT_SOURCE_MOUSE)
1582 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1583 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1584 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1585 .x(300)
1586 .y(400))
1587 .build()));
1588 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1589 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1590
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001591 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001592 injectMotionEvent(mDispatcher,
1593 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1594 AINPUT_SOURCE_MOUSE)
1595 .buttonState(0)
1596 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1597 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1598 .x(300)
1599 .y(400))
1600 .build()));
1601 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1602 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1603
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_UP, AINPUT_SOURCE_MOUSE)
1607 .buttonState(0)
1608 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1609 .x(300)
1610 .y(400))
1611 .build()));
1612 window->consumeMotionUp(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_HOVER_EXIT,
1617 AINPUT_SOURCE_MOUSE)
1618 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1619 .x(300)
1620 .y(400))
1621 .build()));
1622 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1623 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1624}
1625
Garfield Tan00f511d2019-06-12 16:55:40 -07001626TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07001627 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07001628
1629 sp<FakeWindowHandle> windowLeft =
1630 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1631 windowLeft->setFrame(Rect(0, 0, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001632 windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07001633 sp<FakeWindowHandle> windowRight =
1634 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1635 windowRight->setFrame(Rect(600, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001636 windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07001637
1638 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1639
Arthur Hung72d8dc32020-03-28 00:48:39 +00001640 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07001641
1642 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
1643 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001644 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07001645 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001646 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001647 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07001648 windowRight->assertNoEvents();
1649}
1650
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001651TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001652 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001653 sp<FakeWindowHandle> window =
1654 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07001655 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001656
Arthur Hung72d8dc32020-03-28 00:48:39 +00001657 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001658 setFocusedWindow(window);
1659
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001660 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001661
1662 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1663 mDispatcher->notifyKey(&keyArgs);
1664
1665 // Window should receive key down event.
1666 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1667
1668 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
1669 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001670 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001671 mDispatcher->notifyDeviceReset(&args);
1672 window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
1673 AKEY_EVENT_FLAG_CANCELED);
1674}
1675
1676TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001677 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001678 sp<FakeWindowHandle> window =
1679 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1680
Arthur Hung72d8dc32020-03-28 00:48:39 +00001681 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001682
1683 NotifyMotionArgs motionArgs =
1684 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1685 ADISPLAY_ID_DEFAULT);
1686 mDispatcher->notifyMotion(&motionArgs);
1687
1688 // Window should receive motion down event.
1689 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1690
1691 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
1692 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001693 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001694 mDispatcher->notifyDeviceReset(&args);
1695 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT,
1696 0 /*expectedFlags*/);
1697}
1698
Svet Ganov5d3bc372020-01-26 23:11:07 -08001699TEST_F(InputDispatcherTest, TransferTouchFocus_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07001700 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001701
1702 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001703 sp<FakeWindowHandle> firstWindow =
1704 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1705 sp<FakeWindowHandle> secondWindow =
1706 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001707
1708 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001709 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001710
1711 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001712 NotifyMotionArgs downMotionArgs =
1713 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1714 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001715 mDispatcher->notifyMotion(&downMotionArgs);
1716 // Only the first window should get the down event
1717 firstWindow->consumeMotionDown();
1718 secondWindow->assertNoEvents();
1719
1720 // Transfer touch focus to the second window
1721 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1722 // The first window gets cancel and the second gets down
1723 firstWindow->consumeMotionCancel();
1724 secondWindow->consumeMotionDown();
1725
1726 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001727 NotifyMotionArgs upMotionArgs =
1728 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1729 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001730 mDispatcher->notifyMotion(&upMotionArgs);
1731 // The first window gets no events and the second gets up
1732 firstWindow->assertNoEvents();
1733 secondWindow->consumeMotionUp();
1734}
1735
1736TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointerNoSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001737 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001738
1739 PointF touchPoint = {10, 10};
1740
1741 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001742 sp<FakeWindowHandle> firstWindow =
1743 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1744 sp<FakeWindowHandle> secondWindow =
1745 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001746
1747 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001748 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001749
1750 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001751 NotifyMotionArgs downMotionArgs =
1752 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1753 ADISPLAY_ID_DEFAULT, {touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001754 mDispatcher->notifyMotion(&downMotionArgs);
1755 // Only the first window should get the down event
1756 firstWindow->consumeMotionDown();
1757 secondWindow->assertNoEvents();
1758
1759 // Send pointer down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001760 NotifyMotionArgs pointerDownMotionArgs =
1761 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1762 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1763 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1764 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001765 mDispatcher->notifyMotion(&pointerDownMotionArgs);
1766 // Only the first window should get the pointer down event
1767 firstWindow->consumeMotionPointerDown(1);
1768 secondWindow->assertNoEvents();
1769
1770 // Transfer touch focus to the second window
1771 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1772 // The first window gets cancel and the second gets down and pointer down
1773 firstWindow->consumeMotionCancel();
1774 secondWindow->consumeMotionDown();
1775 secondWindow->consumeMotionPointerDown(1);
1776
1777 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001778 NotifyMotionArgs pointerUpMotionArgs =
1779 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1780 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1781 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1782 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001783 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1784 // The first window gets nothing and the second gets pointer up
1785 firstWindow->assertNoEvents();
1786 secondWindow->consumeMotionPointerUp(1);
1787
1788 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001789 NotifyMotionArgs upMotionArgs =
1790 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1791 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001792 mDispatcher->notifyMotion(&upMotionArgs);
1793 // The first window gets nothing and the second gets up
1794 firstWindow->assertNoEvents();
1795 secondWindow->consumeMotionUp();
1796}
1797
1798TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001799 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001800
1801 // Create a non touch modal window that supports split touch
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001802 sp<FakeWindowHandle> firstWindow =
1803 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001804 firstWindow->setFrame(Rect(0, 0, 600, 400));
Michael Wright44753b12020-07-08 13:48:11 +01001805 firstWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1806 InputWindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001807
1808 // Create a non touch modal window that supports split touch
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001809 sp<FakeWindowHandle> secondWindow =
1810 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001811 secondWindow->setFrame(Rect(0, 400, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001812 secondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1813 InputWindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001814
1815 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001816 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001817
1818 PointF pointInFirst = {300, 200};
1819 PointF pointInSecond = {300, 600};
1820
1821 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001822 NotifyMotionArgs firstDownMotionArgs =
1823 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1824 ADISPLAY_ID_DEFAULT, {pointInFirst});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001825 mDispatcher->notifyMotion(&firstDownMotionArgs);
1826 // Only the first window should get the down event
1827 firstWindow->consumeMotionDown();
1828 secondWindow->assertNoEvents();
1829
1830 // Send down to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001831 NotifyMotionArgs secondDownMotionArgs =
1832 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1833 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1834 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1835 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001836 mDispatcher->notifyMotion(&secondDownMotionArgs);
1837 // The first window gets a move and the second a down
1838 firstWindow->consumeMotionMove();
1839 secondWindow->consumeMotionDown();
1840
1841 // Transfer touch focus to the second window
1842 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1843 // The first window gets cancel and the new gets pointer down (it already saw down)
1844 firstWindow->consumeMotionCancel();
1845 secondWindow->consumeMotionPointerDown(1);
1846
1847 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001848 NotifyMotionArgs pointerUpMotionArgs =
1849 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1850 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1851 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1852 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001853 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1854 // The first window gets nothing and the second gets pointer up
1855 firstWindow->assertNoEvents();
1856 secondWindow->consumeMotionPointerUp(1);
1857
1858 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001859 NotifyMotionArgs upMotionArgs =
1860 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1861 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001862 mDispatcher->notifyMotion(&upMotionArgs);
1863 // The first window gets nothing and the second gets up
1864 firstWindow->assertNoEvents();
1865 secondWindow->consumeMotionUp();
1866}
1867
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001868TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07001869 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001870 sp<FakeWindowHandle> window =
1871 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1872
Vishnu Nair47074b82020-08-14 11:54:47 -07001873 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001874 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001875 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001876
1877 window->consumeFocusEvent(true);
1878
1879 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1880 mDispatcher->notifyKey(&keyArgs);
1881
1882 // Window should receive key down event.
1883 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1884}
1885
1886TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07001887 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001888 sp<FakeWindowHandle> window =
1889 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1890
Arthur Hung72d8dc32020-03-28 00:48:39 +00001891 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001892
1893 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1894 mDispatcher->notifyKey(&keyArgs);
1895 mDispatcher->waitForIdle();
1896
1897 window->assertNoEvents();
1898}
1899
1900// If a window is touchable, but does not have focus, it should receive motion events, but not keys
1901TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07001902 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001903 sp<FakeWindowHandle> window =
1904 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1905
Arthur Hung72d8dc32020-03-28 00:48:39 +00001906 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001907
1908 // Send key
1909 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1910 mDispatcher->notifyKey(&keyArgs);
1911 // Send motion
1912 NotifyMotionArgs motionArgs =
1913 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1914 ADISPLAY_ID_DEFAULT);
1915 mDispatcher->notifyMotion(&motionArgs);
1916
1917 // Window should receive only the motion event
1918 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1919 window->assertNoEvents(); // Key event or focus event will not be received
1920}
1921
arthurhungea3f4fc2020-12-21 23:18:53 +08001922TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
1923 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1924
1925 // Create first non touch modal window that supports split touch
1926 sp<FakeWindowHandle> firstWindow =
1927 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1928 firstWindow->setFrame(Rect(0, 0, 600, 400));
1929 firstWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1930 InputWindowInfo::Flag::SPLIT_TOUCH);
1931
1932 // Create second non touch modal window that supports split touch
1933 sp<FakeWindowHandle> secondWindow =
1934 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
1935 secondWindow->setFrame(Rect(0, 400, 600, 800));
1936 secondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1937 InputWindowInfo::Flag::SPLIT_TOUCH);
1938
1939 // Add the windows to the dispatcher
1940 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
1941
1942 PointF pointInFirst = {300, 200};
1943 PointF pointInSecond = {300, 600};
1944
1945 // Send down to the first window
1946 NotifyMotionArgs firstDownMotionArgs =
1947 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1948 ADISPLAY_ID_DEFAULT, {pointInFirst});
1949 mDispatcher->notifyMotion(&firstDownMotionArgs);
1950 // Only the first window should get the down event
1951 firstWindow->consumeMotionDown();
1952 secondWindow->assertNoEvents();
1953
1954 // Send down to the second window
1955 NotifyMotionArgs secondDownMotionArgs =
1956 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1957 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1958 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1959 {pointInFirst, pointInSecond});
1960 mDispatcher->notifyMotion(&secondDownMotionArgs);
1961 // The first window gets a move and the second a down
1962 firstWindow->consumeMotionMove();
1963 secondWindow->consumeMotionDown();
1964
1965 // Send pointer cancel to the second window
1966 NotifyMotionArgs pointerUpMotionArgs =
1967 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1968 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1969 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1970 {pointInFirst, pointInSecond});
1971 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
1972 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1973 // The first window gets move and the second gets cancel.
1974 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
1975 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
1976
1977 // Send up event.
1978 NotifyMotionArgs upMotionArgs =
1979 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1980 ADISPLAY_ID_DEFAULT);
1981 mDispatcher->notifyMotion(&upMotionArgs);
1982 // The first window gets up and the second gets nothing.
1983 firstWindow->consumeMotionUp();
1984 secondWindow->assertNoEvents();
1985}
1986
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001987TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
1988 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1989
1990 sp<FakeWindowHandle> window =
1991 new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1992 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1993 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
1994 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
1995 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
1996
1997 window->sendTimeline(1 /*inputEventId*/, graphicsTimeline);
1998 window->assertNoEvents();
1999 mDispatcher->waitForIdle();
2000}
2001
chaviwd1c23182019-12-20 18:44:56 -08002002class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00002003public:
2004 FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name,
chaviwd1c23182019-12-20 18:44:56 -08002005 int32_t displayId, bool isGestureMonitor = false) {
Garfield Tan15601662020-09-22 15:32:38 -07002006 base::Result<std::unique_ptr<InputChannel>> channel =
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +00002007 dispatcher->createInputMonitor(displayId, isGestureMonitor, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07002008 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00002009 }
2010
chaviwd1c23182019-12-20 18:44:56 -08002011 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
2012
2013 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
2014 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN,
2015 expectedDisplayId, expectedFlags);
2016 }
2017
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002018 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
2019
2020 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
2021
chaviwd1c23182019-12-20 18:44:56 -08002022 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
2023 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
2024 expectedDisplayId, expectedFlags);
2025 }
2026
2027 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
2028 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP,
2029 expectedDisplayId, expectedFlags);
2030 }
2031
2032 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
2033
2034private:
2035 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00002036};
2037
2038// Tests for gesture monitors
2039TEST_F(InputDispatcherTest, GestureMonitor_ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07002040 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00002041 sp<FakeWindowHandle> window =
2042 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002043 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00002044
chaviwd1c23182019-12-20 18:44:56 -08002045 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
2046 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00002047
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002048 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00002049 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002050 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00002051 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002052 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00002053}
2054
2055TEST_F(InputDispatcherTest, GestureMonitor_DoesNotReceiveKeyEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07002056 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00002057 sp<FakeWindowHandle> window =
2058 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2059
2060 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002061 window->setFocusable(true);
Michael Wright3a240c42019-12-10 20:53:41 +00002062
Arthur Hung72d8dc32020-03-28 00:48:39 +00002063 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002064 setFocusedWindow(window);
2065
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002066 window->consumeFocusEvent(true);
Michael Wright3a240c42019-12-10 20:53:41 +00002067
chaviwd1c23182019-12-20 18:44:56 -08002068 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
2069 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00002070
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002071 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2072 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00002073 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002074 monitor.assertNoEvents();
Michael Wright3a240c42019-12-10 20:53:41 +00002075}
2076
2077TEST_F(InputDispatcherTest, GestureMonitor_CanPilferAfterWindowIsRemovedMidStream) {
Chris Yea209fde2020-07-22 13:54:51 -07002078 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00002079 sp<FakeWindowHandle> window =
2080 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002081 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00002082
chaviwd1c23182019-12-20 18:44:56 -08002083 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
2084 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00002085
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002086 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00002087 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002088 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00002089 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002090 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00002091
2092 window->releaseChannel();
2093
chaviwd1c23182019-12-20 18:44:56 -08002094 mDispatcher->pilferPointers(monitor.getToken());
Michael Wright3a240c42019-12-10 20:53:41 +00002095
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002096 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00002097 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002098 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08002099 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00002100}
2101
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002102TEST_F(InputDispatcherTest, UnresponsiveGestureMonitor_GetsAnr) {
2103 FakeMonitorReceiver monitor =
2104 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
2105 true /*isGestureMonitor*/);
2106
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002107 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002108 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
2109 std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
2110 ASSERT_TRUE(consumeSeq);
2111
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00002112 mFakePolicy->assertNotifyMonitorUnresponsiveWasCalled(DISPATCHING_TIMEOUT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002113 monitor.finishEvent(*consumeSeq);
2114 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00002115 mFakePolicy->assertNotifyMonitorResponsiveWasCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002116}
2117
chaviw81e2bb92019-12-18 15:03:51 -08002118TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002119 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
chaviw81e2bb92019-12-18 15:03:51 -08002120 sp<FakeWindowHandle> window =
2121 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2122
Arthur Hung72d8dc32020-03-28 00:48:39 +00002123 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08002124
2125 NotifyMotionArgs motionArgs =
2126 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2127 ADISPLAY_ID_DEFAULT);
2128
2129 mDispatcher->notifyMotion(&motionArgs);
2130 // Window should receive motion down event.
2131 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2132
2133 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002134 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08002135 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
2136 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
2137 motionArgs.pointerCoords[0].getX() - 10);
2138
2139 mDispatcher->notifyMotion(&motionArgs);
2140 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
2141 0 /*expectedFlags*/);
2142}
2143
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002144/**
2145 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
2146 * the device default right away. In the test scenario, we check both the default value,
2147 * and the action of enabling / disabling.
2148 */
2149TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07002150 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002151 sp<FakeWindowHandle> window =
2152 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2153
2154 // Set focused application.
2155 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002156 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002157
2158 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00002159 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002160 setFocusedWindow(window);
2161
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002162 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2163
2164 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07002165 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002166 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002167 window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/);
2168
2169 SCOPED_TRACE("Disable touch mode");
2170 mDispatcher->setInTouchMode(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07002171 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002172 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002173 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002174 window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/);
2175
2176 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07002177 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002178 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002179 window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/);
2180
2181 SCOPED_TRACE("Enable touch mode again");
2182 mDispatcher->setInTouchMode(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07002183 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002184 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002185 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002186 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2187
2188 window->assertNoEvents();
2189}
2190
Gang Wange9087892020-01-07 12:17:14 -05002191TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002192 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Gang Wange9087892020-01-07 12:17:14 -05002193 sp<FakeWindowHandle> window =
2194 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2195
2196 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002197 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05002198
Arthur Hung72d8dc32020-03-28 00:48:39 +00002199 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002200 setFocusedWindow(window);
2201
Gang Wange9087892020-01-07 12:17:14 -05002202 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2203
2204 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
2205 mDispatcher->notifyKey(&keyArgs);
2206
2207 InputEvent* event = window->consume();
2208 ASSERT_NE(event, nullptr);
2209
2210 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
2211 ASSERT_NE(verified, nullptr);
2212 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
2213
2214 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
2215 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
2216 ASSERT_EQ(keyArgs.source, verified->source);
2217 ASSERT_EQ(keyArgs.displayId, verified->displayId);
2218
2219 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
2220
2221 ASSERT_EQ(keyArgs.action, verifiedKey.action);
2222 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05002223 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
2224 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
2225 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
2226 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
2227 ASSERT_EQ(0, verifiedKey.repeatCount);
2228}
2229
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002230TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002231 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002232 sp<FakeWindowHandle> window =
2233 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2234
2235 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2236
Arthur Hung72d8dc32020-03-28 00:48:39 +00002237 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002238
2239 NotifyMotionArgs motionArgs =
2240 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2241 ADISPLAY_ID_DEFAULT);
2242 mDispatcher->notifyMotion(&motionArgs);
2243
2244 InputEvent* event = window->consume();
2245 ASSERT_NE(event, nullptr);
2246
2247 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
2248 ASSERT_NE(verified, nullptr);
2249 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
2250
2251 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
2252 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
2253 EXPECT_EQ(motionArgs.source, verified->source);
2254 EXPECT_EQ(motionArgs.displayId, verified->displayId);
2255
2256 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
2257
2258 EXPECT_EQ(motionArgs.pointerCoords[0].getX(), verifiedMotion.rawX);
2259 EXPECT_EQ(motionArgs.pointerCoords[0].getY(), verifiedMotion.rawY);
2260 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
2261 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
2262 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
2263 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
2264 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
2265}
2266
Prabir Pradhanbd527712021-03-09 19:17:09 -08002267TEST_F(InputDispatcherTest, NonPointerMotionEvent_NotTransformed) {
yunho.shinf4a80b82020-11-16 21:13:57 +09002268 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2269 sp<FakeWindowHandle> window =
2270 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2271 const std::string name = window->getName();
2272
2273 // Window gets transformed by offset values.
2274 window->setWindowOffset(500.0f, 500.0f);
2275
2276 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2277 window->setFocusable(true);
2278
2279 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2280
2281 // First, we set focused window so that focusedWindowHandle is not null.
2282 setFocusedWindow(window);
2283
2284 // Second, we consume focus event if it is right or wrong according to onFocusChangedLocked.
2285 window->consumeFocusEvent(true);
2286
Prabir Pradhanbd527712021-03-09 19:17:09 -08002287 constexpr const std::array nonPointerSources = {AINPUT_SOURCE_TRACKBALL,
2288 AINPUT_SOURCE_MOUSE_RELATIVE,
2289 AINPUT_SOURCE_JOYSTICK};
2290 for (const int source : nonPointerSources) {
2291 // Notify motion with a non-pointer source.
2292 NotifyMotionArgs motionArgs =
2293 generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, source, ADISPLAY_ID_DEFAULT);
2294 mDispatcher->notifyMotion(&motionArgs);
yunho.shinf4a80b82020-11-16 21:13:57 +09002295
Prabir Pradhanbd527712021-03-09 19:17:09 -08002296 InputEvent* event = window->consume();
2297 ASSERT_NE(event, nullptr);
2298 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
2299 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
2300 << " event, got " << inputEventTypeToString(event->getType()) << " event";
yunho.shinf4a80b82020-11-16 21:13:57 +09002301
Prabir Pradhanbd527712021-03-09 19:17:09 -08002302 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
2303 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, motionEvent.getAction());
2304 EXPECT_EQ(motionArgs.pointerCount, motionEvent.getPointerCount());
yunho.shinf4a80b82020-11-16 21:13:57 +09002305
Prabir Pradhanbd527712021-03-09 19:17:09 -08002306 float expectedX = motionArgs.pointerCoords[0].getX();
2307 float expectedY = motionArgs.pointerCoords[0].getY();
yunho.shinf4a80b82020-11-16 21:13:57 +09002308
Prabir Pradhanbd527712021-03-09 19:17:09 -08002309 // Ensure the axis values from the final motion event are not transformed.
2310 EXPECT_EQ(expectedX, motionEvent.getX(0))
2311 << "expected " << expectedX << " for x coord of " << name.c_str() << ", got "
2312 << motionEvent.getX(0);
2313 EXPECT_EQ(expectedY, motionEvent.getY(0))
2314 << "expected " << expectedY << " for y coord of " << name.c_str() << ", got "
2315 << motionEvent.getY(0);
2316 // Ensure the raw and transformed axis values for the motion event are the same.
2317 EXPECT_EQ(motionEvent.getRawX(0), motionEvent.getX(0))
2318 << "expected raw and transformed X-axis values to be equal";
2319 EXPECT_EQ(motionEvent.getRawY(0), motionEvent.getY(0))
2320 << "expected raw and transformed Y-axis values to be equal";
2321 }
yunho.shinf4a80b82020-11-16 21:13:57 +09002322}
2323
chaviw09c8d2d2020-08-24 15:48:26 -07002324/**
2325 * Ensure that separate calls to sign the same data are generating the same key.
2326 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
2327 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
2328 * tests.
2329 */
2330TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
2331 KeyEvent event = getTestKeyEvent();
2332 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
2333
2334 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
2335 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
2336 ASSERT_EQ(hmac1, hmac2);
2337}
2338
2339/**
2340 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
2341 */
2342TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
2343 KeyEvent event = getTestKeyEvent();
2344 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
2345 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
2346
2347 verifiedEvent.deviceId += 1;
2348 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2349
2350 verifiedEvent.source += 1;
2351 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2352
2353 verifiedEvent.eventTimeNanos += 1;
2354 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2355
2356 verifiedEvent.displayId += 1;
2357 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2358
2359 verifiedEvent.action += 1;
2360 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2361
2362 verifiedEvent.downTimeNanos += 1;
2363 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2364
2365 verifiedEvent.flags += 1;
2366 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2367
2368 verifiedEvent.keyCode += 1;
2369 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2370
2371 verifiedEvent.scanCode += 1;
2372 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2373
2374 verifiedEvent.metaState += 1;
2375 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2376
2377 verifiedEvent.repeatCount += 1;
2378 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2379}
2380
Vishnu Nair958da932020-08-21 17:12:37 -07002381TEST_F(InputDispatcherTest, SetFocusedWindow) {
2382 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2383 sp<FakeWindowHandle> windowTop =
2384 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2385 sp<FakeWindowHandle> windowSecond =
2386 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2387 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2388
2389 // Top window is also focusable but is not granted focus.
2390 windowTop->setFocusable(true);
2391 windowSecond->setFocusable(true);
2392 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2393 setFocusedWindow(windowSecond);
2394
2395 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002396 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2397 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002398
2399 // Focused window should receive event.
2400 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
2401 windowTop->assertNoEvents();
2402}
2403
2404TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
2405 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2406 sp<FakeWindowHandle> window =
2407 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2408 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2409
2410 window->setFocusable(true);
2411 // Release channel for window is no longer valid.
2412 window->releaseChannel();
2413 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2414 setFocusedWindow(window);
2415
2416 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002417 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2418 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002419
2420 // window channel is invalid, so it should not receive any input event.
2421 window->assertNoEvents();
2422}
2423
2424TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
2425 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2426 sp<FakeWindowHandle> window =
2427 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2428 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2429
2430 // Window is not focusable.
2431 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2432 setFocusedWindow(window);
2433
2434 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002435 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2436 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002437
2438 // window is invalid, so it should not receive any input event.
2439 window->assertNoEvents();
2440}
2441
2442TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
2443 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2444 sp<FakeWindowHandle> windowTop =
2445 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2446 sp<FakeWindowHandle> windowSecond =
2447 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2448 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2449
2450 windowTop->setFocusable(true);
2451 windowSecond->setFocusable(true);
2452 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2453 setFocusedWindow(windowTop);
2454 windowTop->consumeFocusEvent(true);
2455
2456 setFocusedWindow(windowSecond, windowTop);
2457 windowSecond->consumeFocusEvent(true);
2458 windowTop->consumeFocusEvent(false);
2459
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002460 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2461 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002462
2463 // Focused window should receive event.
2464 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
2465}
2466
2467TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) {
2468 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2469 sp<FakeWindowHandle> windowTop =
2470 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2471 sp<FakeWindowHandle> windowSecond =
2472 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2473 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2474
2475 windowTop->setFocusable(true);
2476 windowSecond->setFocusable(true);
2477 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2478 setFocusedWindow(windowSecond, windowTop);
2479
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002480 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2481 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002482
2483 // Event should be dropped.
2484 windowTop->assertNoEvents();
2485 windowSecond->assertNoEvents();
2486}
2487
2488TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
2489 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2490 sp<FakeWindowHandle> window =
2491 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2492 sp<FakeWindowHandle> previousFocusedWindow =
2493 new FakeWindowHandle(application, mDispatcher, "previousFocusedWindow",
2494 ADISPLAY_ID_DEFAULT);
2495 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2496
2497 window->setFocusable(true);
2498 previousFocusedWindow->setFocusable(true);
2499 window->setVisible(false);
2500 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
2501 setFocusedWindow(previousFocusedWindow);
2502 previousFocusedWindow->consumeFocusEvent(true);
2503
2504 // Requesting focus on invisible window takes focus from currently focused window.
2505 setFocusedWindow(window);
2506 previousFocusedWindow->consumeFocusEvent(false);
2507
2508 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002509 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07002510 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002511 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07002512
2513 // Window does not get focus event or key down.
2514 window->assertNoEvents();
2515
2516 // Window becomes visible.
2517 window->setVisible(true);
2518 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2519
2520 // Window receives focus event.
2521 window->consumeFocusEvent(true);
2522 // Focused window receives key down.
2523 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2524}
2525
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002526/**
2527 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
2528 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
2529 * of the 'slipperyEnterWindow'.
2530 *
2531 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
2532 * a way so that the touched location is no longer covered by the top window.
2533 *
2534 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
2535 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
2536 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
2537 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
2538 * with ACTION_DOWN).
2539 * Thus, the touch has been transferred from the top window into the bottom window, because the top
2540 * window moved itself away from the touched location and had Flag::SLIPPERY.
2541 *
2542 * Even though the top window moved away from the touched location, it is still obscuring the bottom
2543 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
2544 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
2545 *
2546 * In this test, we ensure that the event received by the bottom window has
2547 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
2548 */
2549TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
2550 constexpr int32_t SLIPPERY_PID = INJECTOR_PID + 1;
2551 constexpr int32_t SLIPPERY_UID = INJECTOR_UID + 1;
2552
2553 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2554 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2555
2556 sp<FakeWindowHandle> slipperyExitWindow =
2557 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2558 slipperyExitWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
2559 InputWindowInfo::Flag::SLIPPERY);
2560 // Make sure this one overlaps the bottom window
2561 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
2562 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
2563 // one. Windows with the same owner are not considered to be occluding each other.
2564 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
2565
2566 sp<FakeWindowHandle> slipperyEnterWindow =
2567 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2568 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
2569
2570 mDispatcher->setInputWindows(
2571 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
2572
2573 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
2574 NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2575 ADISPLAY_ID_DEFAULT, {{50, 50}});
2576 mDispatcher->notifyMotion(&args);
2577 slipperyExitWindow->consumeMotionDown();
2578 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
2579 mDispatcher->setInputWindows(
2580 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
2581
2582 args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2583 ADISPLAY_ID_DEFAULT, {{51, 51}});
2584 mDispatcher->notifyMotion(&args);
2585
2586 slipperyExitWindow->consumeMotionCancel();
2587
2588 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
2589 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
2590}
2591
Garfield Tan1c7bc862020-01-28 13:24:04 -08002592class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
2593protected:
2594 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
2595 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
2596
Chris Yea209fde2020-07-22 13:54:51 -07002597 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002598 sp<FakeWindowHandle> mWindow;
2599
2600 virtual void SetUp() override {
2601 mFakePolicy = new FakeInputDispatcherPolicy();
2602 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
2603 mDispatcher = new InputDispatcher(mFakePolicy);
2604 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
2605 ASSERT_EQ(OK, mDispatcher->start());
2606
2607 setUpWindow();
2608 }
2609
2610 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07002611 mApp = std::make_shared<FakeApplicationHandle>();
Garfield Tan1c7bc862020-01-28 13:24:04 -08002612 mWindow = new FakeWindowHandle(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2613
Vishnu Nair47074b82020-08-14 11:54:47 -07002614 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002615 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002616 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002617 mWindow->consumeFocusEvent(true);
2618 }
2619
Chris Ye2ad95392020-09-01 13:44:44 -07002620 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08002621 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07002622 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002623 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
2624 mDispatcher->notifyKey(&keyArgs);
2625
2626 // Window should receive key down event.
2627 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2628 }
2629
2630 void expectKeyRepeatOnce(int32_t repeatCount) {
2631 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
2632 InputEvent* repeatEvent = mWindow->consume();
2633 ASSERT_NE(nullptr, repeatEvent);
2634
2635 uint32_t eventType = repeatEvent->getType();
2636 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType);
2637
2638 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
2639 uint32_t eventAction = repeatKeyEvent->getAction();
2640 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
2641 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
2642 }
2643
Chris Ye2ad95392020-09-01 13:44:44 -07002644 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08002645 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07002646 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002647 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
2648 mDispatcher->notifyKey(&keyArgs);
2649
2650 // Window should receive key down event.
2651 mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
2652 0 /*expectedFlags*/);
2653 }
2654};
2655
2656TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Chris Ye2ad95392020-09-01 13:44:44 -07002657 sendAndConsumeKeyDown(1 /* deviceId */);
2658 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2659 expectKeyRepeatOnce(repeatCount);
2660 }
2661}
2662
2663TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
2664 sendAndConsumeKeyDown(1 /* deviceId */);
2665 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2666 expectKeyRepeatOnce(repeatCount);
2667 }
2668 sendAndConsumeKeyDown(2 /* deviceId */);
2669 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08002670 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2671 expectKeyRepeatOnce(repeatCount);
2672 }
2673}
2674
2675TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Chris Ye2ad95392020-09-01 13:44:44 -07002676 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002677 expectKeyRepeatOnce(1 /*repeatCount*/);
Chris Ye2ad95392020-09-01 13:44:44 -07002678 sendAndConsumeKeyUp(1 /* deviceId */);
2679 mWindow->assertNoEvents();
2680}
2681
2682TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
2683 sendAndConsumeKeyDown(1 /* deviceId */);
2684 expectKeyRepeatOnce(1 /*repeatCount*/);
2685 sendAndConsumeKeyDown(2 /* deviceId */);
2686 expectKeyRepeatOnce(1 /*repeatCount*/);
2687 // Stale key up from device 1.
2688 sendAndConsumeKeyUp(1 /* deviceId */);
2689 // Device 2 is still down, keep repeating
2690 expectKeyRepeatOnce(2 /*repeatCount*/);
2691 expectKeyRepeatOnce(3 /*repeatCount*/);
2692 // Device 2 key up
2693 sendAndConsumeKeyUp(2 /* deviceId */);
2694 mWindow->assertNoEvents();
2695}
2696
2697TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
2698 sendAndConsumeKeyDown(1 /* deviceId */);
2699 expectKeyRepeatOnce(1 /*repeatCount*/);
2700 sendAndConsumeKeyDown(2 /* deviceId */);
2701 expectKeyRepeatOnce(1 /*repeatCount*/);
2702 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
2703 sendAndConsumeKeyUp(2 /* deviceId */);
2704 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08002705 mWindow->assertNoEvents();
2706}
2707
2708TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Chris Ye2ad95392020-09-01 13:44:44 -07002709 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002710 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2711 InputEvent* repeatEvent = mWindow->consume();
2712 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2713 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
2714 IdGenerator::getSource(repeatEvent->getId()));
2715 }
2716}
2717
2718TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Chris Ye2ad95392020-09-01 13:44:44 -07002719 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002720
2721 std::unordered_set<int32_t> idSet;
2722 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2723 InputEvent* repeatEvent = mWindow->consume();
2724 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2725 int32_t id = repeatEvent->getId();
2726 EXPECT_EQ(idSet.end(), idSet.find(id));
2727 idSet.insert(id);
2728 }
2729}
2730
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002731/* Test InputDispatcher for MultiDisplay */
2732class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
2733public:
2734 static constexpr int32_t SECOND_DISPLAY_ID = 1;
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002735 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002736 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08002737
Chris Yea209fde2020-07-22 13:54:51 -07002738 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002739 windowInPrimary =
2740 new FakeWindowHandle(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002741
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002742 // Set focus window for primary display, but focused display would be second one.
2743 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07002744 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002745 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002746 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002747 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08002748
Chris Yea209fde2020-07-22 13:54:51 -07002749 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002750 windowInSecondary =
2751 new FakeWindowHandle(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002752 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002753 // Set focus display to second one.
2754 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
2755 // Set focus window for second display.
2756 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07002757 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002758 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002759 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002760 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002761 }
2762
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002763 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002764 InputDispatcherTest::TearDown();
2765
Chris Yea209fde2020-07-22 13:54:51 -07002766 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002767 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07002768 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002769 windowInSecondary.clear();
2770 }
2771
2772protected:
Chris Yea209fde2020-07-22 13:54:51 -07002773 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002774 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07002775 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002776 sp<FakeWindowHandle> windowInSecondary;
2777};
2778
2779TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
2780 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002781 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2782 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2783 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002784 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08002785 windowInSecondary->assertNoEvents();
2786
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002787 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002788 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2789 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
2790 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08002791 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002792 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08002793}
2794
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002795TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08002796 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08002797 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2798 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002799 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002800 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08002801 windowInSecondary->assertNoEvents();
2802
2803 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08002804 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002805 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08002806 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002807 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08002808
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002809 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00002810 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08002811
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002812 // Old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002813 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
2814 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08002815
2816 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08002817 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002818 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08002819 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002820 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08002821 windowInSecondary->assertNoEvents();
2822}
2823
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002824// Test per-display input monitors for motion event.
2825TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08002826 FakeMonitorReceiver monitorInPrimary =
2827 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
2828 FakeMonitorReceiver monitorInSecondary =
2829 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002830
2831 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002832 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2833 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2834 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002835 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002836 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002837 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002838 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002839
2840 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002841 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2842 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
2843 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002844 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002845 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002846 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08002847 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002848
2849 // Test inject a non-pointer motion event.
2850 // If specific a display, it will dispatch to the focused window of particular display,
2851 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002852 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2853 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
2854 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002855 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002856 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002857 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08002858 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002859}
2860
2861// Test per-display input monitors for key event.
2862TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002863 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08002864 FakeMonitorReceiver monitorInPrimary =
2865 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
2866 FakeMonitorReceiver monitorInSecondary =
2867 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002868
2869 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002870 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2871 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002872 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002873 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002874 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08002875 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002876}
2877
Vishnu Nair958da932020-08-21 17:12:37 -07002878TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
2879 sp<FakeWindowHandle> secondWindowInPrimary =
2880 new FakeWindowHandle(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
2881 secondWindowInPrimary->setFocusable(true);
2882 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
2883 setFocusedWindow(secondWindowInPrimary);
2884 windowInPrimary->consumeFocusEvent(false);
2885 secondWindowInPrimary->consumeFocusEvent(true);
2886
2887 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002888 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2889 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002890 windowInPrimary->assertNoEvents();
2891 windowInSecondary->assertNoEvents();
2892 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2893}
2894
Jackal Guof9696682018-10-05 12:23:23 +08002895class InputFilterTest : public InputDispatcherTest {
2896protected:
2897 static constexpr int32_t SECOND_DISPLAY_ID = 1;
2898
2899 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) {
2900 NotifyMotionArgs motionArgs;
2901
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002902 motionArgs =
2903 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08002904 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002905 motionArgs =
2906 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08002907 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002908 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08002909 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08002910 mFakePolicy->assertFilterInputEventWasCalled(motionArgs);
Jackal Guof9696682018-10-05 12:23:23 +08002911 } else {
2912 mFakePolicy->assertFilterInputEventWasNotCalled();
2913 }
2914 }
2915
2916 void testNotifyKey(bool expectToBeFiltered) {
2917 NotifyKeyArgs keyArgs;
2918
2919 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
2920 mDispatcher->notifyKey(&keyArgs);
2921 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
2922 mDispatcher->notifyKey(&keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002923 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08002924
2925 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08002926 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08002927 } else {
2928 mFakePolicy->assertFilterInputEventWasNotCalled();
2929 }
2930 }
2931};
2932
2933// Test InputFilter for MotionEvent
2934TEST_F(InputFilterTest, MotionEvent_InputFilter) {
2935 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
2936 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
2937 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
2938
2939 // Enable InputFilter
2940 mDispatcher->setInputFilterEnabled(true);
2941 // Test touch on both primary and second display, and check if both events are filtered.
2942 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
2943 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
2944
2945 // Disable InputFilter
2946 mDispatcher->setInputFilterEnabled(false);
2947 // Test touch on both primary and second display, and check if both events aren't filtered.
2948 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
2949 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
2950}
2951
2952// Test InputFilter for KeyEvent
2953TEST_F(InputFilterTest, KeyEvent_InputFilter) {
2954 // Since the InputFilter is disabled by default, check if key event aren't filtered.
2955 testNotifyKey(/*expectToBeFiltered*/ false);
2956
2957 // Enable InputFilter
2958 mDispatcher->setInputFilterEnabled(true);
2959 // Send a key event, and check if it is filtered.
2960 testNotifyKey(/*expectToBeFiltered*/ true);
2961
2962 // Disable InputFilter
2963 mDispatcher->setInputFilterEnabled(false);
2964 // Send a key event, and check if it isn't filtered.
2965 testNotifyKey(/*expectToBeFiltered*/ false);
2966}
2967
chaviwfd6d3512019-03-25 13:23:49 -07002968class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002969 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07002970 InputDispatcherTest::SetUp();
2971
Chris Yea209fde2020-07-22 13:54:51 -07002972 std::shared_ptr<FakeApplicationHandle> application =
2973 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002974 mUnfocusedWindow =
2975 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07002976 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
2977 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
2978 // window.
Michael Wright44753b12020-07-08 13:48:11 +01002979 mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07002980
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002981 mFocusedWindow =
2982 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2983 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01002984 mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07002985
2986 // Set focused application.
2987 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002988 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07002989
2990 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00002991 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002992 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002993 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07002994 }
2995
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002996 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07002997 InputDispatcherTest::TearDown();
2998
2999 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003000 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07003001 }
3002
3003protected:
3004 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003005 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003006 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07003007};
3008
3009// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
3010// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
3011// the onPointerDownOutsideFocus callback.
3012TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003013 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003014 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3015 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003016 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003017 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07003018
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003019 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07003020 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
3021}
3022
3023// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
3024// DOWN on the window that doesn't have focus. Ensure no window received the
3025// onPointerDownOutsideFocus callback.
3026TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003027 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003028 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003029 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003030 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07003031
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003032 ASSERT_TRUE(mDispatcher->waitForIdle());
3033 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07003034}
3035
3036// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
3037// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
3038TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003039 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3040 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003041 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003042 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07003043
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003044 ASSERT_TRUE(mDispatcher->waitForIdle());
3045 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07003046}
3047
3048// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
3049// DOWN on the window that already has focus. Ensure no window received the
3050// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003051TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003052 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003053 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003054 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003055 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003056 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07003057
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003058 ASSERT_TRUE(mDispatcher->waitForIdle());
3059 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07003060}
3061
chaviwaf87b3e2019-10-01 16:59:28 -07003062// These tests ensures we can send touch events to a single client when there are multiple input
3063// windows that point to the same client token.
3064class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
3065 virtual void SetUp() override {
3066 InputDispatcherTest::SetUp();
3067
Chris Yea209fde2020-07-22 13:54:51 -07003068 std::shared_ptr<FakeApplicationHandle> application =
3069 std::make_shared<FakeApplicationHandle>();
chaviwaf87b3e2019-10-01 16:59:28 -07003070 mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1",
3071 ADISPLAY_ID_DEFAULT);
3072 // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window.
3073 // We also need FLAG_SPLIT_TOUCH or we won't be able to get touches for both windows.
Michael Wright44753b12020-07-08 13:48:11 +01003074 mWindow1->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3075 InputWindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07003076 mWindow1->setFrame(Rect(0, 0, 100, 100));
3077
3078 mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2",
3079 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
Michael Wright44753b12020-07-08 13:48:11 +01003080 mWindow2->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3081 InputWindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07003082 mWindow2->setFrame(Rect(100, 100, 200, 200));
3083
Arthur Hung72d8dc32020-03-28 00:48:39 +00003084 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07003085 }
3086
3087protected:
3088 sp<FakeWindowHandle> mWindow1;
3089 sp<FakeWindowHandle> mWindow2;
3090
3091 // Helper function to convert the point from screen coordinates into the window's space
3092 static PointF getPointInWindow(const InputWindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07003093 vec2 vals = windowInfo->transform.transform(point.x, point.y);
3094 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07003095 }
3096
3097 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
3098 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003099 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07003100 InputEvent* event = window->consume();
3101
3102 ASSERT_NE(nullptr, event) << name.c_str()
3103 << ": consumer should have returned non-NULL event.";
3104
3105 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
3106 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
3107 << " event, got " << inputEventTypeToString(event->getType()) << " event";
3108
3109 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
3110 EXPECT_EQ(expectedAction, motionEvent.getAction());
3111
3112 for (size_t i = 0; i < points.size(); i++) {
3113 float expectedX = points[i].x;
3114 float expectedY = points[i].y;
3115
3116 EXPECT_EQ(expectedX, motionEvent.getX(i))
3117 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
3118 << ", got " << motionEvent.getX(i);
3119 EXPECT_EQ(expectedY, motionEvent.getY(i))
3120 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
3121 << ", got " << motionEvent.getY(i);
3122 }
3123 }
chaviw9eaa22c2020-07-01 16:21:27 -07003124
3125 void touchAndAssertPositions(int32_t action, std::vector<PointF> touchedPoints,
3126 std::vector<PointF> expectedPoints) {
3127 NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
3128 ADISPLAY_ID_DEFAULT, touchedPoints);
3129 mDispatcher->notifyMotion(&motionArgs);
3130
3131 // Always consume from window1 since it's the window that has the InputReceiver
3132 consumeMotionEvent(mWindow1, action, expectedPoints);
3133 }
chaviwaf87b3e2019-10-01 16:59:28 -07003134};
3135
3136TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
3137 // Touch Window 1
3138 PointF touchedPoint = {10, 10};
3139 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003140 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003141
3142 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07003143 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003144
3145 // Touch Window 2
3146 touchedPoint = {150, 150};
3147 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003148 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003149}
3150
chaviw9eaa22c2020-07-01 16:21:27 -07003151TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
3152 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07003153 mWindow2->setWindowScale(0.5f, 0.5f);
3154
3155 // Touch Window 1
3156 PointF touchedPoint = {10, 10};
3157 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003158 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003159 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07003160 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003161
3162 // Touch Window 2
3163 touchedPoint = {150, 150};
3164 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003165 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
3166 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003167
chaviw9eaa22c2020-07-01 16:21:27 -07003168 // Update the transform so rotation is set
3169 mWindow2->setWindowTransform(0, -1, 1, 0);
3170 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
3171 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003172}
3173
chaviw9eaa22c2020-07-01 16:21:27 -07003174TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003175 mWindow2->setWindowScale(0.5f, 0.5f);
3176
3177 // Touch Window 1
3178 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3179 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003180 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003181
3182 // Touch Window 2
3183 int32_t actionPointerDown =
3184 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003185 touchedPoints.push_back(PointF{150, 150});
3186 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3187 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003188
chaviw9eaa22c2020-07-01 16:21:27 -07003189 // Release Window 2
3190 int32_t actionPointerUp =
3191 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3192 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
3193 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003194
chaviw9eaa22c2020-07-01 16:21:27 -07003195 // Update the transform so rotation is set for Window 2
3196 mWindow2->setWindowTransform(0, -1, 1, 0);
3197 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3198 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003199}
3200
chaviw9eaa22c2020-07-01 16:21:27 -07003201TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003202 mWindow2->setWindowScale(0.5f, 0.5f);
3203
3204 // Touch Window 1
3205 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3206 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003207 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003208
3209 // Touch Window 2
3210 int32_t actionPointerDown =
3211 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003212 touchedPoints.push_back(PointF{150, 150});
3213 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003214
chaviw9eaa22c2020-07-01 16:21:27 -07003215 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003216
3217 // Move both windows
3218 touchedPoints = {{20, 20}, {175, 175}};
3219 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3220 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3221
chaviw9eaa22c2020-07-01 16:21:27 -07003222 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003223
chaviw9eaa22c2020-07-01 16:21:27 -07003224 // Release Window 2
3225 int32_t actionPointerUp =
3226 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3227 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
3228 expectedPoints.pop_back();
3229
3230 // Touch Window 2
3231 mWindow2->setWindowTransform(0, -1, 1, 0);
3232 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3233 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
3234
3235 // Move both windows
3236 touchedPoints = {{20, 20}, {175, 175}};
3237 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3238 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3239
3240 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003241}
3242
3243TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
3244 mWindow1->setWindowScale(0.5f, 0.5f);
3245
3246 // Touch Window 1
3247 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3248 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003249 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003250
3251 // Touch Window 2
3252 int32_t actionPointerDown =
3253 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003254 touchedPoints.push_back(PointF{150, 150});
3255 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003256
chaviw9eaa22c2020-07-01 16:21:27 -07003257 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003258
3259 // Move both windows
3260 touchedPoints = {{20, 20}, {175, 175}};
3261 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3262 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3263
chaviw9eaa22c2020-07-01 16:21:27 -07003264 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003265}
3266
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003267class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
3268 virtual void SetUp() override {
3269 InputDispatcherTest::SetUp();
3270
Chris Yea209fde2020-07-22 13:54:51 -07003271 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003272 mApplication->setDispatchingTimeout(20ms);
3273 mWindow =
3274 new FakeWindowHandle(mApplication, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
3275 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05003276 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07003277 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003278 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3279 // window.
Michael Wright44753b12020-07-08 13:48:11 +01003280 mWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003281
3282 // Set focused application.
3283 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
3284
3285 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003286 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003287 mWindow->consumeFocusEvent(true);
3288 }
3289
3290 virtual void TearDown() override {
3291 InputDispatcherTest::TearDown();
3292 mWindow.clear();
3293 }
3294
3295protected:
Chris Yea209fde2020-07-22 13:54:51 -07003296 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003297 sp<FakeWindowHandle> mWindow;
3298 static constexpr PointF WINDOW_LOCATION = {20, 20};
3299
3300 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003301 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003302 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3303 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003304 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003305 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3306 WINDOW_LOCATION));
3307 }
3308};
3309
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003310// Send a tap and respond, which should not cause an ANR.
3311TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
3312 tapOnWindow();
3313 mWindow->consumeMotionDown();
3314 mWindow->consumeMotionUp();
3315 ASSERT_TRUE(mDispatcher->waitForIdle());
3316 mFakePolicy->assertNotifyAnrWasNotCalled();
3317}
3318
3319// Send a regular key and respond, which should not cause an ANR.
3320TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003321 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003322 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
3323 ASSERT_TRUE(mDispatcher->waitForIdle());
3324 mFakePolicy->assertNotifyAnrWasNotCalled();
3325}
3326
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003327TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
3328 mWindow->setFocusable(false);
3329 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3330 mWindow->consumeFocusEvent(false);
3331
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003332 InputEventInjectionResult result =
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003333 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003334 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/,
3335 false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003336 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003337 // Key will not go to window because we have no focused window.
3338 // The 'no focused window' ANR timer should start instead.
3339
3340 // Now, the focused application goes away.
3341 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
3342 // The key should get dropped and there should be no ANR.
3343
3344 ASSERT_TRUE(mDispatcher->waitForIdle());
3345 mFakePolicy->assertNotifyAnrWasNotCalled();
3346}
3347
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003348// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003349// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
3350// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003351TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003352 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003353 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3354 WINDOW_LOCATION));
3355
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003356 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
3357 ASSERT_TRUE(sequenceNum);
3358 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003359 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003360
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003361 mWindow->finishEvent(*sequenceNum);
3362 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3363 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003364 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003365 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003366}
3367
3368// Send a key to the app and have the app not respond right away.
3369TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
3370 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003371 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003372 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
3373 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003374 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003375 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003376 ASSERT_TRUE(mDispatcher->waitForIdle());
3377}
3378
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003379// We have a focused application, but no focused window
3380TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003381 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003382 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3383 mWindow->consumeFocusEvent(false);
3384
3385 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003386 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003387 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3388 WINDOW_LOCATION));
3389 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
3390 mDispatcher->waitForIdle();
3391 mFakePolicy->assertNotifyAnrWasNotCalled();
3392
3393 // Once a focused event arrives, we get an ANR for this application
3394 // We specify the injection timeout to be smaller than the application timeout, to ensure that
3395 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003396 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003397 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003398 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003399 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003400 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003401 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003402 ASSERT_TRUE(mDispatcher->waitForIdle());
3403}
3404
3405// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003406// Make sure that we don't notify policy twice about the same ANR.
3407TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003408 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003409 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3410 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003411
3412 // Once a focused event arrives, we get an ANR for this application
3413 // We specify the injection timeout to be smaller than the application timeout, to ensure that
3414 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003415 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003416 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003417 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003418 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003419 const std::chrono::duration appTimeout =
3420 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003421 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003422
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003423 std::this_thread::sleep_for(appTimeout);
3424 // ANR should not be raised again. It is up to policy to do that if it desires.
3425 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003426
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003427 // If we now get a focused window, the ANR should stop, but the policy handles that via
3428 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003429 ASSERT_TRUE(mDispatcher->waitForIdle());
3430}
3431
3432// We have a focused application, but no focused window
3433TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003434 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003435 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3436 mWindow->consumeFocusEvent(false);
3437
3438 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003439 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003440 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003441 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3442 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003443
3444 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003445 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003446
3447 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003448 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003449 ASSERT_TRUE(mDispatcher->waitForIdle());
3450 mWindow->assertNoEvents();
3451}
3452
3453/**
3454 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
3455 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
3456 * If we process 1 of the events, but ANR on the second event with the same timestamp,
3457 * the ANR mechanism should still work.
3458 *
3459 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
3460 * DOWN event, while not responding on the second one.
3461 */
3462TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
3463 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
3464 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3465 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
3466 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
3467 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003468 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003469
3470 // Now send ACTION_UP, with identical timestamp
3471 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3472 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
3473 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
3474 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003475 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003476
3477 // We have now sent down and up. Let's consume first event and then ANR on the second.
3478 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3479 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003480 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003481}
3482
3483// If an app is not responding to a key event, gesture monitors should continue to receive
3484// new motion events
3485TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnKey) {
3486 FakeMonitorReceiver monitor =
3487 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
3488 true /*isGestureMonitor*/);
3489
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003490 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3491 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003492 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003493 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003494
3495 // Stuck on the ACTION_UP
3496 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003497 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003498
3499 // New tap will go to the gesture monitor, but not to the window
3500 tapOnWindow();
3501 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3502 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3503
3504 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
3505 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003506 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003507 mWindow->assertNoEvents();
3508 monitor.assertNoEvents();
3509}
3510
3511// If an app is not responding to a motion event, gesture monitors should continue to receive
3512// new motion events
3513TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnMotion) {
3514 FakeMonitorReceiver monitor =
3515 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
3516 true /*isGestureMonitor*/);
3517
3518 tapOnWindow();
3519 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3520 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3521
3522 mWindow->consumeMotionDown();
3523 // Stuck on the ACTION_UP
3524 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003525 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003526
3527 // New tap will go to the gesture monitor, but not to the window
3528 tapOnWindow();
3529 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3530 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3531
3532 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
3533 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003534 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003535 mWindow->assertNoEvents();
3536 monitor.assertNoEvents();
3537}
3538
3539// If a window is unresponsive, then you get anr. if the window later catches up and starts to
3540// process events, you don't get an anr. When the window later becomes unresponsive again, you
3541// get an ANR again.
3542// 1. tap -> block on ACTION_UP -> receive ANR
3543// 2. consume all pending events (= queue becomes healthy again)
3544// 3. tap again -> block on ACTION_UP again -> receive ANR second time
3545TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
3546 tapOnWindow();
3547
3548 mWindow->consumeMotionDown();
3549 // Block on ACTION_UP
3550 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003551 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003552 mWindow->consumeMotionUp(); // Now the connection should be healthy again
3553 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003554 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003555 mWindow->assertNoEvents();
3556
3557 tapOnWindow();
3558 mWindow->consumeMotionDown();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003559 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003560 mWindow->consumeMotionUp();
3561
3562 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003563 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003564 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003565 mWindow->assertNoEvents();
3566}
3567
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003568// If a connection remains unresponsive for a while, make sure policy is only notified once about
3569// it.
3570TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003571 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003572 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3573 WINDOW_LOCATION));
3574
3575 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003576 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003577 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003578 // 'notifyConnectionUnresponsive' should only be called once per connection
3579 mFakePolicy->assertNotifyAnrWasNotCalled();
3580 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003581 mWindow->consumeMotionDown();
3582 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3583 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3584 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003585 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003586 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003587 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003588}
3589
3590/**
3591 * If a window is processing a motion event, and then a key event comes in, the key event should
3592 * not to to the focused window until the motion is processed.
3593 *
3594 * Warning!!!
3595 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
3596 * and the injection timeout that we specify when injecting the key.
3597 * We must have the injection timeout (10ms) be smaller than
3598 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
3599 *
3600 * If that value changes, this test should also change.
3601 */
3602TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
3603 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3604 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3605
3606 tapOnWindow();
3607 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3608 ASSERT_TRUE(downSequenceNum);
3609 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3610 ASSERT_TRUE(upSequenceNum);
3611 // Don't finish the events yet, and send a key
3612 // Injection will "succeed" because we will eventually give up and send the key to the focused
3613 // window even if motions are still being processed. But because the injection timeout is short,
3614 // we will receive INJECTION_TIMED_OUT as the result.
3615
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003616 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003617 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003618 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3619 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003620 // Key will not be sent to the window, yet, because the window is still processing events
3621 // and the key remains pending, waiting for the touch events to be processed
3622 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3623 ASSERT_FALSE(keySequenceNum);
3624
3625 std::this_thread::sleep_for(500ms);
3626 // if we wait long enough though, dispatcher will give up, and still send the key
3627 // to the focused window, even though we have not yet finished the motion event
3628 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3629 mWindow->finishEvent(*downSequenceNum);
3630 mWindow->finishEvent(*upSequenceNum);
3631}
3632
3633/**
3634 * If a window is processing a motion event, and then a key event comes in, the key event should
3635 * not go to the focused window until the motion is processed.
3636 * If then a new motion comes in, then the pending key event should be going to the currently
3637 * focused window right away.
3638 */
3639TEST_F(InputDispatcherSingleWindowAnr,
3640 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
3641 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3642 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3643
3644 tapOnWindow();
3645 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3646 ASSERT_TRUE(downSequenceNum);
3647 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3648 ASSERT_TRUE(upSequenceNum);
3649 // Don't finish the events yet, and send a key
3650 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003651 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003652 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003653 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003654 // At this point, key is still pending, and should not be sent to the application yet.
3655 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3656 ASSERT_FALSE(keySequenceNum);
3657
3658 // Now tap down again. It should cause the pending key to go to the focused window right away.
3659 tapOnWindow();
3660 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
3661 // the other events yet. We can finish events in any order.
3662 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
3663 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
3664 mWindow->consumeMotionDown();
3665 mWindow->consumeMotionUp();
3666 mWindow->assertNoEvents();
3667}
3668
3669class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
3670 virtual void SetUp() override {
3671 InputDispatcherTest::SetUp();
3672
Chris Yea209fde2020-07-22 13:54:51 -07003673 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003674 mApplication->setDispatchingTimeout(10ms);
3675 mUnfocusedWindow =
3676 new FakeWindowHandle(mApplication, mDispatcher, "Unfocused", ADISPLAY_ID_DEFAULT);
3677 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
3678 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3679 // window.
3680 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Michael Wright44753b12020-07-08 13:48:11 +01003681 mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3682 InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH |
3683 InputWindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003684
3685 mFocusedWindow =
3686 new FakeWindowHandle(mApplication, mDispatcher, "Focused", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05003687 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003688 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01003689 mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3690 InputWindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003691
3692 // Set focused application.
3693 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07003694 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003695
3696 // Expect one focus window exist in display.
3697 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003698 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003699 mFocusedWindow->consumeFocusEvent(true);
3700 }
3701
3702 virtual void TearDown() override {
3703 InputDispatcherTest::TearDown();
3704
3705 mUnfocusedWindow.clear();
3706 mFocusedWindow.clear();
3707 }
3708
3709protected:
Chris Yea209fde2020-07-22 13:54:51 -07003710 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003711 sp<FakeWindowHandle> mUnfocusedWindow;
3712 sp<FakeWindowHandle> mFocusedWindow;
3713 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
3714 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
3715 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
3716
3717 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
3718
3719 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
3720
3721private:
3722 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003723 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003724 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3725 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003726 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003727 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3728 location));
3729 }
3730};
3731
3732// If we have 2 windows that are both unresponsive, the one with the shortest timeout
3733// should be ANR'd first.
3734TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003735 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003736 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3737 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003738 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003739 mFocusedWindow->consumeMotionDown();
3740 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3741 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3742 // We consumed all events, so no ANR
3743 ASSERT_TRUE(mDispatcher->waitForIdle());
3744 mFakePolicy->assertNotifyAnrWasNotCalled();
3745
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003746 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003747 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3748 FOCUSED_WINDOW_LOCATION));
3749 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
3750 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003751
3752 const std::chrono::duration timeout =
3753 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003754 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003755 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
3756 // sequence to make it consistent
3757 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003758 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003759 mFocusedWindow->consumeMotionDown();
3760 // This cancel is generated because the connection was unresponsive
3761 mFocusedWindow->consumeMotionCancel();
3762 mFocusedWindow->assertNoEvents();
3763 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003764 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003765 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003766 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003767}
3768
3769// If we have 2 windows with identical timeouts that are both unresponsive,
3770// it doesn't matter which order they should have ANR.
3771// But we should receive ANR for both.
3772TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
3773 // Set the timeout for unfocused window to match the focused window
3774 mUnfocusedWindow->setDispatchingTimeout(10ms);
3775 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
3776
3777 tapOnFocusedWindow();
3778 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003779 sp<IBinder> anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms);
3780 sp<IBinder> anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003781
3782 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003783 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
3784 mFocusedWindow->getToken() == anrConnectionToken2);
3785 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
3786 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003787
3788 ASSERT_TRUE(mDispatcher->waitForIdle());
3789 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003790
3791 mFocusedWindow->consumeMotionDown();
3792 mFocusedWindow->consumeMotionUp();
3793 mUnfocusedWindow->consumeMotionOutside();
3794
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003795 sp<IBinder> responsiveToken1 = mFakePolicy->getResponsiveWindowToken();
3796 sp<IBinder> responsiveToken2 = mFakePolicy->getResponsiveWindowToken();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003797
3798 // Both applications should be marked as responsive, in any order
3799 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
3800 mFocusedWindow->getToken() == responsiveToken2);
3801 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
3802 mUnfocusedWindow->getToken() == responsiveToken2);
3803 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003804}
3805
3806// If a window is already not responding, the second tap on the same window should be ignored.
3807// We should also log an error to account for the dropped event (not tested here).
3808// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
3809TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
3810 tapOnFocusedWindow();
3811 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3812 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3813 // Receive the events, but don't respond
3814 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
3815 ASSERT_TRUE(downEventSequenceNum);
3816 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
3817 ASSERT_TRUE(upEventSequenceNum);
3818 const std::chrono::duration timeout =
3819 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003820 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003821
3822 // Tap once again
3823 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003824 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003825 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3826 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003827 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003828 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3829 FOCUSED_WINDOW_LOCATION));
3830 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
3831 // valid touch target
3832 mUnfocusedWindow->assertNoEvents();
3833
3834 // Consume the first tap
3835 mFocusedWindow->finishEvent(*downEventSequenceNum);
3836 mFocusedWindow->finishEvent(*upEventSequenceNum);
3837 ASSERT_TRUE(mDispatcher->waitForIdle());
3838 // The second tap did not go to the focused window
3839 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003840 // Since all events are finished, connection should be deemed healthy again
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003841 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003842 mFakePolicy->assertNotifyAnrWasNotCalled();
3843}
3844
3845// If you tap outside of all windows, there will not be ANR
3846TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003847 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003848 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3849 LOCATION_OUTSIDE_ALL_WINDOWS));
3850 ASSERT_TRUE(mDispatcher->waitForIdle());
3851 mFakePolicy->assertNotifyAnrWasNotCalled();
3852}
3853
3854// Since the focused window is paused, tapping on it should not produce any events
3855TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
3856 mFocusedWindow->setPaused(true);
3857 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
3858
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003859 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003860 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3861 FOCUSED_WINDOW_LOCATION));
3862
3863 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
3864 ASSERT_TRUE(mDispatcher->waitForIdle());
3865 // Should not ANR because the window is paused, and touches shouldn't go to it
3866 mFakePolicy->assertNotifyAnrWasNotCalled();
3867
3868 mFocusedWindow->assertNoEvents();
3869 mUnfocusedWindow->assertNoEvents();
3870}
3871
3872/**
3873 * If a window is processing a motion event, and then a key event comes in, the key event should
3874 * not to to the focused window until the motion is processed.
3875 * If a different window becomes focused at this time, the key should go to that window instead.
3876 *
3877 * Warning!!!
3878 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
3879 * and the injection timeout that we specify when injecting the key.
3880 * We must have the injection timeout (10ms) be smaller than
3881 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
3882 *
3883 * If that value changes, this test should also change.
3884 */
3885TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
3886 // Set a long ANR timeout to prevent it from triggering
3887 mFocusedWindow->setDispatchingTimeout(2s);
3888 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
3889
3890 tapOnUnfocusedWindow();
3891 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
3892 ASSERT_TRUE(downSequenceNum);
3893 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
3894 ASSERT_TRUE(upSequenceNum);
3895 // Don't finish the events yet, and send a key
3896 // Injection will succeed because we will eventually give up and send the key to the focused
3897 // window even if motions are still being processed.
3898
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003899 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003900 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003901 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
3902 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003903 // Key will not be sent to the window, yet, because the window is still processing events
3904 // and the key remains pending, waiting for the touch events to be processed
3905 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
3906 ASSERT_FALSE(keySequenceNum);
3907
3908 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07003909 mFocusedWindow->setFocusable(false);
3910 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003911 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003912 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003913
3914 // Focus events should precede the key events
3915 mUnfocusedWindow->consumeFocusEvent(true);
3916 mFocusedWindow->consumeFocusEvent(false);
3917
3918 // Finish the tap events, which should unblock dispatcher
3919 mUnfocusedWindow->finishEvent(*downSequenceNum);
3920 mUnfocusedWindow->finishEvent(*upSequenceNum);
3921
3922 // Now that all queues are cleared and no backlog in the connections, the key event
3923 // can finally go to the newly focused "mUnfocusedWindow".
3924 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3925 mFocusedWindow->assertNoEvents();
3926 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003927 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003928}
3929
3930// When the touch stream is split across 2 windows, and one of them does not respond,
3931// then ANR should be raised and the touch should be canceled for the unresponsive window.
3932// The other window should not be affected by that.
3933TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
3934 // Touch Window 1
3935 NotifyMotionArgs motionArgs =
3936 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3937 ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION});
3938 mDispatcher->notifyMotion(&motionArgs);
3939 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3940 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3941
3942 // Touch Window 2
3943 int32_t actionPointerDown =
3944 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3945
3946 motionArgs =
3947 generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3948 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION});
3949 mDispatcher->notifyMotion(&motionArgs);
3950
3951 const std::chrono::duration timeout =
3952 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003953 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003954
3955 mUnfocusedWindow->consumeMotionDown();
3956 mFocusedWindow->consumeMotionDown();
3957 // Focused window may or may not receive ACTION_MOVE
3958 // But it should definitely receive ACTION_CANCEL due to the ANR
3959 InputEvent* event;
3960 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
3961 ASSERT_TRUE(moveOrCancelSequenceNum);
3962 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
3963 ASSERT_NE(nullptr, event);
3964 ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION);
3965 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
3966 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
3967 mFocusedWindow->consumeMotionCancel();
3968 } else {
3969 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
3970 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003971 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003972 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003973
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003974 mUnfocusedWindow->assertNoEvents();
3975 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003976 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003977}
3978
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05003979/**
3980 * If we have no focused window, and a key comes in, we start the ANR timer.
3981 * The focused application should add a focused window before the timer runs out to prevent ANR.
3982 *
3983 * If the user touches another application during this time, the key should be dropped.
3984 * Next, if a new focused window comes in, without toggling the focused application,
3985 * then no ANR should occur.
3986 *
3987 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
3988 * but in some cases the policy may not update the focused application.
3989 */
3990TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
3991 std::shared_ptr<FakeApplicationHandle> focusedApplication =
3992 std::make_shared<FakeApplicationHandle>();
3993 focusedApplication->setDispatchingTimeout(60ms);
3994 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
3995 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
3996 mFocusedWindow->setFocusable(false);
3997
3998 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
3999 mFocusedWindow->consumeFocusEvent(false);
4000
4001 // Send a key. The ANR timer should start because there is no focused window.
4002 // 'focusedApplication' will get blamed if this timer completes.
4003 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004004 InputEventInjectionResult result =
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05004005 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004006 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/,
4007 false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004008 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05004009
4010 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
4011 // then the injected touches won't cause the focused event to get dropped.
4012 // The dispatcher only checks for whether the queue should be pruned upon queueing.
4013 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
4014 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
4015 // For this test, it means that the key would get delivered to the window once it becomes
4016 // focused.
4017 std::this_thread::sleep_for(10ms);
4018
4019 // Touch unfocused window. This should force the pending key to get dropped.
4020 NotifyMotionArgs motionArgs =
4021 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4022 ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION});
4023 mDispatcher->notifyMotion(&motionArgs);
4024
4025 // We do not consume the motion right away, because that would require dispatcher to first
4026 // process (== drop) the key event, and by that time, ANR will be raised.
4027 // Set the focused window first.
4028 mFocusedWindow->setFocusable(true);
4029 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
4030 setFocusedWindow(mFocusedWindow);
4031 mFocusedWindow->consumeFocusEvent(true);
4032 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
4033 // to another application. This could be a bug / behaviour in the policy.
4034
4035 mUnfocusedWindow->consumeMotionDown();
4036
4037 ASSERT_TRUE(mDispatcher->waitForIdle());
4038 // Should not ANR because we actually have a focused window. It was just added too slowly.
4039 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
4040}
4041
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05004042// These tests ensure we cannot send touch events to a window that's positioned behind a window
4043// that has feature NO_INPUT_CHANNEL.
4044// Layout:
4045// Top (closest to user)
4046// mNoInputWindow (above all windows)
4047// mBottomWindow
4048// Bottom (furthest from user)
4049class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
4050 virtual void SetUp() override {
4051 InputDispatcherTest::SetUp();
4052
4053 mApplication = std::make_shared<FakeApplicationHandle>();
4054 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
4055 "Window without input channel", ADISPLAY_ID_DEFAULT,
4056 std::make_optional<sp<IBinder>>(nullptr) /*token*/);
4057
4058 mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL);
4059 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
4060 // It's perfectly valid for this window to not have an associated input channel
4061
4062 mBottomWindow = new FakeWindowHandle(mApplication, mDispatcher, "Bottom window",
4063 ADISPLAY_ID_DEFAULT);
4064 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
4065
4066 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
4067 }
4068
4069protected:
4070 std::shared_ptr<FakeApplicationHandle> mApplication;
4071 sp<FakeWindowHandle> mNoInputWindow;
4072 sp<FakeWindowHandle> mBottomWindow;
4073};
4074
4075TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
4076 PointF touchedPoint = {10, 10};
4077
4078 NotifyMotionArgs motionArgs =
4079 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4080 ADISPLAY_ID_DEFAULT, {touchedPoint});
4081 mDispatcher->notifyMotion(&motionArgs);
4082
4083 mNoInputWindow->assertNoEvents();
4084 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
4085 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
4086 // and therefore should prevent mBottomWindow from receiving touches
4087 mBottomWindow->assertNoEvents();
4088}
4089
4090/**
4091 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
4092 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
4093 */
4094TEST_F(InputDispatcherMultiWindowOcclusionTests,
4095 NoInputChannelFeature_DropsTouchesWithValidChannel) {
4096 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
4097 "Window with input channel and NO_INPUT_CHANNEL",
4098 ADISPLAY_ID_DEFAULT);
4099
4100 mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL);
4101 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
4102 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
4103
4104 PointF touchedPoint = {10, 10};
4105
4106 NotifyMotionArgs motionArgs =
4107 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4108 ADISPLAY_ID_DEFAULT, {touchedPoint});
4109 mDispatcher->notifyMotion(&motionArgs);
4110
4111 mNoInputWindow->assertNoEvents();
4112 mBottomWindow->assertNoEvents();
4113}
4114
Vishnu Nair958da932020-08-21 17:12:37 -07004115class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
4116protected:
4117 std::shared_ptr<FakeApplicationHandle> mApp;
4118 sp<FakeWindowHandle> mWindow;
4119 sp<FakeWindowHandle> mMirror;
4120
4121 virtual void SetUp() override {
4122 InputDispatcherTest::SetUp();
4123 mApp = std::make_shared<FakeApplicationHandle>();
4124 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
4125 mMirror = new FakeWindowHandle(mApp, mDispatcher, "TestWindowMirror", ADISPLAY_ID_DEFAULT,
4126 mWindow->getToken());
4127 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
4128 mWindow->setFocusable(true);
4129 mMirror->setFocusable(true);
4130 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4131 }
4132};
4133
4134TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
4135 // Request focus on a mirrored window
4136 setFocusedWindow(mMirror);
4137
4138 // window gets focused
4139 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004140 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4141 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004142 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
4143}
4144
4145// A focused & mirrored window remains focused only if the window and its mirror are both
4146// focusable.
4147TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
4148 setFocusedWindow(mMirror);
4149
4150 // window gets focused
4151 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004152 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4153 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004154 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004155 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4156 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004157 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4158
4159 mMirror->setFocusable(false);
4160 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4161
4162 // window loses focus since one of the windows associated with the token in not focusable
4163 mWindow->consumeFocusEvent(false);
4164
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004165 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4166 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004167 mWindow->assertNoEvents();
4168}
4169
4170// A focused & mirrored window remains focused until the window and its mirror both become
4171// invisible.
4172TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
4173 setFocusedWindow(mMirror);
4174
4175 // window gets focused
4176 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004177 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4178 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004179 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004180 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4181 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004182 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4183
4184 mMirror->setVisible(false);
4185 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4186
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004187 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4188 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004189 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004190 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4191 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004192 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4193
4194 mWindow->setVisible(false);
4195 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4196
4197 // window loses focus only after all windows associated with the token become invisible.
4198 mWindow->consumeFocusEvent(false);
4199
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004200 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4201 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004202 mWindow->assertNoEvents();
4203}
4204
4205// A focused & mirrored window remains focused until both windows are removed.
4206TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
4207 setFocusedWindow(mMirror);
4208
4209 // window gets focused
4210 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004211 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4212 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004213 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004214 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4215 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004216 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4217
4218 // single window is removed but the window token remains focused
4219 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
4220
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004221 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4222 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004223 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004224 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4225 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004226 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4227
4228 // Both windows are removed
4229 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
4230 mWindow->consumeFocusEvent(false);
4231
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004232 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4233 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004234 mWindow->assertNoEvents();
4235}
4236
4237// Focus request can be pending until one window becomes visible.
4238TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
4239 // Request focus on an invisible mirror.
4240 mWindow->setVisible(false);
4241 mMirror->setVisible(false);
4242 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4243 setFocusedWindow(mMirror);
4244
4245 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004246 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07004247 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004248 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07004249
4250 mMirror->setVisible(true);
4251 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4252
4253 // window gets focused
4254 mWindow->consumeFocusEvent(true);
4255 // window gets the pending key event
4256 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4257}
Prabir Pradhan99987712020-11-10 18:43:05 -08004258
4259class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
4260protected:
4261 std::shared_ptr<FakeApplicationHandle> mApp;
4262 sp<FakeWindowHandle> mWindow;
4263 sp<FakeWindowHandle> mSecondWindow;
4264
4265 void SetUp() override {
4266 InputDispatcherTest::SetUp();
4267 mApp = std::make_shared<FakeApplicationHandle>();
4268 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
4269 mWindow->setFocusable(true);
4270 mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
4271 mSecondWindow->setFocusable(true);
4272
4273 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
4274 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
4275
4276 setFocusedWindow(mWindow);
4277 mWindow->consumeFocusEvent(true);
4278 }
4279
4280 void notifyPointerCaptureChanged(bool enabled) {
4281 const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(enabled);
4282 mDispatcher->notifyPointerCaptureChanged(&args);
4283 }
4284
4285 void requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, bool enabled) {
4286 mDispatcher->requestPointerCapture(window->getToken(), enabled);
4287 mFakePolicy->waitForSetPointerCapture(enabled);
4288 notifyPointerCaptureChanged(enabled);
4289 window->consumeCaptureEvent(enabled);
4290 }
4291};
4292
4293TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
4294 // Ensure that capture cannot be obtained for unfocused windows.
4295 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
4296 mFakePolicy->assertSetPointerCaptureNotCalled();
4297 mSecondWindow->assertNoEvents();
4298
4299 // Ensure that capture can be enabled from the focus window.
4300 requestAndVerifyPointerCapture(mWindow, true);
4301
4302 // Ensure that capture cannot be disabled from a window that does not have capture.
4303 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
4304 mFakePolicy->assertSetPointerCaptureNotCalled();
4305
4306 // Ensure that capture can be disabled from the window with capture.
4307 requestAndVerifyPointerCapture(mWindow, false);
4308}
4309
4310TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
4311 requestAndVerifyPointerCapture(mWindow, true);
4312
4313 setFocusedWindow(mSecondWindow);
4314
4315 // Ensure that the capture disabled event was sent first.
4316 mWindow->consumeCaptureEvent(false);
4317 mWindow->consumeFocusEvent(false);
4318 mSecondWindow->consumeFocusEvent(true);
4319 mFakePolicy->waitForSetPointerCapture(false);
4320
4321 // Ensure that additional state changes from InputReader are not sent to the window.
4322 notifyPointerCaptureChanged(false);
4323 notifyPointerCaptureChanged(true);
4324 notifyPointerCaptureChanged(false);
4325 mWindow->assertNoEvents();
4326 mSecondWindow->assertNoEvents();
4327 mFakePolicy->assertSetPointerCaptureNotCalled();
4328}
4329
4330TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
4331 requestAndVerifyPointerCapture(mWindow, true);
4332
4333 // InputReader unexpectedly disables and enables pointer capture.
4334 notifyPointerCaptureChanged(false);
4335 notifyPointerCaptureChanged(true);
4336
4337 // Ensure that Pointer Capture is disabled.
Prabir Pradhan7d030382020-12-21 07:58:35 -08004338 mFakePolicy->waitForSetPointerCapture(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08004339 mWindow->consumeCaptureEvent(false);
4340 mWindow->assertNoEvents();
4341}
4342
Prabir Pradhan167e6d92021-02-04 16:18:17 -08004343TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
4344 requestAndVerifyPointerCapture(mWindow, true);
4345
4346 // The first window loses focus.
4347 setFocusedWindow(mSecondWindow);
4348 mFakePolicy->waitForSetPointerCapture(false);
4349 mWindow->consumeCaptureEvent(false);
4350
4351 // Request Pointer Capture from the second window before the notification from InputReader
4352 // arrives.
4353 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
4354 mFakePolicy->waitForSetPointerCapture(true);
4355
4356 // InputReader notifies Pointer Capture was disabled (because of the focus change).
4357 notifyPointerCaptureChanged(false);
4358
4359 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
4360 notifyPointerCaptureChanged(true);
4361
4362 mSecondWindow->consumeFocusEvent(true);
4363 mSecondWindow->consumeCaptureEvent(true);
4364}
4365
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004366class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
4367protected:
4368 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00004369
4370 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
4371 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
4372
4373 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
4374 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
4375
4376 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
4377 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
4378 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
4379 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
4380 MAXIMUM_OBSCURING_OPACITY);
4381
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004382 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004383 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004384 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004385
4386 sp<FakeWindowHandle> mTouchWindow;
4387
4388 virtual void SetUp() override {
4389 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004390 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004391 mDispatcher->setBlockUntrustedTouchesMode(android::os::BlockUntrustedTouchesMode::BLOCK);
4392 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
4393 }
4394
4395 virtual void TearDown() override {
4396 InputDispatcherTest::TearDown();
4397 mTouchWindow.clear();
4398 }
4399
4400 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name,
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004401 os::TouchOcclusionMode mode, float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004402 sp<FakeWindowHandle> window = getWindow(uid, name);
4403 window->setFlags(InputWindowInfo::Flag::NOT_TOUCHABLE);
4404 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004405 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004406 return window;
4407 }
4408
4409 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
4410 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
4411 sp<FakeWindowHandle> window =
4412 new FakeWindowHandle(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
4413 // Generate an arbitrary PID based on the UID
4414 window->setOwnerInfo(1777 + (uid % 10000), uid);
4415 return window;
4416 }
4417
4418 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
4419 NotifyMotionArgs args =
4420 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4421 ADISPLAY_ID_DEFAULT, points);
4422 mDispatcher->notifyMotion(&args);
4423 }
4424};
4425
4426TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004427 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004428 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004429 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004430
4431 touch();
4432
4433 mTouchWindow->assertNoEvents();
4434}
4435
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004436TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00004437 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
4438 const sp<FakeWindowHandle>& w =
4439 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
4440 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4441
4442 touch();
4443
4444 mTouchWindow->assertNoEvents();
4445}
4446
4447TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004448 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
4449 const sp<FakeWindowHandle>& w =
4450 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
4451 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4452
4453 touch();
4454
4455 w->assertNoEvents();
4456}
4457
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004458TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004459 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
4460 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004461
4462 touch();
4463
4464 mTouchWindow->consumeAnyMotionDown();
4465}
4466
4467TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004468 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004469 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004470 w->setFrame(Rect(0, 0, 50, 50));
4471 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004472
4473 touch({PointF{100, 100}});
4474
4475 mTouchWindow->consumeAnyMotionDown();
4476}
4477
4478TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004479 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004480 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004481 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4482
4483 touch();
4484
4485 mTouchWindow->consumeAnyMotionDown();
4486}
4487
4488TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
4489 const sp<FakeWindowHandle>& w =
4490 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4491 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004492
4493 touch();
4494
4495 mTouchWindow->consumeAnyMotionDown();
4496}
4497
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004498TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
4499 const sp<FakeWindowHandle>& w =
4500 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4501 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4502
4503 touch();
4504
4505 w->assertNoEvents();
4506}
4507
4508/**
4509 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
4510 * inside) while letting them pass-through. Note that even though touch passes through the occluding
4511 * window, the occluding window will still receive ACTION_OUTSIDE event.
4512 */
4513TEST_F(InputDispatcherUntrustedTouchesTest,
4514 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
4515 const sp<FakeWindowHandle>& w =
4516 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4517 w->addFlags(InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH);
4518 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4519
4520 touch();
4521
4522 w->consumeMotionOutside();
4523}
4524
4525TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
4526 const sp<FakeWindowHandle>& w =
4527 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4528 w->addFlags(InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH);
4529 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4530
4531 touch();
4532
4533 InputEvent* event = w->consume();
4534 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType());
4535 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
4536 EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getX());
4537 EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getY());
4538}
4539
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004540TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004541 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004542 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4543 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004544 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4545
4546 touch();
4547
4548 mTouchWindow->consumeAnyMotionDown();
4549}
4550
4551TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
4552 const sp<FakeWindowHandle>& w =
4553 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4554 MAXIMUM_OBSCURING_OPACITY);
4555 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004556
4557 touch();
4558
4559 mTouchWindow->consumeAnyMotionDown();
4560}
4561
4562TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004563 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004564 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4565 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004566 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4567
4568 touch();
4569
4570 mTouchWindow->assertNoEvents();
4571}
4572
4573TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
4574 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
4575 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004576 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
4577 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004578 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004579 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
4580 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004581 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
4582
4583 touch();
4584
4585 mTouchWindow->assertNoEvents();
4586}
4587
4588TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
4589 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
4590 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004591 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
4592 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004593 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004594 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
4595 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004596 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
4597
4598 touch();
4599
4600 mTouchWindow->consumeAnyMotionDown();
4601}
4602
4603TEST_F(InputDispatcherUntrustedTouchesTest,
4604 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
4605 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004606 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4607 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004608 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004609 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
4610 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004611 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
4612
4613 touch();
4614
4615 mTouchWindow->consumeAnyMotionDown();
4616}
4617
4618TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
4619 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004620 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4621 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004622 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004623 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
4624 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004625 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004626
4627 touch();
4628
4629 mTouchWindow->assertNoEvents();
4630}
4631
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004632TEST_F(InputDispatcherUntrustedTouchesTest,
4633 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
4634 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004635 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
4636 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004637 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004638 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4639 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004640 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
4641
4642 touch();
4643
4644 mTouchWindow->assertNoEvents();
4645}
4646
4647TEST_F(InputDispatcherUntrustedTouchesTest,
4648 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
4649 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004650 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
4651 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004652 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004653 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4654 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004655 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
4656
4657 touch();
4658
4659 mTouchWindow->consumeAnyMotionDown();
4660}
4661
4662TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
4663 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004664 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
4665 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004666 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4667
4668 touch();
4669
4670 mTouchWindow->consumeAnyMotionDown();
4671}
4672
4673TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
4674 const sp<FakeWindowHandle>& w =
4675 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
4676 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4677
4678 touch();
4679
4680 mTouchWindow->consumeAnyMotionDown();
4681}
4682
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00004683TEST_F(InputDispatcherUntrustedTouchesTest,
4684 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
4685 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
4686 const sp<FakeWindowHandle>& w =
4687 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
4688 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4689
4690 touch();
4691
4692 mTouchWindow->assertNoEvents();
4693}
4694
4695TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
4696 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
4697 const sp<FakeWindowHandle>& w =
4698 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
4699 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4700
4701 touch();
4702
4703 mTouchWindow->consumeAnyMotionDown();
4704}
4705
4706TEST_F(InputDispatcherUntrustedTouchesTest,
4707 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
4708 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
4709 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004710 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4711 OPACITY_ABOVE_THRESHOLD);
4712 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4713
4714 touch();
4715
4716 mTouchWindow->consumeAnyMotionDown();
4717}
4718
4719TEST_F(InputDispatcherUntrustedTouchesTest,
4720 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
4721 const sp<FakeWindowHandle>& w1 =
4722 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
4723 OPACITY_BELOW_THRESHOLD);
4724 const sp<FakeWindowHandle>& w2 =
4725 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4726 OPACITY_BELOW_THRESHOLD);
4727 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
4728
4729 touch();
4730
4731 mTouchWindow->assertNoEvents();
4732}
4733
4734/**
4735 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
4736 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
4737 * (which alone would result in allowing touches) does not affect the blocking behavior.
4738 */
4739TEST_F(InputDispatcherUntrustedTouchesTest,
4740 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
4741 const sp<FakeWindowHandle>& wB =
4742 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
4743 OPACITY_BELOW_THRESHOLD);
4744 const sp<FakeWindowHandle>& wC =
4745 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
4746 OPACITY_BELOW_THRESHOLD);
4747 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
4748
4749 touch();
4750
4751 mTouchWindow->assertNoEvents();
4752}
4753
4754/**
4755 * This test is testing that a window from a different UID but with same application token doesn't
4756 * block the touch. Apps can share the application token for close UI collaboration for example.
4757 */
4758TEST_F(InputDispatcherUntrustedTouchesTest,
4759 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
4760 const sp<FakeWindowHandle>& w =
4761 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
4762 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00004763 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4764
4765 touch();
4766
4767 mTouchWindow->consumeAnyMotionDown();
4768}
4769
arthurhungb89ccb02020-12-30 16:19:01 +08004770class InputDispatcherDragTests : public InputDispatcherTest {
4771protected:
4772 std::shared_ptr<FakeApplicationHandle> mApp;
4773 sp<FakeWindowHandle> mWindow;
4774 sp<FakeWindowHandle> mSecondWindow;
4775 sp<FakeWindowHandle> mDragWindow;
4776
4777 void SetUp() override {
4778 InputDispatcherTest::SetUp();
4779 mApp = std::make_shared<FakeApplicationHandle>();
4780 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
4781 mWindow->setFrame(Rect(0, 0, 100, 100));
4782 mWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
4783
4784 mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
4785 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
4786 mSecondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
4787
4788 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
4789 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
4790 }
4791
4792 // Start performing drag, we will create a drag window and transfer touch to it.
4793 void performDrag() {
4794 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4795 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4796 {50, 50}))
4797 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4798
4799 // Window should receive motion event.
4800 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4801
4802 // The drag window covers the entire display
4803 mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
4804 mDispatcher->setInputWindows(
4805 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
4806
4807 // Transfer touch focus to the drag window
4808 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
4809 true /* isDragDrop */);
4810 mWindow->consumeMotionCancel();
4811 mDragWindow->consumeMotionDown();
4812 }
arthurhung6d4bed92021-03-17 11:59:33 +08004813
4814 // Start performing drag, we will create a drag window and transfer touch to it.
4815 void performStylusDrag() {
4816 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4817 injectMotionEvent(mDispatcher,
4818 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
4819 AINPUT_SOURCE_STYLUS)
4820 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
4821 .pointer(PointerBuilder(0,
4822 AMOTION_EVENT_TOOL_TYPE_STYLUS)
4823 .x(50)
4824 .y(50))
4825 .build()));
4826 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4827
4828 // The drag window covers the entire display
4829 mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
4830 mDispatcher->setInputWindows(
4831 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
4832
4833 // Transfer touch focus to the drag window
4834 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
4835 true /* isDragDrop */);
4836 mWindow->consumeMotionCancel();
4837 mDragWindow->consumeMotionDown();
4838 }
arthurhungb89ccb02020-12-30 16:19:01 +08004839};
4840
4841TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
4842 performDrag();
4843
4844 // Move on window.
4845 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4846 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4847 ADISPLAY_ID_DEFAULT, {50, 50}))
4848 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4849 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
4850 mWindow->consumeDragEvent(false, 50, 50);
4851 mSecondWindow->assertNoEvents();
4852
4853 // Move to another window.
4854 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4855 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4856 ADISPLAY_ID_DEFAULT, {150, 50}))
4857 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4858 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
4859 mWindow->consumeDragEvent(true, 150, 50);
4860 mSecondWindow->consumeDragEvent(false, 50, 50);
4861
4862 // Move back to original window.
4863 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4864 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4865 ADISPLAY_ID_DEFAULT, {50, 50}))
4866 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4867 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
4868 mWindow->consumeDragEvent(false, 50, 50);
4869 mSecondWindow->consumeDragEvent(true, -50, 50);
4870
4871 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4872 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
4873 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4874 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
4875 mWindow->assertNoEvents();
4876 mSecondWindow->assertNoEvents();
4877}
4878
arthurhungf452d0b2021-01-06 00:19:52 +08004879TEST_F(InputDispatcherDragTests, DragAndDrop) {
4880 performDrag();
4881
4882 // Move on window.
4883 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4884 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4885 ADISPLAY_ID_DEFAULT, {50, 50}))
4886 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4887 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
4888 mWindow->consumeDragEvent(false, 50, 50);
4889 mSecondWindow->assertNoEvents();
4890
4891 // Move to another window.
4892 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4893 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4894 ADISPLAY_ID_DEFAULT, {150, 50}))
4895 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4896 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
4897 mWindow->consumeDragEvent(true, 150, 50);
4898 mSecondWindow->consumeDragEvent(false, 50, 50);
4899
4900 // drop to another window.
4901 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4902 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4903 {150, 50}))
4904 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4905 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
4906 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
4907 mWindow->assertNoEvents();
4908 mSecondWindow->assertNoEvents();
4909}
4910
arthurhung6d4bed92021-03-17 11:59:33 +08004911TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
4912 performStylusDrag();
4913
4914 // Move on window and keep button pressed.
4915 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4916 injectMotionEvent(mDispatcher,
4917 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
4918 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
4919 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
4920 .x(50)
4921 .y(50))
4922 .build()))
4923 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4924 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
4925 mWindow->consumeDragEvent(false, 50, 50);
4926 mSecondWindow->assertNoEvents();
4927
4928 // Move to another window and release button, expect to drop item.
4929 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4930 injectMotionEvent(mDispatcher,
4931 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
4932 .buttonState(0)
4933 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
4934 .x(150)
4935 .y(50))
4936 .build()))
4937 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4938 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
4939 mWindow->assertNoEvents();
4940 mSecondWindow->assertNoEvents();
4941 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
4942
4943 // nothing to the window.
4944 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4945 injectMotionEvent(mDispatcher,
4946 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
4947 .buttonState(0)
4948 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
4949 .x(150)
4950 .y(50))
4951 .build()))
4952 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4953 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
4954 mWindow->assertNoEvents();
4955 mSecondWindow->assertNoEvents();
4956}
4957
Arthur Hung6d0571e2021-04-09 20:18:16 +08004958TEST_F(InputDispatcherDragTests, DragAndDrop_InvalidWindow) {
4959 performDrag();
4960
4961 // Set second window invisible.
4962 mSecondWindow->setVisible(false);
4963 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
4964
4965 // Move on window.
4966 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4967 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4968 ADISPLAY_ID_DEFAULT, {50, 50}))
4969 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4970 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
4971 mWindow->consumeDragEvent(false, 50, 50);
4972 mSecondWindow->assertNoEvents();
4973
4974 // Move to another window.
4975 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4976 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4977 ADISPLAY_ID_DEFAULT, {150, 50}))
4978 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4979 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
4980 mWindow->consumeDragEvent(true, 150, 50);
4981 mSecondWindow->assertNoEvents();
4982
4983 // drop to another window.
4984 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4985 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4986 {150, 50}))
4987 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4988 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
4989 mFakePolicy->assertDropTargetEquals(nullptr);
4990 mWindow->assertNoEvents();
4991 mSecondWindow->assertNoEvents();
4992}
4993
Garfield Tane84e6f92019-08-29 17:28:41 -07004994} // namespace android::inputdispatcher