blob: 50b65cad5d4470c1fdbe3c59d4619329c5668631 [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 Pradhanf192a102021-08-06 14:01:18 +0000242 PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -0800243 std::unique_lock lock(mLock);
244 base::ScopedLockAssertion assumeLocked(mLock);
245
246 if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms,
247 [this, enabled]() REQUIRES(mLock) {
Prabir Pradhanf192a102021-08-06 14:01:18 +0000248 return mPointerCaptureRequest->enable ==
Prabir Pradhan99987712020-11-10 18:43:05 -0800249 enabled;
250 })) {
Prabir Pradhanf192a102021-08-06 14:01:18 +0000251 ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled
252 << ") to be called.";
253 return {};
Prabir Pradhan99987712020-11-10 18:43:05 -0800254 }
Prabir Pradhanf192a102021-08-06 14:01:18 +0000255 auto request = *mPointerCaptureRequest;
256 mPointerCaptureRequest.reset();
257 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -0800258 }
259
260 void assertSetPointerCaptureNotCalled() {
261 std::unique_lock lock(mLock);
262 base::ScopedLockAssertion assumeLocked(mLock);
263
264 if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) {
Prabir Pradhanf192a102021-08-06 14:01:18 +0000265 FAIL() << "Expected setPointerCapture(request) to not be called, but was called. "
Prabir Pradhan99987712020-11-10 18:43:05 -0800266 "enabled = "
Prabir Pradhanf192a102021-08-06 14:01:18 +0000267 << std::to_string(mPointerCaptureRequest->enable);
Prabir Pradhan99987712020-11-10 18:43:05 -0800268 }
Prabir Pradhanf192a102021-08-06 14:01:18 +0000269 mPointerCaptureRequest.reset();
Prabir Pradhan99987712020-11-10 18:43:05 -0800270 }
271
arthurhungf452d0b2021-01-06 00:19:52 +0800272 void assertDropTargetEquals(const sp<IBinder>& targetToken) {
273 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800274 ASSERT_TRUE(mNotifyDropWindowWasCalled);
arthurhungf452d0b2021-01-06 00:19:52 +0800275 ASSERT_EQ(targetToken, mDropTargetWindowToken);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800276 mNotifyDropWindowWasCalled = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800277 }
278
Michael Wrightd02c5b62014-02-10 15:10:22 -0800279private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700280 std::mutex mLock;
281 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
282 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
283 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
284 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800285
Prabir Pradhan99987712020-11-10 18:43:05 -0800286 std::condition_variable mPointerCaptureChangedCondition;
Prabir Pradhanf192a102021-08-06 14:01:18 +0000287
288 std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock);
Prabir Pradhan99987712020-11-10 18:43:05 -0800289
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700290 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700291 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000292 std::queue<sp<IBinder>> mAnrWindowTokens GUARDED_BY(mLock);
293 std::queue<sp<IBinder>> mResponsiveWindowTokens GUARDED_BY(mLock);
294 std::queue<int32_t> mAnrMonitorPids GUARDED_BY(mLock);
295 std::queue<int32_t> mResponsiveMonitorPids GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700296 std::condition_variable mNotifyAnr;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700297
arthurhungf452d0b2021-01-06 00:19:52 +0800298 sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800299 bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800300
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600301 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700302 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800303 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800304 }
305
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000306 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700307 std::scoped_lock lock(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000308 mAnrWindowTokens.push(connectionToken);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700309 mNotifyAnr.notify_all();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500310 }
311
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000312 void notifyMonitorUnresponsive(int32_t pid, const std::string&) override {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500313 std::scoped_lock lock(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000314 mAnrMonitorPids.push(pid);
315 mNotifyAnr.notify_all();
316 }
317
318 void notifyWindowResponsive(const sp<IBinder>& connectionToken) override {
319 std::scoped_lock lock(mLock);
320 mResponsiveWindowTokens.push(connectionToken);
321 mNotifyAnr.notify_all();
322 }
323
324 void notifyMonitorResponsive(int32_t pid) override {
325 std::scoped_lock lock(mLock);
326 mResponsiveMonitorPids.push(pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500327 mNotifyAnr.notify_all();
328 }
329
330 void notifyNoFocusedWindowAnr(
331 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
332 std::scoped_lock lock(mLock);
333 mAnrApplications.push(applicationHandle);
334 mNotifyAnr.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800335 }
336
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600337 void notifyInputChannelBroken(const sp<IBinder>&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600339 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700340
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600341 void notifyUntrustedTouch(const std::string& obscuringPackage) override {}
Chris Yef59a2f42020-10-16 12:55:26 -0700342 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
343 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
344 const std::vector<float>& values) override {}
345
346 void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType,
347 InputDeviceSensorAccuracy accuracy) override {}
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000348
Chris Yefb552902021-02-03 17:18:37 -0800349 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
350
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600351 void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800352 *outConfig = mConfig;
353 }
354
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600355 bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700356 std::scoped_lock lock(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800357 switch (inputEvent->getType()) {
358 case AINPUT_EVENT_TYPE_KEY: {
359 const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800360 mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800361 break;
362 }
363
364 case AINPUT_EVENT_TYPE_MOTION: {
365 const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800366 mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800367 break;
368 }
369 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800370 return true;
371 }
372
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600373 void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800374
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600375 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800376
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600377 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800378 return 0;
379 }
380
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600381 bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800382 return false;
383 }
384
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600385 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
386 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700387 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800388 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
389 * essentially a passthrough for notifySwitch.
390 */
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800391 mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800392 }
393
Sean Stoutb4e0a592021-02-23 07:34:53 -0800394 void pokeUserActivity(nsecs_t, int32_t, int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800395
Prabir Pradhan93f342c2021-03-11 15:05:30 -0800396 bool checkInjectEventsPermissionNonReentrant(int32_t pid, int32_t uid) override {
397 return pid == INJECTOR_PID && uid == INJECTOR_UID;
398 }
Jackal Guof9696682018-10-05 12:23:23 +0800399
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600400 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700401 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700402 mOnPointerDownToken = newToken;
403 }
404
Prabir Pradhanf192a102021-08-06 14:01:18 +0000405 void setPointerCapture(const PointerCaptureRequest& request) override {
Prabir Pradhan99987712020-11-10 18:43:05 -0800406 std::scoped_lock lock(mLock);
Prabir Pradhanf192a102021-08-06 14:01:18 +0000407 mPointerCaptureRequest = {request};
Prabir Pradhan99987712020-11-10 18:43:05 -0800408 mPointerCaptureChangedCondition.notify_all();
409 }
410
arthurhungf452d0b2021-01-06 00:19:52 +0800411 void notifyDropWindow(const sp<IBinder>& token, float x, float y) override {
412 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800413 mNotifyDropWindowWasCalled = true;
arthurhungf452d0b2021-01-06 00:19:52 +0800414 mDropTargetWindowToken = token;
415 }
416
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800417 void assertFilterInputEventWasCalled(int type, nsecs_t eventTime, int32_t action,
418 int32_t displayId) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700419 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800420 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
421 ASSERT_EQ(mFilteredEvent->getType(), type);
422
423 if (type == AINPUT_EVENT_TYPE_KEY) {
424 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*mFilteredEvent);
425 EXPECT_EQ(keyEvent.getEventTime(), eventTime);
426 EXPECT_EQ(keyEvent.getAction(), action);
427 EXPECT_EQ(keyEvent.getDisplayId(), displayId);
428 } else if (type == AINPUT_EVENT_TYPE_MOTION) {
429 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*mFilteredEvent);
430 EXPECT_EQ(motionEvent.getEventTime(), eventTime);
431 EXPECT_EQ(motionEvent.getAction(), action);
432 EXPECT_EQ(motionEvent.getDisplayId(), displayId);
433 } else {
434 FAIL() << "Unknown type: " << type;
435 }
436
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800437 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800438 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800439};
440
Michael Wrightd02c5b62014-02-10 15:10:22 -0800441// --- InputDispatcherTest ---
442
443class InputDispatcherTest : public testing::Test {
444protected:
445 sp<FakeInputDispatcherPolicy> mFakePolicy;
446 sp<InputDispatcher> mDispatcher;
447
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000448 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800449 mFakePolicy = new FakeInputDispatcherPolicy();
450 mDispatcher = new InputDispatcher(mFakePolicy);
Arthur Hungb92218b2018-08-14 12:00:21 +0800451 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000452 // Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700453 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800454 }
455
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000456 void TearDown() override {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700457 ASSERT_EQ(OK, mDispatcher->stop());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800458 mFakePolicy.clear();
459 mDispatcher.clear();
460 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700461
462 /**
463 * Used for debugging when writing the test
464 */
465 void dumpDispatcherState() {
466 std::string dump;
467 mDispatcher->dump(dump);
468 std::stringstream ss(dump);
469 std::string to;
470
471 while (std::getline(ss, to, '\n')) {
472 ALOGE("%s", to.c_str());
473 }
474 }
Vishnu Nair958da932020-08-21 17:12:37 -0700475
476 void setFocusedWindow(const sp<InputWindowHandle>& window,
477 const sp<InputWindowHandle>& focusedWindow = nullptr) {
478 FocusRequest request;
479 request.token = window->getToken();
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +0000480 request.windowName = window->getName();
Vishnu Nair958da932020-08-21 17:12:37 -0700481 if (focusedWindow) {
482 request.focusedToken = focusedWindow->getToken();
483 }
484 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
485 request.displayId = window->getInfo()->displayId;
486 mDispatcher->setFocusedWindow(request);
487 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800488};
489
Michael Wrightd02c5b62014-02-10 15:10:22 -0800490TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
491 KeyEvent event;
492
493 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800494 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
495 INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600496 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
497 ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800498 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700499 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800500 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800501 << "Should reject key events with undefined action.";
502
503 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800504 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
505 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600506 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800507 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700508 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800509 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800510 << "Should reject key events with ACTION_MULTIPLE.";
511}
512
513TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
514 MotionEvent event;
515 PointerProperties pointerProperties[MAX_POINTERS + 1];
516 PointerCoords pointerCoords[MAX_POINTERS + 1];
517 for (int i = 0; i <= MAX_POINTERS; i++) {
518 pointerProperties[i].clear();
519 pointerProperties[i].id = i;
520 pointerCoords[i].clear();
521 }
522
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800523 // Some constants commonly used below
524 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
525 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
526 constexpr int32_t metaState = AMETA_NONE;
527 constexpr MotionClassification classification = MotionClassification::NONE;
528
chaviw9eaa22c2020-07-01 16:21:27 -0700529 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800530 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800531 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
chaviw9eaa22c2020-07-01 16:21:27 -0700532 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification,
533 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky84f07f02021-04-16 10:42:42 -0700534 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_DISPLAY_SIZE,
535 AMOTION_EVENT_INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700536 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800537 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700538 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800539 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800540 << "Should reject motion events with undefined action.";
541
542 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800543 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700544 AMOTION_EVENT_ACTION_POINTER_DOWN |
545 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700546 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
547 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky84f07f02021-04-16 10:42:42 -0700548 AMOTION_EVENT_INVALID_DISPLAY_SIZE, AMOTION_EVENT_INVALID_DISPLAY_SIZE,
chaviw9eaa22c2020-07-01 16:21:27 -0700549 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
550 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800551 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700552 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800553 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800554 << "Should reject motion events with pointer down index too large.";
555
Garfield Tanfbe732e2020-01-24 11:26:14 -0800556 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700557 AMOTION_EVENT_ACTION_POINTER_DOWN |
558 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700559 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
560 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky84f07f02021-04-16 10:42:42 -0700561 AMOTION_EVENT_INVALID_DISPLAY_SIZE, AMOTION_EVENT_INVALID_DISPLAY_SIZE,
chaviw9eaa22c2020-07-01 16:21:27 -0700562 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
563 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800564 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700565 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800566 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800567 << "Should reject motion events with pointer down index too small.";
568
569 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800570 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700571 AMOTION_EVENT_ACTION_POINTER_UP |
572 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700573 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
574 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky84f07f02021-04-16 10:42:42 -0700575 AMOTION_EVENT_INVALID_DISPLAY_SIZE, AMOTION_EVENT_INVALID_DISPLAY_SIZE,
chaviw9eaa22c2020-07-01 16:21:27 -0700576 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
577 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800578 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700579 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800580 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800581 << "Should reject motion events with pointer up index too large.";
582
Garfield Tanfbe732e2020-01-24 11:26:14 -0800583 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700584 AMOTION_EVENT_ACTION_POINTER_UP |
585 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700586 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
587 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky84f07f02021-04-16 10:42:42 -0700588 AMOTION_EVENT_INVALID_DISPLAY_SIZE, AMOTION_EVENT_INVALID_DISPLAY_SIZE,
chaviw9eaa22c2020-07-01 16:21:27 -0700589 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
590 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800591 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700592 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800593 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800594 << "Should reject motion events with pointer up index too small.";
595
596 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800597 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
598 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700599 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky84f07f02021-04-16 10:42:42 -0700600 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_DISPLAY_SIZE,
601 AMOTION_EVENT_INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700602 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800603 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700604 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800605 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800606 << "Should reject motion events with 0 pointers.";
607
Garfield Tanfbe732e2020-01-24 11:26:14 -0800608 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
609 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700610 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky84f07f02021-04-16 10:42:42 -0700611 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_DISPLAY_SIZE,
612 AMOTION_EVENT_INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700613 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800614 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700615 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800616 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800617 << "Should reject motion events with more than MAX_POINTERS pointers.";
618
619 // Rejects motion events with invalid pointer ids.
620 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800621 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
622 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700623 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky84f07f02021-04-16 10:42:42 -0700624 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_DISPLAY_SIZE,
625 AMOTION_EVENT_INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700626 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800627 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700628 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800629 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800630 << "Should reject motion events with pointer ids less than 0.";
631
632 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800633 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
634 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700635 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky84f07f02021-04-16 10:42:42 -0700636 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_DISPLAY_SIZE,
637 AMOTION_EVENT_INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700638 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800639 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700640 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800641 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800642 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
643
644 // Rejects motion events with duplicate pointer ids.
645 pointerProperties[0].id = 1;
646 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800647 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
648 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700649 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky84f07f02021-04-16 10:42:42 -0700650 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_DISPLAY_SIZE,
651 AMOTION_EVENT_INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700652 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800653 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700654 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800655 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800656 << "Should reject motion events with duplicate pointer ids.";
657}
658
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800659/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
660
661TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
662 constexpr nsecs_t eventTime = 20;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800663 NotifyConfigurationChangedArgs args(10 /*id*/, eventTime);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800664 mDispatcher->notifyConfigurationChanged(&args);
665 ASSERT_TRUE(mDispatcher->waitForIdle());
666
667 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
668}
669
670TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800671 NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/,
672 2 /*switchMask*/);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800673 mDispatcher->notifySwitch(&args);
674
675 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
676 args.policyFlags |= POLICY_FLAG_TRUSTED;
677 mFakePolicy->assertNotifySwitchWasCalled(args);
678}
679
Arthur Hungb92218b2018-08-14 12:00:21 +0800680// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700681static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700682static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s;
Arthur Hungb92218b2018-08-14 12:00:21 +0800683
684class FakeApplicationHandle : public InputApplicationHandle {
685public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700686 FakeApplicationHandle() {
687 mInfo.name = "Fake Application";
688 mInfo.token = new BBinder();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500689 mInfo.dispatchingTimeoutMillis =
690 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700691 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800692 virtual ~FakeApplicationHandle() {}
693
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000694 virtual bool updateInfo() override { return true; }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700695
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500696 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
697 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700698 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800699};
700
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800701class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800702public:
Garfield Tan15601662020-09-22 15:32:38 -0700703 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800704 : mName(name) {
Garfield Tan15601662020-09-22 15:32:38 -0700705 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800706 }
707
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800708 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700709 InputEvent* event;
710 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
711 if (!consumeSeq) {
712 return nullptr;
713 }
714 finishEvent(*consumeSeq);
715 return event;
716 }
717
718 /**
719 * Receive an event without acknowledging it.
720 * Return the sequence number that could later be used to send finished signal.
721 */
722 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800723 uint32_t consumeSeq;
724 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800725
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800726 std::chrono::time_point start = std::chrono::steady_clock::now();
727 status_t status = WOULD_BLOCK;
728 while (status == WOULD_BLOCK) {
chaviw81e2bb92019-12-18 15:03:51 -0800729 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800730 &event);
731 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
732 if (elapsed > 100ms) {
733 break;
734 }
735 }
736
737 if (status == WOULD_BLOCK) {
738 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700739 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800740 }
741
742 if (status != OK) {
743 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700744 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800745 }
746 if (event == nullptr) {
747 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700748 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800749 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700750 if (outEvent != nullptr) {
751 *outEvent = event;
752 }
753 return consumeSeq;
754 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800755
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700756 /**
757 * To be used together with "receiveEvent" to complete the consumption of an event.
758 */
759 void finishEvent(uint32_t consumeSeq) {
760 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
761 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800762 }
763
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000764 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
765 const status_t status = mConsumer->sendTimeline(inputEventId, timeline);
766 ASSERT_EQ(OK, status);
767 }
768
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000769 void consumeEvent(int32_t expectedEventType, int32_t expectedAction,
770 std::optional<int32_t> expectedDisplayId,
771 std::optional<int32_t> expectedFlags) {
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800772 InputEvent* event = consume();
773
774 ASSERT_NE(nullptr, event) << mName.c_str()
775 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800776 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700777 << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType)
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800778 << " event, got " << inputEventTypeToString(event->getType()) << " event";
Arthur Hungb92218b2018-08-14 12:00:21 +0800779
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000780 if (expectedDisplayId.has_value()) {
781 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
782 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800783
Tiger Huang8664f8c2018-10-11 19:14:35 +0800784 switch (expectedEventType) {
785 case AINPUT_EVENT_TYPE_KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800786 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
787 EXPECT_EQ(expectedAction, keyEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000788 if (expectedFlags.has_value()) {
789 EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags());
790 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800791 break;
792 }
793 case AINPUT_EVENT_TYPE_MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800794 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
795 EXPECT_EQ(expectedAction, motionEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000796 if (expectedFlags.has_value()) {
797 EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags());
798 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800799 break;
800 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100801 case AINPUT_EVENT_TYPE_FOCUS: {
802 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
803 }
Prabir Pradhan99987712020-11-10 18:43:05 -0800804 case AINPUT_EVENT_TYPE_CAPTURE: {
805 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
806 }
arthurhungb89ccb02020-12-30 16:19:01 +0800807 case AINPUT_EVENT_TYPE_DRAG: {
808 FAIL() << "Use 'consumeDragEvent' for DRAG events";
809 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800810 default: {
811 FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType;
812 }
813 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800814 }
815
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100816 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
817 InputEvent* event = consume();
818 ASSERT_NE(nullptr, event) << mName.c_str()
819 << ": consumer should have returned non-NULL event.";
820 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
821 << "Got " << inputEventTypeToString(event->getType())
822 << " event instead of FOCUS event";
823
824 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
825 << mName.c_str() << ": event displayId should always be NONE.";
826
827 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
828 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
829 EXPECT_EQ(inTouchMode, focusEvent->getInTouchMode());
830 }
831
Prabir Pradhan99987712020-11-10 18:43:05 -0800832 void consumeCaptureEvent(bool hasCapture) {
833 const InputEvent* event = consume();
834 ASSERT_NE(nullptr, event) << mName.c_str()
835 << ": consumer should have returned non-NULL event.";
836 ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType())
837 << "Got " << inputEventTypeToString(event->getType())
838 << " event instead of CAPTURE event";
839
840 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
841 << mName.c_str() << ": event displayId should always be NONE.";
842
843 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
844 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
845 }
846
arthurhungb89ccb02020-12-30 16:19:01 +0800847 void consumeDragEvent(bool isExiting, float x, float y) {
848 const InputEvent* event = consume();
849 ASSERT_NE(nullptr, event) << mName.c_str()
850 << ": consumer should have returned non-NULL event.";
851 ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType())
852 << "Got " << inputEventTypeToString(event->getType())
853 << " event instead of DRAG event";
854
855 EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
856 << mName.c_str() << ": event displayId should always be NONE.";
857
858 const auto& dragEvent = static_cast<const DragEvent&>(*event);
859 EXPECT_EQ(isExiting, dragEvent.isExiting());
860 EXPECT_EQ(x, dragEvent.getX());
861 EXPECT_EQ(y, dragEvent.getY());
862 }
863
chaviwd1c23182019-12-20 18:44:56 -0800864 void assertNoEvents() {
865 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700866 if (event == nullptr) {
867 return;
868 }
869 if (event->getType() == AINPUT_EVENT_TYPE_KEY) {
870 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
871 ADD_FAILURE() << "Received key event "
872 << KeyEvent::actionToString(keyEvent.getAction());
873 } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) {
874 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
875 ADD_FAILURE() << "Received motion event "
876 << MotionEvent::actionToString(motionEvent.getAction());
877 } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) {
878 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
879 ADD_FAILURE() << "Received focus event, hasFocus = "
880 << (focusEvent.getHasFocus() ? "true" : "false");
Prabir Pradhan99987712020-11-10 18:43:05 -0800881 } else if (event->getType() == AINPUT_EVENT_TYPE_CAPTURE) {
882 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
883 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
884 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700885 }
886 FAIL() << mName.c_str()
887 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -0800888 }
889
890 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
891
892protected:
893 std::unique_ptr<InputConsumer> mConsumer;
894 PreallocatedInputEventFactory mEventFactory;
895
896 std::string mName;
897};
898
899class FakeWindowHandle : public InputWindowHandle {
900public:
901 static const int32_t WIDTH = 600;
902 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -0800903
Chris Yea209fde2020-07-22 13:54:51 -0700904 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
chaviwd1c23182019-12-20 18:44:56 -0800905 const sp<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500906 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -0800907 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500908 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -0700909 base::Result<std::unique_ptr<InputChannel>> channel =
910 dispatcher->createInputChannel(name);
911 token = (*channel)->getConnectionToken();
912 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -0800913 }
914
915 inputApplicationHandle->updateInfo();
916 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
917
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500918 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700919 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -0800920 mInfo.name = name;
Michael Wright44753b12020-07-08 13:48:11 +0100921 mInfo.type = InputWindowInfo::Type::APPLICATION;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500922 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000923 mInfo.alpha = 1.0;
chaviwd1c23182019-12-20 18:44:56 -0800924 mInfo.frameLeft = 0;
925 mInfo.frameTop = 0;
926 mInfo.frameRight = WIDTH;
927 mInfo.frameBottom = HEIGHT;
chaviw1ff3d1e2020-07-01 15:53:47 -0700928 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -0800929 mInfo.globalScaleFactor = 1.0;
930 mInfo.touchableRegion.clear();
931 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
932 mInfo.visible = true;
Vishnu Nair47074b82020-08-14 11:54:47 -0700933 mInfo.focusable = false;
chaviwd1c23182019-12-20 18:44:56 -0800934 mInfo.hasWallpaper = false;
935 mInfo.paused = false;
chaviwd1c23182019-12-20 18:44:56 -0800936 mInfo.ownerPid = INJECTOR_PID;
937 mInfo.ownerUid = INJECTOR_UID;
chaviwd1c23182019-12-20 18:44:56 -0800938 mInfo.displayId = displayId;
939 }
940
941 virtual bool updateInfo() { return true; }
942
Vishnu Nair47074b82020-08-14 11:54:47 -0700943 void setFocusable(bool focusable) { mInfo.focusable = focusable; }
chaviwd1c23182019-12-20 18:44:56 -0800944
Vishnu Nair958da932020-08-21 17:12:37 -0700945 void setVisible(bool visible) { mInfo.visible = visible; }
946
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700947 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500948 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700949 }
950
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700951 void setPaused(bool paused) { mInfo.paused = paused; }
952
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000953 void setAlpha(float alpha) { mInfo.alpha = alpha; }
954
955 void setTouchOcclusionMode(android::os::TouchOcclusionMode mode) {
956 mInfo.touchOcclusionMode = mode;
957 }
958
Bernardo Rufino7393d172021-02-26 13:56:11 +0000959 void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
960
chaviwd1c23182019-12-20 18:44:56 -0800961 void setFrame(const Rect& frame) {
962 mInfo.frameLeft = frame.left;
963 mInfo.frameTop = frame.top;
964 mInfo.frameRight = frame.right;
965 mInfo.frameBottom = frame.bottom;
arthurhungb89ccb02020-12-30 16:19:01 +0800966 mInfo.transform.set(-frame.left, -frame.top);
chaviwd1c23182019-12-20 18:44:56 -0800967 mInfo.touchableRegion.clear();
968 mInfo.addTouchableRegion(frame);
969 }
970
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +0000971 void addFlags(Flags<InputWindowInfo::Flag> flags) { mInfo.flags |= flags; }
972
Michael Wright44753b12020-07-08 13:48:11 +0100973 void setFlags(Flags<InputWindowInfo::Flag> flags) { mInfo.flags = flags; }
chaviwd1c23182019-12-20 18:44:56 -0800974
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500975 void setInputFeatures(InputWindowInfo::Feature features) { mInfo.inputFeatures = features; }
976
chaviw9eaa22c2020-07-01 16:21:27 -0700977 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
978 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
979 }
980
981 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -0700982
yunho.shinf4a80b82020-11-16 21:13:57 +0900983 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
984
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800985 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
986 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
987 expectedFlags);
988 }
989
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700990 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
991 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
992 }
993
Svet Ganov5d3bc372020-01-26 23:11:07 -0800994 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000995 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800996 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId,
997 expectedFlags);
998 }
999
1000 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001001 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -08001002 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId,
1003 expectedFlags);
1004 }
1005
1006 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001007 int32_t expectedFlags = 0) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001008 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
1009 }
1010
1011 void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
1012 std::optional<int32_t> expectedFlags = std::nullopt) {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001013 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
1014 expectedFlags);
1015 }
1016
Svet Ganov5d3bc372020-01-26 23:11:07 -08001017 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001018 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1019 int32_t expectedFlags = 0) {
1020 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
1021 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001022 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
1023 }
1024
1025 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001026 int32_t expectedFlags = 0) {
1027 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
1028 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001029 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
1030 }
1031
1032 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001033 int32_t expectedFlags = 0) {
Michael Wright3a240c42019-12-10 20:53:41 +00001034 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
1035 expectedFlags);
1036 }
1037
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001038 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1039 int32_t expectedFlags = 0) {
1040 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
1041 expectedFlags);
1042 }
1043
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001044 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
1045 ASSERT_NE(mInputReceiver, nullptr)
1046 << "Cannot consume events from a window with no receiver";
1047 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
1048 }
1049
Prabir Pradhan99987712020-11-10 18:43:05 -08001050 void consumeCaptureEvent(bool hasCapture) {
1051 ASSERT_NE(mInputReceiver, nullptr)
1052 << "Cannot consume events from a window with no receiver";
1053 mInputReceiver->consumeCaptureEvent(hasCapture);
1054 }
1055
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001056 void consumeEvent(int32_t expectedEventType, int32_t expectedAction,
1057 std::optional<int32_t> expectedDisplayId,
1058 std::optional<int32_t> expectedFlags) {
chaviwd1c23182019-12-20 18:44:56 -08001059 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
1060 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
1061 expectedFlags);
1062 }
1063
arthurhungb89ccb02020-12-30 16:19:01 +08001064 void consumeDragEvent(bool isExiting, float x, float y) {
1065 mInputReceiver->consumeDragEvent(isExiting, x, y);
1066 }
1067
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001068 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001069 if (mInputReceiver == nullptr) {
1070 ADD_FAILURE() << "Invalid receive event on window with no receiver";
1071 return std::nullopt;
1072 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001073 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001074 }
1075
1076 void finishEvent(uint32_t sequenceNum) {
1077 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1078 mInputReceiver->finishEvent(sequenceNum);
1079 }
1080
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001081 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
1082 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1083 mInputReceiver->sendTimeline(inputEventId, timeline);
1084 }
1085
chaviwaf87b3e2019-10-01 16:59:28 -07001086 InputEvent* consume() {
1087 if (mInputReceiver == nullptr) {
1088 return nullptr;
1089 }
1090 return mInputReceiver->consume();
1091 }
1092
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001093 MotionEvent* consumeMotion() {
1094 InputEvent* event = consume();
1095 if (event == nullptr) {
1096 ADD_FAILURE() << "Consume failed : no event";
1097 return nullptr;
1098 }
1099 if (event->getType() != AINPUT_EVENT_TYPE_MOTION) {
1100 ADD_FAILURE() << "Instead of motion event, got "
1101 << inputEventTypeToString(event->getType());
1102 return nullptr;
1103 }
1104 return static_cast<MotionEvent*>(event);
1105 }
1106
Arthur Hungb92218b2018-08-14 12:00:21 +08001107 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001108 if (mInputReceiver == nullptr &&
1109 mInfo.inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL)) {
1110 return; // Can't receive events if the window does not have input channel
1111 }
1112 ASSERT_NE(nullptr, mInputReceiver)
1113 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -08001114 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +08001115 }
1116
chaviwaf87b3e2019-10-01 16:59:28 -07001117 sp<IBinder> getToken() { return mInfo.token; }
1118
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001119 const std::string& getName() { return mName; }
1120
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001121 void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) {
1122 mInfo.ownerPid = ownerPid;
1123 mInfo.ownerUid = ownerUid;
1124 }
1125
chaviwd1c23182019-12-20 18:44:56 -08001126private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001127 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -08001128 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001129 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001130};
1131
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001132std::atomic<int32_t> FakeWindowHandle::sId{1};
1133
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001134static InputEventInjectionResult injectKey(
1135 const sp<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
1136 int32_t displayId = ADISPLAY_ID_NONE,
1137 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001138 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
1139 bool allowKeyRepeat = true) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001140 KeyEvent event;
1141 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1142
1143 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001144 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001145 INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE,
1146 repeatCount, currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001147
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001148 int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
1149 if (!allowKeyRepeat) {
1150 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
1151 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001152 // Inject event until dispatch out.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001153 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, syncMode,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001154 injectionTimeout, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001155}
1156
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001157static InputEventInjectionResult injectKeyDown(const sp<InputDispatcher>& dispatcher,
1158 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001159 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId);
1160}
1161
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001162// Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without
1163// sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it
1164// has to be woken up to process the repeating key.
1165static InputEventInjectionResult injectKeyDownNoRepeat(const sp<InputDispatcher>& dispatcher,
1166 int32_t displayId = ADISPLAY_ID_NONE) {
1167 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId,
1168 InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT,
1169 /* allowKeyRepeat */ false);
1170}
1171
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001172static InputEventInjectionResult injectKeyUp(const sp<InputDispatcher>& dispatcher,
1173 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001174 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId);
1175}
1176
Garfield Tandf26e862020-07-01 20:18:19 -07001177class PointerBuilder {
1178public:
1179 PointerBuilder(int32_t id, int32_t toolType) {
1180 mProperties.clear();
1181 mProperties.id = id;
1182 mProperties.toolType = toolType;
1183 mCoords.clear();
1184 }
1185
1186 PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); }
1187
1188 PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); }
1189
1190 PointerBuilder& axis(int32_t axis, float value) {
1191 mCoords.setAxisValue(axis, value);
1192 return *this;
1193 }
1194
1195 PointerProperties buildProperties() const { return mProperties; }
1196
1197 PointerCoords buildCoords() const { return mCoords; }
1198
1199private:
1200 PointerProperties mProperties;
1201 PointerCoords mCoords;
1202};
1203
1204class MotionEventBuilder {
1205public:
1206 MotionEventBuilder(int32_t action, int32_t source) {
1207 mAction = action;
1208 mSource = source;
1209 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1210 }
1211
1212 MotionEventBuilder& eventTime(nsecs_t eventTime) {
1213 mEventTime = eventTime;
1214 return *this;
1215 }
1216
1217 MotionEventBuilder& displayId(int32_t displayId) {
1218 mDisplayId = displayId;
1219 return *this;
1220 }
1221
1222 MotionEventBuilder& actionButton(int32_t actionButton) {
1223 mActionButton = actionButton;
1224 return *this;
1225 }
1226
arthurhung6d4bed92021-03-17 11:59:33 +08001227 MotionEventBuilder& buttonState(int32_t buttonState) {
1228 mButtonState = buttonState;
Garfield Tandf26e862020-07-01 20:18:19 -07001229 return *this;
1230 }
1231
1232 MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) {
1233 mRawXCursorPosition = rawXCursorPosition;
1234 return *this;
1235 }
1236
1237 MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) {
1238 mRawYCursorPosition = rawYCursorPosition;
1239 return *this;
1240 }
1241
1242 MotionEventBuilder& pointer(PointerBuilder pointer) {
1243 mPointers.push_back(pointer);
1244 return *this;
1245 }
1246
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001247 MotionEventBuilder& addFlag(uint32_t flags) {
1248 mFlags |= flags;
1249 return *this;
1250 }
1251
Garfield Tandf26e862020-07-01 20:18:19 -07001252 MotionEvent build() {
1253 std::vector<PointerProperties> pointerProperties;
1254 std::vector<PointerCoords> pointerCoords;
1255 for (const PointerBuilder& pointer : mPointers) {
1256 pointerProperties.push_back(pointer.buildProperties());
1257 pointerCoords.push_back(pointer.buildCoords());
1258 }
1259
1260 // Set mouse cursor position for the most common cases to avoid boilerplate.
1261 if (mSource == AINPUT_SOURCE_MOUSE &&
1262 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) &&
1263 mPointers.size() == 1) {
1264 mRawXCursorPosition = pointerCoords[0].getX();
1265 mRawYCursorPosition = pointerCoords[0].getY();
1266 }
1267
1268 MotionEvent event;
chaviw9eaa22c2020-07-01 16:21:27 -07001269 ui::Transform identityTransform;
Garfield Tandf26e862020-07-01 20:18:19 -07001270 event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001271 mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE,
chaviw9eaa22c2020-07-01 16:21:27 -07001272 mButtonState, MotionClassification::NONE, identityTransform,
1273 /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition,
Evan Rosky84f07f02021-04-16 10:42:42 -07001274 mRawYCursorPosition, mDisplayWidth, mDisplayHeight, mEventTime, mEventTime,
1275 mPointers.size(), pointerProperties.data(), pointerCoords.data());
Garfield Tandf26e862020-07-01 20:18:19 -07001276
1277 return event;
1278 }
1279
1280private:
1281 int32_t mAction;
1282 int32_t mSource;
1283 nsecs_t mEventTime;
1284 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1285 int32_t mActionButton{0};
1286 int32_t mButtonState{0};
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001287 int32_t mFlags{0};
Garfield Tandf26e862020-07-01 20:18:19 -07001288 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1289 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
Evan Rosky84f07f02021-04-16 10:42:42 -07001290 int32_t mDisplayWidth{AMOTION_EVENT_INVALID_DISPLAY_SIZE};
1291 int32_t mDisplayHeight{AMOTION_EVENT_INVALID_DISPLAY_SIZE};
Garfield Tandf26e862020-07-01 20:18:19 -07001292
1293 std::vector<PointerBuilder> mPointers;
1294};
1295
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001296static InputEventInjectionResult injectMotionEvent(
Garfield Tandf26e862020-07-01 20:18:19 -07001297 const sp<InputDispatcher>& dispatcher, const MotionEvent& event,
1298 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001299 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT) {
Garfield Tandf26e862020-07-01 20:18:19 -07001300 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, injectionMode,
1301 injectionTimeout,
1302 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
1303}
1304
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001305static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001306 const sp<InputDispatcher>& dispatcher, int32_t action, int32_t source, int32_t displayId,
1307 const PointF& position,
1308 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001309 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1310 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001311 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001312 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC)) {
Garfield Tandf26e862020-07-01 20:18:19 -07001313 MotionEvent event = MotionEventBuilder(action, source)
1314 .displayId(displayId)
1315 .eventTime(eventTime)
1316 .rawXCursorPosition(cursorPosition.x)
1317 .rawYCursorPosition(cursorPosition.y)
1318 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1319 .x(position.x)
1320 .y(position.y))
1321 .build();
Arthur Hungb92218b2018-08-14 12:00:21 +08001322
1323 // Inject event until dispatch out.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001324 return injectMotionEvent(dispatcher, event, injectionTimeout, injectionMode);
Arthur Hungb92218b2018-08-14 12:00:21 +08001325}
1326
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001327static InputEventInjectionResult injectMotionDown(const sp<InputDispatcher>& dispatcher,
1328 int32_t source, int32_t displayId,
1329 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001330 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001331}
1332
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001333static InputEventInjectionResult injectMotionUp(const sp<InputDispatcher>& dispatcher,
1334 int32_t source, int32_t displayId,
1335 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001336 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001337}
1338
Jackal Guof9696682018-10-05 12:23:23 +08001339static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1340 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1341 // Define a valid key event.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001342 NotifyKeyArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1343 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A,
1344 KEY_A, AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001345
1346 return args;
1347}
1348
chaviwd1c23182019-12-20 18:44:56 -08001349static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId,
1350 const std::vector<PointF>& points) {
1351 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001352 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1353 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1354 }
1355
chaviwd1c23182019-12-20 18:44:56 -08001356 PointerProperties pointerProperties[pointerCount];
1357 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001358
chaviwd1c23182019-12-20 18:44:56 -08001359 for (size_t i = 0; i < pointerCount; i++) {
1360 pointerProperties[i].clear();
1361 pointerProperties[i].id = i;
1362 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001363
chaviwd1c23182019-12-20 18:44:56 -08001364 pointerCoords[i].clear();
1365 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1366 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1367 }
Jackal Guof9696682018-10-05 12:23:23 +08001368
1369 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1370 // Define a valid motion event.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001371 NotifyMotionArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001372 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1373 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001374 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1375 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001376 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1377 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001378
1379 return args;
1380}
1381
chaviwd1c23182019-12-20 18:44:56 -08001382static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1383 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1384}
1385
Prabir Pradhanf192a102021-08-06 14:01:18 +00001386static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(
1387 const PointerCaptureRequest& request) {
1388 return NotifyPointerCaptureChangedArgs(/* id */ 0, systemTime(SYSTEM_TIME_MONOTONIC), request);
Prabir Pradhan99987712020-11-10 18:43:05 -08001389}
1390
Arthur Hungb92218b2018-08-14 12:00:21 +08001391TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001392 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001393 sp<FakeWindowHandle> window =
1394 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001395
Arthur Hung72d8dc32020-03-28 00:48:39 +00001396 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001397 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1398 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1399 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001400
1401 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001402 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001403}
1404
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001405/**
1406 * Calling setInputWindows once with FLAG_NOT_TOUCH_MODAL should not cause any issues.
1407 * To ensure that window receives only events that were directly inside of it, add
1408 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1409 * when finding touched windows.
1410 * This test serves as a sanity check for the next test, where setInputWindows is
1411 * called twice.
1412 */
1413TEST_F(InputDispatcherTest, SetInputWindowOnce_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001414 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001415 sp<FakeWindowHandle> window =
1416 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1417 window->setFrame(Rect(0, 0, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01001418 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001419
1420 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001421 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001422 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1423 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001424 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001425
1426 // Window should receive motion event.
1427 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1428}
1429
1430/**
1431 * Calling setInputWindows twice, with the same info, should not cause any issues.
1432 * To ensure that window receives only events that were directly inside of it, add
1433 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1434 * when finding touched windows.
1435 */
1436TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001437 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001438 sp<FakeWindowHandle> window =
1439 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1440 window->setFrame(Rect(0, 0, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01001441 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001442
1443 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1444 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001445 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001446 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1447 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001448 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001449
1450 // Window should receive motion event.
1451 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1452}
1453
Arthur Hungb92218b2018-08-14 12:00:21 +08001454// The foreground window should receive the first touch down event.
1455TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001456 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001457 sp<FakeWindowHandle> windowTop =
1458 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
1459 sp<FakeWindowHandle> windowSecond =
1460 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001461
Arthur Hung72d8dc32020-03-28 00:48:39 +00001462 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001463 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1464 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1465 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001466
1467 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001468 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001469 windowSecond->assertNoEvents();
1470}
1471
Garfield Tandf26e862020-07-01 20:18:19 -07001472TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07001473 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07001474 sp<FakeWindowHandle> windowLeft =
1475 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1476 windowLeft->setFrame(Rect(0, 0, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001477 windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001478 sp<FakeWindowHandle> windowRight =
1479 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1480 windowRight->setFrame(Rect(600, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001481 windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001482
1483 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1484
1485 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
1486
1487 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001488 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001489 injectMotionEvent(mDispatcher,
1490 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1491 AINPUT_SOURCE_MOUSE)
1492 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1493 .x(900)
1494 .y(400))
1495 .build()));
1496 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1497 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1498 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1499 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1500
1501 // Move cursor into left window
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_HOVER_MOVE,
1505 AINPUT_SOURCE_MOUSE)
1506 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1507 .x(300)
1508 .y(400))
1509 .build()));
1510 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1511 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1512 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1513 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1514 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1515 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1516
1517 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001518 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001519 injectMotionEvent(mDispatcher,
1520 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1521 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1522 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1523 .x(300)
1524 .y(400))
1525 .build()));
1526 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
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_BUTTON_PRESS,
1531 AINPUT_SOURCE_MOUSE)
1532 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1533 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1534 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1535 .x(300)
1536 .y(400))
1537 .build()));
1538 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1539 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1540
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001541 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001542 injectMotionEvent(mDispatcher,
1543 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1544 AINPUT_SOURCE_MOUSE)
1545 .buttonState(0)
1546 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1547 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1548 .x(300)
1549 .y(400))
1550 .build()));
1551 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1552 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1553
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001554 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001555 injectMotionEvent(mDispatcher,
1556 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
1557 .buttonState(0)
1558 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1559 .x(300)
1560 .y(400))
1561 .build()));
1562 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1563
1564 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001565 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001566 injectMotionEvent(mDispatcher,
1567 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1568 AINPUT_SOURCE_MOUSE)
1569 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1570 .x(900)
1571 .y(400))
1572 .build()));
1573 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1574 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1575 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1576 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1577 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1578 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1579}
1580
1581// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
1582// directly in this test.
1583TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07001584 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07001585 sp<FakeWindowHandle> window =
1586 new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1587 window->setFrame(Rect(0, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001588 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001589
1590 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1591
1592 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1593
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001594 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001595 injectMotionEvent(mDispatcher,
1596 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
1597 AINPUT_SOURCE_MOUSE)
1598 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1599 .x(300)
1600 .y(400))
1601 .build()));
1602 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1603 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1604
1605 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001606 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001607 injectMotionEvent(mDispatcher,
1608 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1609 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1610 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1611 .x(300)
1612 .y(400))
1613 .build()));
1614 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
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_BUTTON_PRESS,
1619 AINPUT_SOURCE_MOUSE)
1620 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1621 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1622 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1623 .x(300)
1624 .y(400))
1625 .build()));
1626 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1627 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1628
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001629 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001630 injectMotionEvent(mDispatcher,
1631 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1632 AINPUT_SOURCE_MOUSE)
1633 .buttonState(0)
1634 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1635 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1636 .x(300)
1637 .y(400))
1638 .build()));
1639 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1640 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1641
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001642 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001643 injectMotionEvent(mDispatcher,
1644 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
1645 .buttonState(0)
1646 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1647 .x(300)
1648 .y(400))
1649 .build()));
1650 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1651
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001652 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001653 injectMotionEvent(mDispatcher,
1654 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
1655 AINPUT_SOURCE_MOUSE)
1656 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1657 .x(300)
1658 .y(400))
1659 .build()));
1660 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1661 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1662}
1663
Garfield Tan00f511d2019-06-12 16:55:40 -07001664TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07001665 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07001666
1667 sp<FakeWindowHandle> windowLeft =
1668 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1669 windowLeft->setFrame(Rect(0, 0, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001670 windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07001671 sp<FakeWindowHandle> windowRight =
1672 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1673 windowRight->setFrame(Rect(600, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001674 windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07001675
1676 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1677
Arthur Hung72d8dc32020-03-28 00:48:39 +00001678 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07001679
1680 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
1681 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001682 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07001683 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001684 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001685 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07001686 windowRight->assertNoEvents();
1687}
1688
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001689TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001690 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001691 sp<FakeWindowHandle> window =
1692 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07001693 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001694
Arthur Hung72d8dc32020-03-28 00:48:39 +00001695 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001696 setFocusedWindow(window);
1697
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001698 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001699
1700 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1701 mDispatcher->notifyKey(&keyArgs);
1702
1703 // Window should receive key down event.
1704 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1705
1706 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
1707 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001708 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001709 mDispatcher->notifyDeviceReset(&args);
1710 window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
1711 AKEY_EVENT_FLAG_CANCELED);
1712}
1713
1714TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001715 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001716 sp<FakeWindowHandle> window =
1717 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1718
Arthur Hung72d8dc32020-03-28 00:48:39 +00001719 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001720
1721 NotifyMotionArgs motionArgs =
1722 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1723 ADISPLAY_ID_DEFAULT);
1724 mDispatcher->notifyMotion(&motionArgs);
1725
1726 // Window should receive motion down event.
1727 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1728
1729 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
1730 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001731 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001732 mDispatcher->notifyDeviceReset(&args);
1733 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT,
1734 0 /*expectedFlags*/);
1735}
1736
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001737using TransferFunction =
1738 std::function<bool(sp<InputDispatcher> dispatcher, sp<IBinder>, sp<IBinder>)>;
1739
1740class TransferTouchFixture : public InputDispatcherTest,
1741 public ::testing::WithParamInterface<TransferFunction> {};
1742
1743TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07001744 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001745
1746 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001747 sp<FakeWindowHandle> firstWindow =
1748 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1749 sp<FakeWindowHandle> secondWindow =
1750 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001751
1752 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001753 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001754
1755 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001756 NotifyMotionArgs downMotionArgs =
1757 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1758 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001759 mDispatcher->notifyMotion(&downMotionArgs);
1760 // Only the first window should get the down event
1761 firstWindow->consumeMotionDown();
1762 secondWindow->assertNoEvents();
1763
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001764 // Transfer touch to the second window
1765 TransferFunction f = GetParam();
1766 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
1767 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001768 // The first window gets cancel and the second gets down
1769 firstWindow->consumeMotionCancel();
1770 secondWindow->consumeMotionDown();
1771
1772 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001773 NotifyMotionArgs upMotionArgs =
1774 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1775 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001776 mDispatcher->notifyMotion(&upMotionArgs);
1777 // The first window gets no events and the second gets up
1778 firstWindow->assertNoEvents();
1779 secondWindow->consumeMotionUp();
1780}
1781
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001782TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001783 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001784
1785 PointF touchPoint = {10, 10};
1786
1787 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001788 sp<FakeWindowHandle> firstWindow =
1789 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1790 sp<FakeWindowHandle> secondWindow =
1791 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001792
1793 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001794 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001795
1796 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001797 NotifyMotionArgs downMotionArgs =
1798 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1799 ADISPLAY_ID_DEFAULT, {touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001800 mDispatcher->notifyMotion(&downMotionArgs);
1801 // Only the first window should get the down event
1802 firstWindow->consumeMotionDown();
1803 secondWindow->assertNoEvents();
1804
1805 // Send pointer down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001806 NotifyMotionArgs pointerDownMotionArgs =
1807 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1808 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1809 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1810 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001811 mDispatcher->notifyMotion(&pointerDownMotionArgs);
1812 // Only the first window should get the pointer down event
1813 firstWindow->consumeMotionPointerDown(1);
1814 secondWindow->assertNoEvents();
1815
1816 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001817 TransferFunction f = GetParam();
1818 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
1819 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001820 // The first window gets cancel and the second gets down and pointer down
1821 firstWindow->consumeMotionCancel();
1822 secondWindow->consumeMotionDown();
1823 secondWindow->consumeMotionPointerDown(1);
1824
1825 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001826 NotifyMotionArgs pointerUpMotionArgs =
1827 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1828 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1829 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1830 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001831 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1832 // The first window gets nothing and the second gets pointer up
1833 firstWindow->assertNoEvents();
1834 secondWindow->consumeMotionPointerUp(1);
1835
1836 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001837 NotifyMotionArgs upMotionArgs =
1838 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1839 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001840 mDispatcher->notifyMotion(&upMotionArgs);
1841 // The first window gets nothing and the second gets up
1842 firstWindow->assertNoEvents();
1843 secondWindow->consumeMotionUp();
1844}
1845
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001846// For the cases of single pointer touch and two pointers non-split touch, the api's
1847// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
1848// for the case where there are multiple pointers split across several windows.
1849INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
1850 ::testing::Values(
1851 [&](sp<InputDispatcher> dispatcher, sp<IBinder> /*ignored*/,
1852 sp<IBinder> destChannelToken) {
1853 return dispatcher->transferTouch(destChannelToken);
1854 },
1855 [&](sp<InputDispatcher> dispatcher, sp<IBinder> from,
1856 sp<IBinder> to) {
1857 return dispatcher->transferTouchFocus(from, to,
1858 false /*isDragAndDrop*/);
1859 }));
1860
Svet Ganov5d3bc372020-01-26 23:11:07 -08001861TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001862 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001863
1864 // Create a non touch modal window that supports split touch
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001865 sp<FakeWindowHandle> firstWindow =
1866 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001867 firstWindow->setFrame(Rect(0, 0, 600, 400));
Michael Wright44753b12020-07-08 13:48:11 +01001868 firstWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1869 InputWindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001870
1871 // Create a non touch modal window that supports split touch
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001872 sp<FakeWindowHandle> secondWindow =
1873 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001874 secondWindow->setFrame(Rect(0, 400, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001875 secondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1876 InputWindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001877
1878 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001879 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001880
1881 PointF pointInFirst = {300, 200};
1882 PointF pointInSecond = {300, 600};
1883
1884 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001885 NotifyMotionArgs firstDownMotionArgs =
1886 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1887 ADISPLAY_ID_DEFAULT, {pointInFirst});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001888 mDispatcher->notifyMotion(&firstDownMotionArgs);
1889 // Only the first window should get the down event
1890 firstWindow->consumeMotionDown();
1891 secondWindow->assertNoEvents();
1892
1893 // Send down to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001894 NotifyMotionArgs secondDownMotionArgs =
1895 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1896 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1897 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1898 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001899 mDispatcher->notifyMotion(&secondDownMotionArgs);
1900 // The first window gets a move and the second a down
1901 firstWindow->consumeMotionMove();
1902 secondWindow->consumeMotionDown();
1903
1904 // Transfer touch focus to the second window
1905 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1906 // The first window gets cancel and the new gets pointer down (it already saw down)
1907 firstWindow->consumeMotionCancel();
1908 secondWindow->consumeMotionPointerDown(1);
1909
1910 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001911 NotifyMotionArgs pointerUpMotionArgs =
1912 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1913 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1914 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1915 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001916 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1917 // The first window gets nothing and the second gets pointer up
1918 firstWindow->assertNoEvents();
1919 secondWindow->consumeMotionPointerUp(1);
1920
1921 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001922 NotifyMotionArgs upMotionArgs =
1923 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1924 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001925 mDispatcher->notifyMotion(&upMotionArgs);
1926 // The first window gets nothing and the second gets up
1927 firstWindow->assertNoEvents();
1928 secondWindow->consumeMotionUp();
1929}
1930
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00001931// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
1932// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
1933// touch is not supported, so the touch should continue on those windows and the transferred-to
1934// window should get nothing.
1935TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
1936 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1937
1938 // Create a non touch modal window that supports split touch
1939 sp<FakeWindowHandle> firstWindow =
1940 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1941 firstWindow->setFrame(Rect(0, 0, 600, 400));
1942 firstWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1943 InputWindowInfo::Flag::SPLIT_TOUCH);
1944
1945 // Create a non touch modal window that supports split touch
1946 sp<FakeWindowHandle> secondWindow =
1947 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
1948 secondWindow->setFrame(Rect(0, 400, 600, 800));
1949 secondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1950 InputWindowInfo::Flag::SPLIT_TOUCH);
1951
1952 // Add the windows to the dispatcher
1953 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
1954
1955 PointF pointInFirst = {300, 200};
1956 PointF pointInSecond = {300, 600};
1957
1958 // Send down to the first window
1959 NotifyMotionArgs firstDownMotionArgs =
1960 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1961 ADISPLAY_ID_DEFAULT, {pointInFirst});
1962 mDispatcher->notifyMotion(&firstDownMotionArgs);
1963 // Only the first window should get the down event
1964 firstWindow->consumeMotionDown();
1965 secondWindow->assertNoEvents();
1966
1967 // Send down to the second window
1968 NotifyMotionArgs secondDownMotionArgs =
1969 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1970 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1971 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1972 {pointInFirst, pointInSecond});
1973 mDispatcher->notifyMotion(&secondDownMotionArgs);
1974 // The first window gets a move and the second a down
1975 firstWindow->consumeMotionMove();
1976 secondWindow->consumeMotionDown();
1977
1978 // Transfer touch focus to the second window
1979 const bool transferred = mDispatcher->transferTouch(secondWindow->getToken());
1980 // The 'transferTouch' call should not succeed, because there are 2 touched windows
1981 ASSERT_FALSE(transferred);
1982 firstWindow->assertNoEvents();
1983 secondWindow->assertNoEvents();
1984
1985 // The rest of the dispatch should proceed as normal
1986 // Send pointer up to the second window
1987 NotifyMotionArgs pointerUpMotionArgs =
1988 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1989 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1990 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1991 {pointInFirst, pointInSecond});
1992 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1993 // The first window gets MOVE and the second gets pointer up
1994 firstWindow->consumeMotionMove();
1995 secondWindow->consumeMotionUp();
1996
1997 // Send up event to the first window
1998 NotifyMotionArgs upMotionArgs =
1999 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2000 ADISPLAY_ID_DEFAULT);
2001 mDispatcher->notifyMotion(&upMotionArgs);
2002 // The first window gets nothing and the second gets up
2003 firstWindow->consumeMotionUp();
2004 secondWindow->assertNoEvents();
2005}
2006
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002007TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002008 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002009 sp<FakeWindowHandle> window =
2010 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2011
Vishnu Nair47074b82020-08-14 11:54:47 -07002012 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002013 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002014 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002015
2016 window->consumeFocusEvent(true);
2017
2018 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2019 mDispatcher->notifyKey(&keyArgs);
2020
2021 // Window should receive key down event.
2022 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2023}
2024
2025TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002026 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002027 sp<FakeWindowHandle> window =
2028 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2029
Arthur Hung72d8dc32020-03-28 00:48:39 +00002030 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002031
2032 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2033 mDispatcher->notifyKey(&keyArgs);
2034 mDispatcher->waitForIdle();
2035
2036 window->assertNoEvents();
2037}
2038
2039// If a window is touchable, but does not have focus, it should receive motion events, but not keys
2040TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07002041 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002042 sp<FakeWindowHandle> window =
2043 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2044
Arthur Hung72d8dc32020-03-28 00:48:39 +00002045 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002046
2047 // Send key
2048 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2049 mDispatcher->notifyKey(&keyArgs);
2050 // Send motion
2051 NotifyMotionArgs motionArgs =
2052 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2053 ADISPLAY_ID_DEFAULT);
2054 mDispatcher->notifyMotion(&motionArgs);
2055
2056 // Window should receive only the motion event
2057 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2058 window->assertNoEvents(); // Key event or focus event will not be received
2059}
2060
arthurhungea3f4fc2020-12-21 23:18:53 +08002061TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
2062 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2063
2064 // Create first non touch modal window that supports split touch
2065 sp<FakeWindowHandle> firstWindow =
2066 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
2067 firstWindow->setFrame(Rect(0, 0, 600, 400));
2068 firstWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
2069 InputWindowInfo::Flag::SPLIT_TOUCH);
2070
2071 // Create second non touch modal window that supports split touch
2072 sp<FakeWindowHandle> secondWindow =
2073 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
2074 secondWindow->setFrame(Rect(0, 400, 600, 800));
2075 secondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
2076 InputWindowInfo::Flag::SPLIT_TOUCH);
2077
2078 // Add the windows to the dispatcher
2079 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
2080
2081 PointF pointInFirst = {300, 200};
2082 PointF pointInSecond = {300, 600};
2083
2084 // Send down to the first window
2085 NotifyMotionArgs firstDownMotionArgs =
2086 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2087 ADISPLAY_ID_DEFAULT, {pointInFirst});
2088 mDispatcher->notifyMotion(&firstDownMotionArgs);
2089 // Only the first window should get the down event
2090 firstWindow->consumeMotionDown();
2091 secondWindow->assertNoEvents();
2092
2093 // Send down to the second window
2094 NotifyMotionArgs secondDownMotionArgs =
2095 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
2096 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2097 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2098 {pointInFirst, pointInSecond});
2099 mDispatcher->notifyMotion(&secondDownMotionArgs);
2100 // The first window gets a move and the second a down
2101 firstWindow->consumeMotionMove();
2102 secondWindow->consumeMotionDown();
2103
2104 // Send pointer cancel to the second window
2105 NotifyMotionArgs pointerUpMotionArgs =
2106 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
2107 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2108 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2109 {pointInFirst, pointInSecond});
2110 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
2111 mDispatcher->notifyMotion(&pointerUpMotionArgs);
2112 // The first window gets move and the second gets cancel.
2113 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
2114 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
2115
2116 // Send up event.
2117 NotifyMotionArgs upMotionArgs =
2118 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2119 ADISPLAY_ID_DEFAULT);
2120 mDispatcher->notifyMotion(&upMotionArgs);
2121 // The first window gets up and the second gets nothing.
2122 firstWindow->consumeMotionUp();
2123 secondWindow->assertNoEvents();
2124}
2125
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00002126TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
2127 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2128
2129 sp<FakeWindowHandle> window =
2130 new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2131 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2132 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
2133 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
2134 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
2135
2136 window->sendTimeline(1 /*inputEventId*/, graphicsTimeline);
2137 window->assertNoEvents();
2138 mDispatcher->waitForIdle();
2139}
2140
chaviwd1c23182019-12-20 18:44:56 -08002141class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00002142public:
2143 FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name,
chaviwd1c23182019-12-20 18:44:56 -08002144 int32_t displayId, bool isGestureMonitor = false) {
Garfield Tan15601662020-09-22 15:32:38 -07002145 base::Result<std::unique_ptr<InputChannel>> channel =
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +00002146 dispatcher->createInputMonitor(displayId, isGestureMonitor, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07002147 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00002148 }
2149
chaviwd1c23182019-12-20 18:44:56 -08002150 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
2151
2152 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
2153 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN,
2154 expectedDisplayId, expectedFlags);
2155 }
2156
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002157 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
2158
2159 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
2160
chaviwd1c23182019-12-20 18:44:56 -08002161 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
2162 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
2163 expectedDisplayId, expectedFlags);
2164 }
2165
2166 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
2167 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP,
2168 expectedDisplayId, expectedFlags);
2169 }
2170
Evan Rosky84f07f02021-04-16 10:42:42 -07002171 MotionEvent* consumeMotion() {
2172 InputEvent* event = mInputReceiver->consume();
2173 if (!event) {
2174 ADD_FAILURE() << "No event was produced";
2175 return nullptr;
2176 }
2177 if (event->getType() != AINPUT_EVENT_TYPE_MOTION) {
2178 ADD_FAILURE() << "Received event of type " << event->getType() << " instead of motion";
2179 return nullptr;
2180 }
2181 return static_cast<MotionEvent*>(event);
2182 }
2183
chaviwd1c23182019-12-20 18:44:56 -08002184 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
2185
2186private:
2187 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00002188};
2189
2190// Tests for gesture monitors
2191TEST_F(InputDispatcherTest, GestureMonitor_ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07002192 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00002193 sp<FakeWindowHandle> window =
2194 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002195 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00002196
chaviwd1c23182019-12-20 18:44:56 -08002197 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
2198 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00002199
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002200 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00002201 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002202 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00002203 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002204 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00002205}
2206
2207TEST_F(InputDispatcherTest, GestureMonitor_DoesNotReceiveKeyEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07002208 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00002209 sp<FakeWindowHandle> window =
2210 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2211
2212 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002213 window->setFocusable(true);
Michael Wright3a240c42019-12-10 20:53:41 +00002214
Arthur Hung72d8dc32020-03-28 00:48:39 +00002215 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002216 setFocusedWindow(window);
2217
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002218 window->consumeFocusEvent(true);
Michael Wright3a240c42019-12-10 20:53:41 +00002219
chaviwd1c23182019-12-20 18:44:56 -08002220 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
2221 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00002222
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002223 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2224 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00002225 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002226 monitor.assertNoEvents();
Michael Wright3a240c42019-12-10 20:53:41 +00002227}
2228
2229TEST_F(InputDispatcherTest, GestureMonitor_CanPilferAfterWindowIsRemovedMidStream) {
Chris Yea209fde2020-07-22 13:54:51 -07002230 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00002231 sp<FakeWindowHandle> window =
2232 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002233 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00002234
chaviwd1c23182019-12-20 18:44:56 -08002235 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
2236 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00002237
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002238 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00002239 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002240 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00002241 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002242 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00002243
2244 window->releaseChannel();
2245
chaviwd1c23182019-12-20 18:44:56 -08002246 mDispatcher->pilferPointers(monitor.getToken());
Michael Wright3a240c42019-12-10 20:53:41 +00002247
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002248 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00002249 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002250 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08002251 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00002252}
2253
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002254TEST_F(InputDispatcherTest, UnresponsiveGestureMonitor_GetsAnr) {
2255 FakeMonitorReceiver monitor =
2256 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
2257 true /*isGestureMonitor*/);
2258
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002259 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002260 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
2261 std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
2262 ASSERT_TRUE(consumeSeq);
2263
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00002264 mFakePolicy->assertNotifyMonitorUnresponsiveWasCalled(DISPATCHING_TIMEOUT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002265 monitor.finishEvent(*consumeSeq);
2266 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00002267 mFakePolicy->assertNotifyMonitorResponsiveWasCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002268}
2269
Evan Rosky84f07f02021-04-16 10:42:42 -07002270// Tests for gesture monitors
2271TEST_F(InputDispatcherTest, GestureMonitor_NoWindowTransform) {
2272 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2273 sp<FakeWindowHandle> window =
2274 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2275 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2276 window->setWindowOffset(20, 40);
2277 window->setWindowTransform(0, 1, -1, 0);
2278
2279 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
2280 true /*isGestureMonitor*/);
2281
2282 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2283 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2284 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2285 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2286 MotionEvent* event = monitor.consumeMotion();
2287 // Even though window has transform, gesture monitor must not.
2288 ASSERT_EQ(ui::Transform(), event->getTransform());
2289}
2290
chaviw81e2bb92019-12-18 15:03:51 -08002291TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002292 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
chaviw81e2bb92019-12-18 15:03:51 -08002293 sp<FakeWindowHandle> window =
2294 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2295
Arthur Hung72d8dc32020-03-28 00:48:39 +00002296 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08002297
2298 NotifyMotionArgs motionArgs =
2299 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2300 ADISPLAY_ID_DEFAULT);
2301
2302 mDispatcher->notifyMotion(&motionArgs);
2303 // Window should receive motion down event.
2304 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2305
2306 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002307 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08002308 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
2309 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
2310 motionArgs.pointerCoords[0].getX() - 10);
2311
2312 mDispatcher->notifyMotion(&motionArgs);
2313 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
2314 0 /*expectedFlags*/);
2315}
2316
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002317/**
2318 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
2319 * the device default right away. In the test scenario, we check both the default value,
2320 * and the action of enabling / disabling.
2321 */
2322TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07002323 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002324 sp<FakeWindowHandle> window =
2325 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2326
2327 // Set focused application.
2328 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002329 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002330
2331 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00002332 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002333 setFocusedWindow(window);
2334
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002335 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2336
2337 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07002338 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002339 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002340 window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/);
2341
2342 SCOPED_TRACE("Disable touch mode");
2343 mDispatcher->setInTouchMode(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07002344 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002345 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002346 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002347 window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/);
2348
2349 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07002350 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002351 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002352 window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/);
2353
2354 SCOPED_TRACE("Enable touch mode again");
2355 mDispatcher->setInTouchMode(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07002356 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002357 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002358 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002359 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2360
2361 window->assertNoEvents();
2362}
2363
Gang Wange9087892020-01-07 12:17:14 -05002364TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002365 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Gang Wange9087892020-01-07 12:17:14 -05002366 sp<FakeWindowHandle> window =
2367 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2368
2369 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002370 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05002371
Arthur Hung72d8dc32020-03-28 00:48:39 +00002372 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002373 setFocusedWindow(window);
2374
Gang Wange9087892020-01-07 12:17:14 -05002375 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2376
2377 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
2378 mDispatcher->notifyKey(&keyArgs);
2379
2380 InputEvent* event = window->consume();
2381 ASSERT_NE(event, nullptr);
2382
2383 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
2384 ASSERT_NE(verified, nullptr);
2385 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
2386
2387 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
2388 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
2389 ASSERT_EQ(keyArgs.source, verified->source);
2390 ASSERT_EQ(keyArgs.displayId, verified->displayId);
2391
2392 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
2393
2394 ASSERT_EQ(keyArgs.action, verifiedKey.action);
2395 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05002396 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
2397 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
2398 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
2399 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
2400 ASSERT_EQ(0, verifiedKey.repeatCount);
2401}
2402
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002403TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002404 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002405 sp<FakeWindowHandle> window =
2406 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2407
2408 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2409
Arthur Hung72d8dc32020-03-28 00:48:39 +00002410 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002411
2412 NotifyMotionArgs motionArgs =
2413 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2414 ADISPLAY_ID_DEFAULT);
2415 mDispatcher->notifyMotion(&motionArgs);
2416
2417 InputEvent* event = window->consume();
2418 ASSERT_NE(event, nullptr);
2419
2420 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
2421 ASSERT_NE(verified, nullptr);
2422 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
2423
2424 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
2425 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
2426 EXPECT_EQ(motionArgs.source, verified->source);
2427 EXPECT_EQ(motionArgs.displayId, verified->displayId);
2428
2429 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
2430
2431 EXPECT_EQ(motionArgs.pointerCoords[0].getX(), verifiedMotion.rawX);
2432 EXPECT_EQ(motionArgs.pointerCoords[0].getY(), verifiedMotion.rawY);
2433 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
2434 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
2435 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
2436 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
2437 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
2438}
2439
Prabir Pradhanbd527712021-03-09 19:17:09 -08002440TEST_F(InputDispatcherTest, NonPointerMotionEvent_NotTransformed) {
yunho.shinf4a80b82020-11-16 21:13:57 +09002441 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2442 sp<FakeWindowHandle> window =
2443 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2444 const std::string name = window->getName();
2445
2446 // Window gets transformed by offset values.
2447 window->setWindowOffset(500.0f, 500.0f);
2448
2449 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2450 window->setFocusable(true);
2451
2452 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2453
2454 // First, we set focused window so that focusedWindowHandle is not null.
2455 setFocusedWindow(window);
2456
2457 // Second, we consume focus event if it is right or wrong according to onFocusChangedLocked.
2458 window->consumeFocusEvent(true);
2459
Prabir Pradhanbd527712021-03-09 19:17:09 -08002460 constexpr const std::array nonPointerSources = {AINPUT_SOURCE_TRACKBALL,
2461 AINPUT_SOURCE_MOUSE_RELATIVE,
2462 AINPUT_SOURCE_JOYSTICK};
2463 for (const int source : nonPointerSources) {
2464 // Notify motion with a non-pointer source.
2465 NotifyMotionArgs motionArgs =
2466 generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, source, ADISPLAY_ID_DEFAULT);
2467 mDispatcher->notifyMotion(&motionArgs);
yunho.shinf4a80b82020-11-16 21:13:57 +09002468
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00002469 MotionEvent* event = window->consumeMotion();
Prabir Pradhanbd527712021-03-09 19:17:09 -08002470 ASSERT_NE(event, nullptr);
yunho.shinf4a80b82020-11-16 21:13:57 +09002471
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00002472 const MotionEvent& motionEvent = *event;
Prabir Pradhanbd527712021-03-09 19:17:09 -08002473 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, motionEvent.getAction());
2474 EXPECT_EQ(motionArgs.pointerCount, motionEvent.getPointerCount());
yunho.shinf4a80b82020-11-16 21:13:57 +09002475
Prabir Pradhanbd527712021-03-09 19:17:09 -08002476 float expectedX = motionArgs.pointerCoords[0].getX();
2477 float expectedY = motionArgs.pointerCoords[0].getY();
yunho.shinf4a80b82020-11-16 21:13:57 +09002478
Prabir Pradhanbd527712021-03-09 19:17:09 -08002479 // Ensure the axis values from the final motion event are not transformed.
2480 EXPECT_EQ(expectedX, motionEvent.getX(0))
2481 << "expected " << expectedX << " for x coord of " << name.c_str() << ", got "
2482 << motionEvent.getX(0);
2483 EXPECT_EQ(expectedY, motionEvent.getY(0))
2484 << "expected " << expectedY << " for y coord of " << name.c_str() << ", got "
2485 << motionEvent.getY(0);
2486 // Ensure the raw and transformed axis values for the motion event are the same.
2487 EXPECT_EQ(motionEvent.getRawX(0), motionEvent.getX(0))
2488 << "expected raw and transformed X-axis values to be equal";
2489 EXPECT_EQ(motionEvent.getRawY(0), motionEvent.getY(0))
2490 << "expected raw and transformed Y-axis values to be equal";
2491 }
yunho.shinf4a80b82020-11-16 21:13:57 +09002492}
2493
chaviw09c8d2d2020-08-24 15:48:26 -07002494/**
2495 * Ensure that separate calls to sign the same data are generating the same key.
2496 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
2497 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
2498 * tests.
2499 */
2500TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
2501 KeyEvent event = getTestKeyEvent();
2502 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
2503
2504 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
2505 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
2506 ASSERT_EQ(hmac1, hmac2);
2507}
2508
2509/**
2510 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
2511 */
2512TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
2513 KeyEvent event = getTestKeyEvent();
2514 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
2515 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
2516
2517 verifiedEvent.deviceId += 1;
2518 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2519
2520 verifiedEvent.source += 1;
2521 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2522
2523 verifiedEvent.eventTimeNanos += 1;
2524 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2525
2526 verifiedEvent.displayId += 1;
2527 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2528
2529 verifiedEvent.action += 1;
2530 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2531
2532 verifiedEvent.downTimeNanos += 1;
2533 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2534
2535 verifiedEvent.flags += 1;
2536 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2537
2538 verifiedEvent.keyCode += 1;
2539 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2540
2541 verifiedEvent.scanCode += 1;
2542 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2543
2544 verifiedEvent.metaState += 1;
2545 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2546
2547 verifiedEvent.repeatCount += 1;
2548 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2549}
2550
Vishnu Nair958da932020-08-21 17:12:37 -07002551TEST_F(InputDispatcherTest, SetFocusedWindow) {
2552 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2553 sp<FakeWindowHandle> windowTop =
2554 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2555 sp<FakeWindowHandle> windowSecond =
2556 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2557 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2558
2559 // Top window is also focusable but is not granted focus.
2560 windowTop->setFocusable(true);
2561 windowSecond->setFocusable(true);
2562 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2563 setFocusedWindow(windowSecond);
2564
2565 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002566 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2567 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002568
2569 // Focused window should receive event.
2570 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
2571 windowTop->assertNoEvents();
2572}
2573
2574TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
2575 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2576 sp<FakeWindowHandle> window =
2577 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2578 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2579
2580 window->setFocusable(true);
2581 // Release channel for window is no longer valid.
2582 window->releaseChannel();
2583 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2584 setFocusedWindow(window);
2585
2586 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002587 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2588 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002589
2590 // window channel is invalid, so it should not receive any input event.
2591 window->assertNoEvents();
2592}
2593
2594TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
2595 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2596 sp<FakeWindowHandle> window =
2597 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2598 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2599
2600 // Window is not focusable.
2601 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2602 setFocusedWindow(window);
2603
2604 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002605 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2606 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002607
2608 // window is invalid, so it should not receive any input event.
2609 window->assertNoEvents();
2610}
2611
2612TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
2613 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2614 sp<FakeWindowHandle> windowTop =
2615 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2616 sp<FakeWindowHandle> windowSecond =
2617 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2618 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2619
2620 windowTop->setFocusable(true);
2621 windowSecond->setFocusable(true);
2622 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2623 setFocusedWindow(windowTop);
2624 windowTop->consumeFocusEvent(true);
2625
2626 setFocusedWindow(windowSecond, windowTop);
2627 windowSecond->consumeFocusEvent(true);
2628 windowTop->consumeFocusEvent(false);
2629
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002630 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2631 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002632
2633 // Focused window should receive event.
2634 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
2635}
2636
2637TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) {
2638 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2639 sp<FakeWindowHandle> windowTop =
2640 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2641 sp<FakeWindowHandle> windowSecond =
2642 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2643 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2644
2645 windowTop->setFocusable(true);
2646 windowSecond->setFocusable(true);
2647 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2648 setFocusedWindow(windowSecond, windowTop);
2649
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002650 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2651 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002652
2653 // Event should be dropped.
2654 windowTop->assertNoEvents();
2655 windowSecond->assertNoEvents();
2656}
2657
2658TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
2659 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2660 sp<FakeWindowHandle> window =
2661 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2662 sp<FakeWindowHandle> previousFocusedWindow =
2663 new FakeWindowHandle(application, mDispatcher, "previousFocusedWindow",
2664 ADISPLAY_ID_DEFAULT);
2665 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2666
2667 window->setFocusable(true);
2668 previousFocusedWindow->setFocusable(true);
2669 window->setVisible(false);
2670 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
2671 setFocusedWindow(previousFocusedWindow);
2672 previousFocusedWindow->consumeFocusEvent(true);
2673
2674 // Requesting focus on invisible window takes focus from currently focused window.
2675 setFocusedWindow(window);
2676 previousFocusedWindow->consumeFocusEvent(false);
2677
2678 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002679 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07002680 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002681 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07002682
2683 // Window does not get focus event or key down.
2684 window->assertNoEvents();
2685
2686 // Window becomes visible.
2687 window->setVisible(true);
2688 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2689
2690 // Window receives focus event.
2691 window->consumeFocusEvent(true);
2692 // Focused window receives key down.
2693 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2694}
2695
Vishnu Nair599f1412021-06-21 10:39:58 -07002696TEST_F(InputDispatcherTest, DisplayRemoved) {
2697 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2698 sp<FakeWindowHandle> window =
2699 new FakeWindowHandle(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
2700 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2701
2702 // window is granted focus.
2703 window->setFocusable(true);
2704 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2705 setFocusedWindow(window);
2706 window->consumeFocusEvent(true);
2707
2708 // When a display is removed window loses focus.
2709 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
2710 window->consumeFocusEvent(false);
2711}
2712
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002713/**
2714 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
2715 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
2716 * of the 'slipperyEnterWindow'.
2717 *
2718 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
2719 * a way so that the touched location is no longer covered by the top window.
2720 *
2721 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
2722 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
2723 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
2724 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
2725 * with ACTION_DOWN).
2726 * Thus, the touch has been transferred from the top window into the bottom window, because the top
2727 * window moved itself away from the touched location and had Flag::SLIPPERY.
2728 *
2729 * Even though the top window moved away from the touched location, it is still obscuring the bottom
2730 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
2731 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
2732 *
2733 * In this test, we ensure that the event received by the bottom window has
2734 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
2735 */
2736TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
2737 constexpr int32_t SLIPPERY_PID = INJECTOR_PID + 1;
2738 constexpr int32_t SLIPPERY_UID = INJECTOR_UID + 1;
2739
2740 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2741 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2742
2743 sp<FakeWindowHandle> slipperyExitWindow =
2744 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2745 slipperyExitWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
2746 InputWindowInfo::Flag::SLIPPERY);
2747 // Make sure this one overlaps the bottom window
2748 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
2749 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
2750 // one. Windows with the same owner are not considered to be occluding each other.
2751 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
2752
2753 sp<FakeWindowHandle> slipperyEnterWindow =
2754 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2755 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
2756
2757 mDispatcher->setInputWindows(
2758 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
2759
2760 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
2761 NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2762 ADISPLAY_ID_DEFAULT, {{50, 50}});
2763 mDispatcher->notifyMotion(&args);
2764 slipperyExitWindow->consumeMotionDown();
2765 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
2766 mDispatcher->setInputWindows(
2767 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
2768
2769 args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2770 ADISPLAY_ID_DEFAULT, {{51, 51}});
2771 mDispatcher->notifyMotion(&args);
2772
2773 slipperyExitWindow->consumeMotionCancel();
2774
2775 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
2776 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
2777}
2778
Garfield Tan1c7bc862020-01-28 13:24:04 -08002779class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
2780protected:
2781 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
2782 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
2783
Chris Yea209fde2020-07-22 13:54:51 -07002784 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002785 sp<FakeWindowHandle> mWindow;
2786
2787 virtual void SetUp() override {
2788 mFakePolicy = new FakeInputDispatcherPolicy();
2789 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
2790 mDispatcher = new InputDispatcher(mFakePolicy);
2791 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
2792 ASSERT_EQ(OK, mDispatcher->start());
2793
2794 setUpWindow();
2795 }
2796
2797 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07002798 mApp = std::make_shared<FakeApplicationHandle>();
Garfield Tan1c7bc862020-01-28 13:24:04 -08002799 mWindow = new FakeWindowHandle(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2800
Vishnu Nair47074b82020-08-14 11:54:47 -07002801 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002802 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002803 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002804 mWindow->consumeFocusEvent(true);
2805 }
2806
Chris Ye2ad95392020-09-01 13:44:44 -07002807 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08002808 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07002809 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002810 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
2811 mDispatcher->notifyKey(&keyArgs);
2812
2813 // Window should receive key down event.
2814 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2815 }
2816
2817 void expectKeyRepeatOnce(int32_t repeatCount) {
2818 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
2819 InputEvent* repeatEvent = mWindow->consume();
2820 ASSERT_NE(nullptr, repeatEvent);
2821
2822 uint32_t eventType = repeatEvent->getType();
2823 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType);
2824
2825 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
2826 uint32_t eventAction = repeatKeyEvent->getAction();
2827 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
2828 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
2829 }
2830
Chris Ye2ad95392020-09-01 13:44:44 -07002831 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08002832 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07002833 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002834 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
2835 mDispatcher->notifyKey(&keyArgs);
2836
2837 // Window should receive key down event.
2838 mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
2839 0 /*expectedFlags*/);
2840 }
2841};
2842
2843TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Chris Ye2ad95392020-09-01 13:44:44 -07002844 sendAndConsumeKeyDown(1 /* deviceId */);
2845 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2846 expectKeyRepeatOnce(repeatCount);
2847 }
2848}
2849
2850TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
2851 sendAndConsumeKeyDown(1 /* deviceId */);
2852 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2853 expectKeyRepeatOnce(repeatCount);
2854 }
2855 sendAndConsumeKeyDown(2 /* deviceId */);
2856 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08002857 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2858 expectKeyRepeatOnce(repeatCount);
2859 }
2860}
2861
2862TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Chris Ye2ad95392020-09-01 13:44:44 -07002863 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002864 expectKeyRepeatOnce(1 /*repeatCount*/);
Chris Ye2ad95392020-09-01 13:44:44 -07002865 sendAndConsumeKeyUp(1 /* deviceId */);
2866 mWindow->assertNoEvents();
2867}
2868
2869TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
2870 sendAndConsumeKeyDown(1 /* deviceId */);
2871 expectKeyRepeatOnce(1 /*repeatCount*/);
2872 sendAndConsumeKeyDown(2 /* deviceId */);
2873 expectKeyRepeatOnce(1 /*repeatCount*/);
2874 // Stale key up from device 1.
2875 sendAndConsumeKeyUp(1 /* deviceId */);
2876 // Device 2 is still down, keep repeating
2877 expectKeyRepeatOnce(2 /*repeatCount*/);
2878 expectKeyRepeatOnce(3 /*repeatCount*/);
2879 // Device 2 key up
2880 sendAndConsumeKeyUp(2 /* deviceId */);
2881 mWindow->assertNoEvents();
2882}
2883
2884TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
2885 sendAndConsumeKeyDown(1 /* deviceId */);
2886 expectKeyRepeatOnce(1 /*repeatCount*/);
2887 sendAndConsumeKeyDown(2 /* deviceId */);
2888 expectKeyRepeatOnce(1 /*repeatCount*/);
2889 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
2890 sendAndConsumeKeyUp(2 /* deviceId */);
2891 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08002892 mWindow->assertNoEvents();
2893}
2894
2895TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Chris Ye2ad95392020-09-01 13:44:44 -07002896 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002897 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2898 InputEvent* repeatEvent = mWindow->consume();
2899 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2900 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
2901 IdGenerator::getSource(repeatEvent->getId()));
2902 }
2903}
2904
2905TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Chris Ye2ad95392020-09-01 13:44:44 -07002906 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002907
2908 std::unordered_set<int32_t> idSet;
2909 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2910 InputEvent* repeatEvent = mWindow->consume();
2911 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2912 int32_t id = repeatEvent->getId();
2913 EXPECT_EQ(idSet.end(), idSet.find(id));
2914 idSet.insert(id);
2915 }
2916}
2917
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002918/* Test InputDispatcher for MultiDisplay */
2919class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
2920public:
2921 static constexpr int32_t SECOND_DISPLAY_ID = 1;
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002922 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002923 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08002924
Chris Yea209fde2020-07-22 13:54:51 -07002925 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002926 windowInPrimary =
2927 new FakeWindowHandle(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002928
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002929 // Set focus window for primary display, but focused display would be second one.
2930 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07002931 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002932 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002933 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002934 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08002935
Chris Yea209fde2020-07-22 13:54:51 -07002936 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002937 windowInSecondary =
2938 new FakeWindowHandle(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002939 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002940 // Set focus display to second one.
2941 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
2942 // Set focus window for second display.
2943 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07002944 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002945 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002946 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002947 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002948 }
2949
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002950 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002951 InputDispatcherTest::TearDown();
2952
Chris Yea209fde2020-07-22 13:54:51 -07002953 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002954 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07002955 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002956 windowInSecondary.clear();
2957 }
2958
2959protected:
Chris Yea209fde2020-07-22 13:54:51 -07002960 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002961 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07002962 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002963 sp<FakeWindowHandle> windowInSecondary;
2964};
2965
2966TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
2967 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002968 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2969 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2970 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002971 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08002972 windowInSecondary->assertNoEvents();
2973
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002974 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002975 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2976 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
2977 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08002978 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002979 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08002980}
2981
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002982TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08002983 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08002984 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2985 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002986 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002987 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08002988 windowInSecondary->assertNoEvents();
2989
2990 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08002991 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002992 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08002993 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002994 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08002995
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002996 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00002997 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08002998
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002999 // Old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003000 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
3001 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08003002
3003 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003004 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003005 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08003006 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003007 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08003008 windowInSecondary->assertNoEvents();
3009}
3010
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003011// Test per-display input monitors for motion event.
3012TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08003013 FakeMonitorReceiver monitorInPrimary =
3014 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
3015 FakeMonitorReceiver monitorInSecondary =
3016 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003017
3018 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003019 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3020 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
3021 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003022 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08003023 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003024 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003025 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003026
3027 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003028 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3029 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
3030 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003031 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003032 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003033 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08003034 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003035
3036 // Test inject a non-pointer motion event.
3037 // If specific a display, it will dispatch to the focused window of particular display,
3038 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003039 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3040 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
3041 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003042 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003043 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003044 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08003045 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003046}
3047
3048// Test per-display input monitors for key event.
3049TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003050 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08003051 FakeMonitorReceiver monitorInPrimary =
3052 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
3053 FakeMonitorReceiver monitorInSecondary =
3054 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003055
3056 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003057 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3058 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003059 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003060 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003061 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08003062 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003063}
3064
Vishnu Nair958da932020-08-21 17:12:37 -07003065TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
3066 sp<FakeWindowHandle> secondWindowInPrimary =
3067 new FakeWindowHandle(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
3068 secondWindowInPrimary->setFocusable(true);
3069 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
3070 setFocusedWindow(secondWindowInPrimary);
3071 windowInPrimary->consumeFocusEvent(false);
3072 secondWindowInPrimary->consumeFocusEvent(true);
3073
3074 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003075 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
3076 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003077 windowInPrimary->assertNoEvents();
3078 windowInSecondary->assertNoEvents();
3079 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3080}
3081
Jackal Guof9696682018-10-05 12:23:23 +08003082class InputFilterTest : public InputDispatcherTest {
3083protected:
3084 static constexpr int32_t SECOND_DISPLAY_ID = 1;
3085
3086 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) {
3087 NotifyMotionArgs motionArgs;
3088
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003089 motionArgs =
3090 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08003091 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003092 motionArgs =
3093 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08003094 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003095 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08003096 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08003097 mFakePolicy->assertFilterInputEventWasCalled(motionArgs);
Jackal Guof9696682018-10-05 12:23:23 +08003098 } else {
3099 mFakePolicy->assertFilterInputEventWasNotCalled();
3100 }
3101 }
3102
3103 void testNotifyKey(bool expectToBeFiltered) {
3104 NotifyKeyArgs keyArgs;
3105
3106 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
3107 mDispatcher->notifyKey(&keyArgs);
3108 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
3109 mDispatcher->notifyKey(&keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003110 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08003111
3112 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08003113 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08003114 } else {
3115 mFakePolicy->assertFilterInputEventWasNotCalled();
3116 }
3117 }
3118};
3119
3120// Test InputFilter for MotionEvent
3121TEST_F(InputFilterTest, MotionEvent_InputFilter) {
3122 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
3123 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
3124 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
3125
3126 // Enable InputFilter
3127 mDispatcher->setInputFilterEnabled(true);
3128 // Test touch on both primary and second display, and check if both events are filtered.
3129 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
3130 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
3131
3132 // Disable InputFilter
3133 mDispatcher->setInputFilterEnabled(false);
3134 // Test touch on both primary and second display, and check if both events aren't filtered.
3135 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
3136 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
3137}
3138
3139// Test InputFilter for KeyEvent
3140TEST_F(InputFilterTest, KeyEvent_InputFilter) {
3141 // Since the InputFilter is disabled by default, check if key event aren't filtered.
3142 testNotifyKey(/*expectToBeFiltered*/ false);
3143
3144 // Enable InputFilter
3145 mDispatcher->setInputFilterEnabled(true);
3146 // Send a key event, and check if it is filtered.
3147 testNotifyKey(/*expectToBeFiltered*/ true);
3148
3149 // Disable InputFilter
3150 mDispatcher->setInputFilterEnabled(false);
3151 // Send a key event, and check if it isn't filtered.
3152 testNotifyKey(/*expectToBeFiltered*/ false);
3153}
3154
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003155class InputFilterInjectionPolicyTest : public InputDispatcherTest {
3156protected:
3157 virtual void SetUp() override {
3158 InputDispatcherTest::SetUp();
3159
3160 /**
3161 * We don't need to enable input filter to test the injected event policy, but we enabled it
3162 * here to make the tests more realistic, since this policy only matters when inputfilter is
3163 * on.
3164 */
3165 mDispatcher->setInputFilterEnabled(true);
3166
3167 std::shared_ptr<InputApplicationHandle> application =
3168 std::make_shared<FakeApplicationHandle>();
3169 mWindow =
3170 new FakeWindowHandle(application, mDispatcher, "Test Window", ADISPLAY_ID_DEFAULT);
3171
3172 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3173 mWindow->setFocusable(true);
3174 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3175 setFocusedWindow(mWindow);
3176 mWindow->consumeFocusEvent(true);
3177 }
3178
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003179 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
3180 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003181 KeyEvent event;
3182
3183 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
3184 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
3185 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
3186 KEY_A, AMETA_NONE, 0 /*repeatCount*/, eventTime, eventTime);
3187 const int32_t additionalPolicyFlags =
3188 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
3189 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3190 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
3191 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
3192 policyFlags | additionalPolicyFlags));
3193
3194 InputEvent* received = mWindow->consume();
3195 ASSERT_NE(nullptr, received);
3196 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003197 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_KEY);
3198 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
3199 ASSERT_EQ(flags, keyEvent.getFlags());
3200 }
3201
3202 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
3203 int32_t flags) {
3204 MotionEvent event;
3205 PointerProperties pointerProperties[1];
3206 PointerCoords pointerCoords[1];
3207 pointerProperties[0].clear();
3208 pointerProperties[0].id = 0;
3209 pointerCoords[0].clear();
3210 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
3211 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
3212
3213 ui::Transform identityTransform;
3214 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
3215 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
3216 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
3217 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
3218 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
3219 AMOTION_EVENT_INVALID_CURSOR_POSITION,
3220 0 /*AMOTION_EVENT_INVALID_DISPLAY_SIZE*/,
3221 0 /*AMOTION_EVENT_INVALID_DISPLAY_SIZE*/, eventTime, eventTime,
3222 /*pointerCount*/ 1, pointerProperties, pointerCoords);
3223
3224 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
3225 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3226 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
3227 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
3228 policyFlags | additionalPolicyFlags));
3229
3230 InputEvent* received = mWindow->consume();
3231 ASSERT_NE(nullptr, received);
3232 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
3233 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_MOTION);
3234 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
3235 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003236 }
3237
3238private:
3239 sp<FakeWindowHandle> mWindow;
3240};
3241
3242TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003243 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
3244 // filter. Without it, the event will no different from a regularly injected event, and the
3245 // injected device id will be overwritten.
3246 testInjectedKey(POLICY_FLAG_FILTERED, 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
3247 0 /*flags*/);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003248}
3249
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003250TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003251 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003252 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
3253 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
3254}
3255
3256TEST_F(InputFilterInjectionPolicyTest,
3257 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
3258 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
3259 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
3260 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003261}
3262
3263TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
3264 testInjectedKey(0 /*policyFlags*/, 3 /*injectedDeviceId*/,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003265 VIRTUAL_KEYBOARD_ID /*resolvedDeviceId*/, 0 /*flags*/);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003266}
3267
chaviwfd6d3512019-03-25 13:23:49 -07003268class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07003269 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07003270 InputDispatcherTest::SetUp();
3271
Chris Yea209fde2020-07-22 13:54:51 -07003272 std::shared_ptr<FakeApplicationHandle> application =
3273 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003274 mUnfocusedWindow =
3275 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07003276 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
3277 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3278 // window.
Michael Wright44753b12020-07-08 13:48:11 +01003279 mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07003280
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003281 mFocusedWindow =
3282 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
3283 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01003284 mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07003285
3286 // Set focused application.
3287 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07003288 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07003289
3290 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00003291 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003292 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003293 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07003294 }
3295
Prabir Pradhan3608aad2019-10-02 17:08:26 -07003296 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07003297 InputDispatcherTest::TearDown();
3298
3299 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003300 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07003301 }
3302
3303protected:
3304 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003305 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003306 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07003307};
3308
3309// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
3310// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
3311// the onPointerDownOutsideFocus callback.
3312TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003313 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003314 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3315 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003316 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003317 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07003318
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003319 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07003320 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
3321}
3322
3323// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
3324// DOWN on the window that doesn't have focus. Ensure no window received the
3325// onPointerDownOutsideFocus callback.
3326TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003327 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003328 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003329 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003330 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07003331
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003332 ASSERT_TRUE(mDispatcher->waitForIdle());
3333 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07003334}
3335
3336// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
3337// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
3338TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003339 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3340 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003341 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003342 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07003343
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003344 ASSERT_TRUE(mDispatcher->waitForIdle());
3345 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07003346}
3347
3348// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
3349// DOWN on the window that already has focus. Ensure no window received the
3350// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003351TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003352 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003353 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003354 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003355 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003356 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07003357
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003358 ASSERT_TRUE(mDispatcher->waitForIdle());
3359 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07003360}
3361
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08003362// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
3363// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
3364TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
3365 const MotionEvent event =
3366 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3367 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
3368 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(20).y(20))
3369 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
3370 .build();
3371 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
3372 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3373 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
3374
3375 ASSERT_TRUE(mDispatcher->waitForIdle());
3376 mFakePolicy->assertOnPointerDownWasNotCalled();
3377 // Ensure that the unfocused window did not receive any FOCUS events.
3378 mUnfocusedWindow->assertNoEvents();
3379}
3380
chaviwaf87b3e2019-10-01 16:59:28 -07003381// These tests ensures we can send touch events to a single client when there are multiple input
3382// windows that point to the same client token.
3383class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
3384 virtual void SetUp() override {
3385 InputDispatcherTest::SetUp();
3386
Chris Yea209fde2020-07-22 13:54:51 -07003387 std::shared_ptr<FakeApplicationHandle> application =
3388 std::make_shared<FakeApplicationHandle>();
chaviwaf87b3e2019-10-01 16:59:28 -07003389 mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1",
3390 ADISPLAY_ID_DEFAULT);
3391 // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window.
3392 // We also need FLAG_SPLIT_TOUCH or we won't be able to get touches for both windows.
Michael Wright44753b12020-07-08 13:48:11 +01003393 mWindow1->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3394 InputWindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07003395 mWindow1->setFrame(Rect(0, 0, 100, 100));
3396
3397 mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2",
3398 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
Michael Wright44753b12020-07-08 13:48:11 +01003399 mWindow2->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3400 InputWindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07003401 mWindow2->setFrame(Rect(100, 100, 200, 200));
3402
Arthur Hung72d8dc32020-03-28 00:48:39 +00003403 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07003404 }
3405
3406protected:
3407 sp<FakeWindowHandle> mWindow1;
3408 sp<FakeWindowHandle> mWindow2;
3409
3410 // Helper function to convert the point from screen coordinates into the window's space
3411 static PointF getPointInWindow(const InputWindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07003412 vec2 vals = windowInfo->transform.transform(point.x, point.y);
3413 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07003414 }
3415
3416 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
3417 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003418 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07003419 InputEvent* event = window->consume();
3420
3421 ASSERT_NE(nullptr, event) << name.c_str()
3422 << ": consumer should have returned non-NULL event.";
3423
3424 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
3425 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
3426 << " event, got " << inputEventTypeToString(event->getType()) << " event";
3427
3428 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
3429 EXPECT_EQ(expectedAction, motionEvent.getAction());
3430
3431 for (size_t i = 0; i < points.size(); i++) {
3432 float expectedX = points[i].x;
3433 float expectedY = points[i].y;
3434
3435 EXPECT_EQ(expectedX, motionEvent.getX(i))
3436 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
3437 << ", got " << motionEvent.getX(i);
3438 EXPECT_EQ(expectedY, motionEvent.getY(i))
3439 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
3440 << ", got " << motionEvent.getY(i);
3441 }
3442 }
chaviw9eaa22c2020-07-01 16:21:27 -07003443
3444 void touchAndAssertPositions(int32_t action, std::vector<PointF> touchedPoints,
3445 std::vector<PointF> expectedPoints) {
3446 NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
3447 ADISPLAY_ID_DEFAULT, touchedPoints);
3448 mDispatcher->notifyMotion(&motionArgs);
3449
3450 // Always consume from window1 since it's the window that has the InputReceiver
3451 consumeMotionEvent(mWindow1, action, expectedPoints);
3452 }
chaviwaf87b3e2019-10-01 16:59:28 -07003453};
3454
3455TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
3456 // Touch Window 1
3457 PointF touchedPoint = {10, 10};
3458 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003459 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003460
3461 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07003462 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003463
3464 // Touch Window 2
3465 touchedPoint = {150, 150};
3466 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003467 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003468}
3469
chaviw9eaa22c2020-07-01 16:21:27 -07003470TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
3471 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07003472 mWindow2->setWindowScale(0.5f, 0.5f);
3473
3474 // Touch Window 1
3475 PointF touchedPoint = {10, 10};
3476 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003477 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003478 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07003479 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003480
3481 // Touch Window 2
3482 touchedPoint = {150, 150};
3483 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003484 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
3485 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003486
chaviw9eaa22c2020-07-01 16:21:27 -07003487 // Update the transform so rotation is set
3488 mWindow2->setWindowTransform(0, -1, 1, 0);
3489 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
3490 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003491}
3492
chaviw9eaa22c2020-07-01 16:21:27 -07003493TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003494 mWindow2->setWindowScale(0.5f, 0.5f);
3495
3496 // Touch Window 1
3497 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3498 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003499 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003500
3501 // Touch Window 2
3502 int32_t actionPointerDown =
3503 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003504 touchedPoints.push_back(PointF{150, 150});
3505 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3506 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003507
chaviw9eaa22c2020-07-01 16:21:27 -07003508 // Release Window 2
3509 int32_t actionPointerUp =
3510 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3511 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
3512 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003513
chaviw9eaa22c2020-07-01 16:21:27 -07003514 // Update the transform so rotation is set for Window 2
3515 mWindow2->setWindowTransform(0, -1, 1, 0);
3516 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3517 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003518}
3519
chaviw9eaa22c2020-07-01 16:21:27 -07003520TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003521 mWindow2->setWindowScale(0.5f, 0.5f);
3522
3523 // Touch Window 1
3524 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3525 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003526 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003527
3528 // Touch Window 2
3529 int32_t actionPointerDown =
3530 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003531 touchedPoints.push_back(PointF{150, 150});
3532 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003533
chaviw9eaa22c2020-07-01 16:21:27 -07003534 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003535
3536 // Move both windows
3537 touchedPoints = {{20, 20}, {175, 175}};
3538 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3539 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3540
chaviw9eaa22c2020-07-01 16:21:27 -07003541 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003542
chaviw9eaa22c2020-07-01 16:21:27 -07003543 // Release Window 2
3544 int32_t actionPointerUp =
3545 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3546 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
3547 expectedPoints.pop_back();
3548
3549 // Touch Window 2
3550 mWindow2->setWindowTransform(0, -1, 1, 0);
3551 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3552 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
3553
3554 // Move both windows
3555 touchedPoints = {{20, 20}, {175, 175}};
3556 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3557 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3558
3559 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003560}
3561
3562TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
3563 mWindow1->setWindowScale(0.5f, 0.5f);
3564
3565 // Touch Window 1
3566 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3567 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003568 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003569
3570 // Touch Window 2
3571 int32_t actionPointerDown =
3572 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003573 touchedPoints.push_back(PointF{150, 150});
3574 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003575
chaviw9eaa22c2020-07-01 16:21:27 -07003576 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003577
3578 // Move both windows
3579 touchedPoints = {{20, 20}, {175, 175}};
3580 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3581 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3582
chaviw9eaa22c2020-07-01 16:21:27 -07003583 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003584}
3585
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003586class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
3587 virtual void SetUp() override {
3588 InputDispatcherTest::SetUp();
3589
Chris Yea209fde2020-07-22 13:54:51 -07003590 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003591 mApplication->setDispatchingTimeout(20ms);
3592 mWindow =
3593 new FakeWindowHandle(mApplication, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
3594 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05003595 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07003596 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003597 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3598 // window.
Michael Wright44753b12020-07-08 13:48:11 +01003599 mWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003600
3601 // Set focused application.
3602 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
3603
3604 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003605 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003606 mWindow->consumeFocusEvent(true);
3607 }
3608
3609 virtual void TearDown() override {
3610 InputDispatcherTest::TearDown();
3611 mWindow.clear();
3612 }
3613
3614protected:
Chris Yea209fde2020-07-22 13:54:51 -07003615 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003616 sp<FakeWindowHandle> mWindow;
3617 static constexpr PointF WINDOW_LOCATION = {20, 20};
3618
3619 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003620 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003621 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3622 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003623 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003624 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3625 WINDOW_LOCATION));
3626 }
3627};
3628
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003629// Send a tap and respond, which should not cause an ANR.
3630TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
3631 tapOnWindow();
3632 mWindow->consumeMotionDown();
3633 mWindow->consumeMotionUp();
3634 ASSERT_TRUE(mDispatcher->waitForIdle());
3635 mFakePolicy->assertNotifyAnrWasNotCalled();
3636}
3637
3638// Send a regular key and respond, which should not cause an ANR.
3639TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003640 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003641 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
3642 ASSERT_TRUE(mDispatcher->waitForIdle());
3643 mFakePolicy->assertNotifyAnrWasNotCalled();
3644}
3645
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003646TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
3647 mWindow->setFocusable(false);
3648 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3649 mWindow->consumeFocusEvent(false);
3650
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003651 InputEventInjectionResult result =
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003652 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003653 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/,
3654 false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003655 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003656 // Key will not go to window because we have no focused window.
3657 // The 'no focused window' ANR timer should start instead.
3658
3659 // Now, the focused application goes away.
3660 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
3661 // The key should get dropped and there should be no ANR.
3662
3663 ASSERT_TRUE(mDispatcher->waitForIdle());
3664 mFakePolicy->assertNotifyAnrWasNotCalled();
3665}
3666
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003667// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003668// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
3669// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003670TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003671 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003672 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3673 WINDOW_LOCATION));
3674
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003675 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
3676 ASSERT_TRUE(sequenceNum);
3677 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003678 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003679
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003680 mWindow->finishEvent(*sequenceNum);
3681 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3682 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003683 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003684 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003685}
3686
3687// Send a key to the app and have the app not respond right away.
3688TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
3689 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003690 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003691 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
3692 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003693 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003694 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003695 ASSERT_TRUE(mDispatcher->waitForIdle());
3696}
3697
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003698// We have a focused application, but no focused window
3699TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003700 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003701 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3702 mWindow->consumeFocusEvent(false);
3703
3704 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003705 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003706 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3707 WINDOW_LOCATION));
3708 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
3709 mDispatcher->waitForIdle();
3710 mFakePolicy->assertNotifyAnrWasNotCalled();
3711
3712 // Once a focused event arrives, we get an ANR for this application
3713 // We specify the injection timeout to be smaller than the application timeout, to ensure that
3714 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003715 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003716 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003717 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003718 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003719 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003720 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003721 ASSERT_TRUE(mDispatcher->waitForIdle());
3722}
3723
3724// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003725// Make sure that we don't notify policy twice about the same ANR.
3726TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003727 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003728 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3729 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003730
3731 // Once a focused event arrives, we get an ANR for this application
3732 // We specify the injection timeout to be smaller than the application timeout, to ensure that
3733 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003734 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003735 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003736 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003737 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003738 const std::chrono::duration appTimeout =
3739 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003740 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003741
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003742 std::this_thread::sleep_for(appTimeout);
3743 // ANR should not be raised again. It is up to policy to do that if it desires.
3744 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003745
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003746 // If we now get a focused window, the ANR should stop, but the policy handles that via
3747 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003748 ASSERT_TRUE(mDispatcher->waitForIdle());
3749}
3750
3751// We have a focused application, but no focused window
3752TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003753 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003754 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3755 mWindow->consumeFocusEvent(false);
3756
3757 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003758 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003759 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003760 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3761 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003762
3763 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003764 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003765
3766 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003767 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003768 ASSERT_TRUE(mDispatcher->waitForIdle());
3769 mWindow->assertNoEvents();
3770}
3771
3772/**
3773 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
3774 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
3775 * If we process 1 of the events, but ANR on the second event with the same timestamp,
3776 * the ANR mechanism should still work.
3777 *
3778 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
3779 * DOWN event, while not responding on the second one.
3780 */
3781TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
3782 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
3783 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3784 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
3785 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
3786 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003787 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003788
3789 // Now send ACTION_UP, with identical timestamp
3790 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3791 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
3792 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
3793 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003794 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003795
3796 // We have now sent down and up. Let's consume first event and then ANR on the second.
3797 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3798 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003799 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003800}
3801
3802// If an app is not responding to a key event, gesture monitors should continue to receive
3803// new motion events
3804TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnKey) {
3805 FakeMonitorReceiver monitor =
3806 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
3807 true /*isGestureMonitor*/);
3808
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003809 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3810 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003811 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003812 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003813
3814 // Stuck on the ACTION_UP
3815 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003816 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003817
3818 // New tap will go to the gesture monitor, but not to the window
3819 tapOnWindow();
3820 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3821 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3822
3823 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
3824 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003825 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003826 mWindow->assertNoEvents();
3827 monitor.assertNoEvents();
3828}
3829
3830// If an app is not responding to a motion event, gesture monitors should continue to receive
3831// new motion events
3832TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnMotion) {
3833 FakeMonitorReceiver monitor =
3834 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
3835 true /*isGestureMonitor*/);
3836
3837 tapOnWindow();
3838 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3839 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3840
3841 mWindow->consumeMotionDown();
3842 // Stuck on the ACTION_UP
3843 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003844 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003845
3846 // New tap will go to the gesture monitor, but not to the window
3847 tapOnWindow();
3848 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3849 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3850
3851 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
3852 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003853 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003854 mWindow->assertNoEvents();
3855 monitor.assertNoEvents();
3856}
3857
3858// If a window is unresponsive, then you get anr. if the window later catches up and starts to
3859// process events, you don't get an anr. When the window later becomes unresponsive again, you
3860// get an ANR again.
3861// 1. tap -> block on ACTION_UP -> receive ANR
3862// 2. consume all pending events (= queue becomes healthy again)
3863// 3. tap again -> block on ACTION_UP again -> receive ANR second time
3864TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
3865 tapOnWindow();
3866
3867 mWindow->consumeMotionDown();
3868 // Block on ACTION_UP
3869 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003870 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003871 mWindow->consumeMotionUp(); // Now the connection should be healthy again
3872 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003873 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003874 mWindow->assertNoEvents();
3875
3876 tapOnWindow();
3877 mWindow->consumeMotionDown();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003878 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003879 mWindow->consumeMotionUp();
3880
3881 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003882 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003883 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003884 mWindow->assertNoEvents();
3885}
3886
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003887// If a connection remains unresponsive for a while, make sure policy is only notified once about
3888// it.
3889TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003890 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003891 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3892 WINDOW_LOCATION));
3893
3894 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003895 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003896 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003897 // 'notifyConnectionUnresponsive' should only be called once per connection
3898 mFakePolicy->assertNotifyAnrWasNotCalled();
3899 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003900 mWindow->consumeMotionDown();
3901 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3902 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3903 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003904 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003905 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003906 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003907}
3908
3909/**
3910 * If a window is processing a motion event, and then a key event comes in, the key event should
3911 * not to to the focused window until the motion is processed.
3912 *
3913 * Warning!!!
3914 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
3915 * and the injection timeout that we specify when injecting the key.
3916 * We must have the injection timeout (10ms) be smaller than
3917 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
3918 *
3919 * If that value changes, this test should also change.
3920 */
3921TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
3922 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3923 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3924
3925 tapOnWindow();
3926 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3927 ASSERT_TRUE(downSequenceNum);
3928 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3929 ASSERT_TRUE(upSequenceNum);
3930 // Don't finish the events yet, and send a key
3931 // Injection will "succeed" because we will eventually give up and send the key to the focused
3932 // window even if motions are still being processed. But because the injection timeout is short,
3933 // we will receive INJECTION_TIMED_OUT as the result.
3934
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003935 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003936 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003937 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3938 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003939 // Key will not be sent to the window, yet, because the window is still processing events
3940 // and the key remains pending, waiting for the touch events to be processed
3941 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3942 ASSERT_FALSE(keySequenceNum);
3943
3944 std::this_thread::sleep_for(500ms);
3945 // if we wait long enough though, dispatcher will give up, and still send the key
3946 // to the focused window, even though we have not yet finished the motion event
3947 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3948 mWindow->finishEvent(*downSequenceNum);
3949 mWindow->finishEvent(*upSequenceNum);
3950}
3951
3952/**
3953 * If a window is processing a motion event, and then a key event comes in, the key event should
3954 * not go to the focused window until the motion is processed.
3955 * If then a new motion comes in, then the pending key event should be going to the currently
3956 * focused window right away.
3957 */
3958TEST_F(InputDispatcherSingleWindowAnr,
3959 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
3960 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3961 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3962
3963 tapOnWindow();
3964 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3965 ASSERT_TRUE(downSequenceNum);
3966 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3967 ASSERT_TRUE(upSequenceNum);
3968 // Don't finish the events yet, and send a key
3969 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003970 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003971 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003972 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003973 // At this point, key is still pending, and should not be sent to the application yet.
3974 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3975 ASSERT_FALSE(keySequenceNum);
3976
3977 // Now tap down again. It should cause the pending key to go to the focused window right away.
3978 tapOnWindow();
3979 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
3980 // the other events yet. We can finish events in any order.
3981 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
3982 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
3983 mWindow->consumeMotionDown();
3984 mWindow->consumeMotionUp();
3985 mWindow->assertNoEvents();
3986}
3987
3988class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
3989 virtual void SetUp() override {
3990 InputDispatcherTest::SetUp();
3991
Chris Yea209fde2020-07-22 13:54:51 -07003992 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003993 mApplication->setDispatchingTimeout(10ms);
3994 mUnfocusedWindow =
3995 new FakeWindowHandle(mApplication, mDispatcher, "Unfocused", ADISPLAY_ID_DEFAULT);
3996 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
3997 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3998 // window.
3999 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Michael Wright44753b12020-07-08 13:48:11 +01004000 mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
4001 InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH |
4002 InputWindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004003
4004 mFocusedWindow =
4005 new FakeWindowHandle(mApplication, mDispatcher, "Focused", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05004006 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004007 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01004008 mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
4009 InputWindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004010
4011 // Set focused application.
4012 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07004013 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004014
4015 // Expect one focus window exist in display.
4016 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004017 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004018 mFocusedWindow->consumeFocusEvent(true);
4019 }
4020
4021 virtual void TearDown() override {
4022 InputDispatcherTest::TearDown();
4023
4024 mUnfocusedWindow.clear();
4025 mFocusedWindow.clear();
4026 }
4027
4028protected:
Chris Yea209fde2020-07-22 13:54:51 -07004029 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004030 sp<FakeWindowHandle> mUnfocusedWindow;
4031 sp<FakeWindowHandle> mFocusedWindow;
4032 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
4033 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
4034 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
4035
4036 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
4037
4038 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
4039
4040private:
4041 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004042 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004043 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4044 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004045 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004046 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4047 location));
4048 }
4049};
4050
4051// If we have 2 windows that are both unresponsive, the one with the shortest timeout
4052// should be ANR'd first.
4053TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004054 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004055 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4056 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004057 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004058 mFocusedWindow->consumeMotionDown();
4059 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
4060 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
4061 // We consumed all events, so no ANR
4062 ASSERT_TRUE(mDispatcher->waitForIdle());
4063 mFakePolicy->assertNotifyAnrWasNotCalled();
4064
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004065 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004066 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4067 FOCUSED_WINDOW_LOCATION));
4068 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
4069 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004070
4071 const std::chrono::duration timeout =
4072 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004073 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004074 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
4075 // sequence to make it consistent
4076 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004077 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004078 mFocusedWindow->consumeMotionDown();
4079 // This cancel is generated because the connection was unresponsive
4080 mFocusedWindow->consumeMotionCancel();
4081 mFocusedWindow->assertNoEvents();
4082 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004083 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004084 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004085 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004086}
4087
4088// If we have 2 windows with identical timeouts that are both unresponsive,
4089// it doesn't matter which order they should have ANR.
4090// But we should receive ANR for both.
4091TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
4092 // Set the timeout for unfocused window to match the focused window
4093 mUnfocusedWindow->setDispatchingTimeout(10ms);
4094 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
4095
4096 tapOnFocusedWindow();
4097 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004098 sp<IBinder> anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms);
4099 sp<IBinder> anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004100
4101 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004102 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
4103 mFocusedWindow->getToken() == anrConnectionToken2);
4104 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
4105 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004106
4107 ASSERT_TRUE(mDispatcher->waitForIdle());
4108 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004109
4110 mFocusedWindow->consumeMotionDown();
4111 mFocusedWindow->consumeMotionUp();
4112 mUnfocusedWindow->consumeMotionOutside();
4113
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004114 sp<IBinder> responsiveToken1 = mFakePolicy->getResponsiveWindowToken();
4115 sp<IBinder> responsiveToken2 = mFakePolicy->getResponsiveWindowToken();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004116
4117 // Both applications should be marked as responsive, in any order
4118 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
4119 mFocusedWindow->getToken() == responsiveToken2);
4120 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
4121 mUnfocusedWindow->getToken() == responsiveToken2);
4122 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004123}
4124
4125// If a window is already not responding, the second tap on the same window should be ignored.
4126// We should also log an error to account for the dropped event (not tested here).
4127// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
4128TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
4129 tapOnFocusedWindow();
4130 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
4131 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
4132 // Receive the events, but don't respond
4133 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
4134 ASSERT_TRUE(downEventSequenceNum);
4135 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
4136 ASSERT_TRUE(upEventSequenceNum);
4137 const std::chrono::duration timeout =
4138 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004139 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004140
4141 // Tap once again
4142 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004143 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004144 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4145 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004146 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004147 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4148 FOCUSED_WINDOW_LOCATION));
4149 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
4150 // valid touch target
4151 mUnfocusedWindow->assertNoEvents();
4152
4153 // Consume the first tap
4154 mFocusedWindow->finishEvent(*downEventSequenceNum);
4155 mFocusedWindow->finishEvent(*upEventSequenceNum);
4156 ASSERT_TRUE(mDispatcher->waitForIdle());
4157 // The second tap did not go to the focused window
4158 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004159 // Since all events are finished, connection should be deemed healthy again
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004160 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004161 mFakePolicy->assertNotifyAnrWasNotCalled();
4162}
4163
4164// If you tap outside of all windows, there will not be ANR
4165TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004166 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004167 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4168 LOCATION_OUTSIDE_ALL_WINDOWS));
4169 ASSERT_TRUE(mDispatcher->waitForIdle());
4170 mFakePolicy->assertNotifyAnrWasNotCalled();
4171}
4172
4173// Since the focused window is paused, tapping on it should not produce any events
4174TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
4175 mFocusedWindow->setPaused(true);
4176 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
4177
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004178 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004179 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4180 FOCUSED_WINDOW_LOCATION));
4181
4182 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
4183 ASSERT_TRUE(mDispatcher->waitForIdle());
4184 // Should not ANR because the window is paused, and touches shouldn't go to it
4185 mFakePolicy->assertNotifyAnrWasNotCalled();
4186
4187 mFocusedWindow->assertNoEvents();
4188 mUnfocusedWindow->assertNoEvents();
4189}
4190
4191/**
4192 * If a window is processing a motion event, and then a key event comes in, the key event should
4193 * not to to the focused window until the motion is processed.
4194 * If a different window becomes focused at this time, the key should go to that window instead.
4195 *
4196 * Warning!!!
4197 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
4198 * and the injection timeout that we specify when injecting the key.
4199 * We must have the injection timeout (10ms) be smaller than
4200 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
4201 *
4202 * If that value changes, this test should also change.
4203 */
4204TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
4205 // Set a long ANR timeout to prevent it from triggering
4206 mFocusedWindow->setDispatchingTimeout(2s);
4207 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
4208
4209 tapOnUnfocusedWindow();
4210 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
4211 ASSERT_TRUE(downSequenceNum);
4212 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
4213 ASSERT_TRUE(upSequenceNum);
4214 // Don't finish the events yet, and send a key
4215 // Injection will succeed because we will eventually give up and send the key to the focused
4216 // window even if motions are still being processed.
4217
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004218 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004219 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004220 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
4221 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004222 // Key will not be sent to the window, yet, because the window is still processing events
4223 // and the key remains pending, waiting for the touch events to be processed
4224 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
4225 ASSERT_FALSE(keySequenceNum);
4226
4227 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07004228 mFocusedWindow->setFocusable(false);
4229 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004230 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004231 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004232
4233 // Focus events should precede the key events
4234 mUnfocusedWindow->consumeFocusEvent(true);
4235 mFocusedWindow->consumeFocusEvent(false);
4236
4237 // Finish the tap events, which should unblock dispatcher
4238 mUnfocusedWindow->finishEvent(*downSequenceNum);
4239 mUnfocusedWindow->finishEvent(*upSequenceNum);
4240
4241 // Now that all queues are cleared and no backlog in the connections, the key event
4242 // can finally go to the newly focused "mUnfocusedWindow".
4243 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4244 mFocusedWindow->assertNoEvents();
4245 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004246 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004247}
4248
4249// When the touch stream is split across 2 windows, and one of them does not respond,
4250// then ANR should be raised and the touch should be canceled for the unresponsive window.
4251// The other window should not be affected by that.
4252TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
4253 // Touch Window 1
4254 NotifyMotionArgs motionArgs =
4255 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4256 ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION});
4257 mDispatcher->notifyMotion(&motionArgs);
4258 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
4259 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
4260
4261 // Touch Window 2
4262 int32_t actionPointerDown =
4263 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
4264
4265 motionArgs =
4266 generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4267 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION});
4268 mDispatcher->notifyMotion(&motionArgs);
4269
4270 const std::chrono::duration timeout =
4271 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004272 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004273
4274 mUnfocusedWindow->consumeMotionDown();
4275 mFocusedWindow->consumeMotionDown();
4276 // Focused window may or may not receive ACTION_MOVE
4277 // But it should definitely receive ACTION_CANCEL due to the ANR
4278 InputEvent* event;
4279 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
4280 ASSERT_TRUE(moveOrCancelSequenceNum);
4281 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
4282 ASSERT_NE(nullptr, event);
4283 ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION);
4284 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
4285 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
4286 mFocusedWindow->consumeMotionCancel();
4287 } else {
4288 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
4289 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004290 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004291 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004292
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004293 mUnfocusedWindow->assertNoEvents();
4294 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004295 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004296}
4297
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05004298/**
4299 * If we have no focused window, and a key comes in, we start the ANR timer.
4300 * The focused application should add a focused window before the timer runs out to prevent ANR.
4301 *
4302 * If the user touches another application during this time, the key should be dropped.
4303 * Next, if a new focused window comes in, without toggling the focused application,
4304 * then no ANR should occur.
4305 *
4306 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
4307 * but in some cases the policy may not update the focused application.
4308 */
4309TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
4310 std::shared_ptr<FakeApplicationHandle> focusedApplication =
4311 std::make_shared<FakeApplicationHandle>();
4312 focusedApplication->setDispatchingTimeout(60ms);
4313 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
4314 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
4315 mFocusedWindow->setFocusable(false);
4316
4317 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
4318 mFocusedWindow->consumeFocusEvent(false);
4319
4320 // Send a key. The ANR timer should start because there is no focused window.
4321 // 'focusedApplication' will get blamed if this timer completes.
4322 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004323 InputEventInjectionResult result =
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05004324 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004325 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/,
4326 false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004327 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05004328
4329 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
4330 // then the injected touches won't cause the focused event to get dropped.
4331 // The dispatcher only checks for whether the queue should be pruned upon queueing.
4332 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
4333 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
4334 // For this test, it means that the key would get delivered to the window once it becomes
4335 // focused.
4336 std::this_thread::sleep_for(10ms);
4337
4338 // Touch unfocused window. This should force the pending key to get dropped.
4339 NotifyMotionArgs motionArgs =
4340 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4341 ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION});
4342 mDispatcher->notifyMotion(&motionArgs);
4343
4344 // We do not consume the motion right away, because that would require dispatcher to first
4345 // process (== drop) the key event, and by that time, ANR will be raised.
4346 // Set the focused window first.
4347 mFocusedWindow->setFocusable(true);
4348 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
4349 setFocusedWindow(mFocusedWindow);
4350 mFocusedWindow->consumeFocusEvent(true);
4351 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
4352 // to another application. This could be a bug / behaviour in the policy.
4353
4354 mUnfocusedWindow->consumeMotionDown();
4355
4356 ASSERT_TRUE(mDispatcher->waitForIdle());
4357 // Should not ANR because we actually have a focused window. It was just added too slowly.
4358 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
4359}
4360
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05004361// These tests ensure we cannot send touch events to a window that's positioned behind a window
4362// that has feature NO_INPUT_CHANNEL.
4363// Layout:
4364// Top (closest to user)
4365// mNoInputWindow (above all windows)
4366// mBottomWindow
4367// Bottom (furthest from user)
4368class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
4369 virtual void SetUp() override {
4370 InputDispatcherTest::SetUp();
4371
4372 mApplication = std::make_shared<FakeApplicationHandle>();
4373 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
4374 "Window without input channel", ADISPLAY_ID_DEFAULT,
4375 std::make_optional<sp<IBinder>>(nullptr) /*token*/);
4376
4377 mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL);
4378 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
4379 // It's perfectly valid for this window to not have an associated input channel
4380
4381 mBottomWindow = new FakeWindowHandle(mApplication, mDispatcher, "Bottom window",
4382 ADISPLAY_ID_DEFAULT);
4383 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
4384
4385 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
4386 }
4387
4388protected:
4389 std::shared_ptr<FakeApplicationHandle> mApplication;
4390 sp<FakeWindowHandle> mNoInputWindow;
4391 sp<FakeWindowHandle> mBottomWindow;
4392};
4393
4394TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
4395 PointF touchedPoint = {10, 10};
4396
4397 NotifyMotionArgs motionArgs =
4398 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4399 ADISPLAY_ID_DEFAULT, {touchedPoint});
4400 mDispatcher->notifyMotion(&motionArgs);
4401
4402 mNoInputWindow->assertNoEvents();
4403 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
4404 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
4405 // and therefore should prevent mBottomWindow from receiving touches
4406 mBottomWindow->assertNoEvents();
4407}
4408
4409/**
4410 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
4411 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
4412 */
4413TEST_F(InputDispatcherMultiWindowOcclusionTests,
4414 NoInputChannelFeature_DropsTouchesWithValidChannel) {
4415 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
4416 "Window with input channel and NO_INPUT_CHANNEL",
4417 ADISPLAY_ID_DEFAULT);
4418
4419 mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL);
4420 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
4421 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
4422
4423 PointF touchedPoint = {10, 10};
4424
4425 NotifyMotionArgs motionArgs =
4426 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4427 ADISPLAY_ID_DEFAULT, {touchedPoint});
4428 mDispatcher->notifyMotion(&motionArgs);
4429
4430 mNoInputWindow->assertNoEvents();
4431 mBottomWindow->assertNoEvents();
4432}
4433
Vishnu Nair958da932020-08-21 17:12:37 -07004434class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
4435protected:
4436 std::shared_ptr<FakeApplicationHandle> mApp;
4437 sp<FakeWindowHandle> mWindow;
4438 sp<FakeWindowHandle> mMirror;
4439
4440 virtual void SetUp() override {
4441 InputDispatcherTest::SetUp();
4442 mApp = std::make_shared<FakeApplicationHandle>();
4443 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
4444 mMirror = new FakeWindowHandle(mApp, mDispatcher, "TestWindowMirror", ADISPLAY_ID_DEFAULT,
4445 mWindow->getToken());
4446 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
4447 mWindow->setFocusable(true);
4448 mMirror->setFocusable(true);
4449 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4450 }
4451};
4452
4453TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
4454 // Request focus on a mirrored window
4455 setFocusedWindow(mMirror);
4456
4457 // window gets focused
4458 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004459 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4460 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004461 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
4462}
4463
4464// A focused & mirrored window remains focused only if the window and its mirror are both
4465// focusable.
4466TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
4467 setFocusedWindow(mMirror);
4468
4469 // window gets focused
4470 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004471 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4472 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004473 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004474 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4475 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004476 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4477
4478 mMirror->setFocusable(false);
4479 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4480
4481 // window loses focus since one of the windows associated with the token in not focusable
4482 mWindow->consumeFocusEvent(false);
4483
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004484 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4485 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004486 mWindow->assertNoEvents();
4487}
4488
4489// A focused & mirrored window remains focused until the window and its mirror both become
4490// invisible.
4491TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
4492 setFocusedWindow(mMirror);
4493
4494 // window gets focused
4495 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004496 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4497 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004498 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004499 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4500 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004501 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4502
4503 mMirror->setVisible(false);
4504 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4505
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004506 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4507 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004508 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004509 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4510 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004511 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4512
4513 mWindow->setVisible(false);
4514 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4515
4516 // window loses focus only after all windows associated with the token become invisible.
4517 mWindow->consumeFocusEvent(false);
4518
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004519 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4520 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004521 mWindow->assertNoEvents();
4522}
4523
4524// A focused & mirrored window remains focused until both windows are removed.
4525TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
4526 setFocusedWindow(mMirror);
4527
4528 // window gets focused
4529 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004530 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4531 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004532 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004533 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4534 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004535 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4536
4537 // single window is removed but the window token remains focused
4538 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
4539
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004540 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4541 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004542 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004543 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4544 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004545 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4546
4547 // Both windows are removed
4548 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
4549 mWindow->consumeFocusEvent(false);
4550
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004551 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4552 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004553 mWindow->assertNoEvents();
4554}
4555
4556// Focus request can be pending until one window becomes visible.
4557TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
4558 // Request focus on an invisible mirror.
4559 mWindow->setVisible(false);
4560 mMirror->setVisible(false);
4561 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4562 setFocusedWindow(mMirror);
4563
4564 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004565 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07004566 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004567 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07004568
4569 mMirror->setVisible(true);
4570 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4571
4572 // window gets focused
4573 mWindow->consumeFocusEvent(true);
4574 // window gets the pending key event
4575 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4576}
Prabir Pradhan99987712020-11-10 18:43:05 -08004577
4578class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
4579protected:
4580 std::shared_ptr<FakeApplicationHandle> mApp;
4581 sp<FakeWindowHandle> mWindow;
4582 sp<FakeWindowHandle> mSecondWindow;
4583
4584 void SetUp() override {
4585 InputDispatcherTest::SetUp();
4586 mApp = std::make_shared<FakeApplicationHandle>();
4587 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
4588 mWindow->setFocusable(true);
4589 mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
4590 mSecondWindow->setFocusable(true);
4591
4592 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
4593 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
4594
4595 setFocusedWindow(mWindow);
4596 mWindow->consumeFocusEvent(true);
4597 }
4598
Prabir Pradhanf192a102021-08-06 14:01:18 +00004599 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
4600 const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08004601 mDispatcher->notifyPointerCaptureChanged(&args);
4602 }
4603
Prabir Pradhanf192a102021-08-06 14:01:18 +00004604 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
4605 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08004606 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhanf192a102021-08-06 14:01:18 +00004607 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
4608 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08004609 window->consumeCaptureEvent(enabled);
Prabir Pradhanf192a102021-08-06 14:01:18 +00004610 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08004611 }
4612};
4613
4614TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
4615 // Ensure that capture cannot be obtained for unfocused windows.
4616 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
4617 mFakePolicy->assertSetPointerCaptureNotCalled();
4618 mSecondWindow->assertNoEvents();
4619
4620 // Ensure that capture can be enabled from the focus window.
4621 requestAndVerifyPointerCapture(mWindow, true);
4622
4623 // Ensure that capture cannot be disabled from a window that does not have capture.
4624 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
4625 mFakePolicy->assertSetPointerCaptureNotCalled();
4626
4627 // Ensure that capture can be disabled from the window with capture.
4628 requestAndVerifyPointerCapture(mWindow, false);
4629}
4630
4631TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhanf192a102021-08-06 14:01:18 +00004632 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08004633
4634 setFocusedWindow(mSecondWindow);
4635
4636 // Ensure that the capture disabled event was sent first.
4637 mWindow->consumeCaptureEvent(false);
4638 mWindow->consumeFocusEvent(false);
4639 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhanf192a102021-08-06 14:01:18 +00004640 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08004641
4642 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhanf192a102021-08-06 14:01:18 +00004643 notifyPointerCaptureChanged({});
4644 notifyPointerCaptureChanged(request);
4645 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08004646 mWindow->assertNoEvents();
4647 mSecondWindow->assertNoEvents();
4648 mFakePolicy->assertSetPointerCaptureNotCalled();
4649}
4650
4651TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhanf192a102021-08-06 14:01:18 +00004652 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08004653
4654 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhanf192a102021-08-06 14:01:18 +00004655 notifyPointerCaptureChanged({});
4656 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08004657
4658 // Ensure that Pointer Capture is disabled.
Prabir Pradhanf192a102021-08-06 14:01:18 +00004659 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08004660 mWindow->consumeCaptureEvent(false);
4661 mWindow->assertNoEvents();
4662}
4663
Prabir Pradhan167e6d92021-02-04 16:18:17 -08004664TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
4665 requestAndVerifyPointerCapture(mWindow, true);
4666
4667 // The first window loses focus.
4668 setFocusedWindow(mSecondWindow);
Prabir Pradhanf192a102021-08-06 14:01:18 +00004669 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08004670 mWindow->consumeCaptureEvent(false);
4671
4672 // Request Pointer Capture from the second window before the notification from InputReader
4673 // arrives.
4674 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhanf192a102021-08-06 14:01:18 +00004675 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08004676
4677 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhanf192a102021-08-06 14:01:18 +00004678 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08004679
4680 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhanf192a102021-08-06 14:01:18 +00004681 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08004682
4683 mSecondWindow->consumeFocusEvent(true);
4684 mSecondWindow->consumeCaptureEvent(true);
4685}
4686
Prabir Pradhanf192a102021-08-06 14:01:18 +00004687TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
4688 // App repeatedly enables and disables capture.
4689 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
4690 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
4691 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
4692 mFakePolicy->assertSetPointerCaptureCalled(false);
4693 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
4694 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
4695
4696 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
4697 // first request is now stale, this should do nothing.
4698 notifyPointerCaptureChanged(firstRequest);
4699 mWindow->assertNoEvents();
4700
4701 // InputReader notifies that the second request was enabled.
4702 notifyPointerCaptureChanged(secondRequest);
4703 mWindow->consumeCaptureEvent(true);
4704}
4705
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004706class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
4707protected:
4708 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00004709
4710 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
4711 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
4712
4713 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
4714 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
4715
4716 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
4717 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
4718 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
4719 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
4720 MAXIMUM_OBSCURING_OPACITY);
4721
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004722 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004723 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004724 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004725
4726 sp<FakeWindowHandle> mTouchWindow;
4727
4728 virtual void SetUp() override {
4729 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004730 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004731 mDispatcher->setBlockUntrustedTouchesMode(android::os::BlockUntrustedTouchesMode::BLOCK);
4732 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
4733 }
4734
4735 virtual void TearDown() override {
4736 InputDispatcherTest::TearDown();
4737 mTouchWindow.clear();
4738 }
4739
4740 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name,
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004741 os::TouchOcclusionMode mode, float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004742 sp<FakeWindowHandle> window = getWindow(uid, name);
4743 window->setFlags(InputWindowInfo::Flag::NOT_TOUCHABLE);
4744 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004745 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004746 return window;
4747 }
4748
4749 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
4750 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
4751 sp<FakeWindowHandle> window =
4752 new FakeWindowHandle(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
4753 // Generate an arbitrary PID based on the UID
4754 window->setOwnerInfo(1777 + (uid % 10000), uid);
4755 return window;
4756 }
4757
4758 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
4759 NotifyMotionArgs args =
4760 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4761 ADISPLAY_ID_DEFAULT, points);
4762 mDispatcher->notifyMotion(&args);
4763 }
4764};
4765
4766TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004767 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004768 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004769 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004770
4771 touch();
4772
4773 mTouchWindow->assertNoEvents();
4774}
4775
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004776TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00004777 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
4778 const sp<FakeWindowHandle>& w =
4779 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
4780 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4781
4782 touch();
4783
4784 mTouchWindow->assertNoEvents();
4785}
4786
4787TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004788 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
4789 const sp<FakeWindowHandle>& w =
4790 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
4791 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4792
4793 touch();
4794
4795 w->assertNoEvents();
4796}
4797
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004798TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004799 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
4800 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004801
4802 touch();
4803
4804 mTouchWindow->consumeAnyMotionDown();
4805}
4806
4807TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004808 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004809 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004810 w->setFrame(Rect(0, 0, 50, 50));
4811 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004812
4813 touch({PointF{100, 100}});
4814
4815 mTouchWindow->consumeAnyMotionDown();
4816}
4817
4818TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004819 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004820 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004821 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4822
4823 touch();
4824
4825 mTouchWindow->consumeAnyMotionDown();
4826}
4827
4828TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
4829 const sp<FakeWindowHandle>& w =
4830 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4831 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004832
4833 touch();
4834
4835 mTouchWindow->consumeAnyMotionDown();
4836}
4837
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004838TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
4839 const sp<FakeWindowHandle>& w =
4840 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4841 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4842
4843 touch();
4844
4845 w->assertNoEvents();
4846}
4847
4848/**
4849 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
4850 * inside) while letting them pass-through. Note that even though touch passes through the occluding
4851 * window, the occluding window will still receive ACTION_OUTSIDE event.
4852 */
4853TEST_F(InputDispatcherUntrustedTouchesTest,
4854 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
4855 const sp<FakeWindowHandle>& w =
4856 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4857 w->addFlags(InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH);
4858 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4859
4860 touch();
4861
4862 w->consumeMotionOutside();
4863}
4864
4865TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
4866 const sp<FakeWindowHandle>& w =
4867 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4868 w->addFlags(InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH);
4869 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4870
4871 touch();
4872
4873 InputEvent* event = w->consume();
4874 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType());
4875 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
4876 EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getX());
4877 EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getY());
4878}
4879
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004880TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004881 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004882 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4883 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004884 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4885
4886 touch();
4887
4888 mTouchWindow->consumeAnyMotionDown();
4889}
4890
4891TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
4892 const sp<FakeWindowHandle>& w =
4893 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4894 MAXIMUM_OBSCURING_OPACITY);
4895 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004896
4897 touch();
4898
4899 mTouchWindow->consumeAnyMotionDown();
4900}
4901
4902TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004903 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004904 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4905 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004906 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4907
4908 touch();
4909
4910 mTouchWindow->assertNoEvents();
4911}
4912
4913TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
4914 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
4915 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004916 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
4917 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004918 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004919 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
4920 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004921 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
4922
4923 touch();
4924
4925 mTouchWindow->assertNoEvents();
4926}
4927
4928TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
4929 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
4930 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004931 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
4932 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004933 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004934 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
4935 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004936 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
4937
4938 touch();
4939
4940 mTouchWindow->consumeAnyMotionDown();
4941}
4942
4943TEST_F(InputDispatcherUntrustedTouchesTest,
4944 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
4945 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004946 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4947 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004948 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004949 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
4950 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004951 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
4952
4953 touch();
4954
4955 mTouchWindow->consumeAnyMotionDown();
4956}
4957
4958TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
4959 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004960 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4961 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004962 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004963 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
4964 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004965 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004966
4967 touch();
4968
4969 mTouchWindow->assertNoEvents();
4970}
4971
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004972TEST_F(InputDispatcherUntrustedTouchesTest,
4973 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
4974 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004975 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
4976 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004977 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004978 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4979 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004980 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
4981
4982 touch();
4983
4984 mTouchWindow->assertNoEvents();
4985}
4986
4987TEST_F(InputDispatcherUntrustedTouchesTest,
4988 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
4989 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004990 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
4991 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004992 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004993 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4994 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004995 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
4996
4997 touch();
4998
4999 mTouchWindow->consumeAnyMotionDown();
5000}
5001
5002TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
5003 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005004 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
5005 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00005006 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5007
5008 touch();
5009
5010 mTouchWindow->consumeAnyMotionDown();
5011}
5012
5013TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
5014 const sp<FakeWindowHandle>& w =
5015 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
5016 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5017
5018 touch();
5019
5020 mTouchWindow->consumeAnyMotionDown();
5021}
5022
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00005023TEST_F(InputDispatcherUntrustedTouchesTest,
5024 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
5025 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
5026 const sp<FakeWindowHandle>& w =
5027 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
5028 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5029
5030 touch();
5031
5032 mTouchWindow->assertNoEvents();
5033}
5034
5035TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
5036 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
5037 const sp<FakeWindowHandle>& w =
5038 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
5039 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5040
5041 touch();
5042
5043 mTouchWindow->consumeAnyMotionDown();
5044}
5045
5046TEST_F(InputDispatcherUntrustedTouchesTest,
5047 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
5048 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
5049 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005050 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
5051 OPACITY_ABOVE_THRESHOLD);
5052 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5053
5054 touch();
5055
5056 mTouchWindow->consumeAnyMotionDown();
5057}
5058
5059TEST_F(InputDispatcherUntrustedTouchesTest,
5060 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
5061 const sp<FakeWindowHandle>& w1 =
5062 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
5063 OPACITY_BELOW_THRESHOLD);
5064 const sp<FakeWindowHandle>& w2 =
5065 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
5066 OPACITY_BELOW_THRESHOLD);
5067 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
5068
5069 touch();
5070
5071 mTouchWindow->assertNoEvents();
5072}
5073
5074/**
5075 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
5076 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
5077 * (which alone would result in allowing touches) does not affect the blocking behavior.
5078 */
5079TEST_F(InputDispatcherUntrustedTouchesTest,
5080 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
5081 const sp<FakeWindowHandle>& wB =
5082 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
5083 OPACITY_BELOW_THRESHOLD);
5084 const sp<FakeWindowHandle>& wC =
5085 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
5086 OPACITY_BELOW_THRESHOLD);
5087 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
5088
5089 touch();
5090
5091 mTouchWindow->assertNoEvents();
5092}
5093
5094/**
5095 * This test is testing that a window from a different UID but with same application token doesn't
5096 * block the touch. Apps can share the application token for close UI collaboration for example.
5097 */
5098TEST_F(InputDispatcherUntrustedTouchesTest,
5099 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
5100 const sp<FakeWindowHandle>& w =
5101 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
5102 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00005103 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5104
5105 touch();
5106
5107 mTouchWindow->consumeAnyMotionDown();
5108}
5109
arthurhungb89ccb02020-12-30 16:19:01 +08005110class InputDispatcherDragTests : public InputDispatcherTest {
5111protected:
5112 std::shared_ptr<FakeApplicationHandle> mApp;
5113 sp<FakeWindowHandle> mWindow;
5114 sp<FakeWindowHandle> mSecondWindow;
5115 sp<FakeWindowHandle> mDragWindow;
5116
5117 void SetUp() override {
5118 InputDispatcherTest::SetUp();
5119 mApp = std::make_shared<FakeApplicationHandle>();
5120 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
5121 mWindow->setFrame(Rect(0, 0, 100, 100));
5122 mWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
5123
5124 mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
5125 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
5126 mSecondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
5127
5128 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
5129 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
5130 }
5131
5132 // Start performing drag, we will create a drag window and transfer touch to it.
5133 void performDrag() {
5134 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5135 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5136 {50, 50}))
5137 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5138
5139 // Window should receive motion event.
5140 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5141
5142 // The drag window covers the entire display
5143 mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
5144 mDispatcher->setInputWindows(
5145 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
5146
5147 // Transfer touch focus to the drag window
5148 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
5149 true /* isDragDrop */);
5150 mWindow->consumeMotionCancel();
5151 mDragWindow->consumeMotionDown();
5152 }
arthurhung6d4bed92021-03-17 11:59:33 +08005153
5154 // Start performing drag, we will create a drag window and transfer touch to it.
5155 void performStylusDrag() {
5156 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5157 injectMotionEvent(mDispatcher,
5158 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
5159 AINPUT_SOURCE_STYLUS)
5160 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
5161 .pointer(PointerBuilder(0,
5162 AMOTION_EVENT_TOOL_TYPE_STYLUS)
5163 .x(50)
5164 .y(50))
5165 .build()));
5166 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5167
5168 // The drag window covers the entire display
5169 mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
5170 mDispatcher->setInputWindows(
5171 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
5172
5173 // Transfer touch focus to the drag window
5174 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
5175 true /* isDragDrop */);
5176 mWindow->consumeMotionCancel();
5177 mDragWindow->consumeMotionDown();
5178 }
arthurhungb89ccb02020-12-30 16:19:01 +08005179};
5180
5181TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
5182 performDrag();
5183
5184 // Move on window.
5185 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5186 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5187 ADISPLAY_ID_DEFAULT, {50, 50}))
5188 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5189 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5190 mWindow->consumeDragEvent(false, 50, 50);
5191 mSecondWindow->assertNoEvents();
5192
5193 // Move to another window.
5194 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5195 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5196 ADISPLAY_ID_DEFAULT, {150, 50}))
5197 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5198 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5199 mWindow->consumeDragEvent(true, 150, 50);
5200 mSecondWindow->consumeDragEvent(false, 50, 50);
5201
5202 // Move back to original window.
5203 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5204 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5205 ADISPLAY_ID_DEFAULT, {50, 50}))
5206 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5207 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5208 mWindow->consumeDragEvent(false, 50, 50);
5209 mSecondWindow->consumeDragEvent(true, -50, 50);
5210
5211 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5212 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
5213 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5214 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5215 mWindow->assertNoEvents();
5216 mSecondWindow->assertNoEvents();
5217}
5218
arthurhungf452d0b2021-01-06 00:19:52 +08005219TEST_F(InputDispatcherDragTests, DragAndDrop) {
5220 performDrag();
5221
5222 // Move on window.
5223 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5224 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5225 ADISPLAY_ID_DEFAULT, {50, 50}))
5226 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5227 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5228 mWindow->consumeDragEvent(false, 50, 50);
5229 mSecondWindow->assertNoEvents();
5230
5231 // Move to another window.
5232 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5233 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5234 ADISPLAY_ID_DEFAULT, {150, 50}))
5235 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5236 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5237 mWindow->consumeDragEvent(true, 150, 50);
5238 mSecondWindow->consumeDragEvent(false, 50, 50);
5239
5240 // drop to another window.
5241 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5242 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5243 {150, 50}))
5244 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5245 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5246 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
5247 mWindow->assertNoEvents();
5248 mSecondWindow->assertNoEvents();
5249}
5250
arthurhung6d4bed92021-03-17 11:59:33 +08005251TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
5252 performStylusDrag();
5253
5254 // Move on window and keep button pressed.
5255 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5256 injectMotionEvent(mDispatcher,
5257 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
5258 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
5259 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
5260 .x(50)
5261 .y(50))
5262 .build()))
5263 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5264 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5265 mWindow->consumeDragEvent(false, 50, 50);
5266 mSecondWindow->assertNoEvents();
5267
5268 // Move to another window and release button, expect to drop item.
5269 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5270 injectMotionEvent(mDispatcher,
5271 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
5272 .buttonState(0)
5273 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
5274 .x(150)
5275 .y(50))
5276 .build()))
5277 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5278 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5279 mWindow->assertNoEvents();
5280 mSecondWindow->assertNoEvents();
5281 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
5282
5283 // nothing to the window.
5284 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5285 injectMotionEvent(mDispatcher,
5286 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
5287 .buttonState(0)
5288 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
5289 .x(150)
5290 .y(50))
5291 .build()))
5292 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5293 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5294 mWindow->assertNoEvents();
5295 mSecondWindow->assertNoEvents();
5296}
5297
Arthur Hung6d0571e2021-04-09 20:18:16 +08005298TEST_F(InputDispatcherDragTests, DragAndDrop_InvalidWindow) {
5299 performDrag();
5300
5301 // Set second window invisible.
5302 mSecondWindow->setVisible(false);
5303 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
5304
5305 // Move on window.
5306 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5307 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5308 ADISPLAY_ID_DEFAULT, {50, 50}))
5309 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5310 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5311 mWindow->consumeDragEvent(false, 50, 50);
5312 mSecondWindow->assertNoEvents();
5313
5314 // Move to another window.
5315 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5316 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5317 ADISPLAY_ID_DEFAULT, {150, 50}))
5318 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5319 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5320 mWindow->consumeDragEvent(true, 150, 50);
5321 mSecondWindow->assertNoEvents();
5322
5323 // drop to another window.
5324 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5325 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5326 {150, 50}))
5327 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5328 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5329 mFakePolicy->assertDropTargetEquals(nullptr);
5330 mWindow->assertNoEvents();
5331 mSecondWindow->assertNoEvents();
5332}
5333
Garfield Tane84e6f92019-08-29 17:28:41 -07005334} // namespace android::inputdispatcher