blob: 8456387b589c2118f4bfefec43d396900e8df55e [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Garfield Tan0fc2fa72019-08-29 17:22:15 -070017#include "../dispatcher/InputDispatcher.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080018
Garfield Tan1c7bc862020-01-28 13:24:04 -080019#include <android-base/stringprintf.h>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070020#include <android-base/thread_annotations.h>
Robert Carr803535b2018-08-02 16:38:15 -070021#include <binder/Binder.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080022#include <gtest/gtest.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100023#include <input/Input.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080024#include <linux/input.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100025
Garfield Tan1c7bc862020-01-28 13:24:04 -080026#include <cinttypes>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070027#include <thread>
Garfield Tan1c7bc862020-01-28 13:24:04 -080028#include <unordered_set>
chaviwd1c23182019-12-20 18:44:56 -080029#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080030
Garfield Tan1c7bc862020-01-28 13:24:04 -080031using android::base::StringPrintf;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080032using android::os::InputEventInjectionResult;
33using android::os::InputEventInjectionSync;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000034using android::os::TouchOcclusionMode;
Michael Wright44753b12020-07-08 13:48:11 +010035using namespace android::flag_operators;
Garfield Tan1c7bc862020-01-28 13:24:04 -080036
Garfield Tane84e6f92019-08-29 17:28:41 -070037namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080038
39// An arbitrary time value.
40static const nsecs_t ARBITRARY_TIME = 1234;
41
42// An arbitrary device id.
43static const int32_t DEVICE_ID = 1;
44
Jeff Brownf086ddb2014-02-11 14:28:48 -080045// An arbitrary display id.
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -080046static const int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
Jeff Brownf086ddb2014-02-11 14:28:48 -080047
Michael Wrightd02c5b62014-02-10 15:10:22 -080048// An arbitrary injector pid / uid pair that has permission to inject events.
49static const int32_t INJECTOR_PID = 999;
50static const int32_t INJECTOR_UID = 1001;
51
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +000052// An arbitrary pid of the gesture monitor window
53static constexpr int32_t MONITOR_PID = 2001;
54
chaviwd1c23182019-12-20 18:44:56 -080055struct PointF {
56 float x;
57 float y;
58};
Michael Wrightd02c5b62014-02-10 15:10:22 -080059
Gang Wang342c9272020-01-13 13:15:04 -050060/**
61 * Return a DOWN key event with KEYCODE_A.
62 */
63static KeyEvent getTestKeyEvent() {
64 KeyEvent event;
65
Garfield Tanfbe732e2020-01-24 11:26:14 -080066 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
67 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
68 ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -050069 return event;
70}
71
Michael Wrightd02c5b62014-02-10 15:10:22 -080072// --- FakeInputDispatcherPolicy ---
73
74class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
75 InputDispatcherConfiguration mConfig;
76
77protected:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100078 virtual ~FakeInputDispatcherPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -080079
80public:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100081 FakeInputDispatcherPolicy() {}
Jackal Guof9696682018-10-05 12:23:23 +080082
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080083 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080084 assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_KEY, args.eventTime, args.action,
85 args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080086 }
87
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080088 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args) {
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080089 assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_MOTION, args.eventTime, args.action,
90 args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080091 }
92
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -070093 void assertFilterInputEventWasNotCalled() {
94 std::scoped_lock lock(mLock);
95 ASSERT_EQ(nullptr, mFilteredEvent);
96 }
Michael Wrightd02c5b62014-02-10 15:10:22 -080097
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -080098 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -070099 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800100 ASSERT_TRUE(mConfigurationChangedTime)
101 << "Timed out waiting for configuration changed call";
102 ASSERT_EQ(*mConfigurationChangedTime, when);
103 mConfigurationChangedTime = std::nullopt;
104 }
105
106 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700107 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800108 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800109 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800110 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
111 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
112 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
113 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
114 mLastNotifySwitch = std::nullopt;
115 }
116
chaviwfd6d3512019-03-25 13:23:49 -0700117 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700118 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800119 ASSERT_EQ(touchedToken, mOnPointerDownToken);
120 mOnPointerDownToken.clear();
121 }
122
123 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700124 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800125 ASSERT_TRUE(mOnPointerDownToken == nullptr)
126 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700127 }
128
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700129 // This function must be called soon after the expected ANR timer starts,
130 // because we are also checking how much time has passed.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500131 void assertNotifyNoFocusedWindowAnrWasCalled(
Chris Yea209fde2020-07-22 13:54:51 -0700132 std::chrono::nanoseconds timeout,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500133 const std::shared_ptr<InputApplicationHandle>& expectedApplication) {
134 std::shared_ptr<InputApplicationHandle> application;
135 { // acquire lock
136 std::unique_lock lock(mLock);
137 android::base::ScopedLockAssertion assumeLocked(mLock);
138 ASSERT_NO_FATAL_FAILURE(
139 application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock));
140 } // release lock
141 ASSERT_EQ(expectedApplication, application);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700142 }
143
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000144 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
145 const sp<IBinder>& expectedConnectionToken) {
146 sp<IBinder> connectionToken = getUnresponsiveWindowToken(timeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500147 ASSERT_EQ(expectedConnectionToken, connectionToken);
148 }
149
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000150 void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedConnectionToken) {
151 sp<IBinder> connectionToken = getResponsiveWindowToken();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500152 ASSERT_EQ(expectedConnectionToken, connectionToken);
153 }
154
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000155 void assertNotifyMonitorUnresponsiveWasCalled(std::chrono::nanoseconds timeout) {
156 int32_t pid = getUnresponsiveMonitorPid(timeout);
157 ASSERT_EQ(MONITOR_PID, pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500158 }
159
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000160 void assertNotifyMonitorResponsiveWasCalled() {
161 int32_t pid = getResponsiveMonitorPid();
162 ASSERT_EQ(MONITOR_PID, pid);
163 }
164
165 sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500166 std::unique_lock lock(mLock);
167 android::base::ScopedLockAssertion assumeLocked(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000168 return getAnrTokenLockedInterruptible(timeout, mAnrWindowTokens, lock);
169 }
170
171 sp<IBinder> getResponsiveWindowToken() {
172 std::unique_lock lock(mLock);
173 android::base::ScopedLockAssertion assumeLocked(mLock);
174 return getAnrTokenLockedInterruptible(0s, mResponsiveWindowTokens, lock);
175 }
176
177 int32_t getUnresponsiveMonitorPid(std::chrono::nanoseconds timeout) {
178 std::unique_lock lock(mLock);
179 android::base::ScopedLockAssertion assumeLocked(mLock);
180 return getAnrTokenLockedInterruptible(timeout, mAnrMonitorPids, lock);
181 }
182
183 int32_t getResponsiveMonitorPid() {
184 std::unique_lock lock(mLock);
185 android::base::ScopedLockAssertion assumeLocked(mLock);
186 return getAnrTokenLockedInterruptible(0s, mResponsiveMonitorPids, lock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500187 }
188
189 // All three ANR-related callbacks behave the same way, so we use this generic function to wait
190 // for a specific container to become non-empty. When the container is non-empty, return the
191 // first entry from the container and erase it.
192 template <class T>
193 T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage,
194 std::unique_lock<std::mutex>& lock) REQUIRES(mLock) {
195 const std::chrono::time_point start = std::chrono::steady_clock::now();
196 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700197
198 // If there is an ANR, Dispatcher won't be idle because there are still events
199 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
200 // before checking if ANR was called.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500201 // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need
202 // to provide it some time to act. 100ms seems reasonable.
203 mNotifyAnr.wait_for(lock, timeToWait,
204 [&storage]() REQUIRES(mLock) { return !storage.empty(); });
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700205 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500206 if (storage.empty()) {
207 ADD_FAILURE() << "Did not receive the ANR callback";
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000208 return {};
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700209 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700210 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
211 // the dispatcher started counting before this function was called
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700212 if (std::chrono::abs(timeout - waited) > 100ms) {
213 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
214 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
215 << "ms, but waited "
216 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
217 << "ms instead";
218 }
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500219 T token = storage.front();
220 storage.pop();
221 return token;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700222 }
223
224 void assertNotifyAnrWasNotCalled() {
225 std::scoped_lock lock(mLock);
226 ASSERT_TRUE(mAnrApplications.empty());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000227 ASSERT_TRUE(mAnrWindowTokens.empty());
228 ASSERT_TRUE(mAnrMonitorPids.empty());
229 ASSERT_TRUE(mResponsiveWindowTokens.empty())
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500230 << "ANR was not called, but please also consume the 'connection is responsive' "
231 "signal";
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000232 ASSERT_TRUE(mResponsiveMonitorPids.empty())
233 << "Monitor ANR was not called, but please also consume the 'monitor is responsive'"
234 " signal";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700235 }
236
Garfield Tan1c7bc862020-01-28 13:24:04 -0800237 void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) {
238 mConfig.keyRepeatTimeout = timeout;
239 mConfig.keyRepeatDelay = delay;
240 }
241
Prabir Pradhan99987712020-11-10 18:43:05 -0800242 void waitForSetPointerCapture(bool enabled) {
243 std::unique_lock lock(mLock);
244 base::ScopedLockAssertion assumeLocked(mLock);
245
246 if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms,
247 [this, enabled]() REQUIRES(mLock) {
248 return mPointerCaptureEnabled &&
249 *mPointerCaptureEnabled ==
250 enabled;
251 })) {
252 FAIL() << "Timed out waiting for setPointerCapture(" << enabled << ") to be called.";
253 }
254 mPointerCaptureEnabled.reset();
255 }
256
257 void assertSetPointerCaptureNotCalled() {
258 std::unique_lock lock(mLock);
259 base::ScopedLockAssertion assumeLocked(mLock);
260
261 if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) {
262 FAIL() << "Expected setPointerCapture(enabled) to not be called, but was called. "
263 "enabled = "
264 << *mPointerCaptureEnabled;
265 }
266 mPointerCaptureEnabled.reset();
267 }
268
Michael Wrightd02c5b62014-02-10 15:10:22 -0800269private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700270 std::mutex mLock;
271 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
272 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
273 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
274 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800275
Prabir Pradhan99987712020-11-10 18:43:05 -0800276 std::condition_variable mPointerCaptureChangedCondition;
277 std::optional<bool> mPointerCaptureEnabled GUARDED_BY(mLock);
278
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700279 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700280 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000281 std::queue<sp<IBinder>> mAnrWindowTokens GUARDED_BY(mLock);
282 std::queue<sp<IBinder>> mResponsiveWindowTokens GUARDED_BY(mLock);
283 std::queue<int32_t> mAnrMonitorPids GUARDED_BY(mLock);
284 std::queue<int32_t> mResponsiveMonitorPids GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700285 std::condition_variable mNotifyAnr;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700286
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600287 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700288 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800289 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800290 }
291
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000292 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700293 std::scoped_lock lock(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000294 mAnrWindowTokens.push(connectionToken);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700295 mNotifyAnr.notify_all();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500296 }
297
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000298 void notifyMonitorUnresponsive(int32_t pid, const std::string&) override {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500299 std::scoped_lock lock(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000300 mAnrMonitorPids.push(pid);
301 mNotifyAnr.notify_all();
302 }
303
304 void notifyWindowResponsive(const sp<IBinder>& connectionToken) override {
305 std::scoped_lock lock(mLock);
306 mResponsiveWindowTokens.push(connectionToken);
307 mNotifyAnr.notify_all();
308 }
309
310 void notifyMonitorResponsive(int32_t pid) override {
311 std::scoped_lock lock(mLock);
312 mResponsiveMonitorPids.push(pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500313 mNotifyAnr.notify_all();
314 }
315
316 void notifyNoFocusedWindowAnr(
317 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
318 std::scoped_lock lock(mLock);
319 mAnrApplications.push(applicationHandle);
320 mNotifyAnr.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800321 }
322
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600323 void notifyInputChannelBroken(const sp<IBinder>&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800324
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600325 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700326
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600327 void notifyUntrustedTouch(const std::string& obscuringPackage) override {}
Chris Yef59a2f42020-10-16 12:55:26 -0700328 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
329 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
330 const std::vector<float>& values) override {}
331
332 void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType,
333 InputDeviceSensorAccuracy accuracy) override {}
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000334
Chris Yefb552902021-02-03 17:18:37 -0800335 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
336
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600337 void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338 *outConfig = mConfig;
339 }
340
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600341 bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700342 std::scoped_lock lock(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800343 switch (inputEvent->getType()) {
344 case AINPUT_EVENT_TYPE_KEY: {
345 const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800346 mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800347 break;
348 }
349
350 case AINPUT_EVENT_TYPE_MOTION: {
351 const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800352 mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800353 break;
354 }
355 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800356 return true;
357 }
358
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600359 void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800360
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600361 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800362
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600363 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800364 return 0;
365 }
366
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600367 bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800368 return false;
369 }
370
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600371 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
372 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700373 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800374 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
375 * essentially a passthrough for notifySwitch.
376 */
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800377 mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800378 }
379
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600380 void pokeUserActivity(nsecs_t, int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800381
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600382 bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) override { return false; }
Jackal Guof9696682018-10-05 12:23:23 +0800383
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600384 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700385 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700386 mOnPointerDownToken = newToken;
387 }
388
Prabir Pradhan99987712020-11-10 18:43:05 -0800389 void setPointerCapture(bool enabled) override {
390 std::scoped_lock lock(mLock);
391 mPointerCaptureEnabled = {enabled};
392 mPointerCaptureChangedCondition.notify_all();
393 }
394
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800395 void assertFilterInputEventWasCalled(int type, nsecs_t eventTime, int32_t action,
396 int32_t displayId) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700397 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800398 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
399 ASSERT_EQ(mFilteredEvent->getType(), type);
400
401 if (type == AINPUT_EVENT_TYPE_KEY) {
402 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*mFilteredEvent);
403 EXPECT_EQ(keyEvent.getEventTime(), eventTime);
404 EXPECT_EQ(keyEvent.getAction(), action);
405 EXPECT_EQ(keyEvent.getDisplayId(), displayId);
406 } else if (type == AINPUT_EVENT_TYPE_MOTION) {
407 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*mFilteredEvent);
408 EXPECT_EQ(motionEvent.getEventTime(), eventTime);
409 EXPECT_EQ(motionEvent.getAction(), action);
410 EXPECT_EQ(motionEvent.getDisplayId(), displayId);
411 } else {
412 FAIL() << "Unknown type: " << type;
413 }
414
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800415 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800416 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800417};
418
Michael Wrightd02c5b62014-02-10 15:10:22 -0800419// --- InputDispatcherTest ---
420
421class InputDispatcherTest : public testing::Test {
422protected:
423 sp<FakeInputDispatcherPolicy> mFakePolicy;
424 sp<InputDispatcher> mDispatcher;
425
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700426 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800427 mFakePolicy = new FakeInputDispatcherPolicy();
428 mDispatcher = new InputDispatcher(mFakePolicy);
Arthur Hungb92218b2018-08-14 12:00:21 +0800429 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000430 // Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700431 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800432 }
433
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700434 virtual void TearDown() override {
435 ASSERT_EQ(OK, mDispatcher->stop());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800436 mFakePolicy.clear();
437 mDispatcher.clear();
438 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700439
440 /**
441 * Used for debugging when writing the test
442 */
443 void dumpDispatcherState() {
444 std::string dump;
445 mDispatcher->dump(dump);
446 std::stringstream ss(dump);
447 std::string to;
448
449 while (std::getline(ss, to, '\n')) {
450 ALOGE("%s", to.c_str());
451 }
452 }
Vishnu Nair958da932020-08-21 17:12:37 -0700453
454 void setFocusedWindow(const sp<InputWindowHandle>& window,
455 const sp<InputWindowHandle>& focusedWindow = nullptr) {
456 FocusRequest request;
457 request.token = window->getToken();
458 if (focusedWindow) {
459 request.focusedToken = focusedWindow->getToken();
460 }
461 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
462 request.displayId = window->getInfo()->displayId;
463 mDispatcher->setFocusedWindow(request);
464 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800465};
466
Michael Wrightd02c5b62014-02-10 15:10:22 -0800467TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
468 KeyEvent event;
469
470 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800471 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
472 INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600473 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
474 ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800475 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700476 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800477 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800478 << "Should reject key events with undefined action.";
479
480 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800481 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
482 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600483 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800484 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700485 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800486 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800487 << "Should reject key events with ACTION_MULTIPLE.";
488}
489
490TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
491 MotionEvent event;
492 PointerProperties pointerProperties[MAX_POINTERS + 1];
493 PointerCoords pointerCoords[MAX_POINTERS + 1];
494 for (int i = 0; i <= MAX_POINTERS; i++) {
495 pointerProperties[i].clear();
496 pointerProperties[i].id = i;
497 pointerCoords[i].clear();
498 }
499
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800500 // Some constants commonly used below
501 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
502 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
503 constexpr int32_t metaState = AMETA_NONE;
504 constexpr MotionClassification classification = MotionClassification::NONE;
505
chaviw9eaa22c2020-07-01 16:21:27 -0700506 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800507 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800508 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
chaviw9eaa22c2020-07-01 16:21:27 -0700509 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification,
510 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600511 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700512 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800513 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700514 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800515 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800516 << "Should reject motion events with undefined action.";
517
518 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800519 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700520 AMOTION_EVENT_ACTION_POINTER_DOWN |
521 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700522 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
523 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
524 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
525 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800526 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700527 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800528 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800529 << "Should reject motion events with pointer down index too large.";
530
Garfield Tanfbe732e2020-01-24 11:26:14 -0800531 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700532 AMOTION_EVENT_ACTION_POINTER_DOWN |
533 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700534 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
535 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
536 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
537 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800538 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700539 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800540 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800541 << "Should reject motion events with pointer down index too small.";
542
543 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800544 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700545 AMOTION_EVENT_ACTION_POINTER_UP |
546 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700547 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
548 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
549 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 up 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_UP |
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,
561 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
562 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800563 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700564 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800565 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800566 << "Should reject motion events with pointer up index too small.";
567
568 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800569 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
570 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700571 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
572 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700573 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800574 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700575 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800576 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800577 << "Should reject motion events with 0 pointers.";
578
Garfield Tanfbe732e2020-01-24 11:26:14 -0800579 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
580 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700581 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
582 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700583 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800584 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700585 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800586 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800587 << "Should reject motion events with more than MAX_POINTERS pointers.";
588
589 // Rejects motion events with invalid pointer ids.
590 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800591 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
592 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700593 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
594 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700595 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800596 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700597 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800598 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800599 << "Should reject motion events with pointer ids less than 0.";
600
601 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800602 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
603 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700604 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
605 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700606 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800607 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700608 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800609 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800610 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
611
612 // Rejects motion events with duplicate pointer ids.
613 pointerProperties[0].id = 1;
614 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800615 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
616 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700617 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
618 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700619 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800620 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700621 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800622 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800623 << "Should reject motion events with duplicate pointer ids.";
624}
625
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800626/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
627
628TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
629 constexpr nsecs_t eventTime = 20;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800630 NotifyConfigurationChangedArgs args(10 /*id*/, eventTime);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800631 mDispatcher->notifyConfigurationChanged(&args);
632 ASSERT_TRUE(mDispatcher->waitForIdle());
633
634 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
635}
636
637TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800638 NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/,
639 2 /*switchMask*/);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800640 mDispatcher->notifySwitch(&args);
641
642 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
643 args.policyFlags |= POLICY_FLAG_TRUSTED;
644 mFakePolicy->assertNotifySwitchWasCalled(args);
645}
646
Arthur Hungb92218b2018-08-14 12:00:21 +0800647// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700648static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700649static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s;
Arthur Hungb92218b2018-08-14 12:00:21 +0800650
651class FakeApplicationHandle : public InputApplicationHandle {
652public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700653 FakeApplicationHandle() {
654 mInfo.name = "Fake Application";
655 mInfo.token = new BBinder();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500656 mInfo.dispatchingTimeoutMillis =
657 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700658 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800659 virtual ~FakeApplicationHandle() {}
660
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000661 virtual bool updateInfo() override { return true; }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700662
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500663 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
664 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700665 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800666};
667
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800668class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800669public:
Garfield Tan15601662020-09-22 15:32:38 -0700670 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800671 : mName(name) {
Garfield Tan15601662020-09-22 15:32:38 -0700672 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800673 }
674
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800675 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700676 InputEvent* event;
677 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
678 if (!consumeSeq) {
679 return nullptr;
680 }
681 finishEvent(*consumeSeq);
682 return event;
683 }
684
685 /**
686 * Receive an event without acknowledging it.
687 * Return the sequence number that could later be used to send finished signal.
688 */
689 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800690 uint32_t consumeSeq;
691 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800692
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800693 std::chrono::time_point start = std::chrono::steady_clock::now();
694 status_t status = WOULD_BLOCK;
695 while (status == WOULD_BLOCK) {
chaviw81e2bb92019-12-18 15:03:51 -0800696 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800697 &event);
698 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
699 if (elapsed > 100ms) {
700 break;
701 }
702 }
703
704 if (status == WOULD_BLOCK) {
705 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700706 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800707 }
708
709 if (status != OK) {
710 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700711 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800712 }
713 if (event == nullptr) {
714 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700715 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800716 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700717 if (outEvent != nullptr) {
718 *outEvent = event;
719 }
720 return consumeSeq;
721 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800722
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700723 /**
724 * To be used together with "receiveEvent" to complete the consumption of an event.
725 */
726 void finishEvent(uint32_t consumeSeq) {
727 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
728 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800729 }
730
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000731 void consumeEvent(int32_t expectedEventType, int32_t expectedAction,
732 std::optional<int32_t> expectedDisplayId,
733 std::optional<int32_t> expectedFlags) {
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800734 InputEvent* event = consume();
735
736 ASSERT_NE(nullptr, event) << mName.c_str()
737 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800738 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700739 << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType)
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800740 << " event, got " << inputEventTypeToString(event->getType()) << " event";
Arthur Hungb92218b2018-08-14 12:00:21 +0800741
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000742 if (expectedDisplayId.has_value()) {
743 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
744 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800745
Tiger Huang8664f8c2018-10-11 19:14:35 +0800746 switch (expectedEventType) {
747 case AINPUT_EVENT_TYPE_KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800748 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
749 EXPECT_EQ(expectedAction, keyEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000750 if (expectedFlags.has_value()) {
751 EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags());
752 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800753 break;
754 }
755 case AINPUT_EVENT_TYPE_MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800756 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
757 EXPECT_EQ(expectedAction, motionEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000758 if (expectedFlags.has_value()) {
759 EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags());
760 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800761 break;
762 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100763 case AINPUT_EVENT_TYPE_FOCUS: {
764 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
765 }
Prabir Pradhan99987712020-11-10 18:43:05 -0800766 case AINPUT_EVENT_TYPE_CAPTURE: {
767 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
768 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800769 default: {
770 FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType;
771 }
772 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800773 }
774
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100775 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
776 InputEvent* event = consume();
777 ASSERT_NE(nullptr, event) << mName.c_str()
778 << ": consumer should have returned non-NULL event.";
779 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
780 << "Got " << inputEventTypeToString(event->getType())
781 << " event instead of FOCUS event";
782
783 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
784 << mName.c_str() << ": event displayId should always be NONE.";
785
786 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
787 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
788 EXPECT_EQ(inTouchMode, focusEvent->getInTouchMode());
789 }
790
Prabir Pradhan99987712020-11-10 18:43:05 -0800791 void consumeCaptureEvent(bool hasCapture) {
792 const InputEvent* event = consume();
793 ASSERT_NE(nullptr, event) << mName.c_str()
794 << ": consumer should have returned non-NULL event.";
795 ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType())
796 << "Got " << inputEventTypeToString(event->getType())
797 << " event instead of CAPTURE event";
798
799 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
800 << mName.c_str() << ": event displayId should always be NONE.";
801
802 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
803 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
804 }
805
chaviwd1c23182019-12-20 18:44:56 -0800806 void assertNoEvents() {
807 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700808 if (event == nullptr) {
809 return;
810 }
811 if (event->getType() == AINPUT_EVENT_TYPE_KEY) {
812 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
813 ADD_FAILURE() << "Received key event "
814 << KeyEvent::actionToString(keyEvent.getAction());
815 } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) {
816 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
817 ADD_FAILURE() << "Received motion event "
818 << MotionEvent::actionToString(motionEvent.getAction());
819 } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) {
820 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
821 ADD_FAILURE() << "Received focus event, hasFocus = "
822 << (focusEvent.getHasFocus() ? "true" : "false");
Prabir Pradhan99987712020-11-10 18:43:05 -0800823 } else if (event->getType() == AINPUT_EVENT_TYPE_CAPTURE) {
824 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
825 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
826 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700827 }
828 FAIL() << mName.c_str()
829 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -0800830 }
831
832 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
833
834protected:
835 std::unique_ptr<InputConsumer> mConsumer;
836 PreallocatedInputEventFactory mEventFactory;
837
838 std::string mName;
839};
840
841class FakeWindowHandle : public InputWindowHandle {
842public:
843 static const int32_t WIDTH = 600;
844 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -0800845
Chris Yea209fde2020-07-22 13:54:51 -0700846 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
chaviwd1c23182019-12-20 18:44:56 -0800847 const sp<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500848 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -0800849 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500850 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -0700851 base::Result<std::unique_ptr<InputChannel>> channel =
852 dispatcher->createInputChannel(name);
853 token = (*channel)->getConnectionToken();
854 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -0800855 }
856
857 inputApplicationHandle->updateInfo();
858 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
859
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500860 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700861 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -0800862 mInfo.name = name;
Michael Wright44753b12020-07-08 13:48:11 +0100863 mInfo.type = InputWindowInfo::Type::APPLICATION;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500864 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000865 mInfo.alpha = 1.0;
chaviwd1c23182019-12-20 18:44:56 -0800866 mInfo.frameLeft = 0;
867 mInfo.frameTop = 0;
868 mInfo.frameRight = WIDTH;
869 mInfo.frameBottom = HEIGHT;
chaviw1ff3d1e2020-07-01 15:53:47 -0700870 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -0800871 mInfo.globalScaleFactor = 1.0;
872 mInfo.touchableRegion.clear();
873 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
874 mInfo.visible = true;
Vishnu Nair47074b82020-08-14 11:54:47 -0700875 mInfo.focusable = false;
chaviwd1c23182019-12-20 18:44:56 -0800876 mInfo.hasWallpaper = false;
877 mInfo.paused = false;
chaviwd1c23182019-12-20 18:44:56 -0800878 mInfo.ownerPid = INJECTOR_PID;
879 mInfo.ownerUid = INJECTOR_UID;
chaviwd1c23182019-12-20 18:44:56 -0800880 mInfo.displayId = displayId;
881 }
882
883 virtual bool updateInfo() { return true; }
884
Vishnu Nair47074b82020-08-14 11:54:47 -0700885 void setFocusable(bool focusable) { mInfo.focusable = focusable; }
chaviwd1c23182019-12-20 18:44:56 -0800886
Vishnu Nair958da932020-08-21 17:12:37 -0700887 void setVisible(bool visible) { mInfo.visible = visible; }
888
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700889 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500890 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700891 }
892
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700893 void setPaused(bool paused) { mInfo.paused = paused; }
894
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000895 void setAlpha(float alpha) { mInfo.alpha = alpha; }
896
897 void setTouchOcclusionMode(android::os::TouchOcclusionMode mode) {
898 mInfo.touchOcclusionMode = mode;
899 }
900
chaviwd1c23182019-12-20 18:44:56 -0800901 void setFrame(const Rect& frame) {
902 mInfo.frameLeft = frame.left;
903 mInfo.frameTop = frame.top;
904 mInfo.frameRight = frame.right;
905 mInfo.frameBottom = frame.bottom;
chaviw1ff3d1e2020-07-01 15:53:47 -0700906 mInfo.transform.set(frame.left, frame.top);
chaviwd1c23182019-12-20 18:44:56 -0800907 mInfo.touchableRegion.clear();
908 mInfo.addTouchableRegion(frame);
909 }
910
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +0000911 void addFlags(Flags<InputWindowInfo::Flag> flags) { mInfo.flags |= flags; }
912
Michael Wright44753b12020-07-08 13:48:11 +0100913 void setFlags(Flags<InputWindowInfo::Flag> flags) { mInfo.flags = flags; }
chaviwd1c23182019-12-20 18:44:56 -0800914
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500915 void setInputFeatures(InputWindowInfo::Feature features) { mInfo.inputFeatures = features; }
916
chaviw9eaa22c2020-07-01 16:21:27 -0700917 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
918 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
919 }
920
921 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -0700922
yunho.shinf4a80b82020-11-16 21:13:57 +0900923 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
924
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800925 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
926 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
927 expectedFlags);
928 }
929
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700930 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
931 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
932 }
933
Svet Ganov5d3bc372020-01-26 23:11:07 -0800934 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000935 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800936 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId,
937 expectedFlags);
938 }
939
940 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000941 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800942 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId,
943 expectedFlags);
944 }
945
946 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000947 int32_t expectedFlags = 0) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000948 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
949 }
950
951 void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
952 std::optional<int32_t> expectedFlags = std::nullopt) {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800953 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
954 expectedFlags);
955 }
956
Svet Ganov5d3bc372020-01-26 23:11:07 -0800957 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000958 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
959 int32_t expectedFlags = 0) {
960 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
961 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -0800962 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
963 }
964
965 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000966 int32_t expectedFlags = 0) {
967 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
968 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -0800969 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
970 }
971
972 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000973 int32_t expectedFlags = 0) {
Michael Wright3a240c42019-12-10 20:53:41 +0000974 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
975 expectedFlags);
976 }
977
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500978 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
979 int32_t expectedFlags = 0) {
980 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
981 expectedFlags);
982 }
983
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100984 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
985 ASSERT_NE(mInputReceiver, nullptr)
986 << "Cannot consume events from a window with no receiver";
987 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
988 }
989
Prabir Pradhan99987712020-11-10 18:43:05 -0800990 void consumeCaptureEvent(bool hasCapture) {
991 ASSERT_NE(mInputReceiver, nullptr)
992 << "Cannot consume events from a window with no receiver";
993 mInputReceiver->consumeCaptureEvent(hasCapture);
994 }
995
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000996 void consumeEvent(int32_t expectedEventType, int32_t expectedAction,
997 std::optional<int32_t> expectedDisplayId,
998 std::optional<int32_t> expectedFlags) {
chaviwd1c23182019-12-20 18:44:56 -0800999 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
1000 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
1001 expectedFlags);
1002 }
1003
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001004 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001005 if (mInputReceiver == nullptr) {
1006 ADD_FAILURE() << "Invalid receive event on window with no receiver";
1007 return std::nullopt;
1008 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001009 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001010 }
1011
1012 void finishEvent(uint32_t sequenceNum) {
1013 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1014 mInputReceiver->finishEvent(sequenceNum);
1015 }
1016
chaviwaf87b3e2019-10-01 16:59:28 -07001017 InputEvent* consume() {
1018 if (mInputReceiver == nullptr) {
1019 return nullptr;
1020 }
1021 return mInputReceiver->consume();
1022 }
1023
Arthur Hungb92218b2018-08-14 12:00:21 +08001024 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001025 if (mInputReceiver == nullptr &&
1026 mInfo.inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL)) {
1027 return; // Can't receive events if the window does not have input channel
1028 }
1029 ASSERT_NE(nullptr, mInputReceiver)
1030 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -08001031 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +08001032 }
1033
chaviwaf87b3e2019-10-01 16:59:28 -07001034 sp<IBinder> getToken() { return mInfo.token; }
1035
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001036 const std::string& getName() { return mName; }
1037
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001038 void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) {
1039 mInfo.ownerPid = ownerPid;
1040 mInfo.ownerUid = ownerUid;
1041 }
1042
chaviwd1c23182019-12-20 18:44:56 -08001043private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001044 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -08001045 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001046 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001047};
1048
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001049std::atomic<int32_t> FakeWindowHandle::sId{1};
1050
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001051static InputEventInjectionResult injectKey(
1052 const sp<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
1053 int32_t displayId = ADISPLAY_ID_NONE,
1054 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
1055 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001056 KeyEvent event;
1057 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1058
1059 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001060 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001061 INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE,
1062 repeatCount, currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001063
1064 // Inject event until dispatch out.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001065 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, syncMode,
1066 injectionTimeout,
1067 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
Arthur Hungb92218b2018-08-14 12:00:21 +08001068}
1069
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001070static InputEventInjectionResult injectKeyDown(const sp<InputDispatcher>& dispatcher,
1071 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001072 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId);
1073}
1074
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001075static InputEventInjectionResult injectKeyUp(const sp<InputDispatcher>& dispatcher,
1076 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001077 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId);
1078}
1079
Garfield Tandf26e862020-07-01 20:18:19 -07001080class PointerBuilder {
1081public:
1082 PointerBuilder(int32_t id, int32_t toolType) {
1083 mProperties.clear();
1084 mProperties.id = id;
1085 mProperties.toolType = toolType;
1086 mCoords.clear();
1087 }
1088
1089 PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); }
1090
1091 PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); }
1092
1093 PointerBuilder& axis(int32_t axis, float value) {
1094 mCoords.setAxisValue(axis, value);
1095 return *this;
1096 }
1097
1098 PointerProperties buildProperties() const { return mProperties; }
1099
1100 PointerCoords buildCoords() const { return mCoords; }
1101
1102private:
1103 PointerProperties mProperties;
1104 PointerCoords mCoords;
1105};
1106
1107class MotionEventBuilder {
1108public:
1109 MotionEventBuilder(int32_t action, int32_t source) {
1110 mAction = action;
1111 mSource = source;
1112 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1113 }
1114
1115 MotionEventBuilder& eventTime(nsecs_t eventTime) {
1116 mEventTime = eventTime;
1117 return *this;
1118 }
1119
1120 MotionEventBuilder& displayId(int32_t displayId) {
1121 mDisplayId = displayId;
1122 return *this;
1123 }
1124
1125 MotionEventBuilder& actionButton(int32_t actionButton) {
1126 mActionButton = actionButton;
1127 return *this;
1128 }
1129
1130 MotionEventBuilder& buttonState(int32_t actionButton) {
1131 mActionButton = actionButton;
1132 return *this;
1133 }
1134
1135 MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) {
1136 mRawXCursorPosition = rawXCursorPosition;
1137 return *this;
1138 }
1139
1140 MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) {
1141 mRawYCursorPosition = rawYCursorPosition;
1142 return *this;
1143 }
1144
1145 MotionEventBuilder& pointer(PointerBuilder pointer) {
1146 mPointers.push_back(pointer);
1147 return *this;
1148 }
1149
1150 MotionEvent build() {
1151 std::vector<PointerProperties> pointerProperties;
1152 std::vector<PointerCoords> pointerCoords;
1153 for (const PointerBuilder& pointer : mPointers) {
1154 pointerProperties.push_back(pointer.buildProperties());
1155 pointerCoords.push_back(pointer.buildCoords());
1156 }
1157
1158 // Set mouse cursor position for the most common cases to avoid boilerplate.
1159 if (mSource == AINPUT_SOURCE_MOUSE &&
1160 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) &&
1161 mPointers.size() == 1) {
1162 mRawXCursorPosition = pointerCoords[0].getX();
1163 mRawYCursorPosition = pointerCoords[0].getY();
1164 }
1165
1166 MotionEvent event;
chaviw9eaa22c2020-07-01 16:21:27 -07001167 ui::Transform identityTransform;
Garfield Tandf26e862020-07-01 20:18:19 -07001168 event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC,
1169 mAction, mActionButton, /* flags */ 0, /* edgeFlags */ 0, AMETA_NONE,
chaviw9eaa22c2020-07-01 16:21:27 -07001170 mButtonState, MotionClassification::NONE, identityTransform,
1171 /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition,
1172 mRawYCursorPosition, mEventTime, mEventTime, mPointers.size(),
1173 pointerProperties.data(), pointerCoords.data());
Garfield Tandf26e862020-07-01 20:18:19 -07001174
1175 return event;
1176 }
1177
1178private:
1179 int32_t mAction;
1180 int32_t mSource;
1181 nsecs_t mEventTime;
1182 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1183 int32_t mActionButton{0};
1184 int32_t mButtonState{0};
1185 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1186 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1187
1188 std::vector<PointerBuilder> mPointers;
1189};
1190
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001191static InputEventInjectionResult injectMotionEvent(
Garfield Tandf26e862020-07-01 20:18:19 -07001192 const sp<InputDispatcher>& dispatcher, const MotionEvent& event,
1193 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001194 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT) {
Garfield Tandf26e862020-07-01 20:18:19 -07001195 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, injectionMode,
1196 injectionTimeout,
1197 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
1198}
1199
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001200static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001201 const sp<InputDispatcher>& dispatcher, int32_t action, int32_t source, int32_t displayId,
1202 const PointF& position,
1203 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001204 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1205 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001206 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001207 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC)) {
Garfield Tandf26e862020-07-01 20:18:19 -07001208 MotionEvent event = MotionEventBuilder(action, source)
1209 .displayId(displayId)
1210 .eventTime(eventTime)
1211 .rawXCursorPosition(cursorPosition.x)
1212 .rawYCursorPosition(cursorPosition.y)
1213 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1214 .x(position.x)
1215 .y(position.y))
1216 .build();
Arthur Hungb92218b2018-08-14 12:00:21 +08001217
1218 // Inject event until dispatch out.
Garfield Tandf26e862020-07-01 20:18:19 -07001219 return injectMotionEvent(dispatcher, event);
Arthur Hungb92218b2018-08-14 12:00:21 +08001220}
1221
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001222static InputEventInjectionResult injectMotionDown(const sp<InputDispatcher>& dispatcher,
1223 int32_t source, int32_t displayId,
1224 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001225 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001226}
1227
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001228static InputEventInjectionResult injectMotionUp(const sp<InputDispatcher>& dispatcher,
1229 int32_t source, int32_t displayId,
1230 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001231 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001232}
1233
Jackal Guof9696682018-10-05 12:23:23 +08001234static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1235 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1236 // Define a valid key event.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001237 NotifyKeyArgs args(/* id */ 0, currentTime, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
1238 POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, KEY_A,
1239 AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001240
1241 return args;
1242}
1243
chaviwd1c23182019-12-20 18:44:56 -08001244static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId,
1245 const std::vector<PointF>& points) {
1246 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001247 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1248 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1249 }
1250
chaviwd1c23182019-12-20 18:44:56 -08001251 PointerProperties pointerProperties[pointerCount];
1252 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001253
chaviwd1c23182019-12-20 18:44:56 -08001254 for (size_t i = 0; i < pointerCount; i++) {
1255 pointerProperties[i].clear();
1256 pointerProperties[i].id = i;
1257 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001258
chaviwd1c23182019-12-20 18:44:56 -08001259 pointerCoords[i].clear();
1260 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1261 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1262 }
Jackal Guof9696682018-10-05 12:23:23 +08001263
1264 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1265 // Define a valid motion event.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001266 NotifyMotionArgs args(/* id */ 0, currentTime, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001267 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1268 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001269 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1270 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001271 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1272 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001273
1274 return args;
1275}
1276
chaviwd1c23182019-12-20 18:44:56 -08001277static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1278 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1279}
1280
Prabir Pradhan99987712020-11-10 18:43:05 -08001281static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(bool enabled) {
1282 return NotifyPointerCaptureChangedArgs(/* id */ 0, systemTime(SYSTEM_TIME_MONOTONIC), enabled);
1283}
1284
Arthur Hungb92218b2018-08-14 12:00:21 +08001285TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001286 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001287 sp<FakeWindowHandle> window =
1288 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001289
Arthur Hung72d8dc32020-03-28 00:48:39 +00001290 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001291 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1292 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1293 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001294
1295 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001296 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001297}
1298
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001299/**
1300 * Calling setInputWindows once with FLAG_NOT_TOUCH_MODAL should not cause any issues.
1301 * To ensure that window receives only events that were directly inside of it, add
1302 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1303 * when finding touched windows.
1304 * This test serves as a sanity check for the next test, where setInputWindows is
1305 * called twice.
1306 */
1307TEST_F(InputDispatcherTest, SetInputWindowOnce_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001308 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001309 sp<FakeWindowHandle> window =
1310 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1311 window->setFrame(Rect(0, 0, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01001312 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001313
1314 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001315 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001316 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1317 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001318 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001319
1320 // Window should receive motion event.
1321 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1322}
1323
1324/**
1325 * Calling setInputWindows twice, with the same info, should not cause any issues.
1326 * To ensure that window receives only events that were directly inside of it, add
1327 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1328 * when finding touched windows.
1329 */
1330TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001331 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001332 sp<FakeWindowHandle> window =
1333 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1334 window->setFrame(Rect(0, 0, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01001335 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001336
1337 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1338 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001339 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001340 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1341 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001342 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001343
1344 // Window should receive motion event.
1345 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1346}
1347
Arthur Hungb92218b2018-08-14 12:00:21 +08001348// The foreground window should receive the first touch down event.
1349TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001350 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001351 sp<FakeWindowHandle> windowTop =
1352 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
1353 sp<FakeWindowHandle> windowSecond =
1354 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001355
Arthur Hung72d8dc32020-03-28 00:48:39 +00001356 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001357 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1358 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1359 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001360
1361 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001362 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001363 windowSecond->assertNoEvents();
1364}
1365
Garfield Tandf26e862020-07-01 20:18:19 -07001366TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07001367 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07001368 sp<FakeWindowHandle> windowLeft =
1369 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1370 windowLeft->setFrame(Rect(0, 0, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001371 windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001372 sp<FakeWindowHandle> windowRight =
1373 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1374 windowRight->setFrame(Rect(600, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001375 windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001376
1377 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1378
1379 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
1380
1381 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001382 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001383 injectMotionEvent(mDispatcher,
1384 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1385 AINPUT_SOURCE_MOUSE)
1386 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1387 .x(900)
1388 .y(400))
1389 .build()));
1390 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1391 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1392 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1393 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1394
1395 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001396 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001397 injectMotionEvent(mDispatcher,
1398 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1399 AINPUT_SOURCE_MOUSE)
1400 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1401 .x(300)
1402 .y(400))
1403 .build()));
1404 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1405 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1406 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1407 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1408 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1409 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1410
1411 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001412 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001413 injectMotionEvent(mDispatcher,
1414 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1415 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1416 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1417 .x(300)
1418 .y(400))
1419 .build()));
1420 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1421
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001422 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001423 injectMotionEvent(mDispatcher,
1424 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
1425 AINPUT_SOURCE_MOUSE)
1426 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1427 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1428 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1429 .x(300)
1430 .y(400))
1431 .build()));
1432 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1433 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1434
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001435 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001436 injectMotionEvent(mDispatcher,
1437 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1438 AINPUT_SOURCE_MOUSE)
1439 .buttonState(0)
1440 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1441 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1442 .x(300)
1443 .y(400))
1444 .build()));
1445 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1446 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1447
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001448 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001449 injectMotionEvent(mDispatcher,
1450 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
1451 .buttonState(0)
1452 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1453 .x(300)
1454 .y(400))
1455 .build()));
1456 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1457
1458 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001459 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001460 injectMotionEvent(mDispatcher,
1461 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1462 AINPUT_SOURCE_MOUSE)
1463 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1464 .x(900)
1465 .y(400))
1466 .build()));
1467 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1468 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1469 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1470 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1471 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1472 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1473}
1474
1475// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
1476// directly in this test.
1477TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07001478 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07001479 sp<FakeWindowHandle> window =
1480 new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1481 window->setFrame(Rect(0, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001482 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001483
1484 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1485
1486 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1487
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_ENTER,
1491 AINPUT_SOURCE_MOUSE)
1492 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1493 .x(300)
1494 .y(400))
1495 .build()));
1496 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1497 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1498
1499 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001500 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001501 injectMotionEvent(mDispatcher,
1502 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1503 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1504 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1505 .x(300)
1506 .y(400))
1507 .build()));
1508 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1509
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001510 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001511 injectMotionEvent(mDispatcher,
1512 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
1513 AINPUT_SOURCE_MOUSE)
1514 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1515 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1516 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1517 .x(300)
1518 .y(400))
1519 .build()));
1520 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1521 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1522
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001523 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001524 injectMotionEvent(mDispatcher,
1525 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1526 AINPUT_SOURCE_MOUSE)
1527 .buttonState(0)
1528 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1529 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1530 .x(300)
1531 .y(400))
1532 .build()));
1533 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1534 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1535
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001536 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001537 injectMotionEvent(mDispatcher,
1538 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
1539 .buttonState(0)
1540 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1541 .x(300)
1542 .y(400))
1543 .build()));
1544 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1545
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001546 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001547 injectMotionEvent(mDispatcher,
1548 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
1549 AINPUT_SOURCE_MOUSE)
1550 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1551 .x(300)
1552 .y(400))
1553 .build()));
1554 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1555 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1556}
1557
Garfield Tan00f511d2019-06-12 16:55:40 -07001558TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07001559 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07001560
1561 sp<FakeWindowHandle> windowLeft =
1562 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1563 windowLeft->setFrame(Rect(0, 0, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001564 windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07001565 sp<FakeWindowHandle> windowRight =
1566 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1567 windowRight->setFrame(Rect(600, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001568 windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07001569
1570 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1571
Arthur Hung72d8dc32020-03-28 00:48:39 +00001572 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07001573
1574 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
1575 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001576 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07001577 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001578 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001579 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07001580 windowRight->assertNoEvents();
1581}
1582
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001583TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001584 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001585 sp<FakeWindowHandle> window =
1586 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07001587 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001588
Arthur Hung72d8dc32020-03-28 00:48:39 +00001589 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001590 setFocusedWindow(window);
1591
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001592 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001593
1594 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1595 mDispatcher->notifyKey(&keyArgs);
1596
1597 // Window should receive key down event.
1598 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1599
1600 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
1601 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001602 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001603 mDispatcher->notifyDeviceReset(&args);
1604 window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
1605 AKEY_EVENT_FLAG_CANCELED);
1606}
1607
1608TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001609 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001610 sp<FakeWindowHandle> window =
1611 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1612
Arthur Hung72d8dc32020-03-28 00:48:39 +00001613 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001614
1615 NotifyMotionArgs motionArgs =
1616 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1617 ADISPLAY_ID_DEFAULT);
1618 mDispatcher->notifyMotion(&motionArgs);
1619
1620 // Window should receive motion down event.
1621 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1622
1623 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
1624 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001625 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001626 mDispatcher->notifyDeviceReset(&args);
1627 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT,
1628 0 /*expectedFlags*/);
1629}
1630
Svet Ganov5d3bc372020-01-26 23:11:07 -08001631TEST_F(InputDispatcherTest, TransferTouchFocus_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07001632 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001633
1634 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001635 sp<FakeWindowHandle> firstWindow =
1636 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1637 sp<FakeWindowHandle> secondWindow =
1638 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001639
1640 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001641 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001642
1643 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001644 NotifyMotionArgs downMotionArgs =
1645 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1646 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001647 mDispatcher->notifyMotion(&downMotionArgs);
1648 // Only the first window should get the down event
1649 firstWindow->consumeMotionDown();
1650 secondWindow->assertNoEvents();
1651
1652 // Transfer touch focus to the second window
1653 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1654 // The first window gets cancel and the second gets down
1655 firstWindow->consumeMotionCancel();
1656 secondWindow->consumeMotionDown();
1657
1658 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001659 NotifyMotionArgs upMotionArgs =
1660 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1661 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001662 mDispatcher->notifyMotion(&upMotionArgs);
1663 // The first window gets no events and the second gets up
1664 firstWindow->assertNoEvents();
1665 secondWindow->consumeMotionUp();
1666}
1667
1668TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointerNoSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001669 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001670
1671 PointF touchPoint = {10, 10};
1672
1673 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001674 sp<FakeWindowHandle> firstWindow =
1675 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1676 sp<FakeWindowHandle> secondWindow =
1677 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001678
1679 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001680 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001681
1682 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001683 NotifyMotionArgs downMotionArgs =
1684 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1685 ADISPLAY_ID_DEFAULT, {touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001686 mDispatcher->notifyMotion(&downMotionArgs);
1687 // Only the first window should get the down event
1688 firstWindow->consumeMotionDown();
1689 secondWindow->assertNoEvents();
1690
1691 // Send pointer down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001692 NotifyMotionArgs pointerDownMotionArgs =
1693 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1694 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1695 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1696 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001697 mDispatcher->notifyMotion(&pointerDownMotionArgs);
1698 // Only the first window should get the pointer down event
1699 firstWindow->consumeMotionPointerDown(1);
1700 secondWindow->assertNoEvents();
1701
1702 // Transfer touch focus to the second window
1703 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1704 // The first window gets cancel and the second gets down and pointer down
1705 firstWindow->consumeMotionCancel();
1706 secondWindow->consumeMotionDown();
1707 secondWindow->consumeMotionPointerDown(1);
1708
1709 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001710 NotifyMotionArgs pointerUpMotionArgs =
1711 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1712 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1713 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1714 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001715 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1716 // The first window gets nothing and the second gets pointer up
1717 firstWindow->assertNoEvents();
1718 secondWindow->consumeMotionPointerUp(1);
1719
1720 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001721 NotifyMotionArgs upMotionArgs =
1722 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1723 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001724 mDispatcher->notifyMotion(&upMotionArgs);
1725 // The first window gets nothing and the second gets up
1726 firstWindow->assertNoEvents();
1727 secondWindow->consumeMotionUp();
1728}
1729
1730TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001731 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001732
1733 // Create a non touch modal window that supports split touch
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001734 sp<FakeWindowHandle> firstWindow =
1735 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001736 firstWindow->setFrame(Rect(0, 0, 600, 400));
Michael Wright44753b12020-07-08 13:48:11 +01001737 firstWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1738 InputWindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001739
1740 // Create a non touch modal window that supports split touch
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001741 sp<FakeWindowHandle> secondWindow =
1742 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001743 secondWindow->setFrame(Rect(0, 400, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001744 secondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1745 InputWindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001746
1747 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001748 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001749
1750 PointF pointInFirst = {300, 200};
1751 PointF pointInSecond = {300, 600};
1752
1753 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001754 NotifyMotionArgs firstDownMotionArgs =
1755 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1756 ADISPLAY_ID_DEFAULT, {pointInFirst});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001757 mDispatcher->notifyMotion(&firstDownMotionArgs);
1758 // Only the first window should get the down event
1759 firstWindow->consumeMotionDown();
1760 secondWindow->assertNoEvents();
1761
1762 // Send down to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001763 NotifyMotionArgs secondDownMotionArgs =
1764 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1765 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1766 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1767 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001768 mDispatcher->notifyMotion(&secondDownMotionArgs);
1769 // The first window gets a move and the second a down
1770 firstWindow->consumeMotionMove();
1771 secondWindow->consumeMotionDown();
1772
1773 // Transfer touch focus to the second window
1774 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1775 // The first window gets cancel and the new gets pointer down (it already saw down)
1776 firstWindow->consumeMotionCancel();
1777 secondWindow->consumeMotionPointerDown(1);
1778
1779 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001780 NotifyMotionArgs pointerUpMotionArgs =
1781 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1782 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1783 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1784 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001785 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1786 // The first window gets nothing and the second gets pointer up
1787 firstWindow->assertNoEvents();
1788 secondWindow->consumeMotionPointerUp(1);
1789
1790 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001791 NotifyMotionArgs upMotionArgs =
1792 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1793 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001794 mDispatcher->notifyMotion(&upMotionArgs);
1795 // The first window gets nothing and the second gets up
1796 firstWindow->assertNoEvents();
1797 secondWindow->consumeMotionUp();
1798}
1799
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001800TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07001801 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001802 sp<FakeWindowHandle> window =
1803 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1804
Vishnu Nair47074b82020-08-14 11:54:47 -07001805 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001806 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001807 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001808
1809 window->consumeFocusEvent(true);
1810
1811 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1812 mDispatcher->notifyKey(&keyArgs);
1813
1814 // Window should receive key down event.
1815 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1816}
1817
1818TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07001819 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001820 sp<FakeWindowHandle> window =
1821 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1822
Arthur Hung72d8dc32020-03-28 00:48:39 +00001823 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001824
1825 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1826 mDispatcher->notifyKey(&keyArgs);
1827 mDispatcher->waitForIdle();
1828
1829 window->assertNoEvents();
1830}
1831
1832// If a window is touchable, but does not have focus, it should receive motion events, but not keys
1833TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07001834 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001835 sp<FakeWindowHandle> window =
1836 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1837
Arthur Hung72d8dc32020-03-28 00:48:39 +00001838 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001839
1840 // Send key
1841 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1842 mDispatcher->notifyKey(&keyArgs);
1843 // Send motion
1844 NotifyMotionArgs motionArgs =
1845 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1846 ADISPLAY_ID_DEFAULT);
1847 mDispatcher->notifyMotion(&motionArgs);
1848
1849 // Window should receive only the motion event
1850 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1851 window->assertNoEvents(); // Key event or focus event will not be received
1852}
1853
arthurhungea3f4fc2020-12-21 23:18:53 +08001854TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
1855 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1856
1857 // Create first non touch modal window that supports split touch
1858 sp<FakeWindowHandle> firstWindow =
1859 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1860 firstWindow->setFrame(Rect(0, 0, 600, 400));
1861 firstWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1862 InputWindowInfo::Flag::SPLIT_TOUCH);
1863
1864 // Create second non touch modal window that supports split touch
1865 sp<FakeWindowHandle> secondWindow =
1866 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
1867 secondWindow->setFrame(Rect(0, 400, 600, 800));
1868 secondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1869 InputWindowInfo::Flag::SPLIT_TOUCH);
1870
1871 // Add the windows to the dispatcher
1872 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
1873
1874 PointF pointInFirst = {300, 200};
1875 PointF pointInSecond = {300, 600};
1876
1877 // Send down to the first window
1878 NotifyMotionArgs firstDownMotionArgs =
1879 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1880 ADISPLAY_ID_DEFAULT, {pointInFirst});
1881 mDispatcher->notifyMotion(&firstDownMotionArgs);
1882 // Only the first window should get the down event
1883 firstWindow->consumeMotionDown();
1884 secondWindow->assertNoEvents();
1885
1886 // Send down to the second window
1887 NotifyMotionArgs secondDownMotionArgs =
1888 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1889 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1890 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1891 {pointInFirst, pointInSecond});
1892 mDispatcher->notifyMotion(&secondDownMotionArgs);
1893 // The first window gets a move and the second a down
1894 firstWindow->consumeMotionMove();
1895 secondWindow->consumeMotionDown();
1896
1897 // Send pointer cancel to the second window
1898 NotifyMotionArgs pointerUpMotionArgs =
1899 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1900 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1901 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1902 {pointInFirst, pointInSecond});
1903 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
1904 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1905 // The first window gets move and the second gets cancel.
1906 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
1907 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
1908
1909 // Send up event.
1910 NotifyMotionArgs upMotionArgs =
1911 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1912 ADISPLAY_ID_DEFAULT);
1913 mDispatcher->notifyMotion(&upMotionArgs);
1914 // The first window gets up and the second gets nothing.
1915 firstWindow->consumeMotionUp();
1916 secondWindow->assertNoEvents();
1917}
1918
chaviwd1c23182019-12-20 18:44:56 -08001919class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00001920public:
1921 FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name,
chaviwd1c23182019-12-20 18:44:56 -08001922 int32_t displayId, bool isGestureMonitor = false) {
Garfield Tan15601662020-09-22 15:32:38 -07001923 base::Result<std::unique_ptr<InputChannel>> channel =
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +00001924 dispatcher->createInputMonitor(displayId, isGestureMonitor, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07001925 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00001926 }
1927
chaviwd1c23182019-12-20 18:44:56 -08001928 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
1929
1930 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1931 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN,
1932 expectedDisplayId, expectedFlags);
1933 }
1934
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001935 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
1936
1937 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
1938
chaviwd1c23182019-12-20 18:44:56 -08001939 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1940 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
1941 expectedDisplayId, expectedFlags);
1942 }
1943
1944 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1945 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP,
1946 expectedDisplayId, expectedFlags);
1947 }
1948
1949 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
1950
1951private:
1952 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00001953};
1954
1955// Tests for gesture monitors
1956TEST_F(InputDispatcherTest, GestureMonitor_ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07001957 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00001958 sp<FakeWindowHandle> window =
1959 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001960 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00001961
chaviwd1c23182019-12-20 18:44:56 -08001962 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
1963 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00001964
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001965 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00001966 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001967 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00001968 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001969 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00001970}
1971
1972TEST_F(InputDispatcherTest, GestureMonitor_DoesNotReceiveKeyEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07001973 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00001974 sp<FakeWindowHandle> window =
1975 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1976
1977 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07001978 window->setFocusable(true);
Michael Wright3a240c42019-12-10 20:53:41 +00001979
Arthur Hung72d8dc32020-03-28 00:48:39 +00001980 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001981 setFocusedWindow(window);
1982
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001983 window->consumeFocusEvent(true);
Michael Wright3a240c42019-12-10 20:53:41 +00001984
chaviwd1c23182019-12-20 18:44:56 -08001985 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
1986 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00001987
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001988 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
1989 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00001990 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001991 monitor.assertNoEvents();
Michael Wright3a240c42019-12-10 20:53:41 +00001992}
1993
1994TEST_F(InputDispatcherTest, GestureMonitor_CanPilferAfterWindowIsRemovedMidStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001995 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00001996 sp<FakeWindowHandle> window =
1997 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001998 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00001999
chaviwd1c23182019-12-20 18:44:56 -08002000 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
2001 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00002002
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002003 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00002004 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002005 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00002006 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002007 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00002008
2009 window->releaseChannel();
2010
chaviwd1c23182019-12-20 18:44:56 -08002011 mDispatcher->pilferPointers(monitor.getToken());
Michael Wright3a240c42019-12-10 20:53:41 +00002012
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002013 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00002014 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002015 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08002016 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00002017}
2018
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002019TEST_F(InputDispatcherTest, UnresponsiveGestureMonitor_GetsAnr) {
2020 FakeMonitorReceiver monitor =
2021 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
2022 true /*isGestureMonitor*/);
2023
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002024 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002025 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
2026 std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
2027 ASSERT_TRUE(consumeSeq);
2028
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00002029 mFakePolicy->assertNotifyMonitorUnresponsiveWasCalled(DISPATCHING_TIMEOUT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002030 monitor.finishEvent(*consumeSeq);
2031 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00002032 mFakePolicy->assertNotifyMonitorResponsiveWasCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002033}
2034
chaviw81e2bb92019-12-18 15:03:51 -08002035TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002036 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
chaviw81e2bb92019-12-18 15:03:51 -08002037 sp<FakeWindowHandle> window =
2038 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2039
Arthur Hung72d8dc32020-03-28 00:48:39 +00002040 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08002041
2042 NotifyMotionArgs motionArgs =
2043 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2044 ADISPLAY_ID_DEFAULT);
2045
2046 mDispatcher->notifyMotion(&motionArgs);
2047 // Window should receive motion down event.
2048 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2049
2050 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002051 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08002052 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
2053 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
2054 motionArgs.pointerCoords[0].getX() - 10);
2055
2056 mDispatcher->notifyMotion(&motionArgs);
2057 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
2058 0 /*expectedFlags*/);
2059}
2060
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002061/**
2062 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
2063 * the device default right away. In the test scenario, we check both the default value,
2064 * and the action of enabling / disabling.
2065 */
2066TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07002067 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002068 sp<FakeWindowHandle> window =
2069 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2070
2071 // Set focused application.
2072 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002073 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002074
2075 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00002076 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002077 setFocusedWindow(window);
2078
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002079 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2080
2081 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07002082 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002083 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002084 window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/);
2085
2086 SCOPED_TRACE("Disable touch mode");
2087 mDispatcher->setInTouchMode(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07002088 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002089 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002090 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002091 window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/);
2092
2093 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07002094 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002095 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002096 window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/);
2097
2098 SCOPED_TRACE("Enable touch mode again");
2099 mDispatcher->setInTouchMode(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07002100 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002101 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002102 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002103 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2104
2105 window->assertNoEvents();
2106}
2107
Gang Wange9087892020-01-07 12:17:14 -05002108TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002109 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Gang Wange9087892020-01-07 12:17:14 -05002110 sp<FakeWindowHandle> window =
2111 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2112
2113 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002114 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05002115
Arthur Hung72d8dc32020-03-28 00:48:39 +00002116 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002117 setFocusedWindow(window);
2118
Gang Wange9087892020-01-07 12:17:14 -05002119 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2120
2121 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
2122 mDispatcher->notifyKey(&keyArgs);
2123
2124 InputEvent* event = window->consume();
2125 ASSERT_NE(event, nullptr);
2126
2127 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
2128 ASSERT_NE(verified, nullptr);
2129 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
2130
2131 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
2132 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
2133 ASSERT_EQ(keyArgs.source, verified->source);
2134 ASSERT_EQ(keyArgs.displayId, verified->displayId);
2135
2136 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
2137
2138 ASSERT_EQ(keyArgs.action, verifiedKey.action);
2139 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05002140 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
2141 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
2142 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
2143 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
2144 ASSERT_EQ(0, verifiedKey.repeatCount);
2145}
2146
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002147TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002148 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002149 sp<FakeWindowHandle> window =
2150 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2151
2152 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2153
Arthur Hung72d8dc32020-03-28 00:48:39 +00002154 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002155
2156 NotifyMotionArgs motionArgs =
2157 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2158 ADISPLAY_ID_DEFAULT);
2159 mDispatcher->notifyMotion(&motionArgs);
2160
2161 InputEvent* event = window->consume();
2162 ASSERT_NE(event, nullptr);
2163
2164 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
2165 ASSERT_NE(verified, nullptr);
2166 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
2167
2168 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
2169 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
2170 EXPECT_EQ(motionArgs.source, verified->source);
2171 EXPECT_EQ(motionArgs.displayId, verified->displayId);
2172
2173 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
2174
2175 EXPECT_EQ(motionArgs.pointerCoords[0].getX(), verifiedMotion.rawX);
2176 EXPECT_EQ(motionArgs.pointerCoords[0].getY(), verifiedMotion.rawY);
2177 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
2178 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
2179 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
2180 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
2181 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
2182}
2183
yunho.shinf4a80b82020-11-16 21:13:57 +09002184TEST_F(InputDispatcherTest, NonPointerMotionEvent_JoystickNotTransformed) {
2185 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2186 sp<FakeWindowHandle> window =
2187 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2188 const std::string name = window->getName();
2189
2190 // Window gets transformed by offset values.
2191 window->setWindowOffset(500.0f, 500.0f);
2192
2193 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2194 window->setFocusable(true);
2195
2196 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2197
2198 // First, we set focused window so that focusedWindowHandle is not null.
2199 setFocusedWindow(window);
2200
2201 // Second, we consume focus event if it is right or wrong according to onFocusChangedLocked.
2202 window->consumeFocusEvent(true);
2203
2204 NotifyMotionArgs motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE,
2205 AINPUT_SOURCE_JOYSTICK, ADISPLAY_ID_DEFAULT);
2206 mDispatcher->notifyMotion(&motionArgs);
2207
2208 // Third, we consume motion event.
2209 InputEvent* event = window->consume();
2210 ASSERT_NE(event, nullptr);
2211 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
2212 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
2213 << " event, got " << inputEventTypeToString(event->getType()) << " event";
2214
2215 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
2216 EXPECT_EQ(AINPUT_EVENT_TYPE_MOTION, motionEvent.getAction());
2217
2218 float expectedX = motionArgs.pointerCoords[0].getX();
2219 float expectedY = motionArgs.pointerCoords[0].getY();
2220
2221 // Finally we test if the axis values from the final motion event are not transformed
2222 EXPECT_EQ(expectedX, motionEvent.getX(0)) << "expected " << expectedX << " for x coord of "
2223 << name.c_str() << ", got " << motionEvent.getX(0);
2224 EXPECT_EQ(expectedY, motionEvent.getY(0)) << "expected " << expectedY << " for y coord of "
2225 << name.c_str() << ", got " << motionEvent.getY(0);
2226}
2227
chaviw09c8d2d2020-08-24 15:48:26 -07002228/**
2229 * Ensure that separate calls to sign the same data are generating the same key.
2230 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
2231 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
2232 * tests.
2233 */
2234TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
2235 KeyEvent event = getTestKeyEvent();
2236 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
2237
2238 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
2239 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
2240 ASSERT_EQ(hmac1, hmac2);
2241}
2242
2243/**
2244 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
2245 */
2246TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
2247 KeyEvent event = getTestKeyEvent();
2248 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
2249 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
2250
2251 verifiedEvent.deviceId += 1;
2252 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2253
2254 verifiedEvent.source += 1;
2255 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2256
2257 verifiedEvent.eventTimeNanos += 1;
2258 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2259
2260 verifiedEvent.displayId += 1;
2261 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2262
2263 verifiedEvent.action += 1;
2264 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2265
2266 verifiedEvent.downTimeNanos += 1;
2267 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2268
2269 verifiedEvent.flags += 1;
2270 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2271
2272 verifiedEvent.keyCode += 1;
2273 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2274
2275 verifiedEvent.scanCode += 1;
2276 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2277
2278 verifiedEvent.metaState += 1;
2279 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2280
2281 verifiedEvent.repeatCount += 1;
2282 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2283}
2284
Vishnu Nair958da932020-08-21 17:12:37 -07002285TEST_F(InputDispatcherTest, SetFocusedWindow) {
2286 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2287 sp<FakeWindowHandle> windowTop =
2288 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2289 sp<FakeWindowHandle> windowSecond =
2290 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2291 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2292
2293 // Top window is also focusable but is not granted focus.
2294 windowTop->setFocusable(true);
2295 windowSecond->setFocusable(true);
2296 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2297 setFocusedWindow(windowSecond);
2298
2299 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002300 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2301 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002302
2303 // Focused window should receive event.
2304 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
2305 windowTop->assertNoEvents();
2306}
2307
2308TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
2309 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2310 sp<FakeWindowHandle> window =
2311 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2312 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2313
2314 window->setFocusable(true);
2315 // Release channel for window is no longer valid.
2316 window->releaseChannel();
2317 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2318 setFocusedWindow(window);
2319
2320 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002321 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2322 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002323
2324 // window channel is invalid, so it should not receive any input event.
2325 window->assertNoEvents();
2326}
2327
2328TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
2329 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2330 sp<FakeWindowHandle> window =
2331 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2332 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2333
2334 // Window is not focusable.
2335 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2336 setFocusedWindow(window);
2337
2338 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002339 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2340 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002341
2342 // window is invalid, so it should not receive any input event.
2343 window->assertNoEvents();
2344}
2345
2346TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
2347 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2348 sp<FakeWindowHandle> windowTop =
2349 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2350 sp<FakeWindowHandle> windowSecond =
2351 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2352 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2353
2354 windowTop->setFocusable(true);
2355 windowSecond->setFocusable(true);
2356 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2357 setFocusedWindow(windowTop);
2358 windowTop->consumeFocusEvent(true);
2359
2360 setFocusedWindow(windowSecond, windowTop);
2361 windowSecond->consumeFocusEvent(true);
2362 windowTop->consumeFocusEvent(false);
2363
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002364 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2365 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002366
2367 // Focused window should receive event.
2368 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
2369}
2370
2371TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) {
2372 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2373 sp<FakeWindowHandle> windowTop =
2374 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2375 sp<FakeWindowHandle> windowSecond =
2376 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2377 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2378
2379 windowTop->setFocusable(true);
2380 windowSecond->setFocusable(true);
2381 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2382 setFocusedWindow(windowSecond, windowTop);
2383
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002384 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2385 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002386
2387 // Event should be dropped.
2388 windowTop->assertNoEvents();
2389 windowSecond->assertNoEvents();
2390}
2391
2392TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
2393 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2394 sp<FakeWindowHandle> window =
2395 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2396 sp<FakeWindowHandle> previousFocusedWindow =
2397 new FakeWindowHandle(application, mDispatcher, "previousFocusedWindow",
2398 ADISPLAY_ID_DEFAULT);
2399 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2400
2401 window->setFocusable(true);
2402 previousFocusedWindow->setFocusable(true);
2403 window->setVisible(false);
2404 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
2405 setFocusedWindow(previousFocusedWindow);
2406 previousFocusedWindow->consumeFocusEvent(true);
2407
2408 // Requesting focus on invisible window takes focus from currently focused window.
2409 setFocusedWindow(window);
2410 previousFocusedWindow->consumeFocusEvent(false);
2411
2412 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002413 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07002414 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002415 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07002416
2417 // Window does not get focus event or key down.
2418 window->assertNoEvents();
2419
2420 // Window becomes visible.
2421 window->setVisible(true);
2422 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2423
2424 // Window receives focus event.
2425 window->consumeFocusEvent(true);
2426 // Focused window receives key down.
2427 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2428}
2429
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002430/**
2431 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
2432 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
2433 * of the 'slipperyEnterWindow'.
2434 *
2435 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
2436 * a way so that the touched location is no longer covered by the top window.
2437 *
2438 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
2439 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
2440 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
2441 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
2442 * with ACTION_DOWN).
2443 * Thus, the touch has been transferred from the top window into the bottom window, because the top
2444 * window moved itself away from the touched location and had Flag::SLIPPERY.
2445 *
2446 * Even though the top window moved away from the touched location, it is still obscuring the bottom
2447 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
2448 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
2449 *
2450 * In this test, we ensure that the event received by the bottom window has
2451 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
2452 */
2453TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
2454 constexpr int32_t SLIPPERY_PID = INJECTOR_PID + 1;
2455 constexpr int32_t SLIPPERY_UID = INJECTOR_UID + 1;
2456
2457 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2458 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2459
2460 sp<FakeWindowHandle> slipperyExitWindow =
2461 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2462 slipperyExitWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
2463 InputWindowInfo::Flag::SLIPPERY);
2464 // Make sure this one overlaps the bottom window
2465 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
2466 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
2467 // one. Windows with the same owner are not considered to be occluding each other.
2468 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
2469
2470 sp<FakeWindowHandle> slipperyEnterWindow =
2471 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2472 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
2473
2474 mDispatcher->setInputWindows(
2475 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
2476
2477 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
2478 NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2479 ADISPLAY_ID_DEFAULT, {{50, 50}});
2480 mDispatcher->notifyMotion(&args);
2481 slipperyExitWindow->consumeMotionDown();
2482 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
2483 mDispatcher->setInputWindows(
2484 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
2485
2486 args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2487 ADISPLAY_ID_DEFAULT, {{51, 51}});
2488 mDispatcher->notifyMotion(&args);
2489
2490 slipperyExitWindow->consumeMotionCancel();
2491
2492 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
2493 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
2494}
2495
Garfield Tan1c7bc862020-01-28 13:24:04 -08002496class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
2497protected:
2498 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
2499 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
2500
Chris Yea209fde2020-07-22 13:54:51 -07002501 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002502 sp<FakeWindowHandle> mWindow;
2503
2504 virtual void SetUp() override {
2505 mFakePolicy = new FakeInputDispatcherPolicy();
2506 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
2507 mDispatcher = new InputDispatcher(mFakePolicy);
2508 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
2509 ASSERT_EQ(OK, mDispatcher->start());
2510
2511 setUpWindow();
2512 }
2513
2514 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07002515 mApp = std::make_shared<FakeApplicationHandle>();
Garfield Tan1c7bc862020-01-28 13:24:04 -08002516 mWindow = new FakeWindowHandle(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2517
Vishnu Nair47074b82020-08-14 11:54:47 -07002518 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002519 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002520 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002521 mWindow->consumeFocusEvent(true);
2522 }
2523
Chris Ye2ad95392020-09-01 13:44:44 -07002524 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08002525 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07002526 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002527 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
2528 mDispatcher->notifyKey(&keyArgs);
2529
2530 // Window should receive key down event.
2531 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2532 }
2533
2534 void expectKeyRepeatOnce(int32_t repeatCount) {
2535 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
2536 InputEvent* repeatEvent = mWindow->consume();
2537 ASSERT_NE(nullptr, repeatEvent);
2538
2539 uint32_t eventType = repeatEvent->getType();
2540 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType);
2541
2542 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
2543 uint32_t eventAction = repeatKeyEvent->getAction();
2544 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
2545 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
2546 }
2547
Chris Ye2ad95392020-09-01 13:44:44 -07002548 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08002549 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07002550 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002551 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
2552 mDispatcher->notifyKey(&keyArgs);
2553
2554 // Window should receive key down event.
2555 mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
2556 0 /*expectedFlags*/);
2557 }
2558};
2559
2560TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Chris Ye2ad95392020-09-01 13:44:44 -07002561 sendAndConsumeKeyDown(1 /* deviceId */);
2562 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2563 expectKeyRepeatOnce(repeatCount);
2564 }
2565}
2566
2567TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
2568 sendAndConsumeKeyDown(1 /* deviceId */);
2569 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2570 expectKeyRepeatOnce(repeatCount);
2571 }
2572 sendAndConsumeKeyDown(2 /* deviceId */);
2573 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08002574 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2575 expectKeyRepeatOnce(repeatCount);
2576 }
2577}
2578
2579TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Chris Ye2ad95392020-09-01 13:44:44 -07002580 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002581 expectKeyRepeatOnce(1 /*repeatCount*/);
Chris Ye2ad95392020-09-01 13:44:44 -07002582 sendAndConsumeKeyUp(1 /* deviceId */);
2583 mWindow->assertNoEvents();
2584}
2585
2586TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
2587 sendAndConsumeKeyDown(1 /* deviceId */);
2588 expectKeyRepeatOnce(1 /*repeatCount*/);
2589 sendAndConsumeKeyDown(2 /* deviceId */);
2590 expectKeyRepeatOnce(1 /*repeatCount*/);
2591 // Stale key up from device 1.
2592 sendAndConsumeKeyUp(1 /* deviceId */);
2593 // Device 2 is still down, keep repeating
2594 expectKeyRepeatOnce(2 /*repeatCount*/);
2595 expectKeyRepeatOnce(3 /*repeatCount*/);
2596 // Device 2 key up
2597 sendAndConsumeKeyUp(2 /* deviceId */);
2598 mWindow->assertNoEvents();
2599}
2600
2601TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
2602 sendAndConsumeKeyDown(1 /* deviceId */);
2603 expectKeyRepeatOnce(1 /*repeatCount*/);
2604 sendAndConsumeKeyDown(2 /* deviceId */);
2605 expectKeyRepeatOnce(1 /*repeatCount*/);
2606 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
2607 sendAndConsumeKeyUp(2 /* deviceId */);
2608 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08002609 mWindow->assertNoEvents();
2610}
2611
2612TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Chris Ye2ad95392020-09-01 13:44:44 -07002613 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002614 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2615 InputEvent* repeatEvent = mWindow->consume();
2616 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2617 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
2618 IdGenerator::getSource(repeatEvent->getId()));
2619 }
2620}
2621
2622TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Chris Ye2ad95392020-09-01 13:44:44 -07002623 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002624
2625 std::unordered_set<int32_t> idSet;
2626 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2627 InputEvent* repeatEvent = mWindow->consume();
2628 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2629 int32_t id = repeatEvent->getId();
2630 EXPECT_EQ(idSet.end(), idSet.find(id));
2631 idSet.insert(id);
2632 }
2633}
2634
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002635/* Test InputDispatcher for MultiDisplay */
2636class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
2637public:
2638 static constexpr int32_t SECOND_DISPLAY_ID = 1;
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002639 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002640 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08002641
Chris Yea209fde2020-07-22 13:54:51 -07002642 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002643 windowInPrimary =
2644 new FakeWindowHandle(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002645
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002646 // Set focus window for primary display, but focused display would be second one.
2647 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07002648 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002649 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002650 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002651 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08002652
Chris Yea209fde2020-07-22 13:54:51 -07002653 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002654 windowInSecondary =
2655 new FakeWindowHandle(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002656 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002657 // Set focus display to second one.
2658 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
2659 // Set focus window for second display.
2660 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07002661 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002662 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002663 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002664 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002665 }
2666
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002667 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002668 InputDispatcherTest::TearDown();
2669
Chris Yea209fde2020-07-22 13:54:51 -07002670 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002671 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07002672 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002673 windowInSecondary.clear();
2674 }
2675
2676protected:
Chris Yea209fde2020-07-22 13:54:51 -07002677 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002678 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07002679 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002680 sp<FakeWindowHandle> windowInSecondary;
2681};
2682
2683TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
2684 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002685 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2686 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2687 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002688 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08002689 windowInSecondary->assertNoEvents();
2690
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002691 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002692 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2693 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
2694 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08002695 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002696 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08002697}
2698
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002699TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08002700 // Test inject a key down with display id specified.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002701 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2702 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002703 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08002704 windowInSecondary->assertNoEvents();
2705
2706 // Test inject a key down without display id specified.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002707 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2708 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08002709 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002710 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08002711
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002712 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00002713 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08002714
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002715 // Old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002716 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
2717 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08002718
2719 // Test inject a key down, should timeout because of no target window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002720 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2721 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08002722 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002723 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08002724 windowInSecondary->assertNoEvents();
2725}
2726
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002727// Test per-display input monitors for motion event.
2728TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08002729 FakeMonitorReceiver monitorInPrimary =
2730 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
2731 FakeMonitorReceiver monitorInSecondary =
2732 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002733
2734 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002735 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2736 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2737 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002738 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002739 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002740 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002741 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002742
2743 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002744 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2745 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
2746 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002747 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002748 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002749 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08002750 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002751
2752 // Test inject a non-pointer motion event.
2753 // If specific a display, it will dispatch to the focused window of particular display,
2754 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002755 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2756 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
2757 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002758 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002759 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002760 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08002761 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002762}
2763
2764// Test per-display input monitors for key event.
2765TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002766 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08002767 FakeMonitorReceiver monitorInPrimary =
2768 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
2769 FakeMonitorReceiver monitorInSecondary =
2770 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002771
2772 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002773 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2774 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002775 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002776 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002777 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08002778 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002779}
2780
Vishnu Nair958da932020-08-21 17:12:37 -07002781TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
2782 sp<FakeWindowHandle> secondWindowInPrimary =
2783 new FakeWindowHandle(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
2784 secondWindowInPrimary->setFocusable(true);
2785 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
2786 setFocusedWindow(secondWindowInPrimary);
2787 windowInPrimary->consumeFocusEvent(false);
2788 secondWindowInPrimary->consumeFocusEvent(true);
2789
2790 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002791 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2792 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002793 windowInPrimary->assertNoEvents();
2794 windowInSecondary->assertNoEvents();
2795 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2796}
2797
Jackal Guof9696682018-10-05 12:23:23 +08002798class InputFilterTest : public InputDispatcherTest {
2799protected:
2800 static constexpr int32_t SECOND_DISPLAY_ID = 1;
2801
2802 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) {
2803 NotifyMotionArgs motionArgs;
2804
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002805 motionArgs =
2806 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08002807 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002808 motionArgs =
2809 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08002810 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002811 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08002812 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08002813 mFakePolicy->assertFilterInputEventWasCalled(motionArgs);
Jackal Guof9696682018-10-05 12:23:23 +08002814 } else {
2815 mFakePolicy->assertFilterInputEventWasNotCalled();
2816 }
2817 }
2818
2819 void testNotifyKey(bool expectToBeFiltered) {
2820 NotifyKeyArgs keyArgs;
2821
2822 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
2823 mDispatcher->notifyKey(&keyArgs);
2824 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
2825 mDispatcher->notifyKey(&keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002826 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08002827
2828 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08002829 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08002830 } else {
2831 mFakePolicy->assertFilterInputEventWasNotCalled();
2832 }
2833 }
2834};
2835
2836// Test InputFilter for MotionEvent
2837TEST_F(InputFilterTest, MotionEvent_InputFilter) {
2838 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
2839 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
2840 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
2841
2842 // Enable InputFilter
2843 mDispatcher->setInputFilterEnabled(true);
2844 // Test touch on both primary and second display, and check if both events are filtered.
2845 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
2846 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
2847
2848 // Disable InputFilter
2849 mDispatcher->setInputFilterEnabled(false);
2850 // Test touch on both primary and second display, and check if both events aren't filtered.
2851 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
2852 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
2853}
2854
2855// Test InputFilter for KeyEvent
2856TEST_F(InputFilterTest, KeyEvent_InputFilter) {
2857 // Since the InputFilter is disabled by default, check if key event aren't filtered.
2858 testNotifyKey(/*expectToBeFiltered*/ false);
2859
2860 // Enable InputFilter
2861 mDispatcher->setInputFilterEnabled(true);
2862 // Send a key event, and check if it is filtered.
2863 testNotifyKey(/*expectToBeFiltered*/ true);
2864
2865 // Disable InputFilter
2866 mDispatcher->setInputFilterEnabled(false);
2867 // Send a key event, and check if it isn't filtered.
2868 testNotifyKey(/*expectToBeFiltered*/ false);
2869}
2870
chaviwfd6d3512019-03-25 13:23:49 -07002871class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002872 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07002873 InputDispatcherTest::SetUp();
2874
Chris Yea209fde2020-07-22 13:54:51 -07002875 std::shared_ptr<FakeApplicationHandle> application =
2876 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002877 mUnfocusedWindow =
2878 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07002879 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
2880 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
2881 // window.
Michael Wright44753b12020-07-08 13:48:11 +01002882 mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07002883
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002884 mFocusedWindow =
2885 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2886 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01002887 mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07002888
2889 // Set focused application.
2890 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002891 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07002892
2893 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00002894 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002895 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002896 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07002897 }
2898
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002899 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07002900 InputDispatcherTest::TearDown();
2901
2902 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002903 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07002904 }
2905
2906protected:
2907 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002908 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002909 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07002910};
2911
2912// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
2913// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
2914// the onPointerDownOutsideFocus callback.
2915TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002916 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002917 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2918 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002919 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002920 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07002921
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002922 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07002923 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
2924}
2925
2926// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
2927// DOWN on the window that doesn't have focus. Ensure no window received the
2928// onPointerDownOutsideFocus callback.
2929TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002930 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002931 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002932 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002933 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07002934
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002935 ASSERT_TRUE(mDispatcher->waitForIdle());
2936 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07002937}
2938
2939// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
2940// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
2941TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002942 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2943 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002944 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07002945
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002946 ASSERT_TRUE(mDispatcher->waitForIdle());
2947 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07002948}
2949
2950// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
2951// DOWN on the window that already has focus. Ensure no window received the
2952// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002953TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002954 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002955 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002956 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002957 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002958 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07002959
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002960 ASSERT_TRUE(mDispatcher->waitForIdle());
2961 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07002962}
2963
chaviwaf87b3e2019-10-01 16:59:28 -07002964// These tests ensures we can send touch events to a single client when there are multiple input
2965// windows that point to the same client token.
2966class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
2967 virtual void SetUp() override {
2968 InputDispatcherTest::SetUp();
2969
Chris Yea209fde2020-07-22 13:54:51 -07002970 std::shared_ptr<FakeApplicationHandle> application =
2971 std::make_shared<FakeApplicationHandle>();
chaviwaf87b3e2019-10-01 16:59:28 -07002972 mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1",
2973 ADISPLAY_ID_DEFAULT);
2974 // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window.
2975 // 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 +01002976 mWindow1->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
2977 InputWindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07002978 mWindow1->setFrame(Rect(0, 0, 100, 100));
2979
2980 mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2",
2981 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
Michael Wright44753b12020-07-08 13:48:11 +01002982 mWindow2->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
2983 InputWindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07002984 mWindow2->setFrame(Rect(100, 100, 200, 200));
2985
Arthur Hung72d8dc32020-03-28 00:48:39 +00002986 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07002987 }
2988
2989protected:
2990 sp<FakeWindowHandle> mWindow1;
2991 sp<FakeWindowHandle> mWindow2;
2992
2993 // Helper function to convert the point from screen coordinates into the window's space
2994 static PointF getPointInWindow(const InputWindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07002995 vec2 vals = windowInfo->transform.transform(point.x, point.y);
2996 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07002997 }
2998
2999 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
3000 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003001 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07003002 InputEvent* event = window->consume();
3003
3004 ASSERT_NE(nullptr, event) << name.c_str()
3005 << ": consumer should have returned non-NULL event.";
3006
3007 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
3008 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
3009 << " event, got " << inputEventTypeToString(event->getType()) << " event";
3010
3011 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
3012 EXPECT_EQ(expectedAction, motionEvent.getAction());
3013
3014 for (size_t i = 0; i < points.size(); i++) {
3015 float expectedX = points[i].x;
3016 float expectedY = points[i].y;
3017
3018 EXPECT_EQ(expectedX, motionEvent.getX(i))
3019 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
3020 << ", got " << motionEvent.getX(i);
3021 EXPECT_EQ(expectedY, motionEvent.getY(i))
3022 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
3023 << ", got " << motionEvent.getY(i);
3024 }
3025 }
chaviw9eaa22c2020-07-01 16:21:27 -07003026
3027 void touchAndAssertPositions(int32_t action, std::vector<PointF> touchedPoints,
3028 std::vector<PointF> expectedPoints) {
3029 NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
3030 ADISPLAY_ID_DEFAULT, touchedPoints);
3031 mDispatcher->notifyMotion(&motionArgs);
3032
3033 // Always consume from window1 since it's the window that has the InputReceiver
3034 consumeMotionEvent(mWindow1, action, expectedPoints);
3035 }
chaviwaf87b3e2019-10-01 16:59:28 -07003036};
3037
3038TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
3039 // Touch Window 1
3040 PointF touchedPoint = {10, 10};
3041 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003042 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003043
3044 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07003045 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003046
3047 // Touch Window 2
3048 touchedPoint = {150, 150};
3049 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003050 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003051}
3052
chaviw9eaa22c2020-07-01 16:21:27 -07003053TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
3054 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07003055 mWindow2->setWindowScale(0.5f, 0.5f);
3056
3057 // Touch Window 1
3058 PointF touchedPoint = {10, 10};
3059 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003060 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003061 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07003062 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003063
3064 // Touch Window 2
3065 touchedPoint = {150, 150};
3066 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003067 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
3068 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003069
chaviw9eaa22c2020-07-01 16:21:27 -07003070 // Update the transform so rotation is set
3071 mWindow2->setWindowTransform(0, -1, 1, 0);
3072 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
3073 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003074}
3075
chaviw9eaa22c2020-07-01 16:21:27 -07003076TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003077 mWindow2->setWindowScale(0.5f, 0.5f);
3078
3079 // Touch Window 1
3080 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3081 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003082 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003083
3084 // Touch Window 2
3085 int32_t actionPointerDown =
3086 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003087 touchedPoints.push_back(PointF{150, 150});
3088 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3089 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003090
chaviw9eaa22c2020-07-01 16:21:27 -07003091 // Release Window 2
3092 int32_t actionPointerUp =
3093 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3094 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
3095 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003096
chaviw9eaa22c2020-07-01 16:21:27 -07003097 // Update the transform so rotation is set for Window 2
3098 mWindow2->setWindowTransform(0, -1, 1, 0);
3099 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3100 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003101}
3102
chaviw9eaa22c2020-07-01 16:21:27 -07003103TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003104 mWindow2->setWindowScale(0.5f, 0.5f);
3105
3106 // Touch Window 1
3107 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3108 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003109 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003110
3111 // Touch Window 2
3112 int32_t actionPointerDown =
3113 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003114 touchedPoints.push_back(PointF{150, 150});
3115 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003116
chaviw9eaa22c2020-07-01 16:21:27 -07003117 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003118
3119 // Move both windows
3120 touchedPoints = {{20, 20}, {175, 175}};
3121 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3122 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3123
chaviw9eaa22c2020-07-01 16:21:27 -07003124 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003125
chaviw9eaa22c2020-07-01 16:21:27 -07003126 // Release Window 2
3127 int32_t actionPointerUp =
3128 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3129 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
3130 expectedPoints.pop_back();
3131
3132 // Touch Window 2
3133 mWindow2->setWindowTransform(0, -1, 1, 0);
3134 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3135 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
3136
3137 // Move both windows
3138 touchedPoints = {{20, 20}, {175, 175}};
3139 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3140 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3141
3142 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003143}
3144
3145TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
3146 mWindow1->setWindowScale(0.5f, 0.5f);
3147
3148 // Touch Window 1
3149 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3150 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003151 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003152
3153 // Touch Window 2
3154 int32_t actionPointerDown =
3155 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003156 touchedPoints.push_back(PointF{150, 150});
3157 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003158
chaviw9eaa22c2020-07-01 16:21:27 -07003159 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003160
3161 // Move both windows
3162 touchedPoints = {{20, 20}, {175, 175}};
3163 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3164 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3165
chaviw9eaa22c2020-07-01 16:21:27 -07003166 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003167}
3168
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003169class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
3170 virtual void SetUp() override {
3171 InputDispatcherTest::SetUp();
3172
Chris Yea209fde2020-07-22 13:54:51 -07003173 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003174 mApplication->setDispatchingTimeout(20ms);
3175 mWindow =
3176 new FakeWindowHandle(mApplication, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
3177 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05003178 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07003179 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003180 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3181 // window.
Michael Wright44753b12020-07-08 13:48:11 +01003182 mWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003183
3184 // Set focused application.
3185 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
3186
3187 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003188 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003189 mWindow->consumeFocusEvent(true);
3190 }
3191
3192 virtual void TearDown() override {
3193 InputDispatcherTest::TearDown();
3194 mWindow.clear();
3195 }
3196
3197protected:
Chris Yea209fde2020-07-22 13:54:51 -07003198 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003199 sp<FakeWindowHandle> mWindow;
3200 static constexpr PointF WINDOW_LOCATION = {20, 20};
3201
3202 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003203 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003204 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3205 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003206 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003207 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3208 WINDOW_LOCATION));
3209 }
3210};
3211
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003212// Send a tap and respond, which should not cause an ANR.
3213TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
3214 tapOnWindow();
3215 mWindow->consumeMotionDown();
3216 mWindow->consumeMotionUp();
3217 ASSERT_TRUE(mDispatcher->waitForIdle());
3218 mFakePolicy->assertNotifyAnrWasNotCalled();
3219}
3220
3221// Send a regular key and respond, which should not cause an ANR.
3222TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003223 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003224 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
3225 ASSERT_TRUE(mDispatcher->waitForIdle());
3226 mFakePolicy->assertNotifyAnrWasNotCalled();
3227}
3228
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003229TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
3230 mWindow->setFocusable(false);
3231 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3232 mWindow->consumeFocusEvent(false);
3233
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003234 InputEventInjectionResult result =
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003235 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003236 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
3237 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003238 // Key will not go to window because we have no focused window.
3239 // The 'no focused window' ANR timer should start instead.
3240
3241 // Now, the focused application goes away.
3242 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
3243 // The key should get dropped and there should be no ANR.
3244
3245 ASSERT_TRUE(mDispatcher->waitForIdle());
3246 mFakePolicy->assertNotifyAnrWasNotCalled();
3247}
3248
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003249// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003250// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
3251// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003252TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003253 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003254 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3255 WINDOW_LOCATION));
3256
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003257 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
3258 ASSERT_TRUE(sequenceNum);
3259 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003260 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003261
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003262 mWindow->finishEvent(*sequenceNum);
3263 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3264 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003265 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003266 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003267}
3268
3269// Send a key to the app and have the app not respond right away.
3270TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
3271 // Inject a key, and don't respond - expect that ANR is called.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003272 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003273 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
3274 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003275 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003276 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003277 ASSERT_TRUE(mDispatcher->waitForIdle());
3278}
3279
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003280// We have a focused application, but no focused window
3281TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003282 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003283 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3284 mWindow->consumeFocusEvent(false);
3285
3286 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003287 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003288 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3289 WINDOW_LOCATION));
3290 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
3291 mDispatcher->waitForIdle();
3292 mFakePolicy->assertNotifyAnrWasNotCalled();
3293
3294 // Once a focused event arrives, we get an ANR for this application
3295 // We specify the injection timeout to be smaller than the application timeout, to ensure that
3296 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003297 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003298 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003299 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3300 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003301 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003302 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003303 ASSERT_TRUE(mDispatcher->waitForIdle());
3304}
3305
3306// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003307// Make sure that we don't notify policy twice about the same ANR.
3308TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003309 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003310 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3311 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003312
3313 // Once a focused event arrives, we get an ANR for this application
3314 // We specify the injection timeout to be smaller than the application timeout, to ensure that
3315 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003316 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003317 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003318 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3319 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003320 const std::chrono::duration appTimeout =
3321 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003322 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003323
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003324 std::this_thread::sleep_for(appTimeout);
3325 // ANR should not be raised again. It is up to policy to do that if it desires.
3326 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003327
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003328 // If we now get a focused window, the ANR should stop, but the policy handles that via
3329 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003330 ASSERT_TRUE(mDispatcher->waitForIdle());
3331}
3332
3333// We have a focused application, but no focused window
3334TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003335 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003336 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3337 mWindow->consumeFocusEvent(false);
3338
3339 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003340 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003341 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003342 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3343 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003344
3345 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003346 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003347
3348 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003349 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003350 ASSERT_TRUE(mDispatcher->waitForIdle());
3351 mWindow->assertNoEvents();
3352}
3353
3354/**
3355 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
3356 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
3357 * If we process 1 of the events, but ANR on the second event with the same timestamp,
3358 * the ANR mechanism should still work.
3359 *
3360 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
3361 * DOWN event, while not responding on the second one.
3362 */
3363TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
3364 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
3365 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3366 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
3367 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
3368 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003369 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003370
3371 // Now send ACTION_UP, with identical timestamp
3372 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3373 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
3374 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
3375 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003376 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003377
3378 // We have now sent down and up. Let's consume first event and then ANR on the second.
3379 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3380 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003381 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003382}
3383
3384// If an app is not responding to a key event, gesture monitors should continue to receive
3385// new motion events
3386TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnKey) {
3387 FakeMonitorReceiver monitor =
3388 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
3389 true /*isGestureMonitor*/);
3390
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003391 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3392 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003393 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003394 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003395
3396 // Stuck on the ACTION_UP
3397 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003398 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003399
3400 // New tap will go to the gesture monitor, but not to the window
3401 tapOnWindow();
3402 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3403 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3404
3405 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
3406 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003407 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003408 mWindow->assertNoEvents();
3409 monitor.assertNoEvents();
3410}
3411
3412// If an app is not responding to a motion event, gesture monitors should continue to receive
3413// new motion events
3414TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnMotion) {
3415 FakeMonitorReceiver monitor =
3416 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
3417 true /*isGestureMonitor*/);
3418
3419 tapOnWindow();
3420 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3421 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3422
3423 mWindow->consumeMotionDown();
3424 // Stuck on the ACTION_UP
3425 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003426 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003427
3428 // New tap will go to the gesture monitor, but not to the window
3429 tapOnWindow();
3430 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3431 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3432
3433 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
3434 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003435 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003436 mWindow->assertNoEvents();
3437 monitor.assertNoEvents();
3438}
3439
3440// If a window is unresponsive, then you get anr. if the window later catches up and starts to
3441// process events, you don't get an anr. When the window later becomes unresponsive again, you
3442// get an ANR again.
3443// 1. tap -> block on ACTION_UP -> receive ANR
3444// 2. consume all pending events (= queue becomes healthy again)
3445// 3. tap again -> block on ACTION_UP again -> receive ANR second time
3446TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
3447 tapOnWindow();
3448
3449 mWindow->consumeMotionDown();
3450 // Block on ACTION_UP
3451 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003452 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003453 mWindow->consumeMotionUp(); // Now the connection should be healthy again
3454 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003455 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003456 mWindow->assertNoEvents();
3457
3458 tapOnWindow();
3459 mWindow->consumeMotionDown();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003460 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003461 mWindow->consumeMotionUp();
3462
3463 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003464 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003465 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003466 mWindow->assertNoEvents();
3467}
3468
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003469// If a connection remains unresponsive for a while, make sure policy is only notified once about
3470// it.
3471TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003472 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003473 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3474 WINDOW_LOCATION));
3475
3476 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003477 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003478 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003479 // 'notifyConnectionUnresponsive' should only be called once per connection
3480 mFakePolicy->assertNotifyAnrWasNotCalled();
3481 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003482 mWindow->consumeMotionDown();
3483 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3484 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3485 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003486 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003487 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003488 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003489}
3490
3491/**
3492 * If a window is processing a motion event, and then a key event comes in, the key event should
3493 * not to to the focused window until the motion is processed.
3494 *
3495 * Warning!!!
3496 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
3497 * and the injection timeout that we specify when injecting the key.
3498 * We must have the injection timeout (10ms) be smaller than
3499 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
3500 *
3501 * If that value changes, this test should also change.
3502 */
3503TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
3504 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3505 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3506
3507 tapOnWindow();
3508 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3509 ASSERT_TRUE(downSequenceNum);
3510 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3511 ASSERT_TRUE(upSequenceNum);
3512 // Don't finish the events yet, and send a key
3513 // Injection will "succeed" because we will eventually give up and send the key to the focused
3514 // window even if motions are still being processed. But because the injection timeout is short,
3515 // we will receive INJECTION_TIMED_OUT as the result.
3516
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003517 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003518 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003519 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3520 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003521 // Key will not be sent to the window, yet, because the window is still processing events
3522 // and the key remains pending, waiting for the touch events to be processed
3523 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3524 ASSERT_FALSE(keySequenceNum);
3525
3526 std::this_thread::sleep_for(500ms);
3527 // if we wait long enough though, dispatcher will give up, and still send the key
3528 // to the focused window, even though we have not yet finished the motion event
3529 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3530 mWindow->finishEvent(*downSequenceNum);
3531 mWindow->finishEvent(*upSequenceNum);
3532}
3533
3534/**
3535 * If a window is processing a motion event, and then a key event comes in, the key event should
3536 * not go to the focused window until the motion is processed.
3537 * If then a new motion comes in, then the pending key event should be going to the currently
3538 * focused window right away.
3539 */
3540TEST_F(InputDispatcherSingleWindowAnr,
3541 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
3542 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3543 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3544
3545 tapOnWindow();
3546 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3547 ASSERT_TRUE(downSequenceNum);
3548 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3549 ASSERT_TRUE(upSequenceNum);
3550 // Don't finish the events yet, and send a key
3551 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003552 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003553 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003554 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003555 // At this point, key is still pending, and should not be sent to the application yet.
3556 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3557 ASSERT_FALSE(keySequenceNum);
3558
3559 // Now tap down again. It should cause the pending key to go to the focused window right away.
3560 tapOnWindow();
3561 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
3562 // the other events yet. We can finish events in any order.
3563 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
3564 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
3565 mWindow->consumeMotionDown();
3566 mWindow->consumeMotionUp();
3567 mWindow->assertNoEvents();
3568}
3569
3570class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
3571 virtual void SetUp() override {
3572 InputDispatcherTest::SetUp();
3573
Chris Yea209fde2020-07-22 13:54:51 -07003574 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003575 mApplication->setDispatchingTimeout(10ms);
3576 mUnfocusedWindow =
3577 new FakeWindowHandle(mApplication, mDispatcher, "Unfocused", ADISPLAY_ID_DEFAULT);
3578 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
3579 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3580 // window.
3581 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Michael Wright44753b12020-07-08 13:48:11 +01003582 mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3583 InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH |
3584 InputWindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003585
3586 mFocusedWindow =
3587 new FakeWindowHandle(mApplication, mDispatcher, "Focused", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05003588 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003589 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01003590 mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3591 InputWindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003592
3593 // Set focused application.
3594 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07003595 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003596
3597 // Expect one focus window exist in display.
3598 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003599 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003600 mFocusedWindow->consumeFocusEvent(true);
3601 }
3602
3603 virtual void TearDown() override {
3604 InputDispatcherTest::TearDown();
3605
3606 mUnfocusedWindow.clear();
3607 mFocusedWindow.clear();
3608 }
3609
3610protected:
Chris Yea209fde2020-07-22 13:54:51 -07003611 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003612 sp<FakeWindowHandle> mUnfocusedWindow;
3613 sp<FakeWindowHandle> mFocusedWindow;
3614 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
3615 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
3616 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
3617
3618 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
3619
3620 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
3621
3622private:
3623 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003624 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003625 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3626 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003627 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003628 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3629 location));
3630 }
3631};
3632
3633// If we have 2 windows that are both unresponsive, the one with the shortest timeout
3634// should be ANR'd first.
3635TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003636 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003637 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3638 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003639 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003640 mFocusedWindow->consumeMotionDown();
3641 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3642 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3643 // We consumed all events, so no ANR
3644 ASSERT_TRUE(mDispatcher->waitForIdle());
3645 mFakePolicy->assertNotifyAnrWasNotCalled();
3646
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003647 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003648 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3649 FOCUSED_WINDOW_LOCATION));
3650 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
3651 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003652
3653 const std::chrono::duration timeout =
3654 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003655 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003656 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
3657 // sequence to make it consistent
3658 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003659 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003660 mFocusedWindow->consumeMotionDown();
3661 // This cancel is generated because the connection was unresponsive
3662 mFocusedWindow->consumeMotionCancel();
3663 mFocusedWindow->assertNoEvents();
3664 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003665 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003666 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003667 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003668}
3669
3670// If we have 2 windows with identical timeouts that are both unresponsive,
3671// it doesn't matter which order they should have ANR.
3672// But we should receive ANR for both.
3673TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
3674 // Set the timeout for unfocused window to match the focused window
3675 mUnfocusedWindow->setDispatchingTimeout(10ms);
3676 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
3677
3678 tapOnFocusedWindow();
3679 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003680 sp<IBinder> anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms);
3681 sp<IBinder> anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003682
3683 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003684 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
3685 mFocusedWindow->getToken() == anrConnectionToken2);
3686 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
3687 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003688
3689 ASSERT_TRUE(mDispatcher->waitForIdle());
3690 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003691
3692 mFocusedWindow->consumeMotionDown();
3693 mFocusedWindow->consumeMotionUp();
3694 mUnfocusedWindow->consumeMotionOutside();
3695
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003696 sp<IBinder> responsiveToken1 = mFakePolicy->getResponsiveWindowToken();
3697 sp<IBinder> responsiveToken2 = mFakePolicy->getResponsiveWindowToken();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003698
3699 // Both applications should be marked as responsive, in any order
3700 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
3701 mFocusedWindow->getToken() == responsiveToken2);
3702 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
3703 mUnfocusedWindow->getToken() == responsiveToken2);
3704 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003705}
3706
3707// If a window is already not responding, the second tap on the same window should be ignored.
3708// We should also log an error to account for the dropped event (not tested here).
3709// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
3710TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
3711 tapOnFocusedWindow();
3712 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3713 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3714 // Receive the events, but don't respond
3715 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
3716 ASSERT_TRUE(downEventSequenceNum);
3717 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
3718 ASSERT_TRUE(upEventSequenceNum);
3719 const std::chrono::duration timeout =
3720 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003721 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003722
3723 // Tap once again
3724 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003725 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003726 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3727 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003728 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003729 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3730 FOCUSED_WINDOW_LOCATION));
3731 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
3732 // valid touch target
3733 mUnfocusedWindow->assertNoEvents();
3734
3735 // Consume the first tap
3736 mFocusedWindow->finishEvent(*downEventSequenceNum);
3737 mFocusedWindow->finishEvent(*upEventSequenceNum);
3738 ASSERT_TRUE(mDispatcher->waitForIdle());
3739 // The second tap did not go to the focused window
3740 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003741 // Since all events are finished, connection should be deemed healthy again
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003742 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003743 mFakePolicy->assertNotifyAnrWasNotCalled();
3744}
3745
3746// If you tap outside of all windows, there will not be ANR
3747TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003748 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003749 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3750 LOCATION_OUTSIDE_ALL_WINDOWS));
3751 ASSERT_TRUE(mDispatcher->waitForIdle());
3752 mFakePolicy->assertNotifyAnrWasNotCalled();
3753}
3754
3755// Since the focused window is paused, tapping on it should not produce any events
3756TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
3757 mFocusedWindow->setPaused(true);
3758 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
3759
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003760 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003761 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3762 FOCUSED_WINDOW_LOCATION));
3763
3764 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
3765 ASSERT_TRUE(mDispatcher->waitForIdle());
3766 // Should not ANR because the window is paused, and touches shouldn't go to it
3767 mFakePolicy->assertNotifyAnrWasNotCalled();
3768
3769 mFocusedWindow->assertNoEvents();
3770 mUnfocusedWindow->assertNoEvents();
3771}
3772
3773/**
3774 * If a window is processing a motion event, and then a key event comes in, the key event should
3775 * not to to the focused window until the motion is processed.
3776 * If a different window becomes focused at this time, the key should go to that window instead.
3777 *
3778 * Warning!!!
3779 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
3780 * and the injection timeout that we specify when injecting the key.
3781 * We must have the injection timeout (10ms) be smaller than
3782 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
3783 *
3784 * If that value changes, this test should also change.
3785 */
3786TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
3787 // Set a long ANR timeout to prevent it from triggering
3788 mFocusedWindow->setDispatchingTimeout(2s);
3789 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
3790
3791 tapOnUnfocusedWindow();
3792 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
3793 ASSERT_TRUE(downSequenceNum);
3794 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
3795 ASSERT_TRUE(upSequenceNum);
3796 // Don't finish the events yet, and send a key
3797 // Injection will succeed because we will eventually give up and send the key to the focused
3798 // window even if motions are still being processed.
3799
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003800 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003801 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003802 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
3803 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003804 // Key will not be sent to the window, yet, because the window is still processing events
3805 // and the key remains pending, waiting for the touch events to be processed
3806 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
3807 ASSERT_FALSE(keySequenceNum);
3808
3809 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07003810 mFocusedWindow->setFocusable(false);
3811 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003812 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003813 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003814
3815 // Focus events should precede the key events
3816 mUnfocusedWindow->consumeFocusEvent(true);
3817 mFocusedWindow->consumeFocusEvent(false);
3818
3819 // Finish the tap events, which should unblock dispatcher
3820 mUnfocusedWindow->finishEvent(*downSequenceNum);
3821 mUnfocusedWindow->finishEvent(*upSequenceNum);
3822
3823 // Now that all queues are cleared and no backlog in the connections, the key event
3824 // can finally go to the newly focused "mUnfocusedWindow".
3825 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3826 mFocusedWindow->assertNoEvents();
3827 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003828 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003829}
3830
3831// When the touch stream is split across 2 windows, and one of them does not respond,
3832// then ANR should be raised and the touch should be canceled for the unresponsive window.
3833// The other window should not be affected by that.
3834TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
3835 // Touch Window 1
3836 NotifyMotionArgs motionArgs =
3837 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3838 ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION});
3839 mDispatcher->notifyMotion(&motionArgs);
3840 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3841 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3842
3843 // Touch Window 2
3844 int32_t actionPointerDown =
3845 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3846
3847 motionArgs =
3848 generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3849 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION});
3850 mDispatcher->notifyMotion(&motionArgs);
3851
3852 const std::chrono::duration timeout =
3853 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003854 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003855
3856 mUnfocusedWindow->consumeMotionDown();
3857 mFocusedWindow->consumeMotionDown();
3858 // Focused window may or may not receive ACTION_MOVE
3859 // But it should definitely receive ACTION_CANCEL due to the ANR
3860 InputEvent* event;
3861 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
3862 ASSERT_TRUE(moveOrCancelSequenceNum);
3863 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
3864 ASSERT_NE(nullptr, event);
3865 ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION);
3866 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
3867 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
3868 mFocusedWindow->consumeMotionCancel();
3869 } else {
3870 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
3871 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003872 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003873 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003874
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003875 mUnfocusedWindow->assertNoEvents();
3876 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003877 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003878}
3879
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05003880/**
3881 * If we have no focused window, and a key comes in, we start the ANR timer.
3882 * The focused application should add a focused window before the timer runs out to prevent ANR.
3883 *
3884 * If the user touches another application during this time, the key should be dropped.
3885 * Next, if a new focused window comes in, without toggling the focused application,
3886 * then no ANR should occur.
3887 *
3888 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
3889 * but in some cases the policy may not update the focused application.
3890 */
3891TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
3892 std::shared_ptr<FakeApplicationHandle> focusedApplication =
3893 std::make_shared<FakeApplicationHandle>();
3894 focusedApplication->setDispatchingTimeout(60ms);
3895 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
3896 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
3897 mFocusedWindow->setFocusable(false);
3898
3899 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
3900 mFocusedWindow->consumeFocusEvent(false);
3901
3902 // Send a key. The ANR timer should start because there is no focused window.
3903 // 'focusedApplication' will get blamed if this timer completes.
3904 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003905 InputEventInjectionResult result =
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05003906 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003907 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
3908 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05003909
3910 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
3911 // then the injected touches won't cause the focused event to get dropped.
3912 // The dispatcher only checks for whether the queue should be pruned upon queueing.
3913 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
3914 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
3915 // For this test, it means that the key would get delivered to the window once it becomes
3916 // focused.
3917 std::this_thread::sleep_for(10ms);
3918
3919 // Touch unfocused window. This should force the pending key to get dropped.
3920 NotifyMotionArgs motionArgs =
3921 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3922 ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION});
3923 mDispatcher->notifyMotion(&motionArgs);
3924
3925 // We do not consume the motion right away, because that would require dispatcher to first
3926 // process (== drop) the key event, and by that time, ANR will be raised.
3927 // Set the focused window first.
3928 mFocusedWindow->setFocusable(true);
3929 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
3930 setFocusedWindow(mFocusedWindow);
3931 mFocusedWindow->consumeFocusEvent(true);
3932 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
3933 // to another application. This could be a bug / behaviour in the policy.
3934
3935 mUnfocusedWindow->consumeMotionDown();
3936
3937 ASSERT_TRUE(mDispatcher->waitForIdle());
3938 // Should not ANR because we actually have a focused window. It was just added too slowly.
3939 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
3940}
3941
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05003942// These tests ensure we cannot send touch events to a window that's positioned behind a window
3943// that has feature NO_INPUT_CHANNEL.
3944// Layout:
3945// Top (closest to user)
3946// mNoInputWindow (above all windows)
3947// mBottomWindow
3948// Bottom (furthest from user)
3949class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
3950 virtual void SetUp() override {
3951 InputDispatcherTest::SetUp();
3952
3953 mApplication = std::make_shared<FakeApplicationHandle>();
3954 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
3955 "Window without input channel", ADISPLAY_ID_DEFAULT,
3956 std::make_optional<sp<IBinder>>(nullptr) /*token*/);
3957
3958 mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL);
3959 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
3960 // It's perfectly valid for this window to not have an associated input channel
3961
3962 mBottomWindow = new FakeWindowHandle(mApplication, mDispatcher, "Bottom window",
3963 ADISPLAY_ID_DEFAULT);
3964 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
3965
3966 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
3967 }
3968
3969protected:
3970 std::shared_ptr<FakeApplicationHandle> mApplication;
3971 sp<FakeWindowHandle> mNoInputWindow;
3972 sp<FakeWindowHandle> mBottomWindow;
3973};
3974
3975TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
3976 PointF touchedPoint = {10, 10};
3977
3978 NotifyMotionArgs motionArgs =
3979 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3980 ADISPLAY_ID_DEFAULT, {touchedPoint});
3981 mDispatcher->notifyMotion(&motionArgs);
3982
3983 mNoInputWindow->assertNoEvents();
3984 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
3985 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
3986 // and therefore should prevent mBottomWindow from receiving touches
3987 mBottomWindow->assertNoEvents();
3988}
3989
3990/**
3991 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
3992 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
3993 */
3994TEST_F(InputDispatcherMultiWindowOcclusionTests,
3995 NoInputChannelFeature_DropsTouchesWithValidChannel) {
3996 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
3997 "Window with input channel and NO_INPUT_CHANNEL",
3998 ADISPLAY_ID_DEFAULT);
3999
4000 mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL);
4001 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
4002 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
4003
4004 PointF touchedPoint = {10, 10};
4005
4006 NotifyMotionArgs motionArgs =
4007 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4008 ADISPLAY_ID_DEFAULT, {touchedPoint});
4009 mDispatcher->notifyMotion(&motionArgs);
4010
4011 mNoInputWindow->assertNoEvents();
4012 mBottomWindow->assertNoEvents();
4013}
4014
Vishnu Nair958da932020-08-21 17:12:37 -07004015class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
4016protected:
4017 std::shared_ptr<FakeApplicationHandle> mApp;
4018 sp<FakeWindowHandle> mWindow;
4019 sp<FakeWindowHandle> mMirror;
4020
4021 virtual void SetUp() override {
4022 InputDispatcherTest::SetUp();
4023 mApp = std::make_shared<FakeApplicationHandle>();
4024 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
4025 mMirror = new FakeWindowHandle(mApp, mDispatcher, "TestWindowMirror", ADISPLAY_ID_DEFAULT,
4026 mWindow->getToken());
4027 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
4028 mWindow->setFocusable(true);
4029 mMirror->setFocusable(true);
4030 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4031 }
4032};
4033
4034TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
4035 // Request focus on a mirrored window
4036 setFocusedWindow(mMirror);
4037
4038 // window gets focused
4039 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004040 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4041 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004042 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
4043}
4044
4045// A focused & mirrored window remains focused only if the window and its mirror are both
4046// focusable.
4047TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
4048 setFocusedWindow(mMirror);
4049
4050 // window gets focused
4051 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004052 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4053 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004054 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004055 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4056 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004057 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4058
4059 mMirror->setFocusable(false);
4060 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4061
4062 // window loses focus since one of the windows associated with the token in not focusable
4063 mWindow->consumeFocusEvent(false);
4064
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004065 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4066 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004067 mWindow->assertNoEvents();
4068}
4069
4070// A focused & mirrored window remains focused until the window and its mirror both become
4071// invisible.
4072TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
4073 setFocusedWindow(mMirror);
4074
4075 // window gets focused
4076 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004077 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4078 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004079 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004080 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4081 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004082 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4083
4084 mMirror->setVisible(false);
4085 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4086
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004087 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4088 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004089 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004090 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4091 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004092 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4093
4094 mWindow->setVisible(false);
4095 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4096
4097 // window loses focus only after all windows associated with the token become invisible.
4098 mWindow->consumeFocusEvent(false);
4099
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004100 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4101 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004102 mWindow->assertNoEvents();
4103}
4104
4105// A focused & mirrored window remains focused until both windows are removed.
4106TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
4107 setFocusedWindow(mMirror);
4108
4109 // window gets focused
4110 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004111 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4112 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004113 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004114 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4115 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004116 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4117
4118 // single window is removed but the window token remains focused
4119 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
4120
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004121 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4122 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004123 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004124 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4125 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004126 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4127
4128 // Both windows are removed
4129 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
4130 mWindow->consumeFocusEvent(false);
4131
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004132 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4133 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004134 mWindow->assertNoEvents();
4135}
4136
4137// Focus request can be pending until one window becomes visible.
4138TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
4139 // Request focus on an invisible mirror.
4140 mWindow->setVisible(false);
4141 mMirror->setVisible(false);
4142 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4143 setFocusedWindow(mMirror);
4144
4145 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004146 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07004147 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004148 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07004149
4150 mMirror->setVisible(true);
4151 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4152
4153 // window gets focused
4154 mWindow->consumeFocusEvent(true);
4155 // window gets the pending key event
4156 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4157}
Prabir Pradhan99987712020-11-10 18:43:05 -08004158
4159class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
4160protected:
4161 std::shared_ptr<FakeApplicationHandle> mApp;
4162 sp<FakeWindowHandle> mWindow;
4163 sp<FakeWindowHandle> mSecondWindow;
4164
4165 void SetUp() override {
4166 InputDispatcherTest::SetUp();
4167 mApp = std::make_shared<FakeApplicationHandle>();
4168 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
4169 mWindow->setFocusable(true);
4170 mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
4171 mSecondWindow->setFocusable(true);
4172
4173 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
4174 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
4175
4176 setFocusedWindow(mWindow);
4177 mWindow->consumeFocusEvent(true);
4178 }
4179
4180 void notifyPointerCaptureChanged(bool enabled) {
4181 const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(enabled);
4182 mDispatcher->notifyPointerCaptureChanged(&args);
4183 }
4184
4185 void requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, bool enabled) {
4186 mDispatcher->requestPointerCapture(window->getToken(), enabled);
4187 mFakePolicy->waitForSetPointerCapture(enabled);
4188 notifyPointerCaptureChanged(enabled);
4189 window->consumeCaptureEvent(enabled);
4190 }
4191};
4192
4193TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
4194 // Ensure that capture cannot be obtained for unfocused windows.
4195 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
4196 mFakePolicy->assertSetPointerCaptureNotCalled();
4197 mSecondWindow->assertNoEvents();
4198
4199 // Ensure that capture can be enabled from the focus window.
4200 requestAndVerifyPointerCapture(mWindow, true);
4201
4202 // Ensure that capture cannot be disabled from a window that does not have capture.
4203 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
4204 mFakePolicy->assertSetPointerCaptureNotCalled();
4205
4206 // Ensure that capture can be disabled from the window with capture.
4207 requestAndVerifyPointerCapture(mWindow, false);
4208}
4209
4210TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
4211 requestAndVerifyPointerCapture(mWindow, true);
4212
4213 setFocusedWindow(mSecondWindow);
4214
4215 // Ensure that the capture disabled event was sent first.
4216 mWindow->consumeCaptureEvent(false);
4217 mWindow->consumeFocusEvent(false);
4218 mSecondWindow->consumeFocusEvent(true);
4219 mFakePolicy->waitForSetPointerCapture(false);
4220
4221 // Ensure that additional state changes from InputReader are not sent to the window.
4222 notifyPointerCaptureChanged(false);
4223 notifyPointerCaptureChanged(true);
4224 notifyPointerCaptureChanged(false);
4225 mWindow->assertNoEvents();
4226 mSecondWindow->assertNoEvents();
4227 mFakePolicy->assertSetPointerCaptureNotCalled();
4228}
4229
4230TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
4231 requestAndVerifyPointerCapture(mWindow, true);
4232
4233 // InputReader unexpectedly disables and enables pointer capture.
4234 notifyPointerCaptureChanged(false);
4235 notifyPointerCaptureChanged(true);
4236
4237 // Ensure that Pointer Capture is disabled.
Prabir Pradhan7d030382020-12-21 07:58:35 -08004238 mFakePolicy->waitForSetPointerCapture(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08004239 mWindow->consumeCaptureEvent(false);
4240 mWindow->assertNoEvents();
4241}
4242
Prabir Pradhan167e6d92021-02-04 16:18:17 -08004243TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
4244 requestAndVerifyPointerCapture(mWindow, true);
4245
4246 // The first window loses focus.
4247 setFocusedWindow(mSecondWindow);
4248 mFakePolicy->waitForSetPointerCapture(false);
4249 mWindow->consumeCaptureEvent(false);
4250
4251 // Request Pointer Capture from the second window before the notification from InputReader
4252 // arrives.
4253 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
4254 mFakePolicy->waitForSetPointerCapture(true);
4255
4256 // InputReader notifies Pointer Capture was disabled (because of the focus change).
4257 notifyPointerCaptureChanged(false);
4258
4259 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
4260 notifyPointerCaptureChanged(true);
4261
4262 mSecondWindow->consumeFocusEvent(true);
4263 mSecondWindow->consumeCaptureEvent(true);
4264}
4265
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004266class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
4267protected:
4268 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004269 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004270 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004271 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004272
4273 sp<FakeWindowHandle> mTouchWindow;
4274
4275 virtual void SetUp() override {
4276 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004277 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004278 mDispatcher->setBlockUntrustedTouchesMode(android::os::BlockUntrustedTouchesMode::BLOCK);
4279 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
4280 }
4281
4282 virtual void TearDown() override {
4283 InputDispatcherTest::TearDown();
4284 mTouchWindow.clear();
4285 }
4286
4287 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name,
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004288 os::TouchOcclusionMode mode, float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004289 sp<FakeWindowHandle> window = getWindow(uid, name);
4290 window->setFlags(InputWindowInfo::Flag::NOT_TOUCHABLE);
4291 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004292 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004293 return window;
4294 }
4295
4296 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
4297 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
4298 sp<FakeWindowHandle> window =
4299 new FakeWindowHandle(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
4300 // Generate an arbitrary PID based on the UID
4301 window->setOwnerInfo(1777 + (uid % 10000), uid);
4302 return window;
4303 }
4304
4305 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
4306 NotifyMotionArgs args =
4307 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4308 ADISPLAY_ID_DEFAULT, points);
4309 mDispatcher->notifyMotion(&args);
4310 }
4311};
4312
4313TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004314 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004315 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004316 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004317
4318 touch();
4319
4320 mTouchWindow->assertNoEvents();
4321}
4322
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004323TEST_F(InputDispatcherUntrustedTouchesTest,
4324 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
4325 const sp<FakeWindowHandle>& w =
4326 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
4327 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4328
4329 touch();
4330
4331 w->assertNoEvents();
4332}
4333
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004334TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004335 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
4336 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004337
4338 touch();
4339
4340 mTouchWindow->consumeAnyMotionDown();
4341}
4342
4343TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004344 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004345 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004346 w->setFrame(Rect(0, 0, 50, 50));
4347 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004348
4349 touch({PointF{100, 100}});
4350
4351 mTouchWindow->consumeAnyMotionDown();
4352}
4353
4354TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004355 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004356 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004357 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4358
4359 touch();
4360
4361 mTouchWindow->consumeAnyMotionDown();
4362}
4363
4364TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
4365 const sp<FakeWindowHandle>& w =
4366 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4367 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004368
4369 touch();
4370
4371 mTouchWindow->consumeAnyMotionDown();
4372}
4373
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004374TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
4375 const sp<FakeWindowHandle>& w =
4376 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4377 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4378
4379 touch();
4380
4381 w->assertNoEvents();
4382}
4383
4384/**
4385 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
4386 * inside) while letting them pass-through. Note that even though touch passes through the occluding
4387 * window, the occluding window will still receive ACTION_OUTSIDE event.
4388 */
4389TEST_F(InputDispatcherUntrustedTouchesTest,
4390 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
4391 const sp<FakeWindowHandle>& w =
4392 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4393 w->addFlags(InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH);
4394 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4395
4396 touch();
4397
4398 w->consumeMotionOutside();
4399}
4400
4401TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
4402 const sp<FakeWindowHandle>& w =
4403 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4404 w->addFlags(InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH);
4405 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4406
4407 touch();
4408
4409 InputEvent* event = w->consume();
4410 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType());
4411 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
4412 EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getX());
4413 EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getY());
4414}
4415
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004416TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004417 const sp<FakeWindowHandle>& w =
4418 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.7f);
4419 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4420
4421 touch();
4422
4423 mTouchWindow->consumeAnyMotionDown();
4424}
4425
4426TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
4427 const sp<FakeWindowHandle>& w =
4428 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4429 MAXIMUM_OBSCURING_OPACITY);
4430 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004431
4432 touch();
4433
4434 mTouchWindow->consumeAnyMotionDown();
4435}
4436
4437TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004438 const sp<FakeWindowHandle>& w =
4439 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.9f);
4440 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4441
4442 touch();
4443
4444 mTouchWindow->assertNoEvents();
4445}
4446
4447TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
4448 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
4449 const sp<FakeWindowHandle>& w1 =
4450 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, 0.7f);
4451 const sp<FakeWindowHandle>& w2 =
4452 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, 0.7f);
4453 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
4454
4455 touch();
4456
4457 mTouchWindow->assertNoEvents();
4458}
4459
4460TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
4461 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
4462 const sp<FakeWindowHandle>& w1 =
4463 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, 0.5f);
4464 const sp<FakeWindowHandle>& w2 =
4465 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, 0.5f);
4466 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
4467
4468 touch();
4469
4470 mTouchWindow->consumeAnyMotionDown();
4471}
4472
4473TEST_F(InputDispatcherUntrustedTouchesTest,
4474 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
4475 const sp<FakeWindowHandle>& wB =
4476 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.7f);
4477 const sp<FakeWindowHandle>& wC =
4478 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, 0.7f);
4479 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
4480
4481 touch();
4482
4483 mTouchWindow->consumeAnyMotionDown();
4484}
4485
4486TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
4487 const sp<FakeWindowHandle>& wB =
4488 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.7f);
4489 const sp<FakeWindowHandle>& wC =
4490 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, 0.9f);
4491 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004492
4493 touch();
4494
4495 mTouchWindow->assertNoEvents();
4496}
4497
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004498TEST_F(InputDispatcherUntrustedTouchesTest,
4499 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
4500 const sp<FakeWindowHandle>& wA =
4501 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, 0.7f);
4502 const sp<FakeWindowHandle>& wB =
4503 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.9f);
4504 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
4505
4506 touch();
4507
4508 mTouchWindow->assertNoEvents();
4509}
4510
4511TEST_F(InputDispatcherUntrustedTouchesTest,
4512 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
4513 const sp<FakeWindowHandle>& wA =
4514 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, 0.9f);
4515 const sp<FakeWindowHandle>& wB =
4516 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.7f);
4517 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
4518
4519 touch();
4520
4521 mTouchWindow->consumeAnyMotionDown();
4522}
4523
4524TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
4525 const sp<FakeWindowHandle>& w =
4526 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, 0.9f);
4527 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4528
4529 touch();
4530
4531 mTouchWindow->consumeAnyMotionDown();
4532}
4533
4534TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
4535 const sp<FakeWindowHandle>& w =
4536 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
4537 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4538
4539 touch();
4540
4541 mTouchWindow->consumeAnyMotionDown();
4542}
4543
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00004544TEST_F(InputDispatcherUntrustedTouchesTest,
4545 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
4546 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
4547 const sp<FakeWindowHandle>& w =
4548 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
4549 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4550
4551 touch();
4552
4553 mTouchWindow->assertNoEvents();
4554}
4555
4556TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
4557 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
4558 const sp<FakeWindowHandle>& w =
4559 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
4560 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4561
4562 touch();
4563
4564 mTouchWindow->consumeAnyMotionDown();
4565}
4566
4567TEST_F(InputDispatcherUntrustedTouchesTest,
4568 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
4569 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
4570 const sp<FakeWindowHandle>& w =
4571 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.9f);
4572 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4573
4574 touch();
4575
4576 mTouchWindow->consumeAnyMotionDown();
4577}
4578
Garfield Tane84e6f92019-08-29 17:28:41 -07004579} // namespace android::inputdispatcher