blob: 9687c83ca2c7a6a468f21cd6f9764c5cd9b885fc [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,
Evan Rosky84f07f02021-04-16 10:42:42 -0700529 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_DISPLAY_SIZE,
530 AMOTION_EVENT_INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700531 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800532 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700533 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800534 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800535 << "Should reject motion events with undefined action.";
536
537 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800538 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700539 AMOTION_EVENT_ACTION_POINTER_DOWN |
540 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700541 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
542 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky84f07f02021-04-16 10:42:42 -0700543 AMOTION_EVENT_INVALID_DISPLAY_SIZE, AMOTION_EVENT_INVALID_DISPLAY_SIZE,
chaviw9eaa22c2020-07-01 16:21:27 -0700544 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
545 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800546 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700547 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800548 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800549 << "Should reject motion events with pointer down index too large.";
550
Garfield Tanfbe732e2020-01-24 11:26:14 -0800551 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700552 AMOTION_EVENT_ACTION_POINTER_DOWN |
553 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700554 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
555 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky84f07f02021-04-16 10:42:42 -0700556 AMOTION_EVENT_INVALID_DISPLAY_SIZE, AMOTION_EVENT_INVALID_DISPLAY_SIZE,
chaviw9eaa22c2020-07-01 16:21:27 -0700557 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
558 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800559 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700560 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800561 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800562 << "Should reject motion events with pointer down index too small.";
563
564 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800565 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700566 AMOTION_EVENT_ACTION_POINTER_UP |
567 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700568 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
569 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky84f07f02021-04-16 10:42:42 -0700570 AMOTION_EVENT_INVALID_DISPLAY_SIZE, AMOTION_EVENT_INVALID_DISPLAY_SIZE,
chaviw9eaa22c2020-07-01 16:21:27 -0700571 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
572 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800573 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700574 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800575 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800576 << "Should reject motion events with pointer up index too large.";
577
Garfield Tanfbe732e2020-01-24 11:26:14 -0800578 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700579 AMOTION_EVENT_ACTION_POINTER_UP |
580 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700581 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
582 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky84f07f02021-04-16 10:42:42 -0700583 AMOTION_EVENT_INVALID_DISPLAY_SIZE, AMOTION_EVENT_INVALID_DISPLAY_SIZE,
chaviw9eaa22c2020-07-01 16:21:27 -0700584 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
585 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800586 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700587 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800588 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800589 << "Should reject motion events with pointer up index too small.";
590
591 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800592 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
593 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700594 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky84f07f02021-04-16 10:42:42 -0700595 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_DISPLAY_SIZE,
596 AMOTION_EVENT_INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700597 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800598 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700599 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800600 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800601 << "Should reject motion events with 0 pointers.";
602
Garfield Tanfbe732e2020-01-24 11:26:14 -0800603 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
604 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700605 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky84f07f02021-04-16 10:42:42 -0700606 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_DISPLAY_SIZE,
607 AMOTION_EVENT_INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700608 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800609 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700610 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800611 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800612 << "Should reject motion events with more than MAX_POINTERS pointers.";
613
614 // Rejects motion events with invalid pointer ids.
615 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800616 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
617 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700618 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky84f07f02021-04-16 10:42:42 -0700619 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_DISPLAY_SIZE,
620 AMOTION_EVENT_INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700621 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800622 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700623 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800624 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800625 << "Should reject motion events with pointer ids less than 0.";
626
627 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800628 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
629 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700630 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky84f07f02021-04-16 10:42:42 -0700631 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_DISPLAY_SIZE,
632 AMOTION_EVENT_INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700633 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800634 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700635 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800636 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800637 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
638
639 // Rejects motion events with duplicate pointer ids.
640 pointerProperties[0].id = 1;
641 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800642 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
643 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700644 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky84f07f02021-04-16 10:42:42 -0700645 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_DISPLAY_SIZE,
646 AMOTION_EVENT_INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700647 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800648 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700649 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800650 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800651 << "Should reject motion events with duplicate pointer ids.";
652}
653
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800654/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
655
656TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
657 constexpr nsecs_t eventTime = 20;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800658 NotifyConfigurationChangedArgs args(10 /*id*/, eventTime);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800659 mDispatcher->notifyConfigurationChanged(&args);
660 ASSERT_TRUE(mDispatcher->waitForIdle());
661
662 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
663}
664
665TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800666 NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/,
667 2 /*switchMask*/);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800668 mDispatcher->notifySwitch(&args);
669
670 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
671 args.policyFlags |= POLICY_FLAG_TRUSTED;
672 mFakePolicy->assertNotifySwitchWasCalled(args);
673}
674
Arthur Hungb92218b2018-08-14 12:00:21 +0800675// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700676static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700677static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s;
Arthur Hungb92218b2018-08-14 12:00:21 +0800678
679class FakeApplicationHandle : public InputApplicationHandle {
680public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700681 FakeApplicationHandle() {
682 mInfo.name = "Fake Application";
683 mInfo.token = new BBinder();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500684 mInfo.dispatchingTimeoutMillis =
685 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700686 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800687 virtual ~FakeApplicationHandle() {}
688
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000689 virtual bool updateInfo() override { return true; }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700690
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500691 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
692 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700693 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800694};
695
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800696class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800697public:
Garfield Tan15601662020-09-22 15:32:38 -0700698 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800699 : mName(name) {
Garfield Tan15601662020-09-22 15:32:38 -0700700 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800701 }
702
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800703 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700704 InputEvent* event;
705 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
706 if (!consumeSeq) {
707 return nullptr;
708 }
709 finishEvent(*consumeSeq);
710 return event;
711 }
712
713 /**
714 * Receive an event without acknowledging it.
715 * Return the sequence number that could later be used to send finished signal.
716 */
717 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800718 uint32_t consumeSeq;
719 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800720
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800721 std::chrono::time_point start = std::chrono::steady_clock::now();
722 status_t status = WOULD_BLOCK;
723 while (status == WOULD_BLOCK) {
chaviw81e2bb92019-12-18 15:03:51 -0800724 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800725 &event);
726 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
727 if (elapsed > 100ms) {
728 break;
729 }
730 }
731
732 if (status == WOULD_BLOCK) {
733 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700734 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800735 }
736
737 if (status != OK) {
738 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700739 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800740 }
741 if (event == nullptr) {
742 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700743 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800744 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700745 if (outEvent != nullptr) {
746 *outEvent = event;
747 }
748 return consumeSeq;
749 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800750
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700751 /**
752 * To be used together with "receiveEvent" to complete the consumption of an event.
753 */
754 void finishEvent(uint32_t consumeSeq) {
755 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
756 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800757 }
758
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000759 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
760 const status_t status = mConsumer->sendTimeline(inputEventId, timeline);
761 ASSERT_EQ(OK, status);
762 }
763
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000764 void consumeEvent(int32_t expectedEventType, int32_t expectedAction,
765 std::optional<int32_t> expectedDisplayId,
766 std::optional<int32_t> expectedFlags) {
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800767 InputEvent* event = consume();
768
769 ASSERT_NE(nullptr, event) << mName.c_str()
770 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800771 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700772 << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType)
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800773 << " event, got " << inputEventTypeToString(event->getType()) << " event";
Arthur Hungb92218b2018-08-14 12:00:21 +0800774
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000775 if (expectedDisplayId.has_value()) {
776 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
777 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800778
Tiger Huang8664f8c2018-10-11 19:14:35 +0800779 switch (expectedEventType) {
780 case AINPUT_EVENT_TYPE_KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800781 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
782 EXPECT_EQ(expectedAction, keyEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000783 if (expectedFlags.has_value()) {
784 EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags());
785 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800786 break;
787 }
788 case AINPUT_EVENT_TYPE_MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800789 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
790 EXPECT_EQ(expectedAction, motionEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000791 if (expectedFlags.has_value()) {
792 EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags());
793 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800794 break;
795 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100796 case AINPUT_EVENT_TYPE_FOCUS: {
797 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
798 }
Prabir Pradhan99987712020-11-10 18:43:05 -0800799 case AINPUT_EVENT_TYPE_CAPTURE: {
800 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
801 }
arthurhungb89ccb02020-12-30 16:19:01 +0800802 case AINPUT_EVENT_TYPE_DRAG: {
803 FAIL() << "Use 'consumeDragEvent' for DRAG events";
804 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800805 default: {
806 FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType;
807 }
808 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800809 }
810
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100811 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
812 InputEvent* event = consume();
813 ASSERT_NE(nullptr, event) << mName.c_str()
814 << ": consumer should have returned non-NULL event.";
815 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
816 << "Got " << inputEventTypeToString(event->getType())
817 << " event instead of FOCUS event";
818
819 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
820 << mName.c_str() << ": event displayId should always be NONE.";
821
822 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
823 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
824 EXPECT_EQ(inTouchMode, focusEvent->getInTouchMode());
825 }
826
Prabir Pradhan99987712020-11-10 18:43:05 -0800827 void consumeCaptureEvent(bool hasCapture) {
828 const InputEvent* event = consume();
829 ASSERT_NE(nullptr, event) << mName.c_str()
830 << ": consumer should have returned non-NULL event.";
831 ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType())
832 << "Got " << inputEventTypeToString(event->getType())
833 << " event instead of CAPTURE event";
834
835 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
836 << mName.c_str() << ": event displayId should always be NONE.";
837
838 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
839 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
840 }
841
arthurhungb89ccb02020-12-30 16:19:01 +0800842 void consumeDragEvent(bool isExiting, float x, float y) {
843 const InputEvent* event = consume();
844 ASSERT_NE(nullptr, event) << mName.c_str()
845 << ": consumer should have returned non-NULL event.";
846 ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType())
847 << "Got " << inputEventTypeToString(event->getType())
848 << " event instead of DRAG event";
849
850 EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
851 << mName.c_str() << ": event displayId should always be NONE.";
852
853 const auto& dragEvent = static_cast<const DragEvent&>(*event);
854 EXPECT_EQ(isExiting, dragEvent.isExiting());
855 EXPECT_EQ(x, dragEvent.getX());
856 EXPECT_EQ(y, dragEvent.getY());
857 }
858
chaviwd1c23182019-12-20 18:44:56 -0800859 void assertNoEvents() {
860 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700861 if (event == nullptr) {
862 return;
863 }
864 if (event->getType() == AINPUT_EVENT_TYPE_KEY) {
865 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
866 ADD_FAILURE() << "Received key event "
867 << KeyEvent::actionToString(keyEvent.getAction());
868 } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) {
869 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
870 ADD_FAILURE() << "Received motion event "
871 << MotionEvent::actionToString(motionEvent.getAction());
872 } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) {
873 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
874 ADD_FAILURE() << "Received focus event, hasFocus = "
875 << (focusEvent.getHasFocus() ? "true" : "false");
Prabir Pradhan99987712020-11-10 18:43:05 -0800876 } else if (event->getType() == AINPUT_EVENT_TYPE_CAPTURE) {
877 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
878 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
879 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700880 }
881 FAIL() << mName.c_str()
882 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -0800883 }
884
885 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
886
887protected:
888 std::unique_ptr<InputConsumer> mConsumer;
889 PreallocatedInputEventFactory mEventFactory;
890
891 std::string mName;
892};
893
894class FakeWindowHandle : public InputWindowHandle {
895public:
896 static const int32_t WIDTH = 600;
897 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -0800898
Chris Yea209fde2020-07-22 13:54:51 -0700899 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
chaviwd1c23182019-12-20 18:44:56 -0800900 const sp<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500901 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -0800902 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500903 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -0700904 base::Result<std::unique_ptr<InputChannel>> channel =
905 dispatcher->createInputChannel(name);
906 token = (*channel)->getConnectionToken();
907 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -0800908 }
909
910 inputApplicationHandle->updateInfo();
911 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
912
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500913 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700914 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -0800915 mInfo.name = name;
Michael Wright44753b12020-07-08 13:48:11 +0100916 mInfo.type = InputWindowInfo::Type::APPLICATION;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500917 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000918 mInfo.alpha = 1.0;
chaviwd1c23182019-12-20 18:44:56 -0800919 mInfo.frameLeft = 0;
920 mInfo.frameTop = 0;
921 mInfo.frameRight = WIDTH;
922 mInfo.frameBottom = HEIGHT;
chaviw1ff3d1e2020-07-01 15:53:47 -0700923 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -0800924 mInfo.globalScaleFactor = 1.0;
925 mInfo.touchableRegion.clear();
926 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
927 mInfo.visible = true;
Vishnu Nair47074b82020-08-14 11:54:47 -0700928 mInfo.focusable = false;
chaviwd1c23182019-12-20 18:44:56 -0800929 mInfo.hasWallpaper = false;
930 mInfo.paused = false;
chaviwd1c23182019-12-20 18:44:56 -0800931 mInfo.ownerPid = INJECTOR_PID;
932 mInfo.ownerUid = INJECTOR_UID;
chaviwd1c23182019-12-20 18:44:56 -0800933 mInfo.displayId = displayId;
934 }
935
936 virtual bool updateInfo() { return true; }
937
Vishnu Nair47074b82020-08-14 11:54:47 -0700938 void setFocusable(bool focusable) { mInfo.focusable = focusable; }
chaviwd1c23182019-12-20 18:44:56 -0800939
Vishnu Nair958da932020-08-21 17:12:37 -0700940 void setVisible(bool visible) { mInfo.visible = visible; }
941
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700942 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500943 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700944 }
945
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700946 void setPaused(bool paused) { mInfo.paused = paused; }
947
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000948 void setAlpha(float alpha) { mInfo.alpha = alpha; }
949
950 void setTouchOcclusionMode(android::os::TouchOcclusionMode mode) {
951 mInfo.touchOcclusionMode = mode;
952 }
953
Bernardo Rufino7393d172021-02-26 13:56:11 +0000954 void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
955
chaviwd1c23182019-12-20 18:44:56 -0800956 void setFrame(const Rect& frame) {
957 mInfo.frameLeft = frame.left;
958 mInfo.frameTop = frame.top;
959 mInfo.frameRight = frame.right;
960 mInfo.frameBottom = frame.bottom;
arthurhungb89ccb02020-12-30 16:19:01 +0800961 mInfo.transform.set(-frame.left, -frame.top);
chaviwd1c23182019-12-20 18:44:56 -0800962 mInfo.touchableRegion.clear();
963 mInfo.addTouchableRegion(frame);
964 }
965
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +0000966 void addFlags(Flags<InputWindowInfo::Flag> flags) { mInfo.flags |= flags; }
967
Michael Wright44753b12020-07-08 13:48:11 +0100968 void setFlags(Flags<InputWindowInfo::Flag> flags) { mInfo.flags = flags; }
chaviwd1c23182019-12-20 18:44:56 -0800969
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500970 void setInputFeatures(InputWindowInfo::Feature features) { mInfo.inputFeatures = features; }
971
chaviw9eaa22c2020-07-01 16:21:27 -0700972 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
973 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
974 }
975
976 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -0700977
yunho.shinf4a80b82020-11-16 21:13:57 +0900978 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
979
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800980 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
981 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
982 expectedFlags);
983 }
984
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700985 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
986 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
987 }
988
Svet Ganov5d3bc372020-01-26 23:11:07 -0800989 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000990 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800991 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId,
992 expectedFlags);
993 }
994
995 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000996 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800997 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId,
998 expectedFlags);
999 }
1000
1001 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001002 int32_t expectedFlags = 0) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001003 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
1004 }
1005
1006 void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
1007 std::optional<int32_t> expectedFlags = std::nullopt) {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001008 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
1009 expectedFlags);
1010 }
1011
Svet Ganov5d3bc372020-01-26 23:11:07 -08001012 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001013 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1014 int32_t expectedFlags = 0) {
1015 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
1016 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001017 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
1018 }
1019
1020 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001021 int32_t expectedFlags = 0) {
1022 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
1023 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001024 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
1025 }
1026
1027 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001028 int32_t expectedFlags = 0) {
Michael Wright3a240c42019-12-10 20:53:41 +00001029 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
1030 expectedFlags);
1031 }
1032
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001033 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1034 int32_t expectedFlags = 0) {
1035 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
1036 expectedFlags);
1037 }
1038
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001039 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
1040 ASSERT_NE(mInputReceiver, nullptr)
1041 << "Cannot consume events from a window with no receiver";
1042 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
1043 }
1044
Prabir Pradhan99987712020-11-10 18:43:05 -08001045 void consumeCaptureEvent(bool hasCapture) {
1046 ASSERT_NE(mInputReceiver, nullptr)
1047 << "Cannot consume events from a window with no receiver";
1048 mInputReceiver->consumeCaptureEvent(hasCapture);
1049 }
1050
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001051 void consumeEvent(int32_t expectedEventType, int32_t expectedAction,
1052 std::optional<int32_t> expectedDisplayId,
1053 std::optional<int32_t> expectedFlags) {
chaviwd1c23182019-12-20 18:44:56 -08001054 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
1055 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
1056 expectedFlags);
1057 }
1058
arthurhungb89ccb02020-12-30 16:19:01 +08001059 void consumeDragEvent(bool isExiting, float x, float y) {
1060 mInputReceiver->consumeDragEvent(isExiting, x, y);
1061 }
1062
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001063 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001064 if (mInputReceiver == nullptr) {
1065 ADD_FAILURE() << "Invalid receive event on window with no receiver";
1066 return std::nullopt;
1067 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001068 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001069 }
1070
1071 void finishEvent(uint32_t sequenceNum) {
1072 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1073 mInputReceiver->finishEvent(sequenceNum);
1074 }
1075
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001076 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
1077 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1078 mInputReceiver->sendTimeline(inputEventId, timeline);
1079 }
1080
chaviwaf87b3e2019-10-01 16:59:28 -07001081 InputEvent* consume() {
1082 if (mInputReceiver == nullptr) {
1083 return nullptr;
1084 }
1085 return mInputReceiver->consume();
1086 }
1087
Arthur Hungb92218b2018-08-14 12:00:21 +08001088 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001089 if (mInputReceiver == nullptr &&
1090 mInfo.inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL)) {
1091 return; // Can't receive events if the window does not have input channel
1092 }
1093 ASSERT_NE(nullptr, mInputReceiver)
1094 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -08001095 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +08001096 }
1097
chaviwaf87b3e2019-10-01 16:59:28 -07001098 sp<IBinder> getToken() { return mInfo.token; }
1099
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001100 const std::string& getName() { return mName; }
1101
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001102 void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) {
1103 mInfo.ownerPid = ownerPid;
1104 mInfo.ownerUid = ownerUid;
1105 }
1106
chaviwd1c23182019-12-20 18:44:56 -08001107private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001108 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -08001109 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001110 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001111};
1112
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001113std::atomic<int32_t> FakeWindowHandle::sId{1};
1114
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001115static InputEventInjectionResult injectKey(
1116 const sp<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
1117 int32_t displayId = ADISPLAY_ID_NONE,
1118 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001119 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
1120 bool allowKeyRepeat = true) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001121 KeyEvent event;
1122 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1123
1124 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001125 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001126 INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE,
1127 repeatCount, currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001128
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001129 int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
1130 if (!allowKeyRepeat) {
1131 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
1132 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001133 // Inject event until dispatch out.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001134 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, syncMode,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001135 injectionTimeout, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001136}
1137
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001138static InputEventInjectionResult injectKeyDown(const sp<InputDispatcher>& dispatcher,
1139 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001140 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId);
1141}
1142
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001143// Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without
1144// sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it
1145// has to be woken up to process the repeating key.
1146static InputEventInjectionResult injectKeyDownNoRepeat(const sp<InputDispatcher>& dispatcher,
1147 int32_t displayId = ADISPLAY_ID_NONE) {
1148 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId,
1149 InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT,
1150 /* allowKeyRepeat */ false);
1151}
1152
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001153static InputEventInjectionResult injectKeyUp(const sp<InputDispatcher>& dispatcher,
1154 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001155 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId);
1156}
1157
Garfield Tandf26e862020-07-01 20:18:19 -07001158class PointerBuilder {
1159public:
1160 PointerBuilder(int32_t id, int32_t toolType) {
1161 mProperties.clear();
1162 mProperties.id = id;
1163 mProperties.toolType = toolType;
1164 mCoords.clear();
1165 }
1166
1167 PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); }
1168
1169 PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); }
1170
1171 PointerBuilder& axis(int32_t axis, float value) {
1172 mCoords.setAxisValue(axis, value);
1173 return *this;
1174 }
1175
1176 PointerProperties buildProperties() const { return mProperties; }
1177
1178 PointerCoords buildCoords() const { return mCoords; }
1179
1180private:
1181 PointerProperties mProperties;
1182 PointerCoords mCoords;
1183};
1184
1185class MotionEventBuilder {
1186public:
1187 MotionEventBuilder(int32_t action, int32_t source) {
1188 mAction = action;
1189 mSource = source;
1190 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1191 }
1192
1193 MotionEventBuilder& eventTime(nsecs_t eventTime) {
1194 mEventTime = eventTime;
1195 return *this;
1196 }
1197
1198 MotionEventBuilder& displayId(int32_t displayId) {
1199 mDisplayId = displayId;
1200 return *this;
1201 }
1202
1203 MotionEventBuilder& actionButton(int32_t actionButton) {
1204 mActionButton = actionButton;
1205 return *this;
1206 }
1207
arthurhung6d4bed92021-03-17 11:59:33 +08001208 MotionEventBuilder& buttonState(int32_t buttonState) {
1209 mButtonState = buttonState;
Garfield Tandf26e862020-07-01 20:18:19 -07001210 return *this;
1211 }
1212
1213 MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) {
1214 mRawXCursorPosition = rawXCursorPosition;
1215 return *this;
1216 }
1217
1218 MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) {
1219 mRawYCursorPosition = rawYCursorPosition;
1220 return *this;
1221 }
1222
1223 MotionEventBuilder& pointer(PointerBuilder pointer) {
1224 mPointers.push_back(pointer);
1225 return *this;
1226 }
1227
1228 MotionEvent build() {
1229 std::vector<PointerProperties> pointerProperties;
1230 std::vector<PointerCoords> pointerCoords;
1231 for (const PointerBuilder& pointer : mPointers) {
1232 pointerProperties.push_back(pointer.buildProperties());
1233 pointerCoords.push_back(pointer.buildCoords());
1234 }
1235
1236 // Set mouse cursor position for the most common cases to avoid boilerplate.
1237 if (mSource == AINPUT_SOURCE_MOUSE &&
1238 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) &&
1239 mPointers.size() == 1) {
1240 mRawXCursorPosition = pointerCoords[0].getX();
1241 mRawYCursorPosition = pointerCoords[0].getY();
1242 }
1243
1244 MotionEvent event;
chaviw9eaa22c2020-07-01 16:21:27 -07001245 ui::Transform identityTransform;
Garfield Tandf26e862020-07-01 20:18:19 -07001246 event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC,
1247 mAction, mActionButton, /* flags */ 0, /* edgeFlags */ 0, AMETA_NONE,
chaviw9eaa22c2020-07-01 16:21:27 -07001248 mButtonState, MotionClassification::NONE, identityTransform,
1249 /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition,
Evan Rosky84f07f02021-04-16 10:42:42 -07001250 mRawYCursorPosition, mDisplayWidth, mDisplayHeight, mEventTime, mEventTime,
1251 mPointers.size(), pointerProperties.data(), pointerCoords.data());
Garfield Tandf26e862020-07-01 20:18:19 -07001252
1253 return event;
1254 }
1255
1256private:
1257 int32_t mAction;
1258 int32_t mSource;
1259 nsecs_t mEventTime;
1260 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1261 int32_t mActionButton{0};
1262 int32_t mButtonState{0};
1263 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1264 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
Evan Rosky84f07f02021-04-16 10:42:42 -07001265 int32_t mDisplayWidth{AMOTION_EVENT_INVALID_DISPLAY_SIZE};
1266 int32_t mDisplayHeight{AMOTION_EVENT_INVALID_DISPLAY_SIZE};
Garfield Tandf26e862020-07-01 20:18:19 -07001267
1268 std::vector<PointerBuilder> mPointers;
1269};
1270
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001271static InputEventInjectionResult injectMotionEvent(
Garfield Tandf26e862020-07-01 20:18:19 -07001272 const sp<InputDispatcher>& dispatcher, const MotionEvent& event,
1273 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001274 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT) {
Garfield Tandf26e862020-07-01 20:18:19 -07001275 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, injectionMode,
1276 injectionTimeout,
1277 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
1278}
1279
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001280static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001281 const sp<InputDispatcher>& dispatcher, int32_t action, int32_t source, int32_t displayId,
1282 const PointF& position,
1283 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001284 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1285 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001286 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001287 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC)) {
Garfield Tandf26e862020-07-01 20:18:19 -07001288 MotionEvent event = MotionEventBuilder(action, source)
1289 .displayId(displayId)
1290 .eventTime(eventTime)
1291 .rawXCursorPosition(cursorPosition.x)
1292 .rawYCursorPosition(cursorPosition.y)
1293 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1294 .x(position.x)
1295 .y(position.y))
1296 .build();
Arthur Hungb92218b2018-08-14 12:00:21 +08001297
1298 // Inject event until dispatch out.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001299 return injectMotionEvent(dispatcher, event, injectionTimeout, injectionMode);
Arthur Hungb92218b2018-08-14 12:00:21 +08001300}
1301
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001302static InputEventInjectionResult injectMotionDown(const sp<InputDispatcher>& dispatcher,
1303 int32_t source, int32_t displayId,
1304 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001305 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001306}
1307
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001308static InputEventInjectionResult injectMotionUp(const sp<InputDispatcher>& dispatcher,
1309 int32_t source, int32_t displayId,
1310 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001311 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001312}
1313
Jackal Guof9696682018-10-05 12:23:23 +08001314static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1315 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1316 // Define a valid key event.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001317 NotifyKeyArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1318 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A,
1319 KEY_A, AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001320
1321 return args;
1322}
1323
chaviwd1c23182019-12-20 18:44:56 -08001324static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId,
1325 const std::vector<PointF>& points) {
1326 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001327 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1328 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1329 }
1330
chaviwd1c23182019-12-20 18:44:56 -08001331 PointerProperties pointerProperties[pointerCount];
1332 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001333
chaviwd1c23182019-12-20 18:44:56 -08001334 for (size_t i = 0; i < pointerCount; i++) {
1335 pointerProperties[i].clear();
1336 pointerProperties[i].id = i;
1337 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001338
chaviwd1c23182019-12-20 18:44:56 -08001339 pointerCoords[i].clear();
1340 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1341 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1342 }
Jackal Guof9696682018-10-05 12:23:23 +08001343
1344 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1345 // Define a valid motion event.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001346 NotifyMotionArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001347 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1348 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001349 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1350 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001351 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1352 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001353
1354 return args;
1355}
1356
chaviwd1c23182019-12-20 18:44:56 -08001357static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1358 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1359}
1360
Prabir Pradhan99987712020-11-10 18:43:05 -08001361static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(bool enabled) {
1362 return NotifyPointerCaptureChangedArgs(/* id */ 0, systemTime(SYSTEM_TIME_MONOTONIC), enabled);
1363}
1364
Arthur Hungb92218b2018-08-14 12:00:21 +08001365TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001366 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001367 sp<FakeWindowHandle> window =
1368 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001369
Arthur Hung72d8dc32020-03-28 00:48:39 +00001370 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001371 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1372 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1373 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001374
1375 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001376 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001377}
1378
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001379/**
1380 * Calling setInputWindows once with FLAG_NOT_TOUCH_MODAL should not cause any issues.
1381 * To ensure that window receives only events that were directly inside of it, add
1382 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1383 * when finding touched windows.
1384 * This test serves as a sanity check for the next test, where setInputWindows is
1385 * called twice.
1386 */
1387TEST_F(InputDispatcherTest, SetInputWindowOnce_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001388 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001389 sp<FakeWindowHandle> window =
1390 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1391 window->setFrame(Rect(0, 0, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01001392 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001393
1394 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001395 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001396 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1397 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001398 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001399
1400 // Window should receive motion event.
1401 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1402}
1403
1404/**
1405 * Calling setInputWindows twice, with the same info, should not cause any issues.
1406 * To ensure that window receives only events that were directly inside of it, add
1407 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1408 * when finding touched windows.
1409 */
1410TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001411 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001412 sp<FakeWindowHandle> window =
1413 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1414 window->setFrame(Rect(0, 0, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01001415 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001416
1417 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1418 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001419 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001420 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1421 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001422 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001423
1424 // Window should receive motion event.
1425 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1426}
1427
Arthur Hungb92218b2018-08-14 12:00:21 +08001428// The foreground window should receive the first touch down event.
1429TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001430 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001431 sp<FakeWindowHandle> windowTop =
1432 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
1433 sp<FakeWindowHandle> windowSecond =
1434 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001435
Arthur Hung72d8dc32020-03-28 00:48:39 +00001436 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001437 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1438 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1439 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001440
1441 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001442 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001443 windowSecond->assertNoEvents();
1444}
1445
Garfield Tandf26e862020-07-01 20:18:19 -07001446TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07001447 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07001448 sp<FakeWindowHandle> windowLeft =
1449 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1450 windowLeft->setFrame(Rect(0, 0, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001451 windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001452 sp<FakeWindowHandle> windowRight =
1453 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1454 windowRight->setFrame(Rect(600, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001455 windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001456
1457 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1458
1459 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
1460
1461 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001462 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001463 injectMotionEvent(mDispatcher,
1464 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1465 AINPUT_SOURCE_MOUSE)
1466 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1467 .x(900)
1468 .y(400))
1469 .build()));
1470 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1471 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1472 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1473 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1474
1475 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001476 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001477 injectMotionEvent(mDispatcher,
1478 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1479 AINPUT_SOURCE_MOUSE)
1480 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1481 .x(300)
1482 .y(400))
1483 .build()));
1484 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1485 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1486 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1487 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1488 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1489 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1490
1491 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001492 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001493 injectMotionEvent(mDispatcher,
1494 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1495 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1496 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1497 .x(300)
1498 .y(400))
1499 .build()));
1500 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1501
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001502 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001503 injectMotionEvent(mDispatcher,
1504 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
1505 AINPUT_SOURCE_MOUSE)
1506 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1507 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1508 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1509 .x(300)
1510 .y(400))
1511 .build()));
1512 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1513 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1514
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001515 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001516 injectMotionEvent(mDispatcher,
1517 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1518 AINPUT_SOURCE_MOUSE)
1519 .buttonState(0)
1520 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1521 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1522 .x(300)
1523 .y(400))
1524 .build()));
1525 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1526 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1527
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001528 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001529 injectMotionEvent(mDispatcher,
1530 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
1531 .buttonState(0)
1532 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1533 .x(300)
1534 .y(400))
1535 .build()));
1536 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1537
1538 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001539 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001540 injectMotionEvent(mDispatcher,
1541 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1542 AINPUT_SOURCE_MOUSE)
1543 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1544 .x(900)
1545 .y(400))
1546 .build()));
1547 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1548 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1549 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1550 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1551 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1552 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1553}
1554
1555// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
1556// directly in this test.
1557TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07001558 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07001559 sp<FakeWindowHandle> window =
1560 new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1561 window->setFrame(Rect(0, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001562 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001563
1564 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1565
1566 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1567
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_HOVER_ENTER,
1571 AINPUT_SOURCE_MOUSE)
1572 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1573 .x(300)
1574 .y(400))
1575 .build()));
1576 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1577 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1578
1579 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001580 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001581 injectMotionEvent(mDispatcher,
1582 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1583 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1584 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1585 .x(300)
1586 .y(400))
1587 .build()));
1588 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1589
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001590 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001591 injectMotionEvent(mDispatcher,
1592 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
1593 AINPUT_SOURCE_MOUSE)
1594 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1595 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1596 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1597 .x(300)
1598 .y(400))
1599 .build()));
1600 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1601 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1602
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001603 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001604 injectMotionEvent(mDispatcher,
1605 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1606 AINPUT_SOURCE_MOUSE)
1607 .buttonState(0)
1608 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1609 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1610 .x(300)
1611 .y(400))
1612 .build()));
1613 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1614 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1615
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001616 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001617 injectMotionEvent(mDispatcher,
1618 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
1619 .buttonState(0)
1620 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1621 .x(300)
1622 .y(400))
1623 .build()));
1624 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1625
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001626 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001627 injectMotionEvent(mDispatcher,
1628 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
1629 AINPUT_SOURCE_MOUSE)
1630 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1631 .x(300)
1632 .y(400))
1633 .build()));
1634 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1635 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1636}
1637
Garfield Tan00f511d2019-06-12 16:55:40 -07001638TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07001639 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07001640
1641 sp<FakeWindowHandle> windowLeft =
1642 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1643 windowLeft->setFrame(Rect(0, 0, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001644 windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07001645 sp<FakeWindowHandle> windowRight =
1646 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1647 windowRight->setFrame(Rect(600, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001648 windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07001649
1650 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1651
Arthur Hung72d8dc32020-03-28 00:48:39 +00001652 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07001653
1654 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
1655 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001656 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07001657 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001658 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001659 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07001660 windowRight->assertNoEvents();
1661}
1662
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001663TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001664 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001665 sp<FakeWindowHandle> window =
1666 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07001667 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001668
Arthur Hung72d8dc32020-03-28 00:48:39 +00001669 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001670 setFocusedWindow(window);
1671
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001672 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001673
1674 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1675 mDispatcher->notifyKey(&keyArgs);
1676
1677 // Window should receive key down event.
1678 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1679
1680 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
1681 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001682 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001683 mDispatcher->notifyDeviceReset(&args);
1684 window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
1685 AKEY_EVENT_FLAG_CANCELED);
1686}
1687
1688TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001689 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001690 sp<FakeWindowHandle> window =
1691 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1692
Arthur Hung72d8dc32020-03-28 00:48:39 +00001693 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001694
1695 NotifyMotionArgs motionArgs =
1696 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1697 ADISPLAY_ID_DEFAULT);
1698 mDispatcher->notifyMotion(&motionArgs);
1699
1700 // Window should receive motion down event.
1701 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1702
1703 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
1704 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001705 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001706 mDispatcher->notifyDeviceReset(&args);
1707 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT,
1708 0 /*expectedFlags*/);
1709}
1710
Svet Ganov5d3bc372020-01-26 23:11:07 -08001711TEST_F(InputDispatcherTest, TransferTouchFocus_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07001712 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001713
1714 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001715 sp<FakeWindowHandle> firstWindow =
1716 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1717 sp<FakeWindowHandle> secondWindow =
1718 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001719
1720 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001721 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001722
1723 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001724 NotifyMotionArgs downMotionArgs =
1725 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1726 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001727 mDispatcher->notifyMotion(&downMotionArgs);
1728 // Only the first window should get the down event
1729 firstWindow->consumeMotionDown();
1730 secondWindow->assertNoEvents();
1731
1732 // Transfer touch focus to the second window
1733 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1734 // The first window gets cancel and the second gets down
1735 firstWindow->consumeMotionCancel();
1736 secondWindow->consumeMotionDown();
1737
1738 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001739 NotifyMotionArgs upMotionArgs =
1740 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1741 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001742 mDispatcher->notifyMotion(&upMotionArgs);
1743 // The first window gets no events and the second gets up
1744 firstWindow->assertNoEvents();
1745 secondWindow->consumeMotionUp();
1746}
1747
1748TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointerNoSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001749 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001750
1751 PointF touchPoint = {10, 10};
1752
1753 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001754 sp<FakeWindowHandle> firstWindow =
1755 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1756 sp<FakeWindowHandle> secondWindow =
1757 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001758
1759 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001760 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001761
1762 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001763 NotifyMotionArgs downMotionArgs =
1764 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1765 ADISPLAY_ID_DEFAULT, {touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001766 mDispatcher->notifyMotion(&downMotionArgs);
1767 // Only the first window should get the down event
1768 firstWindow->consumeMotionDown();
1769 secondWindow->assertNoEvents();
1770
1771 // Send pointer down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001772 NotifyMotionArgs pointerDownMotionArgs =
1773 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1774 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1775 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1776 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001777 mDispatcher->notifyMotion(&pointerDownMotionArgs);
1778 // Only the first window should get the pointer down event
1779 firstWindow->consumeMotionPointerDown(1);
1780 secondWindow->assertNoEvents();
1781
1782 // Transfer touch focus to the second window
1783 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1784 // The first window gets cancel and the second gets down and pointer down
1785 firstWindow->consumeMotionCancel();
1786 secondWindow->consumeMotionDown();
1787 secondWindow->consumeMotionPointerDown(1);
1788
1789 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001790 NotifyMotionArgs pointerUpMotionArgs =
1791 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1792 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1793 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1794 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001795 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1796 // The first window gets nothing and the second gets pointer up
1797 firstWindow->assertNoEvents();
1798 secondWindow->consumeMotionPointerUp(1);
1799
1800 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001801 NotifyMotionArgs upMotionArgs =
1802 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1803 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001804 mDispatcher->notifyMotion(&upMotionArgs);
1805 // The first window gets nothing and the second gets up
1806 firstWindow->assertNoEvents();
1807 secondWindow->consumeMotionUp();
1808}
1809
1810TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001811 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001812
1813 // Create a non touch modal window that supports split touch
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001814 sp<FakeWindowHandle> firstWindow =
1815 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001816 firstWindow->setFrame(Rect(0, 0, 600, 400));
Michael Wright44753b12020-07-08 13:48:11 +01001817 firstWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1818 InputWindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001819
1820 // Create a non touch modal window that supports split touch
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001821 sp<FakeWindowHandle> secondWindow =
1822 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001823 secondWindow->setFrame(Rect(0, 400, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001824 secondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1825 InputWindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001826
1827 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001828 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001829
1830 PointF pointInFirst = {300, 200};
1831 PointF pointInSecond = {300, 600};
1832
1833 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001834 NotifyMotionArgs firstDownMotionArgs =
1835 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1836 ADISPLAY_ID_DEFAULT, {pointInFirst});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001837 mDispatcher->notifyMotion(&firstDownMotionArgs);
1838 // Only the first window should get the down event
1839 firstWindow->consumeMotionDown();
1840 secondWindow->assertNoEvents();
1841
1842 // Send down to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001843 NotifyMotionArgs secondDownMotionArgs =
1844 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1845 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1846 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1847 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001848 mDispatcher->notifyMotion(&secondDownMotionArgs);
1849 // The first window gets a move and the second a down
1850 firstWindow->consumeMotionMove();
1851 secondWindow->consumeMotionDown();
1852
1853 // Transfer touch focus to the second window
1854 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1855 // The first window gets cancel and the new gets pointer down (it already saw down)
1856 firstWindow->consumeMotionCancel();
1857 secondWindow->consumeMotionPointerDown(1);
1858
1859 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001860 NotifyMotionArgs pointerUpMotionArgs =
1861 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1862 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1863 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1864 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001865 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1866 // The first window gets nothing and the second gets pointer up
1867 firstWindow->assertNoEvents();
1868 secondWindow->consumeMotionPointerUp(1);
1869
1870 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001871 NotifyMotionArgs upMotionArgs =
1872 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1873 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001874 mDispatcher->notifyMotion(&upMotionArgs);
1875 // The first window gets nothing and the second gets up
1876 firstWindow->assertNoEvents();
1877 secondWindow->consumeMotionUp();
1878}
1879
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001880TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07001881 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001882 sp<FakeWindowHandle> window =
1883 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1884
Vishnu Nair47074b82020-08-14 11:54:47 -07001885 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001886 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001887 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001888
1889 window->consumeFocusEvent(true);
1890
1891 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1892 mDispatcher->notifyKey(&keyArgs);
1893
1894 // Window should receive key down event.
1895 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1896}
1897
1898TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07001899 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001900 sp<FakeWindowHandle> window =
1901 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1902
Arthur Hung72d8dc32020-03-28 00:48:39 +00001903 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001904
1905 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1906 mDispatcher->notifyKey(&keyArgs);
1907 mDispatcher->waitForIdle();
1908
1909 window->assertNoEvents();
1910}
1911
1912// If a window is touchable, but does not have focus, it should receive motion events, but not keys
1913TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07001914 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001915 sp<FakeWindowHandle> window =
1916 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1917
Arthur Hung72d8dc32020-03-28 00:48:39 +00001918 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001919
1920 // Send key
1921 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1922 mDispatcher->notifyKey(&keyArgs);
1923 // Send motion
1924 NotifyMotionArgs motionArgs =
1925 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1926 ADISPLAY_ID_DEFAULT);
1927 mDispatcher->notifyMotion(&motionArgs);
1928
1929 // Window should receive only the motion event
1930 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1931 window->assertNoEvents(); // Key event or focus event will not be received
1932}
1933
arthurhungea3f4fc2020-12-21 23:18:53 +08001934TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
1935 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1936
1937 // Create first non touch modal window that supports split touch
1938 sp<FakeWindowHandle> firstWindow =
1939 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1940 firstWindow->setFrame(Rect(0, 0, 600, 400));
1941 firstWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1942 InputWindowInfo::Flag::SPLIT_TOUCH);
1943
1944 // Create second non touch modal window that supports split touch
1945 sp<FakeWindowHandle> secondWindow =
1946 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
1947 secondWindow->setFrame(Rect(0, 400, 600, 800));
1948 secondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1949 InputWindowInfo::Flag::SPLIT_TOUCH);
1950
1951 // Add the windows to the dispatcher
1952 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
1953
1954 PointF pointInFirst = {300, 200};
1955 PointF pointInSecond = {300, 600};
1956
1957 // Send down to the first window
1958 NotifyMotionArgs firstDownMotionArgs =
1959 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1960 ADISPLAY_ID_DEFAULT, {pointInFirst});
1961 mDispatcher->notifyMotion(&firstDownMotionArgs);
1962 // Only the first window should get the down event
1963 firstWindow->consumeMotionDown();
1964 secondWindow->assertNoEvents();
1965
1966 // Send down to the second window
1967 NotifyMotionArgs secondDownMotionArgs =
1968 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1969 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1970 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1971 {pointInFirst, pointInSecond});
1972 mDispatcher->notifyMotion(&secondDownMotionArgs);
1973 // The first window gets a move and the second a down
1974 firstWindow->consumeMotionMove();
1975 secondWindow->consumeMotionDown();
1976
1977 // Send pointer cancel to the second window
1978 NotifyMotionArgs pointerUpMotionArgs =
1979 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1980 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1981 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1982 {pointInFirst, pointInSecond});
1983 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
1984 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1985 // The first window gets move and the second gets cancel.
1986 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
1987 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
1988
1989 // Send up event.
1990 NotifyMotionArgs upMotionArgs =
1991 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1992 ADISPLAY_ID_DEFAULT);
1993 mDispatcher->notifyMotion(&upMotionArgs);
1994 // The first window gets up and the second gets nothing.
1995 firstWindow->consumeMotionUp();
1996 secondWindow->assertNoEvents();
1997}
1998
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001999TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
2000 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2001
2002 sp<FakeWindowHandle> window =
2003 new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2004 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2005 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
2006 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
2007 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
2008
2009 window->sendTimeline(1 /*inputEventId*/, graphicsTimeline);
2010 window->assertNoEvents();
2011 mDispatcher->waitForIdle();
2012}
2013
chaviwd1c23182019-12-20 18:44:56 -08002014class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00002015public:
2016 FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name,
chaviwd1c23182019-12-20 18:44:56 -08002017 int32_t displayId, bool isGestureMonitor = false) {
Garfield Tan15601662020-09-22 15:32:38 -07002018 base::Result<std::unique_ptr<InputChannel>> channel =
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +00002019 dispatcher->createInputMonitor(displayId, isGestureMonitor, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07002020 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00002021 }
2022
chaviwd1c23182019-12-20 18:44:56 -08002023 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
2024
2025 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
2026 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN,
2027 expectedDisplayId, expectedFlags);
2028 }
2029
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002030 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
2031
2032 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
2033
chaviwd1c23182019-12-20 18:44:56 -08002034 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
2035 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
2036 expectedDisplayId, expectedFlags);
2037 }
2038
2039 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
2040 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP,
2041 expectedDisplayId, expectedFlags);
2042 }
2043
Evan Rosky84f07f02021-04-16 10:42:42 -07002044 MotionEvent* consumeMotion() {
2045 InputEvent* event = mInputReceiver->consume();
2046 if (!event) {
2047 ADD_FAILURE() << "No event was produced";
2048 return nullptr;
2049 }
2050 if (event->getType() != AINPUT_EVENT_TYPE_MOTION) {
2051 ADD_FAILURE() << "Received event of type " << event->getType() << " instead of motion";
2052 return nullptr;
2053 }
2054 return static_cast<MotionEvent*>(event);
2055 }
2056
chaviwd1c23182019-12-20 18:44:56 -08002057 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
2058
2059private:
2060 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00002061};
2062
2063// Tests for gesture monitors
2064TEST_F(InputDispatcherTest, GestureMonitor_ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07002065 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00002066 sp<FakeWindowHandle> window =
2067 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002068 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00002069
chaviwd1c23182019-12-20 18:44:56 -08002070 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
2071 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00002072
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002073 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00002074 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002075 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00002076 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002077 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00002078}
2079
2080TEST_F(InputDispatcherTest, GestureMonitor_DoesNotReceiveKeyEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07002081 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00002082 sp<FakeWindowHandle> window =
2083 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2084
2085 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002086 window->setFocusable(true);
Michael Wright3a240c42019-12-10 20:53:41 +00002087
Arthur Hung72d8dc32020-03-28 00:48:39 +00002088 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002089 setFocusedWindow(window);
2090
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002091 window->consumeFocusEvent(true);
Michael Wright3a240c42019-12-10 20:53:41 +00002092
chaviwd1c23182019-12-20 18:44:56 -08002093 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
2094 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00002095
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002096 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2097 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00002098 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002099 monitor.assertNoEvents();
Michael Wright3a240c42019-12-10 20:53:41 +00002100}
2101
2102TEST_F(InputDispatcherTest, GestureMonitor_CanPilferAfterWindowIsRemovedMidStream) {
Chris Yea209fde2020-07-22 13:54:51 -07002103 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00002104 sp<FakeWindowHandle> window =
2105 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002106 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00002107
chaviwd1c23182019-12-20 18:44:56 -08002108 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
2109 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00002110
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002111 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00002112 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002113 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00002114 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002115 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00002116
2117 window->releaseChannel();
2118
chaviwd1c23182019-12-20 18:44:56 -08002119 mDispatcher->pilferPointers(monitor.getToken());
Michael Wright3a240c42019-12-10 20:53:41 +00002120
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002121 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00002122 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002123 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08002124 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00002125}
2126
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002127TEST_F(InputDispatcherTest, UnresponsiveGestureMonitor_GetsAnr) {
2128 FakeMonitorReceiver monitor =
2129 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
2130 true /*isGestureMonitor*/);
2131
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002132 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002133 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
2134 std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
2135 ASSERT_TRUE(consumeSeq);
2136
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00002137 mFakePolicy->assertNotifyMonitorUnresponsiveWasCalled(DISPATCHING_TIMEOUT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002138 monitor.finishEvent(*consumeSeq);
2139 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00002140 mFakePolicy->assertNotifyMonitorResponsiveWasCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002141}
2142
Evan Rosky84f07f02021-04-16 10:42:42 -07002143// Tests for gesture monitors
2144TEST_F(InputDispatcherTest, GestureMonitor_NoWindowTransform) {
2145 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2146 sp<FakeWindowHandle> window =
2147 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2148 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2149 window->setWindowOffset(20, 40);
2150 window->setWindowTransform(0, 1, -1, 0);
2151
2152 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
2153 true /*isGestureMonitor*/);
2154
2155 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2156 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2157 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2158 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2159 MotionEvent* event = monitor.consumeMotion();
2160 // Even though window has transform, gesture monitor must not.
2161 ASSERT_EQ(ui::Transform(), event->getTransform());
2162}
2163
chaviw81e2bb92019-12-18 15:03:51 -08002164TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002165 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
chaviw81e2bb92019-12-18 15:03:51 -08002166 sp<FakeWindowHandle> window =
2167 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2168
Arthur Hung72d8dc32020-03-28 00:48:39 +00002169 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08002170
2171 NotifyMotionArgs motionArgs =
2172 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2173 ADISPLAY_ID_DEFAULT);
2174
2175 mDispatcher->notifyMotion(&motionArgs);
2176 // Window should receive motion down event.
2177 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2178
2179 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002180 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08002181 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
2182 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
2183 motionArgs.pointerCoords[0].getX() - 10);
2184
2185 mDispatcher->notifyMotion(&motionArgs);
2186 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
2187 0 /*expectedFlags*/);
2188}
2189
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002190/**
2191 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
2192 * the device default right away. In the test scenario, we check both the default value,
2193 * and the action of enabling / disabling.
2194 */
2195TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07002196 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002197 sp<FakeWindowHandle> window =
2198 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2199
2200 // Set focused application.
2201 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002202 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002203
2204 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00002205 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002206 setFocusedWindow(window);
2207
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002208 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2209
2210 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07002211 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002212 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002213 window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/);
2214
2215 SCOPED_TRACE("Disable touch mode");
2216 mDispatcher->setInTouchMode(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07002217 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002218 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002219 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002220 window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/);
2221
2222 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07002223 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002224 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002225 window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/);
2226
2227 SCOPED_TRACE("Enable touch mode again");
2228 mDispatcher->setInTouchMode(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07002229 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002230 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002231 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002232 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2233
2234 window->assertNoEvents();
2235}
2236
Gang Wange9087892020-01-07 12:17:14 -05002237TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002238 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Gang Wange9087892020-01-07 12:17:14 -05002239 sp<FakeWindowHandle> window =
2240 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2241
2242 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002243 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05002244
Arthur Hung72d8dc32020-03-28 00:48:39 +00002245 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002246 setFocusedWindow(window);
2247
Gang Wange9087892020-01-07 12:17:14 -05002248 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2249
2250 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
2251 mDispatcher->notifyKey(&keyArgs);
2252
2253 InputEvent* event = window->consume();
2254 ASSERT_NE(event, nullptr);
2255
2256 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
2257 ASSERT_NE(verified, nullptr);
2258 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
2259
2260 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
2261 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
2262 ASSERT_EQ(keyArgs.source, verified->source);
2263 ASSERT_EQ(keyArgs.displayId, verified->displayId);
2264
2265 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
2266
2267 ASSERT_EQ(keyArgs.action, verifiedKey.action);
2268 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05002269 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
2270 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
2271 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
2272 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
2273 ASSERT_EQ(0, verifiedKey.repeatCount);
2274}
2275
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002276TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002277 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002278 sp<FakeWindowHandle> window =
2279 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2280
2281 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2282
Arthur Hung72d8dc32020-03-28 00:48:39 +00002283 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002284
2285 NotifyMotionArgs motionArgs =
2286 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2287 ADISPLAY_ID_DEFAULT);
2288 mDispatcher->notifyMotion(&motionArgs);
2289
2290 InputEvent* event = window->consume();
2291 ASSERT_NE(event, nullptr);
2292
2293 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
2294 ASSERT_NE(verified, nullptr);
2295 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
2296
2297 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
2298 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
2299 EXPECT_EQ(motionArgs.source, verified->source);
2300 EXPECT_EQ(motionArgs.displayId, verified->displayId);
2301
2302 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
2303
2304 EXPECT_EQ(motionArgs.pointerCoords[0].getX(), verifiedMotion.rawX);
2305 EXPECT_EQ(motionArgs.pointerCoords[0].getY(), verifiedMotion.rawY);
2306 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
2307 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
2308 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
2309 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
2310 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
2311}
2312
Prabir Pradhanbd527712021-03-09 19:17:09 -08002313TEST_F(InputDispatcherTest, NonPointerMotionEvent_NotTransformed) {
yunho.shinf4a80b82020-11-16 21:13:57 +09002314 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2315 sp<FakeWindowHandle> window =
2316 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2317 const std::string name = window->getName();
2318
2319 // Window gets transformed by offset values.
2320 window->setWindowOffset(500.0f, 500.0f);
2321
2322 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2323 window->setFocusable(true);
2324
2325 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2326
2327 // First, we set focused window so that focusedWindowHandle is not null.
2328 setFocusedWindow(window);
2329
2330 // Second, we consume focus event if it is right or wrong according to onFocusChangedLocked.
2331 window->consumeFocusEvent(true);
2332
Prabir Pradhanbd527712021-03-09 19:17:09 -08002333 constexpr const std::array nonPointerSources = {AINPUT_SOURCE_TRACKBALL,
2334 AINPUT_SOURCE_MOUSE_RELATIVE,
2335 AINPUT_SOURCE_JOYSTICK};
2336 for (const int source : nonPointerSources) {
2337 // Notify motion with a non-pointer source.
2338 NotifyMotionArgs motionArgs =
2339 generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, source, ADISPLAY_ID_DEFAULT);
2340 mDispatcher->notifyMotion(&motionArgs);
yunho.shinf4a80b82020-11-16 21:13:57 +09002341
Prabir Pradhanbd527712021-03-09 19:17:09 -08002342 InputEvent* event = window->consume();
2343 ASSERT_NE(event, nullptr);
2344 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
2345 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
2346 << " event, got " << inputEventTypeToString(event->getType()) << " event";
yunho.shinf4a80b82020-11-16 21:13:57 +09002347
Prabir Pradhanbd527712021-03-09 19:17:09 -08002348 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
2349 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, motionEvent.getAction());
2350 EXPECT_EQ(motionArgs.pointerCount, motionEvent.getPointerCount());
yunho.shinf4a80b82020-11-16 21:13:57 +09002351
Prabir Pradhanbd527712021-03-09 19:17:09 -08002352 float expectedX = motionArgs.pointerCoords[0].getX();
2353 float expectedY = motionArgs.pointerCoords[0].getY();
yunho.shinf4a80b82020-11-16 21:13:57 +09002354
Prabir Pradhanbd527712021-03-09 19:17:09 -08002355 // Ensure the axis values from the final motion event are not transformed.
2356 EXPECT_EQ(expectedX, motionEvent.getX(0))
2357 << "expected " << expectedX << " for x coord of " << name.c_str() << ", got "
2358 << motionEvent.getX(0);
2359 EXPECT_EQ(expectedY, motionEvent.getY(0))
2360 << "expected " << expectedY << " for y coord of " << name.c_str() << ", got "
2361 << motionEvent.getY(0);
2362 // Ensure the raw and transformed axis values for the motion event are the same.
2363 EXPECT_EQ(motionEvent.getRawX(0), motionEvent.getX(0))
2364 << "expected raw and transformed X-axis values to be equal";
2365 EXPECT_EQ(motionEvent.getRawY(0), motionEvent.getY(0))
2366 << "expected raw and transformed Y-axis values to be equal";
2367 }
yunho.shinf4a80b82020-11-16 21:13:57 +09002368}
2369
chaviw09c8d2d2020-08-24 15:48:26 -07002370/**
2371 * Ensure that separate calls to sign the same data are generating the same key.
2372 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
2373 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
2374 * tests.
2375 */
2376TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
2377 KeyEvent event = getTestKeyEvent();
2378 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
2379
2380 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
2381 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
2382 ASSERT_EQ(hmac1, hmac2);
2383}
2384
2385/**
2386 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
2387 */
2388TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
2389 KeyEvent event = getTestKeyEvent();
2390 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
2391 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
2392
2393 verifiedEvent.deviceId += 1;
2394 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2395
2396 verifiedEvent.source += 1;
2397 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2398
2399 verifiedEvent.eventTimeNanos += 1;
2400 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2401
2402 verifiedEvent.displayId += 1;
2403 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2404
2405 verifiedEvent.action += 1;
2406 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2407
2408 verifiedEvent.downTimeNanos += 1;
2409 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2410
2411 verifiedEvent.flags += 1;
2412 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2413
2414 verifiedEvent.keyCode += 1;
2415 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2416
2417 verifiedEvent.scanCode += 1;
2418 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2419
2420 verifiedEvent.metaState += 1;
2421 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2422
2423 verifiedEvent.repeatCount += 1;
2424 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2425}
2426
Vishnu Nair958da932020-08-21 17:12:37 -07002427TEST_F(InputDispatcherTest, SetFocusedWindow) {
2428 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2429 sp<FakeWindowHandle> windowTop =
2430 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2431 sp<FakeWindowHandle> windowSecond =
2432 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2433 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2434
2435 // Top window is also focusable but is not granted focus.
2436 windowTop->setFocusable(true);
2437 windowSecond->setFocusable(true);
2438 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2439 setFocusedWindow(windowSecond);
2440
2441 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002442 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2443 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002444
2445 // Focused window should receive event.
2446 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
2447 windowTop->assertNoEvents();
2448}
2449
2450TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
2451 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2452 sp<FakeWindowHandle> window =
2453 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2454 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2455
2456 window->setFocusable(true);
2457 // Release channel for window is no longer valid.
2458 window->releaseChannel();
2459 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2460 setFocusedWindow(window);
2461
2462 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002463 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2464 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002465
2466 // window channel is invalid, so it should not receive any input event.
2467 window->assertNoEvents();
2468}
2469
2470TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
2471 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2472 sp<FakeWindowHandle> window =
2473 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2474 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2475
2476 // Window is not focusable.
2477 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2478 setFocusedWindow(window);
2479
2480 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002481 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2482 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002483
2484 // window is invalid, so it should not receive any input event.
2485 window->assertNoEvents();
2486}
2487
2488TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
2489 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2490 sp<FakeWindowHandle> windowTop =
2491 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2492 sp<FakeWindowHandle> windowSecond =
2493 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2494 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2495
2496 windowTop->setFocusable(true);
2497 windowSecond->setFocusable(true);
2498 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2499 setFocusedWindow(windowTop);
2500 windowTop->consumeFocusEvent(true);
2501
2502 setFocusedWindow(windowSecond, windowTop);
2503 windowSecond->consumeFocusEvent(true);
2504 windowTop->consumeFocusEvent(false);
2505
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002506 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2507 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002508
2509 // Focused window should receive event.
2510 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
2511}
2512
2513TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) {
2514 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2515 sp<FakeWindowHandle> windowTop =
2516 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2517 sp<FakeWindowHandle> windowSecond =
2518 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2519 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2520
2521 windowTop->setFocusable(true);
2522 windowSecond->setFocusable(true);
2523 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2524 setFocusedWindow(windowSecond, windowTop);
2525
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002526 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2527 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002528
2529 // Event should be dropped.
2530 windowTop->assertNoEvents();
2531 windowSecond->assertNoEvents();
2532}
2533
2534TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
2535 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2536 sp<FakeWindowHandle> window =
2537 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2538 sp<FakeWindowHandle> previousFocusedWindow =
2539 new FakeWindowHandle(application, mDispatcher, "previousFocusedWindow",
2540 ADISPLAY_ID_DEFAULT);
2541 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2542
2543 window->setFocusable(true);
2544 previousFocusedWindow->setFocusable(true);
2545 window->setVisible(false);
2546 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
2547 setFocusedWindow(previousFocusedWindow);
2548 previousFocusedWindow->consumeFocusEvent(true);
2549
2550 // Requesting focus on invisible window takes focus from currently focused window.
2551 setFocusedWindow(window);
2552 previousFocusedWindow->consumeFocusEvent(false);
2553
2554 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002555 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07002556 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002557 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07002558
2559 // Window does not get focus event or key down.
2560 window->assertNoEvents();
2561
2562 // Window becomes visible.
2563 window->setVisible(true);
2564 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2565
2566 // Window receives focus event.
2567 window->consumeFocusEvent(true);
2568 // Focused window receives key down.
2569 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2570}
2571
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002572/**
2573 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
2574 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
2575 * of the 'slipperyEnterWindow'.
2576 *
2577 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
2578 * a way so that the touched location is no longer covered by the top window.
2579 *
2580 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
2581 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
2582 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
2583 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
2584 * with ACTION_DOWN).
2585 * Thus, the touch has been transferred from the top window into the bottom window, because the top
2586 * window moved itself away from the touched location and had Flag::SLIPPERY.
2587 *
2588 * Even though the top window moved away from the touched location, it is still obscuring the bottom
2589 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
2590 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
2591 *
2592 * In this test, we ensure that the event received by the bottom window has
2593 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
2594 */
2595TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
2596 constexpr int32_t SLIPPERY_PID = INJECTOR_PID + 1;
2597 constexpr int32_t SLIPPERY_UID = INJECTOR_UID + 1;
2598
2599 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2600 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2601
2602 sp<FakeWindowHandle> slipperyExitWindow =
2603 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2604 slipperyExitWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
2605 InputWindowInfo::Flag::SLIPPERY);
2606 // Make sure this one overlaps the bottom window
2607 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
2608 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
2609 // one. Windows with the same owner are not considered to be occluding each other.
2610 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
2611
2612 sp<FakeWindowHandle> slipperyEnterWindow =
2613 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2614 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
2615
2616 mDispatcher->setInputWindows(
2617 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
2618
2619 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
2620 NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2621 ADISPLAY_ID_DEFAULT, {{50, 50}});
2622 mDispatcher->notifyMotion(&args);
2623 slipperyExitWindow->consumeMotionDown();
2624 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
2625 mDispatcher->setInputWindows(
2626 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
2627
2628 args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2629 ADISPLAY_ID_DEFAULT, {{51, 51}});
2630 mDispatcher->notifyMotion(&args);
2631
2632 slipperyExitWindow->consumeMotionCancel();
2633
2634 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
2635 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
2636}
2637
Garfield Tan1c7bc862020-01-28 13:24:04 -08002638class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
2639protected:
2640 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
2641 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
2642
Chris Yea209fde2020-07-22 13:54:51 -07002643 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002644 sp<FakeWindowHandle> mWindow;
2645
2646 virtual void SetUp() override {
2647 mFakePolicy = new FakeInputDispatcherPolicy();
2648 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
2649 mDispatcher = new InputDispatcher(mFakePolicy);
2650 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
2651 ASSERT_EQ(OK, mDispatcher->start());
2652
2653 setUpWindow();
2654 }
2655
2656 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07002657 mApp = std::make_shared<FakeApplicationHandle>();
Garfield Tan1c7bc862020-01-28 13:24:04 -08002658 mWindow = new FakeWindowHandle(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2659
Vishnu Nair47074b82020-08-14 11:54:47 -07002660 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002661 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002662 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002663 mWindow->consumeFocusEvent(true);
2664 }
2665
Chris Ye2ad95392020-09-01 13:44:44 -07002666 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08002667 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07002668 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002669 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
2670 mDispatcher->notifyKey(&keyArgs);
2671
2672 // Window should receive key down event.
2673 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2674 }
2675
2676 void expectKeyRepeatOnce(int32_t repeatCount) {
2677 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
2678 InputEvent* repeatEvent = mWindow->consume();
2679 ASSERT_NE(nullptr, repeatEvent);
2680
2681 uint32_t eventType = repeatEvent->getType();
2682 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType);
2683
2684 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
2685 uint32_t eventAction = repeatKeyEvent->getAction();
2686 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
2687 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
2688 }
2689
Chris Ye2ad95392020-09-01 13:44:44 -07002690 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08002691 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07002692 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002693 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
2694 mDispatcher->notifyKey(&keyArgs);
2695
2696 // Window should receive key down event.
2697 mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
2698 0 /*expectedFlags*/);
2699 }
2700};
2701
2702TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Chris Ye2ad95392020-09-01 13:44:44 -07002703 sendAndConsumeKeyDown(1 /* deviceId */);
2704 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2705 expectKeyRepeatOnce(repeatCount);
2706 }
2707}
2708
2709TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
2710 sendAndConsumeKeyDown(1 /* deviceId */);
2711 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2712 expectKeyRepeatOnce(repeatCount);
2713 }
2714 sendAndConsumeKeyDown(2 /* deviceId */);
2715 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08002716 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2717 expectKeyRepeatOnce(repeatCount);
2718 }
2719}
2720
2721TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Chris Ye2ad95392020-09-01 13:44:44 -07002722 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002723 expectKeyRepeatOnce(1 /*repeatCount*/);
Chris Ye2ad95392020-09-01 13:44:44 -07002724 sendAndConsumeKeyUp(1 /* deviceId */);
2725 mWindow->assertNoEvents();
2726}
2727
2728TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
2729 sendAndConsumeKeyDown(1 /* deviceId */);
2730 expectKeyRepeatOnce(1 /*repeatCount*/);
2731 sendAndConsumeKeyDown(2 /* deviceId */);
2732 expectKeyRepeatOnce(1 /*repeatCount*/);
2733 // Stale key up from device 1.
2734 sendAndConsumeKeyUp(1 /* deviceId */);
2735 // Device 2 is still down, keep repeating
2736 expectKeyRepeatOnce(2 /*repeatCount*/);
2737 expectKeyRepeatOnce(3 /*repeatCount*/);
2738 // Device 2 key up
2739 sendAndConsumeKeyUp(2 /* deviceId */);
2740 mWindow->assertNoEvents();
2741}
2742
2743TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
2744 sendAndConsumeKeyDown(1 /* deviceId */);
2745 expectKeyRepeatOnce(1 /*repeatCount*/);
2746 sendAndConsumeKeyDown(2 /* deviceId */);
2747 expectKeyRepeatOnce(1 /*repeatCount*/);
2748 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
2749 sendAndConsumeKeyUp(2 /* deviceId */);
2750 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08002751 mWindow->assertNoEvents();
2752}
2753
2754TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Chris Ye2ad95392020-09-01 13:44:44 -07002755 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002756 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2757 InputEvent* repeatEvent = mWindow->consume();
2758 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2759 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
2760 IdGenerator::getSource(repeatEvent->getId()));
2761 }
2762}
2763
2764TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Chris Ye2ad95392020-09-01 13:44:44 -07002765 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002766
2767 std::unordered_set<int32_t> idSet;
2768 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2769 InputEvent* repeatEvent = mWindow->consume();
2770 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2771 int32_t id = repeatEvent->getId();
2772 EXPECT_EQ(idSet.end(), idSet.find(id));
2773 idSet.insert(id);
2774 }
2775}
2776
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002777/* Test InputDispatcher for MultiDisplay */
2778class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
2779public:
2780 static constexpr int32_t SECOND_DISPLAY_ID = 1;
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002781 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002782 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08002783
Chris Yea209fde2020-07-22 13:54:51 -07002784 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002785 windowInPrimary =
2786 new FakeWindowHandle(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002787
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002788 // Set focus window for primary display, but focused display would be second one.
2789 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07002790 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002791 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002792 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002793 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08002794
Chris Yea209fde2020-07-22 13:54:51 -07002795 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002796 windowInSecondary =
2797 new FakeWindowHandle(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002798 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002799 // Set focus display to second one.
2800 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
2801 // Set focus window for second display.
2802 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07002803 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002804 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002805 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002806 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002807 }
2808
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002809 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002810 InputDispatcherTest::TearDown();
2811
Chris Yea209fde2020-07-22 13:54:51 -07002812 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002813 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07002814 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002815 windowInSecondary.clear();
2816 }
2817
2818protected:
Chris Yea209fde2020-07-22 13:54:51 -07002819 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002820 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07002821 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002822 sp<FakeWindowHandle> windowInSecondary;
2823};
2824
2825TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
2826 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002827 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2828 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2829 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002830 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08002831 windowInSecondary->assertNoEvents();
2832
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002833 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002834 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2835 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
2836 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08002837 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002838 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08002839}
2840
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002841TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08002842 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08002843 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2844 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002845 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002846 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08002847 windowInSecondary->assertNoEvents();
2848
2849 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08002850 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002851 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08002852 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002853 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08002854
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002855 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00002856 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08002857
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002858 // Old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002859 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
2860 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08002861
2862 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08002863 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002864 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08002865 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002866 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08002867 windowInSecondary->assertNoEvents();
2868}
2869
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002870// Test per-display input monitors for motion event.
2871TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08002872 FakeMonitorReceiver monitorInPrimary =
2873 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
2874 FakeMonitorReceiver monitorInSecondary =
2875 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002876
2877 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002878 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2879 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2880 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002881 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002882 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002883 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002884 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002885
2886 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002887 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2888 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
2889 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002890 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002891 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002892 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08002893 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002894
2895 // Test inject a non-pointer motion event.
2896 // If specific a display, it will dispatch to the focused window of particular display,
2897 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002898 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2899 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
2900 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002901 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002902 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002903 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08002904 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002905}
2906
2907// Test per-display input monitors for key event.
2908TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002909 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08002910 FakeMonitorReceiver monitorInPrimary =
2911 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
2912 FakeMonitorReceiver monitorInSecondary =
2913 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002914
2915 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002916 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2917 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002918 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002919 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002920 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08002921 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002922}
2923
Vishnu Nair958da932020-08-21 17:12:37 -07002924TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
2925 sp<FakeWindowHandle> secondWindowInPrimary =
2926 new FakeWindowHandle(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
2927 secondWindowInPrimary->setFocusable(true);
2928 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
2929 setFocusedWindow(secondWindowInPrimary);
2930 windowInPrimary->consumeFocusEvent(false);
2931 secondWindowInPrimary->consumeFocusEvent(true);
2932
2933 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002934 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2935 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002936 windowInPrimary->assertNoEvents();
2937 windowInSecondary->assertNoEvents();
2938 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2939}
2940
Jackal Guof9696682018-10-05 12:23:23 +08002941class InputFilterTest : public InputDispatcherTest {
2942protected:
2943 static constexpr int32_t SECOND_DISPLAY_ID = 1;
2944
2945 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) {
2946 NotifyMotionArgs motionArgs;
2947
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002948 motionArgs =
2949 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08002950 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002951 motionArgs =
2952 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08002953 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002954 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08002955 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08002956 mFakePolicy->assertFilterInputEventWasCalled(motionArgs);
Jackal Guof9696682018-10-05 12:23:23 +08002957 } else {
2958 mFakePolicy->assertFilterInputEventWasNotCalled();
2959 }
2960 }
2961
2962 void testNotifyKey(bool expectToBeFiltered) {
2963 NotifyKeyArgs keyArgs;
2964
2965 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
2966 mDispatcher->notifyKey(&keyArgs);
2967 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
2968 mDispatcher->notifyKey(&keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002969 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08002970
2971 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08002972 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08002973 } else {
2974 mFakePolicy->assertFilterInputEventWasNotCalled();
2975 }
2976 }
2977};
2978
2979// Test InputFilter for MotionEvent
2980TEST_F(InputFilterTest, MotionEvent_InputFilter) {
2981 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
2982 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
2983 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
2984
2985 // Enable InputFilter
2986 mDispatcher->setInputFilterEnabled(true);
2987 // Test touch on both primary and second display, and check if both events are filtered.
2988 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
2989 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
2990
2991 // Disable InputFilter
2992 mDispatcher->setInputFilterEnabled(false);
2993 // Test touch on both primary and second display, and check if both events aren't filtered.
2994 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
2995 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
2996}
2997
2998// Test InputFilter for KeyEvent
2999TEST_F(InputFilterTest, KeyEvent_InputFilter) {
3000 // Since the InputFilter is disabled by default, check if key event aren't filtered.
3001 testNotifyKey(/*expectToBeFiltered*/ false);
3002
3003 // Enable InputFilter
3004 mDispatcher->setInputFilterEnabled(true);
3005 // Send a key event, and check if it is filtered.
3006 testNotifyKey(/*expectToBeFiltered*/ true);
3007
3008 // Disable InputFilter
3009 mDispatcher->setInputFilterEnabled(false);
3010 // Send a key event, and check if it isn't filtered.
3011 testNotifyKey(/*expectToBeFiltered*/ false);
3012}
3013
chaviwfd6d3512019-03-25 13:23:49 -07003014class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07003015 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07003016 InputDispatcherTest::SetUp();
3017
Chris Yea209fde2020-07-22 13:54:51 -07003018 std::shared_ptr<FakeApplicationHandle> application =
3019 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003020 mUnfocusedWindow =
3021 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07003022 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
3023 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3024 // window.
Michael Wright44753b12020-07-08 13:48:11 +01003025 mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07003026
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003027 mFocusedWindow =
3028 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
3029 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01003030 mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07003031
3032 // Set focused application.
3033 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07003034 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07003035
3036 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00003037 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003038 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003039 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07003040 }
3041
Prabir Pradhan3608aad2019-10-02 17:08:26 -07003042 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07003043 InputDispatcherTest::TearDown();
3044
3045 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003046 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07003047 }
3048
3049protected:
3050 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003051 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003052 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07003053};
3054
3055// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
3056// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
3057// the onPointerDownOutsideFocus callback.
3058TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003059 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003060 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3061 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003062 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003063 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07003064
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003065 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07003066 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
3067}
3068
3069// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
3070// DOWN on the window that doesn't have focus. Ensure no window received the
3071// onPointerDownOutsideFocus callback.
3072TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003073 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003074 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003075 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003076 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07003077
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003078 ASSERT_TRUE(mDispatcher->waitForIdle());
3079 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07003080}
3081
3082// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
3083// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
3084TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003085 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3086 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003087 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003088 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07003089
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003090 ASSERT_TRUE(mDispatcher->waitForIdle());
3091 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07003092}
3093
3094// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
3095// DOWN on the window that already has focus. Ensure no window received the
3096// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003097TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003098 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003099 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003100 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003101 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003102 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07003103
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003104 ASSERT_TRUE(mDispatcher->waitForIdle());
3105 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07003106}
3107
chaviwaf87b3e2019-10-01 16:59:28 -07003108// These tests ensures we can send touch events to a single client when there are multiple input
3109// windows that point to the same client token.
3110class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
3111 virtual void SetUp() override {
3112 InputDispatcherTest::SetUp();
3113
Chris Yea209fde2020-07-22 13:54:51 -07003114 std::shared_ptr<FakeApplicationHandle> application =
3115 std::make_shared<FakeApplicationHandle>();
chaviwaf87b3e2019-10-01 16:59:28 -07003116 mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1",
3117 ADISPLAY_ID_DEFAULT);
3118 // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window.
3119 // 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 +01003120 mWindow1->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3121 InputWindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07003122 mWindow1->setFrame(Rect(0, 0, 100, 100));
3123
3124 mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2",
3125 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
Michael Wright44753b12020-07-08 13:48:11 +01003126 mWindow2->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3127 InputWindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07003128 mWindow2->setFrame(Rect(100, 100, 200, 200));
3129
Arthur Hung72d8dc32020-03-28 00:48:39 +00003130 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07003131 }
3132
3133protected:
3134 sp<FakeWindowHandle> mWindow1;
3135 sp<FakeWindowHandle> mWindow2;
3136
3137 // Helper function to convert the point from screen coordinates into the window's space
3138 static PointF getPointInWindow(const InputWindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07003139 vec2 vals = windowInfo->transform.transform(point.x, point.y);
3140 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07003141 }
3142
3143 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
3144 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003145 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07003146 InputEvent* event = window->consume();
3147
3148 ASSERT_NE(nullptr, event) << name.c_str()
3149 << ": consumer should have returned non-NULL event.";
3150
3151 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
3152 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
3153 << " event, got " << inputEventTypeToString(event->getType()) << " event";
3154
3155 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
3156 EXPECT_EQ(expectedAction, motionEvent.getAction());
3157
3158 for (size_t i = 0; i < points.size(); i++) {
3159 float expectedX = points[i].x;
3160 float expectedY = points[i].y;
3161
3162 EXPECT_EQ(expectedX, motionEvent.getX(i))
3163 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
3164 << ", got " << motionEvent.getX(i);
3165 EXPECT_EQ(expectedY, motionEvent.getY(i))
3166 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
3167 << ", got " << motionEvent.getY(i);
3168 }
3169 }
chaviw9eaa22c2020-07-01 16:21:27 -07003170
3171 void touchAndAssertPositions(int32_t action, std::vector<PointF> touchedPoints,
3172 std::vector<PointF> expectedPoints) {
3173 NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
3174 ADISPLAY_ID_DEFAULT, touchedPoints);
3175 mDispatcher->notifyMotion(&motionArgs);
3176
3177 // Always consume from window1 since it's the window that has the InputReceiver
3178 consumeMotionEvent(mWindow1, action, expectedPoints);
3179 }
chaviwaf87b3e2019-10-01 16:59:28 -07003180};
3181
3182TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
3183 // Touch Window 1
3184 PointF touchedPoint = {10, 10};
3185 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003186 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003187
3188 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07003189 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003190
3191 // Touch Window 2
3192 touchedPoint = {150, 150};
3193 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003194 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003195}
3196
chaviw9eaa22c2020-07-01 16:21:27 -07003197TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
3198 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07003199 mWindow2->setWindowScale(0.5f, 0.5f);
3200
3201 // Touch Window 1
3202 PointF touchedPoint = {10, 10};
3203 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003204 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003205 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07003206 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003207
3208 // Touch Window 2
3209 touchedPoint = {150, 150};
3210 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003211 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
3212 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003213
chaviw9eaa22c2020-07-01 16:21:27 -07003214 // Update the transform so rotation is set
3215 mWindow2->setWindowTransform(0, -1, 1, 0);
3216 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
3217 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003218}
3219
chaviw9eaa22c2020-07-01 16:21:27 -07003220TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003221 mWindow2->setWindowScale(0.5f, 0.5f);
3222
3223 // Touch Window 1
3224 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3225 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003226 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003227
3228 // Touch Window 2
3229 int32_t actionPointerDown =
3230 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003231 touchedPoints.push_back(PointF{150, 150});
3232 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3233 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003234
chaviw9eaa22c2020-07-01 16:21:27 -07003235 // Release Window 2
3236 int32_t actionPointerUp =
3237 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3238 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
3239 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003240
chaviw9eaa22c2020-07-01 16:21:27 -07003241 // Update the transform so rotation is set for Window 2
3242 mWindow2->setWindowTransform(0, -1, 1, 0);
3243 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3244 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003245}
3246
chaviw9eaa22c2020-07-01 16:21:27 -07003247TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003248 mWindow2->setWindowScale(0.5f, 0.5f);
3249
3250 // Touch Window 1
3251 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3252 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003253 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003254
3255 // Touch Window 2
3256 int32_t actionPointerDown =
3257 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003258 touchedPoints.push_back(PointF{150, 150});
3259 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003260
chaviw9eaa22c2020-07-01 16:21:27 -07003261 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003262
3263 // Move both windows
3264 touchedPoints = {{20, 20}, {175, 175}};
3265 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3266 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3267
chaviw9eaa22c2020-07-01 16:21:27 -07003268 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003269
chaviw9eaa22c2020-07-01 16:21:27 -07003270 // Release Window 2
3271 int32_t actionPointerUp =
3272 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3273 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
3274 expectedPoints.pop_back();
3275
3276 // Touch Window 2
3277 mWindow2->setWindowTransform(0, -1, 1, 0);
3278 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3279 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
3280
3281 // Move both windows
3282 touchedPoints = {{20, 20}, {175, 175}};
3283 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3284 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3285
3286 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003287}
3288
3289TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
3290 mWindow1->setWindowScale(0.5f, 0.5f);
3291
3292 // Touch Window 1
3293 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3294 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003295 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003296
3297 // Touch Window 2
3298 int32_t actionPointerDown =
3299 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003300 touchedPoints.push_back(PointF{150, 150});
3301 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003302
chaviw9eaa22c2020-07-01 16:21:27 -07003303 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003304
3305 // Move both windows
3306 touchedPoints = {{20, 20}, {175, 175}};
3307 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3308 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3309
chaviw9eaa22c2020-07-01 16:21:27 -07003310 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003311}
3312
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003313class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
3314 virtual void SetUp() override {
3315 InputDispatcherTest::SetUp();
3316
Chris Yea209fde2020-07-22 13:54:51 -07003317 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003318 mApplication->setDispatchingTimeout(20ms);
3319 mWindow =
3320 new FakeWindowHandle(mApplication, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
3321 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05003322 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07003323 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003324 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3325 // window.
Michael Wright44753b12020-07-08 13:48:11 +01003326 mWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003327
3328 // Set focused application.
3329 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
3330
3331 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003332 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003333 mWindow->consumeFocusEvent(true);
3334 }
3335
3336 virtual void TearDown() override {
3337 InputDispatcherTest::TearDown();
3338 mWindow.clear();
3339 }
3340
3341protected:
Chris Yea209fde2020-07-22 13:54:51 -07003342 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003343 sp<FakeWindowHandle> mWindow;
3344 static constexpr PointF WINDOW_LOCATION = {20, 20};
3345
3346 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003347 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003348 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3349 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003350 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003351 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3352 WINDOW_LOCATION));
3353 }
3354};
3355
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003356// Send a tap and respond, which should not cause an ANR.
3357TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
3358 tapOnWindow();
3359 mWindow->consumeMotionDown();
3360 mWindow->consumeMotionUp();
3361 ASSERT_TRUE(mDispatcher->waitForIdle());
3362 mFakePolicy->assertNotifyAnrWasNotCalled();
3363}
3364
3365// Send a regular key and respond, which should not cause an ANR.
3366TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003367 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003368 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
3369 ASSERT_TRUE(mDispatcher->waitForIdle());
3370 mFakePolicy->assertNotifyAnrWasNotCalled();
3371}
3372
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003373TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
3374 mWindow->setFocusable(false);
3375 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3376 mWindow->consumeFocusEvent(false);
3377
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003378 InputEventInjectionResult result =
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003379 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003380 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/,
3381 false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003382 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003383 // Key will not go to window because we have no focused window.
3384 // The 'no focused window' ANR timer should start instead.
3385
3386 // Now, the focused application goes away.
3387 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
3388 // The key should get dropped and there should be no ANR.
3389
3390 ASSERT_TRUE(mDispatcher->waitForIdle());
3391 mFakePolicy->assertNotifyAnrWasNotCalled();
3392}
3393
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003394// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003395// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
3396// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003397TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003398 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003399 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3400 WINDOW_LOCATION));
3401
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003402 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
3403 ASSERT_TRUE(sequenceNum);
3404 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003405 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003406
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003407 mWindow->finishEvent(*sequenceNum);
3408 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3409 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003410 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003411 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003412}
3413
3414// Send a key to the app and have the app not respond right away.
3415TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
3416 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003417 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003418 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
3419 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003420 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003421 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003422 ASSERT_TRUE(mDispatcher->waitForIdle());
3423}
3424
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003425// We have a focused application, but no focused window
3426TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003427 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003428 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3429 mWindow->consumeFocusEvent(false);
3430
3431 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003432 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003433 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3434 WINDOW_LOCATION));
3435 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
3436 mDispatcher->waitForIdle();
3437 mFakePolicy->assertNotifyAnrWasNotCalled();
3438
3439 // Once a focused event arrives, we get an ANR for this application
3440 // We specify the injection timeout to be smaller than the application timeout, to ensure that
3441 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003442 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003443 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003444 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003445 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003446 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003447 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003448 ASSERT_TRUE(mDispatcher->waitForIdle());
3449}
3450
3451// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003452// Make sure that we don't notify policy twice about the same ANR.
3453TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003454 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003455 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3456 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003457
3458 // Once a focused event arrives, we get an ANR for this application
3459 // We specify the injection timeout to be smaller than the application timeout, to ensure that
3460 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003461 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003462 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003463 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003464 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003465 const std::chrono::duration appTimeout =
3466 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003467 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003468
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003469 std::this_thread::sleep_for(appTimeout);
3470 // ANR should not be raised again. It is up to policy to do that if it desires.
3471 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003472
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003473 // If we now get a focused window, the ANR should stop, but the policy handles that via
3474 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003475 ASSERT_TRUE(mDispatcher->waitForIdle());
3476}
3477
3478// We have a focused application, but no focused window
3479TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003480 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003481 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3482 mWindow->consumeFocusEvent(false);
3483
3484 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003485 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003486 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003487 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3488 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003489
3490 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003491 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003492
3493 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003494 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003495 ASSERT_TRUE(mDispatcher->waitForIdle());
3496 mWindow->assertNoEvents();
3497}
3498
3499/**
3500 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
3501 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
3502 * If we process 1 of the events, but ANR on the second event with the same timestamp,
3503 * the ANR mechanism should still work.
3504 *
3505 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
3506 * DOWN event, while not responding on the second one.
3507 */
3508TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
3509 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
3510 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3511 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
3512 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
3513 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003514 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003515
3516 // Now send ACTION_UP, with identical timestamp
3517 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3518 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
3519 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
3520 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003521 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003522
3523 // We have now sent down and up. Let's consume first event and then ANR on the second.
3524 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3525 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003526 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003527}
3528
3529// If an app is not responding to a key event, gesture monitors should continue to receive
3530// new motion events
3531TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnKey) {
3532 FakeMonitorReceiver monitor =
3533 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
3534 true /*isGestureMonitor*/);
3535
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003536 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3537 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003538 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003539 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003540
3541 // Stuck on the ACTION_UP
3542 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003543 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003544
3545 // New tap will go to the gesture monitor, but not to the window
3546 tapOnWindow();
3547 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3548 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3549
3550 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
3551 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003552 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003553 mWindow->assertNoEvents();
3554 monitor.assertNoEvents();
3555}
3556
3557// If an app is not responding to a motion event, gesture monitors should continue to receive
3558// new motion events
3559TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnMotion) {
3560 FakeMonitorReceiver monitor =
3561 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
3562 true /*isGestureMonitor*/);
3563
3564 tapOnWindow();
3565 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3566 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3567
3568 mWindow->consumeMotionDown();
3569 // Stuck on the ACTION_UP
3570 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003571 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003572
3573 // New tap will go to the gesture monitor, but not to the window
3574 tapOnWindow();
3575 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3576 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3577
3578 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
3579 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003580 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003581 mWindow->assertNoEvents();
3582 monitor.assertNoEvents();
3583}
3584
3585// If a window is unresponsive, then you get anr. if the window later catches up and starts to
3586// process events, you don't get an anr. When the window later becomes unresponsive again, you
3587// get an ANR again.
3588// 1. tap -> block on ACTION_UP -> receive ANR
3589// 2. consume all pending events (= queue becomes healthy again)
3590// 3. tap again -> block on ACTION_UP again -> receive ANR second time
3591TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
3592 tapOnWindow();
3593
3594 mWindow->consumeMotionDown();
3595 // Block on ACTION_UP
3596 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003597 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003598 mWindow->consumeMotionUp(); // Now the connection should be healthy again
3599 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003600 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003601 mWindow->assertNoEvents();
3602
3603 tapOnWindow();
3604 mWindow->consumeMotionDown();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003605 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003606 mWindow->consumeMotionUp();
3607
3608 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003609 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003610 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003611 mWindow->assertNoEvents();
3612}
3613
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003614// If a connection remains unresponsive for a while, make sure policy is only notified once about
3615// it.
3616TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003617 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003618 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3619 WINDOW_LOCATION));
3620
3621 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003622 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003623 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003624 // 'notifyConnectionUnresponsive' should only be called once per connection
3625 mFakePolicy->assertNotifyAnrWasNotCalled();
3626 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003627 mWindow->consumeMotionDown();
3628 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3629 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3630 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003631 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003632 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003633 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003634}
3635
3636/**
3637 * If a window is processing a motion event, and then a key event comes in, the key event should
3638 * not to to the focused window until the motion is processed.
3639 *
3640 * Warning!!!
3641 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
3642 * and the injection timeout that we specify when injecting the key.
3643 * We must have the injection timeout (10ms) be smaller than
3644 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
3645 *
3646 * If that value changes, this test should also change.
3647 */
3648TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
3649 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3650 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3651
3652 tapOnWindow();
3653 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3654 ASSERT_TRUE(downSequenceNum);
3655 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3656 ASSERT_TRUE(upSequenceNum);
3657 // Don't finish the events yet, and send a key
3658 // Injection will "succeed" because we will eventually give up and send the key to the focused
3659 // window even if motions are still being processed. But because the injection timeout is short,
3660 // we will receive INJECTION_TIMED_OUT as the result.
3661
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003662 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003663 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003664 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3665 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003666 // Key will not be sent to the window, yet, because the window is still processing events
3667 // and the key remains pending, waiting for the touch events to be processed
3668 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3669 ASSERT_FALSE(keySequenceNum);
3670
3671 std::this_thread::sleep_for(500ms);
3672 // if we wait long enough though, dispatcher will give up, and still send the key
3673 // to the focused window, even though we have not yet finished the motion event
3674 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3675 mWindow->finishEvent(*downSequenceNum);
3676 mWindow->finishEvent(*upSequenceNum);
3677}
3678
3679/**
3680 * If a window is processing a motion event, and then a key event comes in, the key event should
3681 * not go to the focused window until the motion is processed.
3682 * If then a new motion comes in, then the pending key event should be going to the currently
3683 * focused window right away.
3684 */
3685TEST_F(InputDispatcherSingleWindowAnr,
3686 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
3687 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3688 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3689
3690 tapOnWindow();
3691 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3692 ASSERT_TRUE(downSequenceNum);
3693 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3694 ASSERT_TRUE(upSequenceNum);
3695 // Don't finish the events yet, and send a key
3696 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003697 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003698 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003699 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003700 // At this point, key is still pending, and should not be sent to the application yet.
3701 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3702 ASSERT_FALSE(keySequenceNum);
3703
3704 // Now tap down again. It should cause the pending key to go to the focused window right away.
3705 tapOnWindow();
3706 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
3707 // the other events yet. We can finish events in any order.
3708 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
3709 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
3710 mWindow->consumeMotionDown();
3711 mWindow->consumeMotionUp();
3712 mWindow->assertNoEvents();
3713}
3714
3715class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
3716 virtual void SetUp() override {
3717 InputDispatcherTest::SetUp();
3718
Chris Yea209fde2020-07-22 13:54:51 -07003719 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003720 mApplication->setDispatchingTimeout(10ms);
3721 mUnfocusedWindow =
3722 new FakeWindowHandle(mApplication, mDispatcher, "Unfocused", ADISPLAY_ID_DEFAULT);
3723 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
3724 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3725 // window.
3726 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Michael Wright44753b12020-07-08 13:48:11 +01003727 mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3728 InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH |
3729 InputWindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003730
3731 mFocusedWindow =
3732 new FakeWindowHandle(mApplication, mDispatcher, "Focused", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05003733 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003734 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01003735 mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3736 InputWindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003737
3738 // Set focused application.
3739 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07003740 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003741
3742 // Expect one focus window exist in display.
3743 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003744 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003745 mFocusedWindow->consumeFocusEvent(true);
3746 }
3747
3748 virtual void TearDown() override {
3749 InputDispatcherTest::TearDown();
3750
3751 mUnfocusedWindow.clear();
3752 mFocusedWindow.clear();
3753 }
3754
3755protected:
Chris Yea209fde2020-07-22 13:54:51 -07003756 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003757 sp<FakeWindowHandle> mUnfocusedWindow;
3758 sp<FakeWindowHandle> mFocusedWindow;
3759 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
3760 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
3761 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
3762
3763 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
3764
3765 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
3766
3767private:
3768 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003769 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003770 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3771 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003772 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003773 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3774 location));
3775 }
3776};
3777
3778// If we have 2 windows that are both unresponsive, the one with the shortest timeout
3779// should be ANR'd first.
3780TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003781 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003782 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3783 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003784 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003785 mFocusedWindow->consumeMotionDown();
3786 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3787 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3788 // We consumed all events, so no ANR
3789 ASSERT_TRUE(mDispatcher->waitForIdle());
3790 mFakePolicy->assertNotifyAnrWasNotCalled();
3791
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003792 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003793 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3794 FOCUSED_WINDOW_LOCATION));
3795 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
3796 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003797
3798 const std::chrono::duration timeout =
3799 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003800 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003801 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
3802 // sequence to make it consistent
3803 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003804 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003805 mFocusedWindow->consumeMotionDown();
3806 // This cancel is generated because the connection was unresponsive
3807 mFocusedWindow->consumeMotionCancel();
3808 mFocusedWindow->assertNoEvents();
3809 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003810 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003811 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003812 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003813}
3814
3815// If we have 2 windows with identical timeouts that are both unresponsive,
3816// it doesn't matter which order they should have ANR.
3817// But we should receive ANR for both.
3818TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
3819 // Set the timeout for unfocused window to match the focused window
3820 mUnfocusedWindow->setDispatchingTimeout(10ms);
3821 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
3822
3823 tapOnFocusedWindow();
3824 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003825 sp<IBinder> anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms);
3826 sp<IBinder> anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003827
3828 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003829 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
3830 mFocusedWindow->getToken() == anrConnectionToken2);
3831 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
3832 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003833
3834 ASSERT_TRUE(mDispatcher->waitForIdle());
3835 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003836
3837 mFocusedWindow->consumeMotionDown();
3838 mFocusedWindow->consumeMotionUp();
3839 mUnfocusedWindow->consumeMotionOutside();
3840
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003841 sp<IBinder> responsiveToken1 = mFakePolicy->getResponsiveWindowToken();
3842 sp<IBinder> responsiveToken2 = mFakePolicy->getResponsiveWindowToken();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003843
3844 // Both applications should be marked as responsive, in any order
3845 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
3846 mFocusedWindow->getToken() == responsiveToken2);
3847 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
3848 mUnfocusedWindow->getToken() == responsiveToken2);
3849 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003850}
3851
3852// If a window is already not responding, the second tap on the same window should be ignored.
3853// We should also log an error to account for the dropped event (not tested here).
3854// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
3855TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
3856 tapOnFocusedWindow();
3857 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3858 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3859 // Receive the events, but don't respond
3860 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
3861 ASSERT_TRUE(downEventSequenceNum);
3862 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
3863 ASSERT_TRUE(upEventSequenceNum);
3864 const std::chrono::duration timeout =
3865 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003866 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003867
3868 // Tap once again
3869 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003870 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003871 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3872 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003873 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003874 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3875 FOCUSED_WINDOW_LOCATION));
3876 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
3877 // valid touch target
3878 mUnfocusedWindow->assertNoEvents();
3879
3880 // Consume the first tap
3881 mFocusedWindow->finishEvent(*downEventSequenceNum);
3882 mFocusedWindow->finishEvent(*upEventSequenceNum);
3883 ASSERT_TRUE(mDispatcher->waitForIdle());
3884 // The second tap did not go to the focused window
3885 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003886 // Since all events are finished, connection should be deemed healthy again
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003887 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003888 mFakePolicy->assertNotifyAnrWasNotCalled();
3889}
3890
3891// If you tap outside of all windows, there will not be ANR
3892TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003893 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003894 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3895 LOCATION_OUTSIDE_ALL_WINDOWS));
3896 ASSERT_TRUE(mDispatcher->waitForIdle());
3897 mFakePolicy->assertNotifyAnrWasNotCalled();
3898}
3899
3900// Since the focused window is paused, tapping on it should not produce any events
3901TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
3902 mFocusedWindow->setPaused(true);
3903 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
3904
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003905 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003906 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3907 FOCUSED_WINDOW_LOCATION));
3908
3909 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
3910 ASSERT_TRUE(mDispatcher->waitForIdle());
3911 // Should not ANR because the window is paused, and touches shouldn't go to it
3912 mFakePolicy->assertNotifyAnrWasNotCalled();
3913
3914 mFocusedWindow->assertNoEvents();
3915 mUnfocusedWindow->assertNoEvents();
3916}
3917
3918/**
3919 * If a window is processing a motion event, and then a key event comes in, the key event should
3920 * not to to the focused window until the motion is processed.
3921 * If a different window becomes focused at this time, the key should go to that window instead.
3922 *
3923 * Warning!!!
3924 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
3925 * and the injection timeout that we specify when injecting the key.
3926 * We must have the injection timeout (10ms) be smaller than
3927 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
3928 *
3929 * If that value changes, this test should also change.
3930 */
3931TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
3932 // Set a long ANR timeout to prevent it from triggering
3933 mFocusedWindow->setDispatchingTimeout(2s);
3934 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
3935
3936 tapOnUnfocusedWindow();
3937 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
3938 ASSERT_TRUE(downSequenceNum);
3939 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
3940 ASSERT_TRUE(upSequenceNum);
3941 // Don't finish the events yet, and send a key
3942 // Injection will succeed because we will eventually give up and send the key to the focused
3943 // window even if motions are still being processed.
3944
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003945 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003946 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003947 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
3948 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003949 // Key will not be sent to the window, yet, because the window is still processing events
3950 // and the key remains pending, waiting for the touch events to be processed
3951 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
3952 ASSERT_FALSE(keySequenceNum);
3953
3954 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07003955 mFocusedWindow->setFocusable(false);
3956 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003957 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003958 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003959
3960 // Focus events should precede the key events
3961 mUnfocusedWindow->consumeFocusEvent(true);
3962 mFocusedWindow->consumeFocusEvent(false);
3963
3964 // Finish the tap events, which should unblock dispatcher
3965 mUnfocusedWindow->finishEvent(*downSequenceNum);
3966 mUnfocusedWindow->finishEvent(*upSequenceNum);
3967
3968 // Now that all queues are cleared and no backlog in the connections, the key event
3969 // can finally go to the newly focused "mUnfocusedWindow".
3970 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3971 mFocusedWindow->assertNoEvents();
3972 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003973 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003974}
3975
3976// When the touch stream is split across 2 windows, and one of them does not respond,
3977// then ANR should be raised and the touch should be canceled for the unresponsive window.
3978// The other window should not be affected by that.
3979TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
3980 // Touch Window 1
3981 NotifyMotionArgs motionArgs =
3982 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3983 ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION});
3984 mDispatcher->notifyMotion(&motionArgs);
3985 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3986 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3987
3988 // Touch Window 2
3989 int32_t actionPointerDown =
3990 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3991
3992 motionArgs =
3993 generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3994 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION});
3995 mDispatcher->notifyMotion(&motionArgs);
3996
3997 const std::chrono::duration timeout =
3998 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003999 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004000
4001 mUnfocusedWindow->consumeMotionDown();
4002 mFocusedWindow->consumeMotionDown();
4003 // Focused window may or may not receive ACTION_MOVE
4004 // But it should definitely receive ACTION_CANCEL due to the ANR
4005 InputEvent* event;
4006 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
4007 ASSERT_TRUE(moveOrCancelSequenceNum);
4008 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
4009 ASSERT_NE(nullptr, event);
4010 ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION);
4011 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
4012 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
4013 mFocusedWindow->consumeMotionCancel();
4014 } else {
4015 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
4016 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004017 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004018 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004019
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004020 mUnfocusedWindow->assertNoEvents();
4021 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004022 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004023}
4024
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05004025/**
4026 * If we have no focused window, and a key comes in, we start the ANR timer.
4027 * The focused application should add a focused window before the timer runs out to prevent ANR.
4028 *
4029 * If the user touches another application during this time, the key should be dropped.
4030 * Next, if a new focused window comes in, without toggling the focused application,
4031 * then no ANR should occur.
4032 *
4033 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
4034 * but in some cases the policy may not update the focused application.
4035 */
4036TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
4037 std::shared_ptr<FakeApplicationHandle> focusedApplication =
4038 std::make_shared<FakeApplicationHandle>();
4039 focusedApplication->setDispatchingTimeout(60ms);
4040 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
4041 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
4042 mFocusedWindow->setFocusable(false);
4043
4044 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
4045 mFocusedWindow->consumeFocusEvent(false);
4046
4047 // Send a key. The ANR timer should start because there is no focused window.
4048 // 'focusedApplication' will get blamed if this timer completes.
4049 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004050 InputEventInjectionResult result =
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05004051 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004052 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/,
4053 false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004054 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05004055
4056 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
4057 // then the injected touches won't cause the focused event to get dropped.
4058 // The dispatcher only checks for whether the queue should be pruned upon queueing.
4059 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
4060 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
4061 // For this test, it means that the key would get delivered to the window once it becomes
4062 // focused.
4063 std::this_thread::sleep_for(10ms);
4064
4065 // Touch unfocused window. This should force the pending key to get dropped.
4066 NotifyMotionArgs motionArgs =
4067 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4068 ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION});
4069 mDispatcher->notifyMotion(&motionArgs);
4070
4071 // We do not consume the motion right away, because that would require dispatcher to first
4072 // process (== drop) the key event, and by that time, ANR will be raised.
4073 // Set the focused window first.
4074 mFocusedWindow->setFocusable(true);
4075 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
4076 setFocusedWindow(mFocusedWindow);
4077 mFocusedWindow->consumeFocusEvent(true);
4078 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
4079 // to another application. This could be a bug / behaviour in the policy.
4080
4081 mUnfocusedWindow->consumeMotionDown();
4082
4083 ASSERT_TRUE(mDispatcher->waitForIdle());
4084 // Should not ANR because we actually have a focused window. It was just added too slowly.
4085 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
4086}
4087
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05004088// These tests ensure we cannot send touch events to a window that's positioned behind a window
4089// that has feature NO_INPUT_CHANNEL.
4090// Layout:
4091// Top (closest to user)
4092// mNoInputWindow (above all windows)
4093// mBottomWindow
4094// Bottom (furthest from user)
4095class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
4096 virtual void SetUp() override {
4097 InputDispatcherTest::SetUp();
4098
4099 mApplication = std::make_shared<FakeApplicationHandle>();
4100 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
4101 "Window without input channel", ADISPLAY_ID_DEFAULT,
4102 std::make_optional<sp<IBinder>>(nullptr) /*token*/);
4103
4104 mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL);
4105 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
4106 // It's perfectly valid for this window to not have an associated input channel
4107
4108 mBottomWindow = new FakeWindowHandle(mApplication, mDispatcher, "Bottom window",
4109 ADISPLAY_ID_DEFAULT);
4110 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
4111
4112 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
4113 }
4114
4115protected:
4116 std::shared_ptr<FakeApplicationHandle> mApplication;
4117 sp<FakeWindowHandle> mNoInputWindow;
4118 sp<FakeWindowHandle> mBottomWindow;
4119};
4120
4121TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
4122 PointF touchedPoint = {10, 10};
4123
4124 NotifyMotionArgs motionArgs =
4125 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4126 ADISPLAY_ID_DEFAULT, {touchedPoint});
4127 mDispatcher->notifyMotion(&motionArgs);
4128
4129 mNoInputWindow->assertNoEvents();
4130 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
4131 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
4132 // and therefore should prevent mBottomWindow from receiving touches
4133 mBottomWindow->assertNoEvents();
4134}
4135
4136/**
4137 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
4138 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
4139 */
4140TEST_F(InputDispatcherMultiWindowOcclusionTests,
4141 NoInputChannelFeature_DropsTouchesWithValidChannel) {
4142 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
4143 "Window with input channel and NO_INPUT_CHANNEL",
4144 ADISPLAY_ID_DEFAULT);
4145
4146 mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL);
4147 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
4148 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
4149
4150 PointF touchedPoint = {10, 10};
4151
4152 NotifyMotionArgs motionArgs =
4153 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4154 ADISPLAY_ID_DEFAULT, {touchedPoint});
4155 mDispatcher->notifyMotion(&motionArgs);
4156
4157 mNoInputWindow->assertNoEvents();
4158 mBottomWindow->assertNoEvents();
4159}
4160
Vishnu Nair958da932020-08-21 17:12:37 -07004161class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
4162protected:
4163 std::shared_ptr<FakeApplicationHandle> mApp;
4164 sp<FakeWindowHandle> mWindow;
4165 sp<FakeWindowHandle> mMirror;
4166
4167 virtual void SetUp() override {
4168 InputDispatcherTest::SetUp();
4169 mApp = std::make_shared<FakeApplicationHandle>();
4170 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
4171 mMirror = new FakeWindowHandle(mApp, mDispatcher, "TestWindowMirror", ADISPLAY_ID_DEFAULT,
4172 mWindow->getToken());
4173 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
4174 mWindow->setFocusable(true);
4175 mMirror->setFocusable(true);
4176 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4177 }
4178};
4179
4180TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
4181 // Request focus on a mirrored window
4182 setFocusedWindow(mMirror);
4183
4184 // window gets focused
4185 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004186 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4187 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004188 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
4189}
4190
4191// A focused & mirrored window remains focused only if the window and its mirror are both
4192// focusable.
4193TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
4194 setFocusedWindow(mMirror);
4195
4196 // window gets focused
4197 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004198 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4199 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004200 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004201 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4202 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004203 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4204
4205 mMirror->setFocusable(false);
4206 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4207
4208 // window loses focus since one of the windows associated with the token in not focusable
4209 mWindow->consumeFocusEvent(false);
4210
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004211 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4212 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004213 mWindow->assertNoEvents();
4214}
4215
4216// A focused & mirrored window remains focused until the window and its mirror both become
4217// invisible.
4218TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
4219 setFocusedWindow(mMirror);
4220
4221 // window gets focused
4222 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004223 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4224 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004225 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004226 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4227 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004228 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4229
4230 mMirror->setVisible(false);
4231 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4232
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004233 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4234 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004235 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004236 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4237 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004238 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4239
4240 mWindow->setVisible(false);
4241 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4242
4243 // window loses focus only after all windows associated with the token become invisible.
4244 mWindow->consumeFocusEvent(false);
4245
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004246 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4247 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004248 mWindow->assertNoEvents();
4249}
4250
4251// A focused & mirrored window remains focused until both windows are removed.
4252TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
4253 setFocusedWindow(mMirror);
4254
4255 // window gets focused
4256 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004257 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4258 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004259 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004260 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4261 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004262 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4263
4264 // single window is removed but the window token remains focused
4265 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
4266
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004267 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4268 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004269 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004270 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4271 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004272 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4273
4274 // Both windows are removed
4275 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
4276 mWindow->consumeFocusEvent(false);
4277
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004278 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4279 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004280 mWindow->assertNoEvents();
4281}
4282
4283// Focus request can be pending until one window becomes visible.
4284TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
4285 // Request focus on an invisible mirror.
4286 mWindow->setVisible(false);
4287 mMirror->setVisible(false);
4288 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4289 setFocusedWindow(mMirror);
4290
4291 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004292 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07004293 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004294 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07004295
4296 mMirror->setVisible(true);
4297 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4298
4299 // window gets focused
4300 mWindow->consumeFocusEvent(true);
4301 // window gets the pending key event
4302 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4303}
Prabir Pradhan99987712020-11-10 18:43:05 -08004304
4305class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
4306protected:
4307 std::shared_ptr<FakeApplicationHandle> mApp;
4308 sp<FakeWindowHandle> mWindow;
4309 sp<FakeWindowHandle> mSecondWindow;
4310
4311 void SetUp() override {
4312 InputDispatcherTest::SetUp();
4313 mApp = std::make_shared<FakeApplicationHandle>();
4314 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
4315 mWindow->setFocusable(true);
4316 mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
4317 mSecondWindow->setFocusable(true);
4318
4319 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
4320 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
4321
4322 setFocusedWindow(mWindow);
4323 mWindow->consumeFocusEvent(true);
4324 }
4325
4326 void notifyPointerCaptureChanged(bool enabled) {
4327 const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(enabled);
4328 mDispatcher->notifyPointerCaptureChanged(&args);
4329 }
4330
4331 void requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, bool enabled) {
4332 mDispatcher->requestPointerCapture(window->getToken(), enabled);
4333 mFakePolicy->waitForSetPointerCapture(enabled);
4334 notifyPointerCaptureChanged(enabled);
4335 window->consumeCaptureEvent(enabled);
4336 }
4337};
4338
4339TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
4340 // Ensure that capture cannot be obtained for unfocused windows.
4341 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
4342 mFakePolicy->assertSetPointerCaptureNotCalled();
4343 mSecondWindow->assertNoEvents();
4344
4345 // Ensure that capture can be enabled from the focus window.
4346 requestAndVerifyPointerCapture(mWindow, true);
4347
4348 // Ensure that capture cannot be disabled from a window that does not have capture.
4349 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
4350 mFakePolicy->assertSetPointerCaptureNotCalled();
4351
4352 // Ensure that capture can be disabled from the window with capture.
4353 requestAndVerifyPointerCapture(mWindow, false);
4354}
4355
4356TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
4357 requestAndVerifyPointerCapture(mWindow, true);
4358
4359 setFocusedWindow(mSecondWindow);
4360
4361 // Ensure that the capture disabled event was sent first.
4362 mWindow->consumeCaptureEvent(false);
4363 mWindow->consumeFocusEvent(false);
4364 mSecondWindow->consumeFocusEvent(true);
4365 mFakePolicy->waitForSetPointerCapture(false);
4366
4367 // Ensure that additional state changes from InputReader are not sent to the window.
4368 notifyPointerCaptureChanged(false);
4369 notifyPointerCaptureChanged(true);
4370 notifyPointerCaptureChanged(false);
4371 mWindow->assertNoEvents();
4372 mSecondWindow->assertNoEvents();
4373 mFakePolicy->assertSetPointerCaptureNotCalled();
4374}
4375
4376TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
4377 requestAndVerifyPointerCapture(mWindow, true);
4378
4379 // InputReader unexpectedly disables and enables pointer capture.
4380 notifyPointerCaptureChanged(false);
4381 notifyPointerCaptureChanged(true);
4382
4383 // Ensure that Pointer Capture is disabled.
Prabir Pradhan7d030382020-12-21 07:58:35 -08004384 mFakePolicy->waitForSetPointerCapture(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08004385 mWindow->consumeCaptureEvent(false);
4386 mWindow->assertNoEvents();
4387}
4388
Prabir Pradhan167e6d92021-02-04 16:18:17 -08004389TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
4390 requestAndVerifyPointerCapture(mWindow, true);
4391
4392 // The first window loses focus.
4393 setFocusedWindow(mSecondWindow);
4394 mFakePolicy->waitForSetPointerCapture(false);
4395 mWindow->consumeCaptureEvent(false);
4396
4397 // Request Pointer Capture from the second window before the notification from InputReader
4398 // arrives.
4399 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
4400 mFakePolicy->waitForSetPointerCapture(true);
4401
4402 // InputReader notifies Pointer Capture was disabled (because of the focus change).
4403 notifyPointerCaptureChanged(false);
4404
4405 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
4406 notifyPointerCaptureChanged(true);
4407
4408 mSecondWindow->consumeFocusEvent(true);
4409 mSecondWindow->consumeCaptureEvent(true);
4410}
4411
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004412class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
4413protected:
4414 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00004415
4416 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
4417 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
4418
4419 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
4420 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
4421
4422 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
4423 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
4424 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
4425 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
4426 MAXIMUM_OBSCURING_OPACITY);
4427
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004428 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004429 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004430 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004431
4432 sp<FakeWindowHandle> mTouchWindow;
4433
4434 virtual void SetUp() override {
4435 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004436 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004437 mDispatcher->setBlockUntrustedTouchesMode(android::os::BlockUntrustedTouchesMode::BLOCK);
4438 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
4439 }
4440
4441 virtual void TearDown() override {
4442 InputDispatcherTest::TearDown();
4443 mTouchWindow.clear();
4444 }
4445
4446 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name,
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004447 os::TouchOcclusionMode mode, float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004448 sp<FakeWindowHandle> window = getWindow(uid, name);
4449 window->setFlags(InputWindowInfo::Flag::NOT_TOUCHABLE);
4450 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004451 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004452 return window;
4453 }
4454
4455 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
4456 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
4457 sp<FakeWindowHandle> window =
4458 new FakeWindowHandle(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
4459 // Generate an arbitrary PID based on the UID
4460 window->setOwnerInfo(1777 + (uid % 10000), uid);
4461 return window;
4462 }
4463
4464 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
4465 NotifyMotionArgs args =
4466 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4467 ADISPLAY_ID_DEFAULT, points);
4468 mDispatcher->notifyMotion(&args);
4469 }
4470};
4471
4472TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004473 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004474 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004475 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004476
4477 touch();
4478
4479 mTouchWindow->assertNoEvents();
4480}
4481
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004482TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00004483 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
4484 const sp<FakeWindowHandle>& w =
4485 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
4486 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4487
4488 touch();
4489
4490 mTouchWindow->assertNoEvents();
4491}
4492
4493TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004494 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
4495 const sp<FakeWindowHandle>& w =
4496 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
4497 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4498
4499 touch();
4500
4501 w->assertNoEvents();
4502}
4503
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004504TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004505 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
4506 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004507
4508 touch();
4509
4510 mTouchWindow->consumeAnyMotionDown();
4511}
4512
4513TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004514 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004515 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004516 w->setFrame(Rect(0, 0, 50, 50));
4517 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004518
4519 touch({PointF{100, 100}});
4520
4521 mTouchWindow->consumeAnyMotionDown();
4522}
4523
4524TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004525 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004526 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004527 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4528
4529 touch();
4530
4531 mTouchWindow->consumeAnyMotionDown();
4532}
4533
4534TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
4535 const sp<FakeWindowHandle>& w =
4536 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4537 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004538
4539 touch();
4540
4541 mTouchWindow->consumeAnyMotionDown();
4542}
4543
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004544TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
4545 const sp<FakeWindowHandle>& w =
4546 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4547 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4548
4549 touch();
4550
4551 w->assertNoEvents();
4552}
4553
4554/**
4555 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
4556 * inside) while letting them pass-through. Note that even though touch passes through the occluding
4557 * window, the occluding window will still receive ACTION_OUTSIDE event.
4558 */
4559TEST_F(InputDispatcherUntrustedTouchesTest,
4560 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
4561 const sp<FakeWindowHandle>& w =
4562 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4563 w->addFlags(InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH);
4564 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4565
4566 touch();
4567
4568 w->consumeMotionOutside();
4569}
4570
4571TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
4572 const sp<FakeWindowHandle>& w =
4573 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4574 w->addFlags(InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH);
4575 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4576
4577 touch();
4578
4579 InputEvent* event = w->consume();
4580 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType());
4581 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
4582 EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getX());
4583 EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getY());
4584}
4585
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004586TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004587 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004588 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4589 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004590 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4591
4592 touch();
4593
4594 mTouchWindow->consumeAnyMotionDown();
4595}
4596
4597TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
4598 const sp<FakeWindowHandle>& w =
4599 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4600 MAXIMUM_OBSCURING_OPACITY);
4601 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004602
4603 touch();
4604
4605 mTouchWindow->consumeAnyMotionDown();
4606}
4607
4608TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004609 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004610 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4611 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004612 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4613
4614 touch();
4615
4616 mTouchWindow->assertNoEvents();
4617}
4618
4619TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
4620 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
4621 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004622 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
4623 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004624 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004625 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
4626 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004627 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
4628
4629 touch();
4630
4631 mTouchWindow->assertNoEvents();
4632}
4633
4634TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
4635 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
4636 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004637 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
4638 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004639 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004640 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
4641 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004642 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
4643
4644 touch();
4645
4646 mTouchWindow->consumeAnyMotionDown();
4647}
4648
4649TEST_F(InputDispatcherUntrustedTouchesTest,
4650 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
4651 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004652 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4653 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004654 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004655 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
4656 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004657 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
4658
4659 touch();
4660
4661 mTouchWindow->consumeAnyMotionDown();
4662}
4663
4664TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
4665 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004666 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4667 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004668 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004669 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
4670 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004671 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004672
4673 touch();
4674
4675 mTouchWindow->assertNoEvents();
4676}
4677
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004678TEST_F(InputDispatcherUntrustedTouchesTest,
4679 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
4680 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004681 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
4682 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004683 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004684 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4685 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004686 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
4687
4688 touch();
4689
4690 mTouchWindow->assertNoEvents();
4691}
4692
4693TEST_F(InputDispatcherUntrustedTouchesTest,
4694 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
4695 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004696 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
4697 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004698 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004699 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4700 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004701 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
4702
4703 touch();
4704
4705 mTouchWindow->consumeAnyMotionDown();
4706}
4707
4708TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
4709 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004710 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
4711 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004712 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4713
4714 touch();
4715
4716 mTouchWindow->consumeAnyMotionDown();
4717}
4718
4719TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
4720 const sp<FakeWindowHandle>& w =
4721 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
4722 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4723
4724 touch();
4725
4726 mTouchWindow->consumeAnyMotionDown();
4727}
4728
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00004729TEST_F(InputDispatcherUntrustedTouchesTest,
4730 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
4731 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
4732 const sp<FakeWindowHandle>& w =
4733 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
4734 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4735
4736 touch();
4737
4738 mTouchWindow->assertNoEvents();
4739}
4740
4741TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
4742 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
4743 const sp<FakeWindowHandle>& w =
4744 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
4745 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4746
4747 touch();
4748
4749 mTouchWindow->consumeAnyMotionDown();
4750}
4751
4752TEST_F(InputDispatcherUntrustedTouchesTest,
4753 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
4754 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
4755 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004756 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4757 OPACITY_ABOVE_THRESHOLD);
4758 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4759
4760 touch();
4761
4762 mTouchWindow->consumeAnyMotionDown();
4763}
4764
4765TEST_F(InputDispatcherUntrustedTouchesTest,
4766 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
4767 const sp<FakeWindowHandle>& w1 =
4768 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
4769 OPACITY_BELOW_THRESHOLD);
4770 const sp<FakeWindowHandle>& w2 =
4771 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4772 OPACITY_BELOW_THRESHOLD);
4773 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
4774
4775 touch();
4776
4777 mTouchWindow->assertNoEvents();
4778}
4779
4780/**
4781 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
4782 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
4783 * (which alone would result in allowing touches) does not affect the blocking behavior.
4784 */
4785TEST_F(InputDispatcherUntrustedTouchesTest,
4786 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
4787 const sp<FakeWindowHandle>& wB =
4788 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
4789 OPACITY_BELOW_THRESHOLD);
4790 const sp<FakeWindowHandle>& wC =
4791 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
4792 OPACITY_BELOW_THRESHOLD);
4793 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
4794
4795 touch();
4796
4797 mTouchWindow->assertNoEvents();
4798}
4799
4800/**
4801 * This test is testing that a window from a different UID but with same application token doesn't
4802 * block the touch. Apps can share the application token for close UI collaboration for example.
4803 */
4804TEST_F(InputDispatcherUntrustedTouchesTest,
4805 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
4806 const sp<FakeWindowHandle>& w =
4807 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
4808 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00004809 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4810
4811 touch();
4812
4813 mTouchWindow->consumeAnyMotionDown();
4814}
4815
arthurhungb89ccb02020-12-30 16:19:01 +08004816class InputDispatcherDragTests : public InputDispatcherTest {
4817protected:
4818 std::shared_ptr<FakeApplicationHandle> mApp;
4819 sp<FakeWindowHandle> mWindow;
4820 sp<FakeWindowHandle> mSecondWindow;
4821 sp<FakeWindowHandle> mDragWindow;
4822
4823 void SetUp() override {
4824 InputDispatcherTest::SetUp();
4825 mApp = std::make_shared<FakeApplicationHandle>();
4826 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
4827 mWindow->setFrame(Rect(0, 0, 100, 100));
4828 mWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
4829
4830 mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
4831 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
4832 mSecondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
4833
4834 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
4835 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
4836 }
4837
4838 // Start performing drag, we will create a drag window and transfer touch to it.
4839 void performDrag() {
4840 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4841 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4842 {50, 50}))
4843 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4844
4845 // Window should receive motion event.
4846 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4847
4848 // The drag window covers the entire display
4849 mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
4850 mDispatcher->setInputWindows(
4851 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
4852
4853 // Transfer touch focus to the drag window
4854 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
4855 true /* isDragDrop */);
4856 mWindow->consumeMotionCancel();
4857 mDragWindow->consumeMotionDown();
4858 }
arthurhung6d4bed92021-03-17 11:59:33 +08004859
4860 // Start performing drag, we will create a drag window and transfer touch to it.
4861 void performStylusDrag() {
4862 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4863 injectMotionEvent(mDispatcher,
4864 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
4865 AINPUT_SOURCE_STYLUS)
4866 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
4867 .pointer(PointerBuilder(0,
4868 AMOTION_EVENT_TOOL_TYPE_STYLUS)
4869 .x(50)
4870 .y(50))
4871 .build()));
4872 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4873
4874 // The drag window covers the entire display
4875 mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
4876 mDispatcher->setInputWindows(
4877 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
4878
4879 // Transfer touch focus to the drag window
4880 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
4881 true /* isDragDrop */);
4882 mWindow->consumeMotionCancel();
4883 mDragWindow->consumeMotionDown();
4884 }
arthurhungb89ccb02020-12-30 16:19:01 +08004885};
4886
4887TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
4888 performDrag();
4889
4890 // Move on window.
4891 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4892 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4893 ADISPLAY_ID_DEFAULT, {50, 50}))
4894 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4895 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
4896 mWindow->consumeDragEvent(false, 50, 50);
4897 mSecondWindow->assertNoEvents();
4898
4899 // Move to another window.
4900 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4901 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4902 ADISPLAY_ID_DEFAULT, {150, 50}))
4903 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4904 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
4905 mWindow->consumeDragEvent(true, 150, 50);
4906 mSecondWindow->consumeDragEvent(false, 50, 50);
4907
4908 // Move back to original window.
4909 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4910 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4911 ADISPLAY_ID_DEFAULT, {50, 50}))
4912 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4913 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
4914 mWindow->consumeDragEvent(false, 50, 50);
4915 mSecondWindow->consumeDragEvent(true, -50, 50);
4916
4917 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4918 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
4919 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4920 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
4921 mWindow->assertNoEvents();
4922 mSecondWindow->assertNoEvents();
4923}
4924
arthurhungf452d0b2021-01-06 00:19:52 +08004925TEST_F(InputDispatcherDragTests, DragAndDrop) {
4926 performDrag();
4927
4928 // Move on window.
4929 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4930 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4931 ADISPLAY_ID_DEFAULT, {50, 50}))
4932 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4933 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
4934 mWindow->consumeDragEvent(false, 50, 50);
4935 mSecondWindow->assertNoEvents();
4936
4937 // Move to another window.
4938 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4939 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4940 ADISPLAY_ID_DEFAULT, {150, 50}))
4941 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4942 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
4943 mWindow->consumeDragEvent(true, 150, 50);
4944 mSecondWindow->consumeDragEvent(false, 50, 50);
4945
4946 // drop to another window.
4947 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4948 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4949 {150, 50}))
4950 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4951 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
4952 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
4953 mWindow->assertNoEvents();
4954 mSecondWindow->assertNoEvents();
4955}
4956
arthurhung6d4bed92021-03-17 11:59:33 +08004957TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
4958 performStylusDrag();
4959
4960 // Move on window and keep button pressed.
4961 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4962 injectMotionEvent(mDispatcher,
4963 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
4964 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
4965 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
4966 .x(50)
4967 .y(50))
4968 .build()))
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 and release button, expect to drop item.
4975 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4976 injectMotionEvent(mDispatcher,
4977 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
4978 .buttonState(0)
4979 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
4980 .x(150)
4981 .y(50))
4982 .build()))
4983 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4984 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
4985 mWindow->assertNoEvents();
4986 mSecondWindow->assertNoEvents();
4987 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
4988
4989 // nothing to the window.
4990 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4991 injectMotionEvent(mDispatcher,
4992 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
4993 .buttonState(0)
4994 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
4995 .x(150)
4996 .y(50))
4997 .build()))
4998 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4999 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5000 mWindow->assertNoEvents();
5001 mSecondWindow->assertNoEvents();
5002}
5003
Arthur Hung6d0571e2021-04-09 20:18:16 +08005004TEST_F(InputDispatcherDragTests, DragAndDrop_InvalidWindow) {
5005 performDrag();
5006
5007 // Set second window invisible.
5008 mSecondWindow->setVisible(false);
5009 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
5010
5011 // Move on window.
5012 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5013 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5014 ADISPLAY_ID_DEFAULT, {50, 50}))
5015 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5016 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5017 mWindow->consumeDragEvent(false, 50, 50);
5018 mSecondWindow->assertNoEvents();
5019
5020 // Move to another window.
5021 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5022 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5023 ADISPLAY_ID_DEFAULT, {150, 50}))
5024 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5025 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5026 mWindow->consumeDragEvent(true, 150, 50);
5027 mSecondWindow->assertNoEvents();
5028
5029 // drop to another window.
5030 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5031 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5032 {150, 50}))
5033 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5034 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5035 mFakePolicy->assertDropTargetEquals(nullptr);
5036 mWindow->assertNoEvents();
5037 mSecondWindow->assertNoEvents();
5038}
5039
Garfield Tane84e6f92019-08-29 17:28:41 -07005040} // namespace android::inputdispatcher