blob: 7d4c6386f873d8492962f7bd76554e08d9efeee3 [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
liushenxiang42232912021-05-21 20:24:09 +08002895TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
2896 sendAndConsumeKeyDown(DEVICE_ID);
2897 expectKeyRepeatOnce(1 /*repeatCount*/);
2898 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
2899 mDispatcher->notifyDeviceReset(&args);
2900 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
2901 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
2902 mWindow->assertNoEvents();
2903}
2904
Garfield Tan1c7bc862020-01-28 13:24:04 -08002905TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Chris Ye2ad95392020-09-01 13:44:44 -07002906 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002907 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2908 InputEvent* repeatEvent = mWindow->consume();
2909 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2910 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
2911 IdGenerator::getSource(repeatEvent->getId()));
2912 }
2913}
2914
2915TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Chris Ye2ad95392020-09-01 13:44:44 -07002916 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002917
2918 std::unordered_set<int32_t> idSet;
2919 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2920 InputEvent* repeatEvent = mWindow->consume();
2921 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2922 int32_t id = repeatEvent->getId();
2923 EXPECT_EQ(idSet.end(), idSet.find(id));
2924 idSet.insert(id);
2925 }
2926}
2927
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002928/* Test InputDispatcher for MultiDisplay */
2929class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
2930public:
2931 static constexpr int32_t SECOND_DISPLAY_ID = 1;
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002932 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002933 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08002934
Chris Yea209fde2020-07-22 13:54:51 -07002935 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002936 windowInPrimary =
2937 new FakeWindowHandle(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002938
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002939 // Set focus window for primary display, but focused display would be second one.
2940 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07002941 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002942 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002943 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002944 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08002945
Chris Yea209fde2020-07-22 13:54:51 -07002946 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002947 windowInSecondary =
2948 new FakeWindowHandle(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002949 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002950 // Set focus display to second one.
2951 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
2952 // Set focus window for second display.
2953 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07002954 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002955 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002956 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002957 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002958 }
2959
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002960 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002961 InputDispatcherTest::TearDown();
2962
Chris Yea209fde2020-07-22 13:54:51 -07002963 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002964 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07002965 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002966 windowInSecondary.clear();
2967 }
2968
2969protected:
Chris Yea209fde2020-07-22 13:54:51 -07002970 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002971 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07002972 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002973 sp<FakeWindowHandle> windowInSecondary;
2974};
2975
2976TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
2977 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002978 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2979 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2980 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002981 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08002982 windowInSecondary->assertNoEvents();
2983
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002984 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002985 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2986 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
2987 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08002988 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002989 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08002990}
2991
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002992TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08002993 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08002994 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2995 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002996 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002997 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08002998 windowInSecondary->assertNoEvents();
2999
3000 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003001 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003002 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08003003 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003004 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08003005
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003006 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00003007 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08003008
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003009 // Old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003010 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
3011 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08003012
3013 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003014 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003015 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08003016 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003017 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08003018 windowInSecondary->assertNoEvents();
3019}
3020
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003021// Test per-display input monitors for motion event.
3022TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08003023 FakeMonitorReceiver monitorInPrimary =
3024 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
3025 FakeMonitorReceiver monitorInSecondary =
3026 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003027
3028 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003029 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3030 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
3031 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003032 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08003033 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003034 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003035 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003036
3037 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003038 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3039 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
3040 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003041 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003042 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003043 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08003044 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003045
3046 // Test inject a non-pointer motion event.
3047 // If specific a display, it will dispatch to the focused window of particular display,
3048 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003049 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3050 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
3051 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003052 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003053 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003054 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08003055 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003056}
3057
3058// Test per-display input monitors for key event.
3059TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003060 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08003061 FakeMonitorReceiver monitorInPrimary =
3062 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
3063 FakeMonitorReceiver monitorInSecondary =
3064 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003065
3066 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003067 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3068 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003069 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08003070 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003071 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08003072 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003073}
3074
Vishnu Nair958da932020-08-21 17:12:37 -07003075TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
3076 sp<FakeWindowHandle> secondWindowInPrimary =
3077 new FakeWindowHandle(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
3078 secondWindowInPrimary->setFocusable(true);
3079 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
3080 setFocusedWindow(secondWindowInPrimary);
3081 windowInPrimary->consumeFocusEvent(false);
3082 secondWindowInPrimary->consumeFocusEvent(true);
3083
3084 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003085 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
3086 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003087 windowInPrimary->assertNoEvents();
3088 windowInSecondary->assertNoEvents();
3089 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3090}
3091
Jackal Guof9696682018-10-05 12:23:23 +08003092class InputFilterTest : public InputDispatcherTest {
3093protected:
3094 static constexpr int32_t SECOND_DISPLAY_ID = 1;
3095
3096 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) {
3097 NotifyMotionArgs motionArgs;
3098
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003099 motionArgs =
3100 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08003101 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003102 motionArgs =
3103 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08003104 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003105 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08003106 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08003107 mFakePolicy->assertFilterInputEventWasCalled(motionArgs);
Jackal Guof9696682018-10-05 12:23:23 +08003108 } else {
3109 mFakePolicy->assertFilterInputEventWasNotCalled();
3110 }
3111 }
3112
3113 void testNotifyKey(bool expectToBeFiltered) {
3114 NotifyKeyArgs keyArgs;
3115
3116 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
3117 mDispatcher->notifyKey(&keyArgs);
3118 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
3119 mDispatcher->notifyKey(&keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003120 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08003121
3122 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08003123 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08003124 } else {
3125 mFakePolicy->assertFilterInputEventWasNotCalled();
3126 }
3127 }
3128};
3129
3130// Test InputFilter for MotionEvent
3131TEST_F(InputFilterTest, MotionEvent_InputFilter) {
3132 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
3133 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
3134 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
3135
3136 // Enable InputFilter
3137 mDispatcher->setInputFilterEnabled(true);
3138 // Test touch on both primary and second display, and check if both events are filtered.
3139 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
3140 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
3141
3142 // Disable InputFilter
3143 mDispatcher->setInputFilterEnabled(false);
3144 // Test touch on both primary and second display, and check if both events aren't filtered.
3145 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
3146 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
3147}
3148
3149// Test InputFilter for KeyEvent
3150TEST_F(InputFilterTest, KeyEvent_InputFilter) {
3151 // Since the InputFilter is disabled by default, check if key event aren't filtered.
3152 testNotifyKey(/*expectToBeFiltered*/ false);
3153
3154 // Enable InputFilter
3155 mDispatcher->setInputFilterEnabled(true);
3156 // Send a key event, and check if it is filtered.
3157 testNotifyKey(/*expectToBeFiltered*/ true);
3158
3159 // Disable InputFilter
3160 mDispatcher->setInputFilterEnabled(false);
3161 // Send a key event, and check if it isn't filtered.
3162 testNotifyKey(/*expectToBeFiltered*/ false);
3163}
3164
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003165class InputFilterInjectionPolicyTest : public InputDispatcherTest {
3166protected:
3167 virtual void SetUp() override {
3168 InputDispatcherTest::SetUp();
3169
3170 /**
3171 * We don't need to enable input filter to test the injected event policy, but we enabled it
3172 * here to make the tests more realistic, since this policy only matters when inputfilter is
3173 * on.
3174 */
3175 mDispatcher->setInputFilterEnabled(true);
3176
3177 std::shared_ptr<InputApplicationHandle> application =
3178 std::make_shared<FakeApplicationHandle>();
3179 mWindow =
3180 new FakeWindowHandle(application, mDispatcher, "Test Window", ADISPLAY_ID_DEFAULT);
3181
3182 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3183 mWindow->setFocusable(true);
3184 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3185 setFocusedWindow(mWindow);
3186 mWindow->consumeFocusEvent(true);
3187 }
3188
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003189 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
3190 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003191 KeyEvent event;
3192
3193 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
3194 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
3195 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
3196 KEY_A, AMETA_NONE, 0 /*repeatCount*/, eventTime, eventTime);
3197 const int32_t additionalPolicyFlags =
3198 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
3199 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3200 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
3201 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
3202 policyFlags | additionalPolicyFlags));
3203
3204 InputEvent* received = mWindow->consume();
3205 ASSERT_NE(nullptr, received);
3206 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003207 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_KEY);
3208 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
3209 ASSERT_EQ(flags, keyEvent.getFlags());
3210 }
3211
3212 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
3213 int32_t flags) {
3214 MotionEvent event;
3215 PointerProperties pointerProperties[1];
3216 PointerCoords pointerCoords[1];
3217 pointerProperties[0].clear();
3218 pointerProperties[0].id = 0;
3219 pointerCoords[0].clear();
3220 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
3221 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
3222
3223 ui::Transform identityTransform;
3224 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
3225 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
3226 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
3227 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
3228 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
3229 AMOTION_EVENT_INVALID_CURSOR_POSITION,
3230 0 /*AMOTION_EVENT_INVALID_DISPLAY_SIZE*/,
3231 0 /*AMOTION_EVENT_INVALID_DISPLAY_SIZE*/, eventTime, eventTime,
3232 /*pointerCount*/ 1, pointerProperties, pointerCoords);
3233
3234 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
3235 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3236 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
3237 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
3238 policyFlags | additionalPolicyFlags));
3239
3240 InputEvent* received = mWindow->consume();
3241 ASSERT_NE(nullptr, received);
3242 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
3243 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_MOTION);
3244 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
3245 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003246 }
3247
3248private:
3249 sp<FakeWindowHandle> mWindow;
3250};
3251
3252TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003253 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
3254 // filter. Without it, the event will no different from a regularly injected event, and the
3255 // injected device id will be overwritten.
3256 testInjectedKey(POLICY_FLAG_FILTERED, 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
3257 0 /*flags*/);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003258}
3259
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003260TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003261 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003262 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
3263 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
3264}
3265
3266TEST_F(InputFilterInjectionPolicyTest,
3267 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
3268 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
3269 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/,
3270 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003271}
3272
3273TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
3274 testInjectedKey(0 /*policyFlags*/, 3 /*injectedDeviceId*/,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00003275 VIRTUAL_KEYBOARD_ID /*resolvedDeviceId*/, 0 /*flags*/);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00003276}
3277
chaviwfd6d3512019-03-25 13:23:49 -07003278class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07003279 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07003280 InputDispatcherTest::SetUp();
3281
Chris Yea209fde2020-07-22 13:54:51 -07003282 std::shared_ptr<FakeApplicationHandle> application =
3283 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003284 mUnfocusedWindow =
3285 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07003286 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
3287 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3288 // window.
Michael Wright44753b12020-07-08 13:48:11 +01003289 mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07003290
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003291 mFocusedWindow =
3292 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
3293 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01003294 mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07003295
3296 // Set focused application.
3297 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07003298 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07003299
3300 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00003301 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003302 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003303 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07003304 }
3305
Prabir Pradhan3608aad2019-10-02 17:08:26 -07003306 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07003307 InputDispatcherTest::TearDown();
3308
3309 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003310 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07003311 }
3312
3313protected:
3314 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003315 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003316 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07003317};
3318
3319// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
3320// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
3321// the onPointerDownOutsideFocus callback.
3322TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003323 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003324 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3325 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003326 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003327 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07003328
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003329 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07003330 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
3331}
3332
3333// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
3334// DOWN on the window that doesn't have focus. Ensure no window received the
3335// onPointerDownOutsideFocus callback.
3336TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003337 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003338 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003339 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003340 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07003341
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003342 ASSERT_TRUE(mDispatcher->waitForIdle());
3343 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07003344}
3345
3346// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
3347// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
3348TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003349 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3350 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003351 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003352 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07003353
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003354 ASSERT_TRUE(mDispatcher->waitForIdle());
3355 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07003356}
3357
3358// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
3359// DOWN on the window that already has focus. Ensure no window received the
3360// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003361TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003362 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003363 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003364 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003365 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003366 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07003367
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003368 ASSERT_TRUE(mDispatcher->waitForIdle());
3369 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07003370}
3371
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08003372// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
3373// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
3374TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
3375 const MotionEvent event =
3376 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3377 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
3378 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(20).y(20))
3379 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
3380 .build();
3381 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
3382 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3383 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
3384
3385 ASSERT_TRUE(mDispatcher->waitForIdle());
3386 mFakePolicy->assertOnPointerDownWasNotCalled();
3387 // Ensure that the unfocused window did not receive any FOCUS events.
3388 mUnfocusedWindow->assertNoEvents();
3389}
3390
chaviwaf87b3e2019-10-01 16:59:28 -07003391// These tests ensures we can send touch events to a single client when there are multiple input
3392// windows that point to the same client token.
3393class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
3394 virtual void SetUp() override {
3395 InputDispatcherTest::SetUp();
3396
Chris Yea209fde2020-07-22 13:54:51 -07003397 std::shared_ptr<FakeApplicationHandle> application =
3398 std::make_shared<FakeApplicationHandle>();
chaviwaf87b3e2019-10-01 16:59:28 -07003399 mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1",
3400 ADISPLAY_ID_DEFAULT);
3401 // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window.
3402 // 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 +01003403 mWindow1->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3404 InputWindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07003405 mWindow1->setFrame(Rect(0, 0, 100, 100));
3406
3407 mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2",
3408 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
Michael Wright44753b12020-07-08 13:48:11 +01003409 mWindow2->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3410 InputWindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07003411 mWindow2->setFrame(Rect(100, 100, 200, 200));
3412
Arthur Hung72d8dc32020-03-28 00:48:39 +00003413 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07003414 }
3415
3416protected:
3417 sp<FakeWindowHandle> mWindow1;
3418 sp<FakeWindowHandle> mWindow2;
3419
3420 // Helper function to convert the point from screen coordinates into the window's space
3421 static PointF getPointInWindow(const InputWindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07003422 vec2 vals = windowInfo->transform.transform(point.x, point.y);
3423 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07003424 }
3425
3426 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
3427 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003428 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07003429 InputEvent* event = window->consume();
3430
3431 ASSERT_NE(nullptr, event) << name.c_str()
3432 << ": consumer should have returned non-NULL event.";
3433
3434 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
3435 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
3436 << " event, got " << inputEventTypeToString(event->getType()) << " event";
3437
3438 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
3439 EXPECT_EQ(expectedAction, motionEvent.getAction());
3440
3441 for (size_t i = 0; i < points.size(); i++) {
3442 float expectedX = points[i].x;
3443 float expectedY = points[i].y;
3444
3445 EXPECT_EQ(expectedX, motionEvent.getX(i))
3446 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
3447 << ", got " << motionEvent.getX(i);
3448 EXPECT_EQ(expectedY, motionEvent.getY(i))
3449 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
3450 << ", got " << motionEvent.getY(i);
3451 }
3452 }
chaviw9eaa22c2020-07-01 16:21:27 -07003453
3454 void touchAndAssertPositions(int32_t action, std::vector<PointF> touchedPoints,
3455 std::vector<PointF> expectedPoints) {
3456 NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
3457 ADISPLAY_ID_DEFAULT, touchedPoints);
3458 mDispatcher->notifyMotion(&motionArgs);
3459
3460 // Always consume from window1 since it's the window that has the InputReceiver
3461 consumeMotionEvent(mWindow1, action, expectedPoints);
3462 }
chaviwaf87b3e2019-10-01 16:59:28 -07003463};
3464
3465TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
3466 // Touch Window 1
3467 PointF touchedPoint = {10, 10};
3468 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003469 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003470
3471 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07003472 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003473
3474 // Touch Window 2
3475 touchedPoint = {150, 150};
3476 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003477 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003478}
3479
chaviw9eaa22c2020-07-01 16:21:27 -07003480TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
3481 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07003482 mWindow2->setWindowScale(0.5f, 0.5f);
3483
3484 // Touch Window 1
3485 PointF touchedPoint = {10, 10};
3486 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003487 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003488 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07003489 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003490
3491 // Touch Window 2
3492 touchedPoint = {150, 150};
3493 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003494 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
3495 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003496
chaviw9eaa22c2020-07-01 16:21:27 -07003497 // Update the transform so rotation is set
3498 mWindow2->setWindowTransform(0, -1, 1, 0);
3499 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
3500 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003501}
3502
chaviw9eaa22c2020-07-01 16:21:27 -07003503TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003504 mWindow2->setWindowScale(0.5f, 0.5f);
3505
3506 // Touch Window 1
3507 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3508 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003509 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003510
3511 // Touch Window 2
3512 int32_t actionPointerDown =
3513 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003514 touchedPoints.push_back(PointF{150, 150});
3515 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3516 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003517
chaviw9eaa22c2020-07-01 16:21:27 -07003518 // Release Window 2
3519 int32_t actionPointerUp =
3520 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3521 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
3522 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003523
chaviw9eaa22c2020-07-01 16:21:27 -07003524 // Update the transform so rotation is set for Window 2
3525 mWindow2->setWindowTransform(0, -1, 1, 0);
3526 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3527 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003528}
3529
chaviw9eaa22c2020-07-01 16:21:27 -07003530TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003531 mWindow2->setWindowScale(0.5f, 0.5f);
3532
3533 // Touch Window 1
3534 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3535 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003536 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003537
3538 // Touch Window 2
3539 int32_t actionPointerDown =
3540 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003541 touchedPoints.push_back(PointF{150, 150});
3542 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003543
chaviw9eaa22c2020-07-01 16:21:27 -07003544 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003545
3546 // Move both windows
3547 touchedPoints = {{20, 20}, {175, 175}};
3548 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3549 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3550
chaviw9eaa22c2020-07-01 16:21:27 -07003551 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003552
chaviw9eaa22c2020-07-01 16:21:27 -07003553 // Release Window 2
3554 int32_t actionPointerUp =
3555 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3556 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
3557 expectedPoints.pop_back();
3558
3559 // Touch Window 2
3560 mWindow2->setWindowTransform(0, -1, 1, 0);
3561 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3562 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
3563
3564 // Move both windows
3565 touchedPoints = {{20, 20}, {175, 175}};
3566 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3567 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3568
3569 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003570}
3571
3572TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
3573 mWindow1->setWindowScale(0.5f, 0.5f);
3574
3575 // Touch Window 1
3576 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3577 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003578 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003579
3580 // Touch Window 2
3581 int32_t actionPointerDown =
3582 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003583 touchedPoints.push_back(PointF{150, 150});
3584 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003585
chaviw9eaa22c2020-07-01 16:21:27 -07003586 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003587
3588 // Move both windows
3589 touchedPoints = {{20, 20}, {175, 175}};
3590 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3591 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3592
chaviw9eaa22c2020-07-01 16:21:27 -07003593 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003594}
3595
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003596class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
3597 virtual void SetUp() override {
3598 InputDispatcherTest::SetUp();
3599
Chris Yea209fde2020-07-22 13:54:51 -07003600 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003601 mApplication->setDispatchingTimeout(20ms);
3602 mWindow =
3603 new FakeWindowHandle(mApplication, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
3604 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05003605 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07003606 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003607 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3608 // window.
Michael Wright44753b12020-07-08 13:48:11 +01003609 mWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003610
3611 // Set focused application.
3612 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
3613
3614 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003615 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003616 mWindow->consumeFocusEvent(true);
3617 }
3618
3619 virtual void TearDown() override {
3620 InputDispatcherTest::TearDown();
3621 mWindow.clear();
3622 }
3623
3624protected:
Chris Yea209fde2020-07-22 13:54:51 -07003625 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003626 sp<FakeWindowHandle> mWindow;
3627 static constexpr PointF WINDOW_LOCATION = {20, 20};
3628
3629 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003630 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003631 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3632 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003633 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003634 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3635 WINDOW_LOCATION));
3636 }
3637};
3638
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003639// Send a tap and respond, which should not cause an ANR.
3640TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
3641 tapOnWindow();
3642 mWindow->consumeMotionDown();
3643 mWindow->consumeMotionUp();
3644 ASSERT_TRUE(mDispatcher->waitForIdle());
3645 mFakePolicy->assertNotifyAnrWasNotCalled();
3646}
3647
3648// Send a regular key and respond, which should not cause an ANR.
3649TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003650 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003651 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
3652 ASSERT_TRUE(mDispatcher->waitForIdle());
3653 mFakePolicy->assertNotifyAnrWasNotCalled();
3654}
3655
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003656TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
3657 mWindow->setFocusable(false);
3658 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3659 mWindow->consumeFocusEvent(false);
3660
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003661 InputEventInjectionResult result =
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003662 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003663 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/,
3664 false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003665 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003666 // Key will not go to window because we have no focused window.
3667 // The 'no focused window' ANR timer should start instead.
3668
3669 // Now, the focused application goes away.
3670 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
3671 // The key should get dropped and there should be no ANR.
3672
3673 ASSERT_TRUE(mDispatcher->waitForIdle());
3674 mFakePolicy->assertNotifyAnrWasNotCalled();
3675}
3676
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003677// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003678// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
3679// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003680TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003681 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003682 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3683 WINDOW_LOCATION));
3684
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003685 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
3686 ASSERT_TRUE(sequenceNum);
3687 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003688 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003689
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003690 mWindow->finishEvent(*sequenceNum);
3691 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3692 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003693 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003694 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003695}
3696
3697// Send a key to the app and have the app not respond right away.
3698TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
3699 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003700 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003701 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
3702 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003703 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003704 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003705 ASSERT_TRUE(mDispatcher->waitForIdle());
3706}
3707
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003708// We have a focused application, but no focused window
3709TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003710 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003711 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3712 mWindow->consumeFocusEvent(false);
3713
3714 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003715 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003716 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3717 WINDOW_LOCATION));
3718 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
3719 mDispatcher->waitForIdle();
3720 mFakePolicy->assertNotifyAnrWasNotCalled();
3721
3722 // Once a focused event arrives, we get an ANR for this application
3723 // We specify the injection timeout to be smaller than the application timeout, to ensure that
3724 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003725 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003726 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003727 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003728 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003729 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003730 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003731 ASSERT_TRUE(mDispatcher->waitForIdle());
3732}
3733
3734// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003735// Make sure that we don't notify policy twice about the same ANR.
3736TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003737 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003738 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3739 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003740
3741 // Once a focused event arrives, we get an ANR for this application
3742 // We specify the injection timeout to be smaller than the application timeout, to ensure that
3743 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003744 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003745 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08003746 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003747 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003748 const std::chrono::duration appTimeout =
3749 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003750 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003751
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003752 std::this_thread::sleep_for(appTimeout);
3753 // ANR should not be raised again. It is up to policy to do that if it desires.
3754 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003755
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003756 // If we now get a focused window, the ANR should stop, but the policy handles that via
3757 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003758 ASSERT_TRUE(mDispatcher->waitForIdle());
3759}
3760
3761// We have a focused application, but no focused window
3762TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003763 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003764 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3765 mWindow->consumeFocusEvent(false);
3766
3767 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003768 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003769 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003770 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3771 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003772
3773 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003774 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003775
3776 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003777 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003778 ASSERT_TRUE(mDispatcher->waitForIdle());
3779 mWindow->assertNoEvents();
3780}
3781
3782/**
3783 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
3784 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
3785 * If we process 1 of the events, but ANR on the second event with the same timestamp,
3786 * the ANR mechanism should still work.
3787 *
3788 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
3789 * DOWN event, while not responding on the second one.
3790 */
3791TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
3792 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
3793 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3794 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
3795 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
3796 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003797 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003798
3799 // Now send ACTION_UP, with identical timestamp
3800 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3801 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
3802 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
3803 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003804 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003805
3806 // We have now sent down and up. Let's consume first event and then ANR on the second.
3807 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3808 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003809 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003810}
3811
3812// If an app is not responding to a key event, gesture monitors should continue to receive
3813// new motion events
3814TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnKey) {
3815 FakeMonitorReceiver monitor =
3816 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
3817 true /*isGestureMonitor*/);
3818
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003819 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3820 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003821 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003822 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003823
3824 // Stuck on the ACTION_UP
3825 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003826 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003827
3828 // New tap will go to the gesture monitor, but not to the window
3829 tapOnWindow();
3830 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3831 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3832
3833 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
3834 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003835 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003836 mWindow->assertNoEvents();
3837 monitor.assertNoEvents();
3838}
3839
3840// If an app is not responding to a motion event, gesture monitors should continue to receive
3841// new motion events
3842TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnMotion) {
3843 FakeMonitorReceiver monitor =
3844 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
3845 true /*isGestureMonitor*/);
3846
3847 tapOnWindow();
3848 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3849 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3850
3851 mWindow->consumeMotionDown();
3852 // Stuck on the ACTION_UP
3853 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003854 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003855
3856 // New tap will go to the gesture monitor, but not to the window
3857 tapOnWindow();
3858 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3859 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3860
3861 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
3862 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003863 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003864 mWindow->assertNoEvents();
3865 monitor.assertNoEvents();
3866}
3867
3868// If a window is unresponsive, then you get anr. if the window later catches up and starts to
3869// process events, you don't get an anr. When the window later becomes unresponsive again, you
3870// get an ANR again.
3871// 1. tap -> block on ACTION_UP -> receive ANR
3872// 2. consume all pending events (= queue becomes healthy again)
3873// 3. tap again -> block on ACTION_UP again -> receive ANR second time
3874TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
3875 tapOnWindow();
3876
3877 mWindow->consumeMotionDown();
3878 // Block on ACTION_UP
3879 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003880 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003881 mWindow->consumeMotionUp(); // Now the connection should be healthy again
3882 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003883 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003884 mWindow->assertNoEvents();
3885
3886 tapOnWindow();
3887 mWindow->consumeMotionDown();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003888 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003889 mWindow->consumeMotionUp();
3890
3891 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003892 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003893 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003894 mWindow->assertNoEvents();
3895}
3896
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003897// If a connection remains unresponsive for a while, make sure policy is only notified once about
3898// it.
3899TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003900 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003901 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3902 WINDOW_LOCATION));
3903
3904 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003905 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003906 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003907 // 'notifyConnectionUnresponsive' should only be called once per connection
3908 mFakePolicy->assertNotifyAnrWasNotCalled();
3909 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003910 mWindow->consumeMotionDown();
3911 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3912 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3913 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003914 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003915 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003916 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003917}
3918
3919/**
3920 * If a window is processing a motion event, and then a key event comes in, the key event should
3921 * not to to the focused window until the motion is processed.
3922 *
3923 * Warning!!!
3924 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
3925 * and the injection timeout that we specify when injecting the key.
3926 * We must have the injection timeout (10ms) be smaller than
3927 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
3928 *
3929 * If that value changes, this test should also change.
3930 */
3931TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
3932 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3933 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3934
3935 tapOnWindow();
3936 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3937 ASSERT_TRUE(downSequenceNum);
3938 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3939 ASSERT_TRUE(upSequenceNum);
3940 // Don't finish the events yet, and send a key
3941 // Injection will "succeed" because we will eventually give up and send the key to the focused
3942 // window even if motions are still being processed. But because the injection timeout is short,
3943 // we will receive INJECTION_TIMED_OUT as the result.
3944
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003945 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003946 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003947 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3948 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003949 // Key will not be sent to the window, yet, because the window is still processing events
3950 // and the key remains pending, waiting for the touch events to be processed
3951 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3952 ASSERT_FALSE(keySequenceNum);
3953
3954 std::this_thread::sleep_for(500ms);
3955 // if we wait long enough though, dispatcher will give up, and still send the key
3956 // to the focused window, even though we have not yet finished the motion event
3957 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3958 mWindow->finishEvent(*downSequenceNum);
3959 mWindow->finishEvent(*upSequenceNum);
3960}
3961
3962/**
3963 * If a window is processing a motion event, and then a key event comes in, the key event should
3964 * not go to the focused window until the motion is processed.
3965 * If then a new motion comes in, then the pending key event should be going to the currently
3966 * focused window right away.
3967 */
3968TEST_F(InputDispatcherSingleWindowAnr,
3969 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
3970 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3971 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3972
3973 tapOnWindow();
3974 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3975 ASSERT_TRUE(downSequenceNum);
3976 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3977 ASSERT_TRUE(upSequenceNum);
3978 // Don't finish the events yet, and send a key
3979 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003980 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003981 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003982 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003983 // At this point, key is still pending, and should not be sent to the application yet.
3984 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3985 ASSERT_FALSE(keySequenceNum);
3986
3987 // Now tap down again. It should cause the pending key to go to the focused window right away.
3988 tapOnWindow();
3989 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
3990 // the other events yet. We can finish events in any order.
3991 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
3992 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
3993 mWindow->consumeMotionDown();
3994 mWindow->consumeMotionUp();
3995 mWindow->assertNoEvents();
3996}
3997
3998class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
3999 virtual void SetUp() override {
4000 InputDispatcherTest::SetUp();
4001
Chris Yea209fde2020-07-22 13:54:51 -07004002 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004003 mApplication->setDispatchingTimeout(10ms);
4004 mUnfocusedWindow =
4005 new FakeWindowHandle(mApplication, mDispatcher, "Unfocused", ADISPLAY_ID_DEFAULT);
4006 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
4007 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
4008 // window.
4009 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Michael Wright44753b12020-07-08 13:48:11 +01004010 mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
4011 InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH |
4012 InputWindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004013
4014 mFocusedWindow =
4015 new FakeWindowHandle(mApplication, mDispatcher, "Focused", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05004016 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004017 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01004018 mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
4019 InputWindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004020
4021 // Set focused application.
4022 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07004023 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004024
4025 // Expect one focus window exist in display.
4026 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004027 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004028 mFocusedWindow->consumeFocusEvent(true);
4029 }
4030
4031 virtual void TearDown() override {
4032 InputDispatcherTest::TearDown();
4033
4034 mUnfocusedWindow.clear();
4035 mFocusedWindow.clear();
4036 }
4037
4038protected:
Chris Yea209fde2020-07-22 13:54:51 -07004039 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004040 sp<FakeWindowHandle> mUnfocusedWindow;
4041 sp<FakeWindowHandle> mFocusedWindow;
4042 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
4043 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
4044 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
4045
4046 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
4047
4048 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
4049
4050private:
4051 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004052 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004053 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4054 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004055 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004056 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4057 location));
4058 }
4059};
4060
4061// If we have 2 windows that are both unresponsive, the one with the shortest timeout
4062// should be ANR'd first.
4063TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004064 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004065 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4066 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004067 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004068 mFocusedWindow->consumeMotionDown();
4069 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
4070 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
4071 // We consumed all events, so no ANR
4072 ASSERT_TRUE(mDispatcher->waitForIdle());
4073 mFakePolicy->assertNotifyAnrWasNotCalled();
4074
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004075 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004076 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4077 FOCUSED_WINDOW_LOCATION));
4078 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
4079 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004080
4081 const std::chrono::duration timeout =
4082 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004083 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004084 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
4085 // sequence to make it consistent
4086 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004087 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004088 mFocusedWindow->consumeMotionDown();
4089 // This cancel is generated because the connection was unresponsive
4090 mFocusedWindow->consumeMotionCancel();
4091 mFocusedWindow->assertNoEvents();
4092 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004093 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004094 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004095 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004096}
4097
4098// If we have 2 windows with identical timeouts that are both unresponsive,
4099// it doesn't matter which order they should have ANR.
4100// But we should receive ANR for both.
4101TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
4102 // Set the timeout for unfocused window to match the focused window
4103 mUnfocusedWindow->setDispatchingTimeout(10ms);
4104 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
4105
4106 tapOnFocusedWindow();
4107 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004108 sp<IBinder> anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms);
4109 sp<IBinder> anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004110
4111 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004112 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
4113 mFocusedWindow->getToken() == anrConnectionToken2);
4114 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
4115 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004116
4117 ASSERT_TRUE(mDispatcher->waitForIdle());
4118 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004119
4120 mFocusedWindow->consumeMotionDown();
4121 mFocusedWindow->consumeMotionUp();
4122 mUnfocusedWindow->consumeMotionOutside();
4123
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004124 sp<IBinder> responsiveToken1 = mFakePolicy->getResponsiveWindowToken();
4125 sp<IBinder> responsiveToken2 = mFakePolicy->getResponsiveWindowToken();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004126
4127 // Both applications should be marked as responsive, in any order
4128 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
4129 mFocusedWindow->getToken() == responsiveToken2);
4130 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
4131 mUnfocusedWindow->getToken() == responsiveToken2);
4132 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004133}
4134
4135// If a window is already not responding, the second tap on the same window should be ignored.
4136// We should also log an error to account for the dropped event (not tested here).
4137// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
4138TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
4139 tapOnFocusedWindow();
4140 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
4141 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
4142 // Receive the events, but don't respond
4143 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
4144 ASSERT_TRUE(downEventSequenceNum);
4145 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
4146 ASSERT_TRUE(upEventSequenceNum);
4147 const std::chrono::duration timeout =
4148 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004149 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004150
4151 // Tap once again
4152 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004153 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004154 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4155 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004156 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004157 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4158 FOCUSED_WINDOW_LOCATION));
4159 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
4160 // valid touch target
4161 mUnfocusedWindow->assertNoEvents();
4162
4163 // Consume the first tap
4164 mFocusedWindow->finishEvent(*downEventSequenceNum);
4165 mFocusedWindow->finishEvent(*upEventSequenceNum);
4166 ASSERT_TRUE(mDispatcher->waitForIdle());
4167 // The second tap did not go to the focused window
4168 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004169 // Since all events are finished, connection should be deemed healthy again
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004170 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004171 mFakePolicy->assertNotifyAnrWasNotCalled();
4172}
4173
4174// If you tap outside of all windows, there will not be ANR
4175TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004176 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004177 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4178 LOCATION_OUTSIDE_ALL_WINDOWS));
4179 ASSERT_TRUE(mDispatcher->waitForIdle());
4180 mFakePolicy->assertNotifyAnrWasNotCalled();
4181}
4182
4183// Since the focused window is paused, tapping on it should not produce any events
4184TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
4185 mFocusedWindow->setPaused(true);
4186 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
4187
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004188 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004189 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4190 FOCUSED_WINDOW_LOCATION));
4191
4192 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
4193 ASSERT_TRUE(mDispatcher->waitForIdle());
4194 // Should not ANR because the window is paused, and touches shouldn't go to it
4195 mFakePolicy->assertNotifyAnrWasNotCalled();
4196
4197 mFocusedWindow->assertNoEvents();
4198 mUnfocusedWindow->assertNoEvents();
4199}
4200
4201/**
4202 * If a window is processing a motion event, and then a key event comes in, the key event should
4203 * not to to the focused window until the motion is processed.
4204 * If a different window becomes focused at this time, the key should go to that window instead.
4205 *
4206 * Warning!!!
4207 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
4208 * and the injection timeout that we specify when injecting the key.
4209 * We must have the injection timeout (10ms) be smaller than
4210 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
4211 *
4212 * If that value changes, this test should also change.
4213 */
4214TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
4215 // Set a long ANR timeout to prevent it from triggering
4216 mFocusedWindow->setDispatchingTimeout(2s);
4217 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
4218
4219 tapOnUnfocusedWindow();
4220 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
4221 ASSERT_TRUE(downSequenceNum);
4222 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
4223 ASSERT_TRUE(upSequenceNum);
4224 // Don't finish the events yet, and send a key
4225 // Injection will succeed because we will eventually give up and send the key to the focused
4226 // window even if motions are still being processed.
4227
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004228 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004229 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004230 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
4231 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004232 // Key will not be sent to the window, yet, because the window is still processing events
4233 // and the key remains pending, waiting for the touch events to be processed
4234 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
4235 ASSERT_FALSE(keySequenceNum);
4236
4237 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07004238 mFocusedWindow->setFocusable(false);
4239 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004240 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004241 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004242
4243 // Focus events should precede the key events
4244 mUnfocusedWindow->consumeFocusEvent(true);
4245 mFocusedWindow->consumeFocusEvent(false);
4246
4247 // Finish the tap events, which should unblock dispatcher
4248 mUnfocusedWindow->finishEvent(*downSequenceNum);
4249 mUnfocusedWindow->finishEvent(*upSequenceNum);
4250
4251 // Now that all queues are cleared and no backlog in the connections, the key event
4252 // can finally go to the newly focused "mUnfocusedWindow".
4253 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4254 mFocusedWindow->assertNoEvents();
4255 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004256 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004257}
4258
4259// When the touch stream is split across 2 windows, and one of them does not respond,
4260// then ANR should be raised and the touch should be canceled for the unresponsive window.
4261// The other window should not be affected by that.
4262TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
4263 // Touch Window 1
4264 NotifyMotionArgs motionArgs =
4265 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4266 ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION});
4267 mDispatcher->notifyMotion(&motionArgs);
4268 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
4269 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
4270
4271 // Touch Window 2
4272 int32_t actionPointerDown =
4273 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
4274
4275 motionArgs =
4276 generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4277 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION});
4278 mDispatcher->notifyMotion(&motionArgs);
4279
4280 const std::chrono::duration timeout =
4281 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004282 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004283
4284 mUnfocusedWindow->consumeMotionDown();
4285 mFocusedWindow->consumeMotionDown();
4286 // Focused window may or may not receive ACTION_MOVE
4287 // But it should definitely receive ACTION_CANCEL due to the ANR
4288 InputEvent* event;
4289 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
4290 ASSERT_TRUE(moveOrCancelSequenceNum);
4291 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
4292 ASSERT_NE(nullptr, event);
4293 ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION);
4294 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
4295 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
4296 mFocusedWindow->consumeMotionCancel();
4297 } else {
4298 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
4299 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004300 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00004301 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004302
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004303 mUnfocusedWindow->assertNoEvents();
4304 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05004305 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004306}
4307
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05004308/**
4309 * If we have no focused window, and a key comes in, we start the ANR timer.
4310 * The focused application should add a focused window before the timer runs out to prevent ANR.
4311 *
4312 * If the user touches another application during this time, the key should be dropped.
4313 * Next, if a new focused window comes in, without toggling the focused application,
4314 * then no ANR should occur.
4315 *
4316 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
4317 * but in some cases the policy may not update the focused application.
4318 */
4319TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
4320 std::shared_ptr<FakeApplicationHandle> focusedApplication =
4321 std::make_shared<FakeApplicationHandle>();
4322 focusedApplication->setDispatchingTimeout(60ms);
4323 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
4324 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
4325 mFocusedWindow->setFocusable(false);
4326
4327 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
4328 mFocusedWindow->consumeFocusEvent(false);
4329
4330 // Send a key. The ANR timer should start because there is no focused window.
4331 // 'focusedApplication' will get blamed if this timer completes.
4332 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004333 InputEventInjectionResult result =
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05004334 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08004335 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/,
4336 false /* allowKeyRepeat */);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004337 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05004338
4339 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
4340 // then the injected touches won't cause the focused event to get dropped.
4341 // The dispatcher only checks for whether the queue should be pruned upon queueing.
4342 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
4343 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
4344 // For this test, it means that the key would get delivered to the window once it becomes
4345 // focused.
4346 std::this_thread::sleep_for(10ms);
4347
4348 // Touch unfocused window. This should force the pending key to get dropped.
4349 NotifyMotionArgs motionArgs =
4350 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4351 ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION});
4352 mDispatcher->notifyMotion(&motionArgs);
4353
4354 // We do not consume the motion right away, because that would require dispatcher to first
4355 // process (== drop) the key event, and by that time, ANR will be raised.
4356 // Set the focused window first.
4357 mFocusedWindow->setFocusable(true);
4358 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
4359 setFocusedWindow(mFocusedWindow);
4360 mFocusedWindow->consumeFocusEvent(true);
4361 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
4362 // to another application. This could be a bug / behaviour in the policy.
4363
4364 mUnfocusedWindow->consumeMotionDown();
4365
4366 ASSERT_TRUE(mDispatcher->waitForIdle());
4367 // Should not ANR because we actually have a focused window. It was just added too slowly.
4368 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
4369}
4370
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05004371// These tests ensure we cannot send touch events to a window that's positioned behind a window
4372// that has feature NO_INPUT_CHANNEL.
4373// Layout:
4374// Top (closest to user)
4375// mNoInputWindow (above all windows)
4376// mBottomWindow
4377// Bottom (furthest from user)
4378class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
4379 virtual void SetUp() override {
4380 InputDispatcherTest::SetUp();
4381
4382 mApplication = std::make_shared<FakeApplicationHandle>();
4383 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
4384 "Window without input channel", ADISPLAY_ID_DEFAULT,
4385 std::make_optional<sp<IBinder>>(nullptr) /*token*/);
4386
4387 mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL);
4388 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
4389 // It's perfectly valid for this window to not have an associated input channel
4390
4391 mBottomWindow = new FakeWindowHandle(mApplication, mDispatcher, "Bottom window",
4392 ADISPLAY_ID_DEFAULT);
4393 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
4394
4395 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
4396 }
4397
4398protected:
4399 std::shared_ptr<FakeApplicationHandle> mApplication;
4400 sp<FakeWindowHandle> mNoInputWindow;
4401 sp<FakeWindowHandle> mBottomWindow;
4402};
4403
4404TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
4405 PointF touchedPoint = {10, 10};
4406
4407 NotifyMotionArgs motionArgs =
4408 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4409 ADISPLAY_ID_DEFAULT, {touchedPoint});
4410 mDispatcher->notifyMotion(&motionArgs);
4411
4412 mNoInputWindow->assertNoEvents();
4413 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
4414 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
4415 // and therefore should prevent mBottomWindow from receiving touches
4416 mBottomWindow->assertNoEvents();
4417}
4418
4419/**
4420 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
4421 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
4422 */
4423TEST_F(InputDispatcherMultiWindowOcclusionTests,
4424 NoInputChannelFeature_DropsTouchesWithValidChannel) {
4425 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
4426 "Window with input channel and NO_INPUT_CHANNEL",
4427 ADISPLAY_ID_DEFAULT);
4428
4429 mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL);
4430 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
4431 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
4432
4433 PointF touchedPoint = {10, 10};
4434
4435 NotifyMotionArgs motionArgs =
4436 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4437 ADISPLAY_ID_DEFAULT, {touchedPoint});
4438 mDispatcher->notifyMotion(&motionArgs);
4439
4440 mNoInputWindow->assertNoEvents();
4441 mBottomWindow->assertNoEvents();
4442}
4443
Vishnu Nair958da932020-08-21 17:12:37 -07004444class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
4445protected:
4446 std::shared_ptr<FakeApplicationHandle> mApp;
4447 sp<FakeWindowHandle> mWindow;
4448 sp<FakeWindowHandle> mMirror;
4449
4450 virtual void SetUp() override {
4451 InputDispatcherTest::SetUp();
4452 mApp = std::make_shared<FakeApplicationHandle>();
4453 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
4454 mMirror = new FakeWindowHandle(mApp, mDispatcher, "TestWindowMirror", ADISPLAY_ID_DEFAULT,
4455 mWindow->getToken());
4456 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
4457 mWindow->setFocusable(true);
4458 mMirror->setFocusable(true);
4459 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4460 }
4461};
4462
4463TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
4464 // Request focus on a mirrored window
4465 setFocusedWindow(mMirror);
4466
4467 // window gets focused
4468 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004469 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4470 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004471 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
4472}
4473
4474// A focused & mirrored window remains focused only if the window and its mirror are both
4475// focusable.
4476TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
4477 setFocusedWindow(mMirror);
4478
4479 // window gets focused
4480 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004481 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4482 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004483 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004484 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4485 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004486 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4487
4488 mMirror->setFocusable(false);
4489 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4490
4491 // window loses focus since one of the windows associated with the token in not focusable
4492 mWindow->consumeFocusEvent(false);
4493
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004494 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4495 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004496 mWindow->assertNoEvents();
4497}
4498
4499// A focused & mirrored window remains focused until the window and its mirror both become
4500// invisible.
4501TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
4502 setFocusedWindow(mMirror);
4503
4504 // window gets focused
4505 mWindow->consumeFocusEvent(true);
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 mMirror->setVisible(false);
4514 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4515
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004516 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4517 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004518 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004519 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4520 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004521 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4522
4523 mWindow->setVisible(false);
4524 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4525
4526 // window loses focus only after all windows associated with the token become invisible.
4527 mWindow->consumeFocusEvent(false);
4528
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004529 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4530 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004531 mWindow->assertNoEvents();
4532}
4533
4534// A focused & mirrored window remains focused until both windows are removed.
4535TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
4536 setFocusedWindow(mMirror);
4537
4538 // window gets focused
4539 mWindow->consumeFocusEvent(true);
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 // single window is removed but the window token remains focused
4548 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
4549
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004550 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4551 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004552 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004553 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4554 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004555 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4556
4557 // Both windows are removed
4558 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
4559 mWindow->consumeFocusEvent(false);
4560
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004561 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4562 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004563 mWindow->assertNoEvents();
4564}
4565
4566// Focus request can be pending until one window becomes visible.
4567TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
4568 // Request focus on an invisible mirror.
4569 mWindow->setVisible(false);
4570 mMirror->setVisible(false);
4571 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4572 setFocusedWindow(mMirror);
4573
4574 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004575 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07004576 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004577 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07004578
4579 mMirror->setVisible(true);
4580 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4581
4582 // window gets focused
4583 mWindow->consumeFocusEvent(true);
4584 // window gets the pending key event
4585 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4586}
Prabir Pradhan99987712020-11-10 18:43:05 -08004587
4588class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
4589protected:
4590 std::shared_ptr<FakeApplicationHandle> mApp;
4591 sp<FakeWindowHandle> mWindow;
4592 sp<FakeWindowHandle> mSecondWindow;
4593
4594 void SetUp() override {
4595 InputDispatcherTest::SetUp();
4596 mApp = std::make_shared<FakeApplicationHandle>();
4597 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
4598 mWindow->setFocusable(true);
4599 mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
4600 mSecondWindow->setFocusable(true);
4601
4602 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
4603 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
4604
4605 setFocusedWindow(mWindow);
4606 mWindow->consumeFocusEvent(true);
4607 }
4608
Prabir Pradhanf192a102021-08-06 14:01:18 +00004609 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
4610 const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08004611 mDispatcher->notifyPointerCaptureChanged(&args);
4612 }
4613
Prabir Pradhanf192a102021-08-06 14:01:18 +00004614 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
4615 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08004616 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhanf192a102021-08-06 14:01:18 +00004617 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
4618 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08004619 window->consumeCaptureEvent(enabled);
Prabir Pradhanf192a102021-08-06 14:01:18 +00004620 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08004621 }
4622};
4623
4624TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
4625 // Ensure that capture cannot be obtained for unfocused windows.
4626 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
4627 mFakePolicy->assertSetPointerCaptureNotCalled();
4628 mSecondWindow->assertNoEvents();
4629
4630 // Ensure that capture can be enabled from the focus window.
4631 requestAndVerifyPointerCapture(mWindow, true);
4632
4633 // Ensure that capture cannot be disabled from a window that does not have capture.
4634 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
4635 mFakePolicy->assertSetPointerCaptureNotCalled();
4636
4637 // Ensure that capture can be disabled from the window with capture.
4638 requestAndVerifyPointerCapture(mWindow, false);
4639}
4640
4641TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhanf192a102021-08-06 14:01:18 +00004642 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08004643
4644 setFocusedWindow(mSecondWindow);
4645
4646 // Ensure that the capture disabled event was sent first.
4647 mWindow->consumeCaptureEvent(false);
4648 mWindow->consumeFocusEvent(false);
4649 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhanf192a102021-08-06 14:01:18 +00004650 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08004651
4652 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhanf192a102021-08-06 14:01:18 +00004653 notifyPointerCaptureChanged({});
4654 notifyPointerCaptureChanged(request);
4655 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08004656 mWindow->assertNoEvents();
4657 mSecondWindow->assertNoEvents();
4658 mFakePolicy->assertSetPointerCaptureNotCalled();
4659}
4660
4661TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhanf192a102021-08-06 14:01:18 +00004662 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08004663
4664 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhanf192a102021-08-06 14:01:18 +00004665 notifyPointerCaptureChanged({});
4666 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08004667
4668 // Ensure that Pointer Capture is disabled.
Prabir Pradhanf192a102021-08-06 14:01:18 +00004669 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08004670 mWindow->consumeCaptureEvent(false);
4671 mWindow->assertNoEvents();
4672}
4673
Prabir Pradhan167e6d92021-02-04 16:18:17 -08004674TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
4675 requestAndVerifyPointerCapture(mWindow, true);
4676
4677 // The first window loses focus.
4678 setFocusedWindow(mSecondWindow);
Prabir Pradhanf192a102021-08-06 14:01:18 +00004679 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08004680 mWindow->consumeCaptureEvent(false);
4681
4682 // Request Pointer Capture from the second window before the notification from InputReader
4683 // arrives.
4684 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhanf192a102021-08-06 14:01:18 +00004685 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08004686
4687 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhanf192a102021-08-06 14:01:18 +00004688 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08004689
4690 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhanf192a102021-08-06 14:01:18 +00004691 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08004692
4693 mSecondWindow->consumeFocusEvent(true);
4694 mSecondWindow->consumeCaptureEvent(true);
4695}
4696
Prabir Pradhanf192a102021-08-06 14:01:18 +00004697TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
4698 // App repeatedly enables and disables capture.
4699 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
4700 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
4701 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
4702 mFakePolicy->assertSetPointerCaptureCalled(false);
4703 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
4704 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
4705
4706 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
4707 // first request is now stale, this should do nothing.
4708 notifyPointerCaptureChanged(firstRequest);
4709 mWindow->assertNoEvents();
4710
4711 // InputReader notifies that the second request was enabled.
4712 notifyPointerCaptureChanged(secondRequest);
4713 mWindow->consumeCaptureEvent(true);
4714}
4715
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004716class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
4717protected:
4718 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00004719
4720 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
4721 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
4722
4723 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
4724 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
4725
4726 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
4727 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
4728 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
4729 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
4730 MAXIMUM_OBSCURING_OPACITY);
4731
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004732 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004733 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004734 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004735
4736 sp<FakeWindowHandle> mTouchWindow;
4737
4738 virtual void SetUp() override {
4739 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004740 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004741 mDispatcher->setBlockUntrustedTouchesMode(android::os::BlockUntrustedTouchesMode::BLOCK);
4742 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
4743 }
4744
4745 virtual void TearDown() override {
4746 InputDispatcherTest::TearDown();
4747 mTouchWindow.clear();
4748 }
4749
4750 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name,
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004751 os::TouchOcclusionMode mode, float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004752 sp<FakeWindowHandle> window = getWindow(uid, name);
4753 window->setFlags(InputWindowInfo::Flag::NOT_TOUCHABLE);
4754 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004755 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004756 return window;
4757 }
4758
4759 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
4760 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
4761 sp<FakeWindowHandle> window =
4762 new FakeWindowHandle(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
4763 // Generate an arbitrary PID based on the UID
4764 window->setOwnerInfo(1777 + (uid % 10000), uid);
4765 return window;
4766 }
4767
4768 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
4769 NotifyMotionArgs args =
4770 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4771 ADISPLAY_ID_DEFAULT, points);
4772 mDispatcher->notifyMotion(&args);
4773 }
4774};
4775
4776TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004777 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004778 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004779 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004780
4781 touch();
4782
4783 mTouchWindow->assertNoEvents();
4784}
4785
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004786TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00004787 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
4788 const sp<FakeWindowHandle>& w =
4789 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
4790 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4791
4792 touch();
4793
4794 mTouchWindow->assertNoEvents();
4795}
4796
4797TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004798 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
4799 const sp<FakeWindowHandle>& w =
4800 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
4801 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4802
4803 touch();
4804
4805 w->assertNoEvents();
4806}
4807
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004808TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004809 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
4810 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004811
4812 touch();
4813
4814 mTouchWindow->consumeAnyMotionDown();
4815}
4816
4817TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004818 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004819 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004820 w->setFrame(Rect(0, 0, 50, 50));
4821 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004822
4823 touch({PointF{100, 100}});
4824
4825 mTouchWindow->consumeAnyMotionDown();
4826}
4827
4828TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004829 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004830 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004831 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4832
4833 touch();
4834
4835 mTouchWindow->consumeAnyMotionDown();
4836}
4837
4838TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
4839 const sp<FakeWindowHandle>& w =
4840 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4841 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004842
4843 touch();
4844
4845 mTouchWindow->consumeAnyMotionDown();
4846}
4847
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004848TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
4849 const sp<FakeWindowHandle>& w =
4850 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4851 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4852
4853 touch();
4854
4855 w->assertNoEvents();
4856}
4857
4858/**
4859 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
4860 * inside) while letting them pass-through. Note that even though touch passes through the occluding
4861 * window, the occluding window will still receive ACTION_OUTSIDE event.
4862 */
4863TEST_F(InputDispatcherUntrustedTouchesTest,
4864 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
4865 const sp<FakeWindowHandle>& w =
4866 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4867 w->addFlags(InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH);
4868 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4869
4870 touch();
4871
4872 w->consumeMotionOutside();
4873}
4874
4875TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
4876 const sp<FakeWindowHandle>& w =
4877 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4878 w->addFlags(InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH);
4879 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4880
4881 touch();
4882
4883 InputEvent* event = w->consume();
4884 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType());
4885 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
4886 EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getX());
4887 EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getY());
4888}
4889
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004890TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004891 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004892 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4893 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004894 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4895
4896 touch();
4897
4898 mTouchWindow->consumeAnyMotionDown();
4899}
4900
4901TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
4902 const sp<FakeWindowHandle>& w =
4903 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4904 MAXIMUM_OBSCURING_OPACITY);
4905 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004906
4907 touch();
4908
4909 mTouchWindow->consumeAnyMotionDown();
4910}
4911
4912TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004913 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004914 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4915 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004916 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4917
4918 touch();
4919
4920 mTouchWindow->assertNoEvents();
4921}
4922
4923TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
4924 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
4925 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004926 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
4927 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004928 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004929 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
4930 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004931 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
4932
4933 touch();
4934
4935 mTouchWindow->assertNoEvents();
4936}
4937
4938TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
4939 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
4940 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004941 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
4942 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004943 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004944 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
4945 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004946 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
4947
4948 touch();
4949
4950 mTouchWindow->consumeAnyMotionDown();
4951}
4952
4953TEST_F(InputDispatcherUntrustedTouchesTest,
4954 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
4955 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004956 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4957 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004958 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004959 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
4960 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004961 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
4962
4963 touch();
4964
4965 mTouchWindow->consumeAnyMotionDown();
4966}
4967
4968TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
4969 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004970 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4971 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004972 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004973 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
4974 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004975 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004976
4977 touch();
4978
4979 mTouchWindow->assertNoEvents();
4980}
4981
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004982TEST_F(InputDispatcherUntrustedTouchesTest,
4983 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
4984 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004985 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
4986 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004987 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004988 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4989 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004990 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
4991
4992 touch();
4993
4994 mTouchWindow->assertNoEvents();
4995}
4996
4997TEST_F(InputDispatcherUntrustedTouchesTest,
4998 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
4999 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005000 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
5001 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00005002 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005003 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
5004 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00005005 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
5006
5007 touch();
5008
5009 mTouchWindow->consumeAnyMotionDown();
5010}
5011
5012TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
5013 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005014 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
5015 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00005016 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5017
5018 touch();
5019
5020 mTouchWindow->consumeAnyMotionDown();
5021}
5022
5023TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
5024 const sp<FakeWindowHandle>& w =
5025 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
5026 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5027
5028 touch();
5029
5030 mTouchWindow->consumeAnyMotionDown();
5031}
5032
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00005033TEST_F(InputDispatcherUntrustedTouchesTest,
5034 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
5035 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
5036 const sp<FakeWindowHandle>& w =
5037 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
5038 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5039
5040 touch();
5041
5042 mTouchWindow->assertNoEvents();
5043}
5044
5045TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
5046 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
5047 const sp<FakeWindowHandle>& w =
5048 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
5049 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5050
5051 touch();
5052
5053 mTouchWindow->consumeAnyMotionDown();
5054}
5055
5056TEST_F(InputDispatcherUntrustedTouchesTest,
5057 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
5058 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
5059 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00005060 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
5061 OPACITY_ABOVE_THRESHOLD);
5062 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5063
5064 touch();
5065
5066 mTouchWindow->consumeAnyMotionDown();
5067}
5068
5069TEST_F(InputDispatcherUntrustedTouchesTest,
5070 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
5071 const sp<FakeWindowHandle>& w1 =
5072 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
5073 OPACITY_BELOW_THRESHOLD);
5074 const sp<FakeWindowHandle>& w2 =
5075 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
5076 OPACITY_BELOW_THRESHOLD);
5077 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
5078
5079 touch();
5080
5081 mTouchWindow->assertNoEvents();
5082}
5083
5084/**
5085 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
5086 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
5087 * (which alone would result in allowing touches) does not affect the blocking behavior.
5088 */
5089TEST_F(InputDispatcherUntrustedTouchesTest,
5090 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
5091 const sp<FakeWindowHandle>& wB =
5092 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
5093 OPACITY_BELOW_THRESHOLD);
5094 const sp<FakeWindowHandle>& wC =
5095 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
5096 OPACITY_BELOW_THRESHOLD);
5097 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
5098
5099 touch();
5100
5101 mTouchWindow->assertNoEvents();
5102}
5103
5104/**
5105 * This test is testing that a window from a different UID but with same application token doesn't
5106 * block the touch. Apps can share the application token for close UI collaboration for example.
5107 */
5108TEST_F(InputDispatcherUntrustedTouchesTest,
5109 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
5110 const sp<FakeWindowHandle>& w =
5111 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
5112 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00005113 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
5114
5115 touch();
5116
5117 mTouchWindow->consumeAnyMotionDown();
5118}
5119
arthurhungb89ccb02020-12-30 16:19:01 +08005120class InputDispatcherDragTests : public InputDispatcherTest {
5121protected:
5122 std::shared_ptr<FakeApplicationHandle> mApp;
5123 sp<FakeWindowHandle> mWindow;
5124 sp<FakeWindowHandle> mSecondWindow;
5125 sp<FakeWindowHandle> mDragWindow;
5126
5127 void SetUp() override {
5128 InputDispatcherTest::SetUp();
5129 mApp = std::make_shared<FakeApplicationHandle>();
5130 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
5131 mWindow->setFrame(Rect(0, 0, 100, 100));
5132 mWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
5133
5134 mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
5135 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
5136 mSecondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
5137
5138 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
5139 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
5140 }
5141
5142 // Start performing drag, we will create a drag window and transfer touch to it.
5143 void performDrag() {
5144 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5145 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5146 {50, 50}))
5147 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5148
5149 // Window should receive motion event.
5150 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5151
5152 // The drag window covers the entire display
5153 mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
5154 mDispatcher->setInputWindows(
5155 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
5156
5157 // Transfer touch focus to the drag window
5158 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
5159 true /* isDragDrop */);
5160 mWindow->consumeMotionCancel();
5161 mDragWindow->consumeMotionDown();
5162 }
arthurhung6d4bed92021-03-17 11:59:33 +08005163
5164 // Start performing drag, we will create a drag window and transfer touch to it.
5165 void performStylusDrag() {
5166 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5167 injectMotionEvent(mDispatcher,
5168 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
5169 AINPUT_SOURCE_STYLUS)
5170 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
5171 .pointer(PointerBuilder(0,
5172 AMOTION_EVENT_TOOL_TYPE_STYLUS)
5173 .x(50)
5174 .y(50))
5175 .build()));
5176 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5177
5178 // The drag window covers the entire display
5179 mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
5180 mDispatcher->setInputWindows(
5181 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
5182
5183 // Transfer touch focus to the drag window
5184 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
5185 true /* isDragDrop */);
5186 mWindow->consumeMotionCancel();
5187 mDragWindow->consumeMotionDown();
5188 }
arthurhungb89ccb02020-12-30 16:19:01 +08005189};
5190
5191TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
5192 performDrag();
5193
5194 // Move on window.
5195 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5196 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5197 ADISPLAY_ID_DEFAULT, {50, 50}))
5198 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5199 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5200 mWindow->consumeDragEvent(false, 50, 50);
5201 mSecondWindow->assertNoEvents();
5202
5203 // Move to another window.
5204 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5205 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5206 ADISPLAY_ID_DEFAULT, {150, 50}))
5207 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5208 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5209 mWindow->consumeDragEvent(true, 150, 50);
5210 mSecondWindow->consumeDragEvent(false, 50, 50);
5211
5212 // Move back to original window.
5213 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5214 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5215 ADISPLAY_ID_DEFAULT, {50, 50}))
5216 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5217 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5218 mWindow->consumeDragEvent(false, 50, 50);
5219 mSecondWindow->consumeDragEvent(true, -50, 50);
5220
5221 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5222 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
5223 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5224 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5225 mWindow->assertNoEvents();
5226 mSecondWindow->assertNoEvents();
5227}
5228
arthurhungf452d0b2021-01-06 00:19:52 +08005229TEST_F(InputDispatcherDragTests, DragAndDrop) {
5230 performDrag();
5231
5232 // Move on window.
5233 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5234 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5235 ADISPLAY_ID_DEFAULT, {50, 50}))
5236 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5237 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5238 mWindow->consumeDragEvent(false, 50, 50);
5239 mSecondWindow->assertNoEvents();
5240
5241 // Move to another window.
5242 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5243 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5244 ADISPLAY_ID_DEFAULT, {150, 50}))
5245 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5246 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5247 mWindow->consumeDragEvent(true, 150, 50);
5248 mSecondWindow->consumeDragEvent(false, 50, 50);
5249
5250 // drop to another window.
5251 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5252 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5253 {150, 50}))
5254 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5255 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5256 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
5257 mWindow->assertNoEvents();
5258 mSecondWindow->assertNoEvents();
5259}
5260
arthurhung6d4bed92021-03-17 11:59:33 +08005261TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
5262 performStylusDrag();
5263
5264 // Move on window and keep button pressed.
5265 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5266 injectMotionEvent(mDispatcher,
5267 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
5268 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
5269 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
5270 .x(50)
5271 .y(50))
5272 .build()))
5273 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5274 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5275 mWindow->consumeDragEvent(false, 50, 50);
5276 mSecondWindow->assertNoEvents();
5277
5278 // Move to another window and release button, expect to drop item.
5279 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5280 injectMotionEvent(mDispatcher,
5281 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
5282 .buttonState(0)
5283 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
5284 .x(150)
5285 .y(50))
5286 .build()))
5287 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5288 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5289 mWindow->assertNoEvents();
5290 mSecondWindow->assertNoEvents();
5291 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
5292
5293 // nothing to the window.
5294 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5295 injectMotionEvent(mDispatcher,
5296 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
5297 .buttonState(0)
5298 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS)
5299 .x(150)
5300 .y(50))
5301 .build()))
5302 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5303 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5304 mWindow->assertNoEvents();
5305 mSecondWindow->assertNoEvents();
5306}
5307
Arthur Hung6d0571e2021-04-09 20:18:16 +08005308TEST_F(InputDispatcherDragTests, DragAndDrop_InvalidWindow) {
5309 performDrag();
5310
5311 // Set second window invisible.
5312 mSecondWindow->setVisible(false);
5313 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
5314
5315 // Move on window.
5316 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5317 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5318 ADISPLAY_ID_DEFAULT, {50, 50}))
5319 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5320 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5321 mWindow->consumeDragEvent(false, 50, 50);
5322 mSecondWindow->assertNoEvents();
5323
5324 // Move to another window.
5325 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5326 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5327 ADISPLAY_ID_DEFAULT, {150, 50}))
5328 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5329 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
5330 mWindow->consumeDragEvent(true, 150, 50);
5331 mSecondWindow->assertNoEvents();
5332
5333 // drop to another window.
5334 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5335 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5336 {150, 50}))
5337 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5338 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
5339 mFakePolicy->assertDropTargetEquals(nullptr);
5340 mWindow->assertNoEvents();
5341 mSecondWindow->assertNoEvents();
5342}
5343
Garfield Tane84e6f92019-08-29 17:28:41 -07005344} // namespace android::inputdispatcher