blob: cedda6e91cac5d805bf6f6960d6596779852fc13 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Garfield Tan0fc2fa72019-08-29 17:22:15 -070017#include "../dispatcher/InputDispatcher.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080018
Garfield Tan1c7bc862020-01-28 13:24:04 -080019#include <android-base/stringprintf.h>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070020#include <android-base/thread_annotations.h>
Robert Carr803535b2018-08-02 16:38:15 -070021#include <binder/Binder.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080022#include <gtest/gtest.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100023#include <input/Input.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080024#include <linux/input.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100025
Garfield Tan1c7bc862020-01-28 13:24:04 -080026#include <cinttypes>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070027#include <thread>
Garfield Tan1c7bc862020-01-28 13:24:04 -080028#include <unordered_set>
chaviwd1c23182019-12-20 18:44:56 -080029#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080030
Garfield Tan1c7bc862020-01-28 13:24:04 -080031using android::base::StringPrintf;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080032using android::os::InputEventInjectionResult;
33using android::os::InputEventInjectionSync;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000034using android::os::TouchOcclusionMode;
Michael Wright44753b12020-07-08 13:48:11 +010035using namespace android::flag_operators;
Garfield Tan1c7bc862020-01-28 13:24:04 -080036
Garfield Tane84e6f92019-08-29 17:28:41 -070037namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080038
39// An arbitrary time value.
40static const nsecs_t ARBITRARY_TIME = 1234;
41
42// An arbitrary device id.
43static const int32_t DEVICE_ID = 1;
44
Jeff Brownf086ddb2014-02-11 14:28:48 -080045// An arbitrary display id.
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -080046static const int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
Jeff Brownf086ddb2014-02-11 14:28:48 -080047
Michael Wrightd02c5b62014-02-10 15:10:22 -080048// An arbitrary injector pid / uid pair that has permission to inject events.
49static const int32_t INJECTOR_PID = 999;
50static const int32_t INJECTOR_UID = 1001;
51
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +000052// An arbitrary pid of the gesture monitor window
53static constexpr int32_t MONITOR_PID = 2001;
54
chaviwd1c23182019-12-20 18:44:56 -080055struct PointF {
56 float x;
57 float y;
58};
Michael Wrightd02c5b62014-02-10 15:10:22 -080059
Gang Wang342c9272020-01-13 13:15:04 -050060/**
61 * Return a DOWN key event with KEYCODE_A.
62 */
63static KeyEvent getTestKeyEvent() {
64 KeyEvent event;
65
Garfield Tanfbe732e2020-01-24 11:26:14 -080066 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
67 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
68 ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -050069 return event;
70}
71
Michael Wrightd02c5b62014-02-10 15:10:22 -080072// --- FakeInputDispatcherPolicy ---
73
74class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
75 InputDispatcherConfiguration mConfig;
76
77protected:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100078 virtual ~FakeInputDispatcherPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -080079
80public:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100081 FakeInputDispatcherPolicy() {}
Jackal Guof9696682018-10-05 12:23:23 +080082
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080083 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080084 assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_KEY, args.eventTime, args.action,
85 args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080086 }
87
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080088 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args) {
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080089 assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_MOTION, args.eventTime, args.action,
90 args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080091 }
92
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -070093 void assertFilterInputEventWasNotCalled() {
94 std::scoped_lock lock(mLock);
95 ASSERT_EQ(nullptr, mFilteredEvent);
96 }
Michael Wrightd02c5b62014-02-10 15:10:22 -080097
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -080098 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -070099 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800100 ASSERT_TRUE(mConfigurationChangedTime)
101 << "Timed out waiting for configuration changed call";
102 ASSERT_EQ(*mConfigurationChangedTime, when);
103 mConfigurationChangedTime = std::nullopt;
104 }
105
106 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700107 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800108 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800109 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800110 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
111 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
112 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
113 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
114 mLastNotifySwitch = std::nullopt;
115 }
116
chaviwfd6d3512019-03-25 13:23:49 -0700117 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700118 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800119 ASSERT_EQ(touchedToken, mOnPointerDownToken);
120 mOnPointerDownToken.clear();
121 }
122
123 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700124 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800125 ASSERT_TRUE(mOnPointerDownToken == nullptr)
126 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700127 }
128
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700129 // This function must be called soon after the expected ANR timer starts,
130 // because we are also checking how much time has passed.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500131 void assertNotifyNoFocusedWindowAnrWasCalled(
Chris Yea209fde2020-07-22 13:54:51 -0700132 std::chrono::nanoseconds timeout,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500133 const std::shared_ptr<InputApplicationHandle>& expectedApplication) {
134 std::shared_ptr<InputApplicationHandle> application;
135 { // acquire lock
136 std::unique_lock lock(mLock);
137 android::base::ScopedLockAssertion assumeLocked(mLock);
138 ASSERT_NO_FATAL_FAILURE(
139 application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock));
140 } // release lock
141 ASSERT_EQ(expectedApplication, application);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700142 }
143
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000144 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
145 const sp<IBinder>& expectedConnectionToken) {
146 sp<IBinder> connectionToken = getUnresponsiveWindowToken(timeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500147 ASSERT_EQ(expectedConnectionToken, connectionToken);
148 }
149
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000150 void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedConnectionToken) {
151 sp<IBinder> connectionToken = getResponsiveWindowToken();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500152 ASSERT_EQ(expectedConnectionToken, connectionToken);
153 }
154
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000155 void assertNotifyMonitorUnresponsiveWasCalled(std::chrono::nanoseconds timeout) {
156 int32_t pid = getUnresponsiveMonitorPid(timeout);
157 ASSERT_EQ(MONITOR_PID, pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500158 }
159
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000160 void assertNotifyMonitorResponsiveWasCalled() {
161 int32_t pid = getResponsiveMonitorPid();
162 ASSERT_EQ(MONITOR_PID, pid);
163 }
164
165 sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500166 std::unique_lock lock(mLock);
167 android::base::ScopedLockAssertion assumeLocked(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000168 return getAnrTokenLockedInterruptible(timeout, mAnrWindowTokens, lock);
169 }
170
171 sp<IBinder> getResponsiveWindowToken() {
172 std::unique_lock lock(mLock);
173 android::base::ScopedLockAssertion assumeLocked(mLock);
174 return getAnrTokenLockedInterruptible(0s, mResponsiveWindowTokens, lock);
175 }
176
177 int32_t getUnresponsiveMonitorPid(std::chrono::nanoseconds timeout) {
178 std::unique_lock lock(mLock);
179 android::base::ScopedLockAssertion assumeLocked(mLock);
180 return getAnrTokenLockedInterruptible(timeout, mAnrMonitorPids, lock);
181 }
182
183 int32_t getResponsiveMonitorPid() {
184 std::unique_lock lock(mLock);
185 android::base::ScopedLockAssertion assumeLocked(mLock);
186 return getAnrTokenLockedInterruptible(0s, mResponsiveMonitorPids, lock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500187 }
188
189 // All three ANR-related callbacks behave the same way, so we use this generic function to wait
190 // for a specific container to become non-empty. When the container is non-empty, return the
191 // first entry from the container and erase it.
192 template <class T>
193 T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage,
194 std::unique_lock<std::mutex>& lock) REQUIRES(mLock) {
195 const std::chrono::time_point start = std::chrono::steady_clock::now();
196 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700197
198 // If there is an ANR, Dispatcher won't be idle because there are still events
199 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
200 // before checking if ANR was called.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500201 // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need
202 // to provide it some time to act. 100ms seems reasonable.
203 mNotifyAnr.wait_for(lock, timeToWait,
204 [&storage]() REQUIRES(mLock) { return !storage.empty(); });
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700205 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500206 if (storage.empty()) {
207 ADD_FAILURE() << "Did not receive the ANR callback";
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000208 return {};
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700209 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700210 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
211 // the dispatcher started counting before this function was called
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700212 if (std::chrono::abs(timeout - waited) > 100ms) {
213 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
214 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
215 << "ms, but waited "
216 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
217 << "ms instead";
218 }
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500219 T token = storage.front();
220 storage.pop();
221 return token;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700222 }
223
224 void assertNotifyAnrWasNotCalled() {
225 std::scoped_lock lock(mLock);
226 ASSERT_TRUE(mAnrApplications.empty());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000227 ASSERT_TRUE(mAnrWindowTokens.empty());
228 ASSERT_TRUE(mAnrMonitorPids.empty());
229 ASSERT_TRUE(mResponsiveWindowTokens.empty())
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500230 << "ANR was not called, but please also consume the 'connection is responsive' "
231 "signal";
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000232 ASSERT_TRUE(mResponsiveMonitorPids.empty())
233 << "Monitor ANR was not called, but please also consume the 'monitor is responsive'"
234 " signal";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700235 }
236
Garfield Tan1c7bc862020-01-28 13:24:04 -0800237 void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) {
238 mConfig.keyRepeatTimeout = timeout;
239 mConfig.keyRepeatDelay = delay;
240 }
241
Prabir Pradhan99987712020-11-10 18:43:05 -0800242 void waitForSetPointerCapture(bool enabled) {
243 std::unique_lock lock(mLock);
244 base::ScopedLockAssertion assumeLocked(mLock);
245
246 if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms,
247 [this, enabled]() REQUIRES(mLock) {
248 return mPointerCaptureEnabled &&
249 *mPointerCaptureEnabled ==
250 enabled;
251 })) {
252 FAIL() << "Timed out waiting for setPointerCapture(" << enabled << ") to be called.";
253 }
254 mPointerCaptureEnabled.reset();
255 }
256
257 void assertSetPointerCaptureNotCalled() {
258 std::unique_lock lock(mLock);
259 base::ScopedLockAssertion assumeLocked(mLock);
260
261 if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) {
262 FAIL() << "Expected setPointerCapture(enabled) to not be called, but was called. "
263 "enabled = "
264 << *mPointerCaptureEnabled;
265 }
266 mPointerCaptureEnabled.reset();
267 }
268
arthurhungf452d0b2021-01-06 00:19:52 +0800269 void assertDropTargetEquals(const sp<IBinder>& targetToken) {
270 std::scoped_lock lock(mLock);
271 ASSERT_EQ(targetToken, mDropTargetWindowToken);
272 }
273
Michael Wrightd02c5b62014-02-10 15:10:22 -0800274private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700275 std::mutex mLock;
276 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
277 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
278 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
279 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800280
Prabir Pradhan99987712020-11-10 18:43:05 -0800281 std::condition_variable mPointerCaptureChangedCondition;
282 std::optional<bool> mPointerCaptureEnabled GUARDED_BY(mLock);
283
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700284 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700285 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000286 std::queue<sp<IBinder>> mAnrWindowTokens GUARDED_BY(mLock);
287 std::queue<sp<IBinder>> mResponsiveWindowTokens GUARDED_BY(mLock);
288 std::queue<int32_t> mAnrMonitorPids GUARDED_BY(mLock);
289 std::queue<int32_t> mResponsiveMonitorPids GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700290 std::condition_variable mNotifyAnr;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700291
arthurhungf452d0b2021-01-06 00:19:52 +0800292 sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock);
293
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600294 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700295 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800296 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800297 }
298
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000299 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700300 std::scoped_lock lock(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000301 mAnrWindowTokens.push(connectionToken);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700302 mNotifyAnr.notify_all();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500303 }
304
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000305 void notifyMonitorUnresponsive(int32_t pid, const std::string&) override {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500306 std::scoped_lock lock(mLock);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000307 mAnrMonitorPids.push(pid);
308 mNotifyAnr.notify_all();
309 }
310
311 void notifyWindowResponsive(const sp<IBinder>& connectionToken) override {
312 std::scoped_lock lock(mLock);
313 mResponsiveWindowTokens.push(connectionToken);
314 mNotifyAnr.notify_all();
315 }
316
317 void notifyMonitorResponsive(int32_t pid) override {
318 std::scoped_lock lock(mLock);
319 mResponsiveMonitorPids.push(pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500320 mNotifyAnr.notify_all();
321 }
322
323 void notifyNoFocusedWindowAnr(
324 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
325 std::scoped_lock lock(mLock);
326 mAnrApplications.push(applicationHandle);
327 mNotifyAnr.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800328 }
329
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600330 void notifyInputChannelBroken(const sp<IBinder>&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800331
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600332 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700333
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600334 void notifyUntrustedTouch(const std::string& obscuringPackage) override {}
Chris Yef59a2f42020-10-16 12:55:26 -0700335 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
336 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
337 const std::vector<float>& values) override {}
338
339 void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType,
340 InputDeviceSensorAccuracy accuracy) override {}
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000341
Chris Yefb552902021-02-03 17:18:37 -0800342 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
343
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600344 void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800345 *outConfig = mConfig;
346 }
347
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600348 bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700349 std::scoped_lock lock(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800350 switch (inputEvent->getType()) {
351 case AINPUT_EVENT_TYPE_KEY: {
352 const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800353 mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800354 break;
355 }
356
357 case AINPUT_EVENT_TYPE_MOTION: {
358 const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800359 mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800360 break;
361 }
362 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800363 return true;
364 }
365
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600366 void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800367
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600368 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800369
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600370 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800371 return 0;
372 }
373
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600374 bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800375 return false;
376 }
377
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600378 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
379 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700380 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800381 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
382 * essentially a passthrough for notifySwitch.
383 */
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800384 mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800385 }
386
Sean Stoutb4e0a592021-02-23 07:34:53 -0800387 void pokeUserActivity(nsecs_t, int32_t, int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800388
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600389 bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) override { return false; }
Jackal Guof9696682018-10-05 12:23:23 +0800390
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600391 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700392 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700393 mOnPointerDownToken = newToken;
394 }
395
Prabir Pradhan99987712020-11-10 18:43:05 -0800396 void setPointerCapture(bool enabled) override {
397 std::scoped_lock lock(mLock);
398 mPointerCaptureEnabled = {enabled};
399 mPointerCaptureChangedCondition.notify_all();
400 }
401
arthurhungf452d0b2021-01-06 00:19:52 +0800402 void notifyDropWindow(const sp<IBinder>& token, float x, float y) override {
403 std::scoped_lock lock(mLock);
404 mDropTargetWindowToken = token;
405 }
406
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800407 void assertFilterInputEventWasCalled(int type, nsecs_t eventTime, int32_t action,
408 int32_t displayId) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700409 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800410 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
411 ASSERT_EQ(mFilteredEvent->getType(), type);
412
413 if (type == AINPUT_EVENT_TYPE_KEY) {
414 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*mFilteredEvent);
415 EXPECT_EQ(keyEvent.getEventTime(), eventTime);
416 EXPECT_EQ(keyEvent.getAction(), action);
417 EXPECT_EQ(keyEvent.getDisplayId(), displayId);
418 } else if (type == AINPUT_EVENT_TYPE_MOTION) {
419 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*mFilteredEvent);
420 EXPECT_EQ(motionEvent.getEventTime(), eventTime);
421 EXPECT_EQ(motionEvent.getAction(), action);
422 EXPECT_EQ(motionEvent.getDisplayId(), displayId);
423 } else {
424 FAIL() << "Unknown type: " << type;
425 }
426
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800427 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800428 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800429};
430
Michael Wrightd02c5b62014-02-10 15:10:22 -0800431// --- InputDispatcherTest ---
432
433class InputDispatcherTest : public testing::Test {
434protected:
435 sp<FakeInputDispatcherPolicy> mFakePolicy;
436 sp<InputDispatcher> mDispatcher;
437
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700438 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800439 mFakePolicy = new FakeInputDispatcherPolicy();
440 mDispatcher = new InputDispatcher(mFakePolicy);
Arthur Hungb92218b2018-08-14 12:00:21 +0800441 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000442 // Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700443 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800444 }
445
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700446 virtual void TearDown() override {
447 ASSERT_EQ(OK, mDispatcher->stop());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800448 mFakePolicy.clear();
449 mDispatcher.clear();
450 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700451
452 /**
453 * Used for debugging when writing the test
454 */
455 void dumpDispatcherState() {
456 std::string dump;
457 mDispatcher->dump(dump);
458 std::stringstream ss(dump);
459 std::string to;
460
461 while (std::getline(ss, to, '\n')) {
462 ALOGE("%s", to.c_str());
463 }
464 }
Vishnu Nair958da932020-08-21 17:12:37 -0700465
466 void setFocusedWindow(const sp<InputWindowHandle>& window,
467 const sp<InputWindowHandle>& focusedWindow = nullptr) {
468 FocusRequest request;
469 request.token = window->getToken();
470 if (focusedWindow) {
471 request.focusedToken = focusedWindow->getToken();
472 }
473 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
474 request.displayId = window->getInfo()->displayId;
475 mDispatcher->setFocusedWindow(request);
476 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800477};
478
Michael Wrightd02c5b62014-02-10 15:10:22 -0800479TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
480 KeyEvent event;
481
482 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800483 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
484 INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600485 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
486 ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800487 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700488 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800489 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800490 << "Should reject key events with undefined action.";
491
492 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800493 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
494 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600495 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800496 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700497 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800498 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800499 << "Should reject key events with ACTION_MULTIPLE.";
500}
501
502TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
503 MotionEvent event;
504 PointerProperties pointerProperties[MAX_POINTERS + 1];
505 PointerCoords pointerCoords[MAX_POINTERS + 1];
506 for (int i = 0; i <= MAX_POINTERS; i++) {
507 pointerProperties[i].clear();
508 pointerProperties[i].id = i;
509 pointerCoords[i].clear();
510 }
511
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800512 // Some constants commonly used below
513 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
514 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
515 constexpr int32_t metaState = AMETA_NONE;
516 constexpr MotionClassification classification = MotionClassification::NONE;
517
chaviw9eaa22c2020-07-01 16:21:27 -0700518 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800519 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800520 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
chaviw9eaa22c2020-07-01 16:21:27 -0700521 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification,
522 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600523 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700524 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800525 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700526 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800527 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800528 << "Should reject motion events with undefined action.";
529
530 // Rejects pointer down with invalid index.
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 (1 << 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 large.";
542
Garfield Tanfbe732e2020-01-24 11:26:14 -0800543 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700544 AMOTION_EVENT_ACTION_POINTER_DOWN |
545 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700546 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
547 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
548 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
549 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800550 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700551 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800552 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800553 << "Should reject motion events with pointer down index too small.";
554
555 // Rejects pointer up with invalid index.
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 (1 << 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 large.";
567
Garfield Tanfbe732e2020-01-24 11:26:14 -0800568 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700569 AMOTION_EVENT_ACTION_POINTER_UP |
570 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700571 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
572 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
573 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
574 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800575 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700576 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800577 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800578 << "Should reject motion events with pointer up index too small.";
579
580 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800581 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
582 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700583 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
584 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700585 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800586 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700587 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800588 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800589 << "Should reject motion events with 0 pointers.";
590
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*/ MAX_POINTERS + 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 more than MAX_POINTERS pointers.";
600
601 // Rejects motion events with invalid pointer ids.
602 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800603 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
604 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700605 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
606 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700607 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800608 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700609 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800610 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800611 << "Should reject motion events with pointer ids less than 0.";
612
613 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800614 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
615 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700616 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
617 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700618 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800619 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700620 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800621 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800622 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
623
624 // Rejects motion events with duplicate pointer ids.
625 pointerProperties[0].id = 1;
626 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800627 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
628 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700629 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
630 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700631 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800632 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700633 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800634 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800635 << "Should reject motion events with duplicate pointer ids.";
636}
637
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800638/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
639
640TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
641 constexpr nsecs_t eventTime = 20;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800642 NotifyConfigurationChangedArgs args(10 /*id*/, eventTime);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800643 mDispatcher->notifyConfigurationChanged(&args);
644 ASSERT_TRUE(mDispatcher->waitForIdle());
645
646 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
647}
648
649TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800650 NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/,
651 2 /*switchMask*/);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800652 mDispatcher->notifySwitch(&args);
653
654 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
655 args.policyFlags |= POLICY_FLAG_TRUSTED;
656 mFakePolicy->assertNotifySwitchWasCalled(args);
657}
658
Arthur Hungb92218b2018-08-14 12:00:21 +0800659// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700660static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700661static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s;
Arthur Hungb92218b2018-08-14 12:00:21 +0800662
663class FakeApplicationHandle : public InputApplicationHandle {
664public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700665 FakeApplicationHandle() {
666 mInfo.name = "Fake Application";
667 mInfo.token = new BBinder();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500668 mInfo.dispatchingTimeoutMillis =
669 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700670 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800671 virtual ~FakeApplicationHandle() {}
672
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000673 virtual bool updateInfo() override { return true; }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700674
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500675 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
676 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700677 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800678};
679
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800680class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800681public:
Garfield Tan15601662020-09-22 15:32:38 -0700682 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800683 : mName(name) {
Garfield Tan15601662020-09-22 15:32:38 -0700684 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800685 }
686
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800687 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700688 InputEvent* event;
689 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
690 if (!consumeSeq) {
691 return nullptr;
692 }
693 finishEvent(*consumeSeq);
694 return event;
695 }
696
697 /**
698 * Receive an event without acknowledging it.
699 * Return the sequence number that could later be used to send finished signal.
700 */
701 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800702 uint32_t consumeSeq;
703 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800704
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800705 std::chrono::time_point start = std::chrono::steady_clock::now();
706 status_t status = WOULD_BLOCK;
707 while (status == WOULD_BLOCK) {
chaviw81e2bb92019-12-18 15:03:51 -0800708 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800709 &event);
710 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
711 if (elapsed > 100ms) {
712 break;
713 }
714 }
715
716 if (status == WOULD_BLOCK) {
717 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700718 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800719 }
720
721 if (status != OK) {
722 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700723 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800724 }
725 if (event == nullptr) {
726 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700727 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800728 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700729 if (outEvent != nullptr) {
730 *outEvent = event;
731 }
732 return consumeSeq;
733 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800734
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700735 /**
736 * To be used together with "receiveEvent" to complete the consumption of an event.
737 */
738 void finishEvent(uint32_t consumeSeq) {
739 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
740 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800741 }
742
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000743 void consumeEvent(int32_t expectedEventType, int32_t expectedAction,
744 std::optional<int32_t> expectedDisplayId,
745 std::optional<int32_t> expectedFlags) {
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800746 InputEvent* event = consume();
747
748 ASSERT_NE(nullptr, event) << mName.c_str()
749 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800750 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700751 << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType)
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800752 << " event, got " << inputEventTypeToString(event->getType()) << " event";
Arthur Hungb92218b2018-08-14 12:00:21 +0800753
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000754 if (expectedDisplayId.has_value()) {
755 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
756 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800757
Tiger Huang8664f8c2018-10-11 19:14:35 +0800758 switch (expectedEventType) {
759 case AINPUT_EVENT_TYPE_KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800760 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
761 EXPECT_EQ(expectedAction, keyEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000762 if (expectedFlags.has_value()) {
763 EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags());
764 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800765 break;
766 }
767 case AINPUT_EVENT_TYPE_MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800768 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
769 EXPECT_EQ(expectedAction, motionEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000770 if (expectedFlags.has_value()) {
771 EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags());
772 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800773 break;
774 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100775 case AINPUT_EVENT_TYPE_FOCUS: {
776 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
777 }
Prabir Pradhan99987712020-11-10 18:43:05 -0800778 case AINPUT_EVENT_TYPE_CAPTURE: {
779 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
780 }
arthurhungb89ccb02020-12-30 16:19:01 +0800781 case AINPUT_EVENT_TYPE_DRAG: {
782 FAIL() << "Use 'consumeDragEvent' for DRAG events";
783 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800784 default: {
785 FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType;
786 }
787 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800788 }
789
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100790 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
791 InputEvent* event = consume();
792 ASSERT_NE(nullptr, event) << mName.c_str()
793 << ": consumer should have returned non-NULL event.";
794 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
795 << "Got " << inputEventTypeToString(event->getType())
796 << " event instead of FOCUS event";
797
798 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
799 << mName.c_str() << ": event displayId should always be NONE.";
800
801 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
802 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
803 EXPECT_EQ(inTouchMode, focusEvent->getInTouchMode());
804 }
805
Prabir Pradhan99987712020-11-10 18:43:05 -0800806 void consumeCaptureEvent(bool hasCapture) {
807 const InputEvent* event = consume();
808 ASSERT_NE(nullptr, event) << mName.c_str()
809 << ": consumer should have returned non-NULL event.";
810 ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType())
811 << "Got " << inputEventTypeToString(event->getType())
812 << " event instead of CAPTURE event";
813
814 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
815 << mName.c_str() << ": event displayId should always be NONE.";
816
817 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
818 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
819 }
820
arthurhungb89ccb02020-12-30 16:19:01 +0800821 void consumeDragEvent(bool isExiting, float x, float y) {
822 const InputEvent* event = consume();
823 ASSERT_NE(nullptr, event) << mName.c_str()
824 << ": consumer should have returned non-NULL event.";
825 ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType())
826 << "Got " << inputEventTypeToString(event->getType())
827 << " event instead of DRAG event";
828
829 EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
830 << mName.c_str() << ": event displayId should always be NONE.";
831
832 const auto& dragEvent = static_cast<const DragEvent&>(*event);
833 EXPECT_EQ(isExiting, dragEvent.isExiting());
834 EXPECT_EQ(x, dragEvent.getX());
835 EXPECT_EQ(y, dragEvent.getY());
836 }
837
chaviwd1c23182019-12-20 18:44:56 -0800838 void assertNoEvents() {
839 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700840 if (event == nullptr) {
841 return;
842 }
843 if (event->getType() == AINPUT_EVENT_TYPE_KEY) {
844 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
845 ADD_FAILURE() << "Received key event "
846 << KeyEvent::actionToString(keyEvent.getAction());
847 } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) {
848 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
849 ADD_FAILURE() << "Received motion event "
850 << MotionEvent::actionToString(motionEvent.getAction());
851 } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) {
852 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
853 ADD_FAILURE() << "Received focus event, hasFocus = "
854 << (focusEvent.getHasFocus() ? "true" : "false");
Prabir Pradhan99987712020-11-10 18:43:05 -0800855 } else if (event->getType() == AINPUT_EVENT_TYPE_CAPTURE) {
856 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
857 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
858 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700859 }
860 FAIL() << mName.c_str()
861 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -0800862 }
863
864 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
865
866protected:
867 std::unique_ptr<InputConsumer> mConsumer;
868 PreallocatedInputEventFactory mEventFactory;
869
870 std::string mName;
871};
872
873class FakeWindowHandle : public InputWindowHandle {
874public:
875 static const int32_t WIDTH = 600;
876 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -0800877
Chris Yea209fde2020-07-22 13:54:51 -0700878 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
chaviwd1c23182019-12-20 18:44:56 -0800879 const sp<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500880 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -0800881 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500882 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -0700883 base::Result<std::unique_ptr<InputChannel>> channel =
884 dispatcher->createInputChannel(name);
885 token = (*channel)->getConnectionToken();
886 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -0800887 }
888
889 inputApplicationHandle->updateInfo();
890 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
891
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500892 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700893 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -0800894 mInfo.name = name;
Michael Wright44753b12020-07-08 13:48:11 +0100895 mInfo.type = InputWindowInfo::Type::APPLICATION;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500896 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000897 mInfo.alpha = 1.0;
chaviwd1c23182019-12-20 18:44:56 -0800898 mInfo.frameLeft = 0;
899 mInfo.frameTop = 0;
900 mInfo.frameRight = WIDTH;
901 mInfo.frameBottom = HEIGHT;
chaviw1ff3d1e2020-07-01 15:53:47 -0700902 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -0800903 mInfo.globalScaleFactor = 1.0;
904 mInfo.touchableRegion.clear();
905 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
906 mInfo.visible = true;
Vishnu Nair47074b82020-08-14 11:54:47 -0700907 mInfo.focusable = false;
chaviwd1c23182019-12-20 18:44:56 -0800908 mInfo.hasWallpaper = false;
909 mInfo.paused = false;
chaviwd1c23182019-12-20 18:44:56 -0800910 mInfo.ownerPid = INJECTOR_PID;
911 mInfo.ownerUid = INJECTOR_UID;
chaviwd1c23182019-12-20 18:44:56 -0800912 mInfo.displayId = displayId;
913 }
914
915 virtual bool updateInfo() { return true; }
916
Vishnu Nair47074b82020-08-14 11:54:47 -0700917 void setFocusable(bool focusable) { mInfo.focusable = focusable; }
chaviwd1c23182019-12-20 18:44:56 -0800918
Vishnu Nair958da932020-08-21 17:12:37 -0700919 void setVisible(bool visible) { mInfo.visible = visible; }
920
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700921 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500922 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700923 }
924
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700925 void setPaused(bool paused) { mInfo.paused = paused; }
926
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000927 void setAlpha(float alpha) { mInfo.alpha = alpha; }
928
929 void setTouchOcclusionMode(android::os::TouchOcclusionMode mode) {
930 mInfo.touchOcclusionMode = mode;
931 }
932
Bernardo Rufino7393d172021-02-26 13:56:11 +0000933 void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
934
chaviwd1c23182019-12-20 18:44:56 -0800935 void setFrame(const Rect& frame) {
936 mInfo.frameLeft = frame.left;
937 mInfo.frameTop = frame.top;
938 mInfo.frameRight = frame.right;
939 mInfo.frameBottom = frame.bottom;
arthurhungb89ccb02020-12-30 16:19:01 +0800940 mInfo.transform.set(-frame.left, -frame.top);
chaviwd1c23182019-12-20 18:44:56 -0800941 mInfo.touchableRegion.clear();
942 mInfo.addTouchableRegion(frame);
943 }
944
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +0000945 void addFlags(Flags<InputWindowInfo::Flag> flags) { mInfo.flags |= flags; }
946
Michael Wright44753b12020-07-08 13:48:11 +0100947 void setFlags(Flags<InputWindowInfo::Flag> flags) { mInfo.flags = flags; }
chaviwd1c23182019-12-20 18:44:56 -0800948
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500949 void setInputFeatures(InputWindowInfo::Feature features) { mInfo.inputFeatures = features; }
950
chaviw9eaa22c2020-07-01 16:21:27 -0700951 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
952 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
953 }
954
955 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -0700956
yunho.shinf4a80b82020-11-16 21:13:57 +0900957 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
958
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800959 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
960 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
961 expectedFlags);
962 }
963
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700964 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
965 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
966 }
967
Svet Ganov5d3bc372020-01-26 23:11:07 -0800968 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000969 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800970 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId,
971 expectedFlags);
972 }
973
974 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000975 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800976 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId,
977 expectedFlags);
978 }
979
980 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000981 int32_t expectedFlags = 0) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000982 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
983 }
984
985 void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
986 std::optional<int32_t> expectedFlags = std::nullopt) {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800987 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
988 expectedFlags);
989 }
990
Svet Ganov5d3bc372020-01-26 23:11:07 -0800991 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000992 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
993 int32_t expectedFlags = 0) {
994 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
995 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -0800996 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
997 }
998
999 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001000 int32_t expectedFlags = 0) {
1001 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
1002 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001003 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
1004 }
1005
1006 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001007 int32_t expectedFlags = 0) {
Michael Wright3a240c42019-12-10 20:53:41 +00001008 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
1009 expectedFlags);
1010 }
1011
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001012 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1013 int32_t expectedFlags = 0) {
1014 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
1015 expectedFlags);
1016 }
1017
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001018 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
1019 ASSERT_NE(mInputReceiver, nullptr)
1020 << "Cannot consume events from a window with no receiver";
1021 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
1022 }
1023
Prabir Pradhan99987712020-11-10 18:43:05 -08001024 void consumeCaptureEvent(bool hasCapture) {
1025 ASSERT_NE(mInputReceiver, nullptr)
1026 << "Cannot consume events from a window with no receiver";
1027 mInputReceiver->consumeCaptureEvent(hasCapture);
1028 }
1029
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001030 void consumeEvent(int32_t expectedEventType, int32_t expectedAction,
1031 std::optional<int32_t> expectedDisplayId,
1032 std::optional<int32_t> expectedFlags) {
chaviwd1c23182019-12-20 18:44:56 -08001033 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
1034 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
1035 expectedFlags);
1036 }
1037
arthurhungb89ccb02020-12-30 16:19:01 +08001038 void consumeDragEvent(bool isExiting, float x, float y) {
1039 mInputReceiver->consumeDragEvent(isExiting, x, y);
1040 }
1041
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001042 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001043 if (mInputReceiver == nullptr) {
1044 ADD_FAILURE() << "Invalid receive event on window with no receiver";
1045 return std::nullopt;
1046 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001047 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001048 }
1049
1050 void finishEvent(uint32_t sequenceNum) {
1051 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1052 mInputReceiver->finishEvent(sequenceNum);
1053 }
1054
chaviwaf87b3e2019-10-01 16:59:28 -07001055 InputEvent* consume() {
1056 if (mInputReceiver == nullptr) {
1057 return nullptr;
1058 }
1059 return mInputReceiver->consume();
1060 }
1061
Arthur Hungb92218b2018-08-14 12:00:21 +08001062 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001063 if (mInputReceiver == nullptr &&
1064 mInfo.inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL)) {
1065 return; // Can't receive events if the window does not have input channel
1066 }
1067 ASSERT_NE(nullptr, mInputReceiver)
1068 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -08001069 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +08001070 }
1071
chaviwaf87b3e2019-10-01 16:59:28 -07001072 sp<IBinder> getToken() { return mInfo.token; }
1073
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001074 const std::string& getName() { return mName; }
1075
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001076 void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) {
1077 mInfo.ownerPid = ownerPid;
1078 mInfo.ownerUid = ownerUid;
1079 }
1080
chaviwd1c23182019-12-20 18:44:56 -08001081private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001082 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -08001083 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001084 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001085};
1086
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001087std::atomic<int32_t> FakeWindowHandle::sId{1};
1088
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001089static InputEventInjectionResult injectKey(
1090 const sp<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
1091 int32_t displayId = ADISPLAY_ID_NONE,
1092 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
1093 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001094 KeyEvent event;
1095 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1096
1097 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001098 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001099 INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE,
1100 repeatCount, currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001101
1102 // Inject event until dispatch out.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001103 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, syncMode,
1104 injectionTimeout,
1105 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
Arthur Hungb92218b2018-08-14 12:00:21 +08001106}
1107
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001108static InputEventInjectionResult injectKeyDown(const sp<InputDispatcher>& dispatcher,
1109 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001110 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId);
1111}
1112
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001113static InputEventInjectionResult injectKeyUp(const sp<InputDispatcher>& dispatcher,
1114 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001115 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId);
1116}
1117
Garfield Tandf26e862020-07-01 20:18:19 -07001118class PointerBuilder {
1119public:
1120 PointerBuilder(int32_t id, int32_t toolType) {
1121 mProperties.clear();
1122 mProperties.id = id;
1123 mProperties.toolType = toolType;
1124 mCoords.clear();
1125 }
1126
1127 PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); }
1128
1129 PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); }
1130
1131 PointerBuilder& axis(int32_t axis, float value) {
1132 mCoords.setAxisValue(axis, value);
1133 return *this;
1134 }
1135
1136 PointerProperties buildProperties() const { return mProperties; }
1137
1138 PointerCoords buildCoords() const { return mCoords; }
1139
1140private:
1141 PointerProperties mProperties;
1142 PointerCoords mCoords;
1143};
1144
1145class MotionEventBuilder {
1146public:
1147 MotionEventBuilder(int32_t action, int32_t source) {
1148 mAction = action;
1149 mSource = source;
1150 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1151 }
1152
1153 MotionEventBuilder& eventTime(nsecs_t eventTime) {
1154 mEventTime = eventTime;
1155 return *this;
1156 }
1157
1158 MotionEventBuilder& displayId(int32_t displayId) {
1159 mDisplayId = displayId;
1160 return *this;
1161 }
1162
1163 MotionEventBuilder& actionButton(int32_t actionButton) {
1164 mActionButton = actionButton;
1165 return *this;
1166 }
1167
1168 MotionEventBuilder& buttonState(int32_t actionButton) {
1169 mActionButton = actionButton;
1170 return *this;
1171 }
1172
1173 MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) {
1174 mRawXCursorPosition = rawXCursorPosition;
1175 return *this;
1176 }
1177
1178 MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) {
1179 mRawYCursorPosition = rawYCursorPosition;
1180 return *this;
1181 }
1182
1183 MotionEventBuilder& pointer(PointerBuilder pointer) {
1184 mPointers.push_back(pointer);
1185 return *this;
1186 }
1187
1188 MotionEvent build() {
1189 std::vector<PointerProperties> pointerProperties;
1190 std::vector<PointerCoords> pointerCoords;
1191 for (const PointerBuilder& pointer : mPointers) {
1192 pointerProperties.push_back(pointer.buildProperties());
1193 pointerCoords.push_back(pointer.buildCoords());
1194 }
1195
1196 // Set mouse cursor position for the most common cases to avoid boilerplate.
1197 if (mSource == AINPUT_SOURCE_MOUSE &&
1198 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) &&
1199 mPointers.size() == 1) {
1200 mRawXCursorPosition = pointerCoords[0].getX();
1201 mRawYCursorPosition = pointerCoords[0].getY();
1202 }
1203
1204 MotionEvent event;
chaviw9eaa22c2020-07-01 16:21:27 -07001205 ui::Transform identityTransform;
Garfield Tandf26e862020-07-01 20:18:19 -07001206 event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC,
1207 mAction, mActionButton, /* flags */ 0, /* edgeFlags */ 0, AMETA_NONE,
chaviw9eaa22c2020-07-01 16:21:27 -07001208 mButtonState, MotionClassification::NONE, identityTransform,
1209 /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition,
1210 mRawYCursorPosition, mEventTime, mEventTime, mPointers.size(),
1211 pointerProperties.data(), pointerCoords.data());
Garfield Tandf26e862020-07-01 20:18:19 -07001212
1213 return event;
1214 }
1215
1216private:
1217 int32_t mAction;
1218 int32_t mSource;
1219 nsecs_t mEventTime;
1220 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1221 int32_t mActionButton{0};
1222 int32_t mButtonState{0};
1223 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1224 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1225
1226 std::vector<PointerBuilder> mPointers;
1227};
1228
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001229static InputEventInjectionResult injectMotionEvent(
Garfield Tandf26e862020-07-01 20:18:19 -07001230 const sp<InputDispatcher>& dispatcher, const MotionEvent& event,
1231 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001232 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT) {
Garfield Tandf26e862020-07-01 20:18:19 -07001233 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, injectionMode,
1234 injectionTimeout,
1235 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
1236}
1237
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001238static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001239 const sp<InputDispatcher>& dispatcher, int32_t action, int32_t source, int32_t displayId,
1240 const PointF& position,
1241 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001242 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1243 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001244 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001245 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC)) {
Garfield Tandf26e862020-07-01 20:18:19 -07001246 MotionEvent event = MotionEventBuilder(action, source)
1247 .displayId(displayId)
1248 .eventTime(eventTime)
1249 .rawXCursorPosition(cursorPosition.x)
1250 .rawYCursorPosition(cursorPosition.y)
1251 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1252 .x(position.x)
1253 .y(position.y))
1254 .build();
Arthur Hungb92218b2018-08-14 12:00:21 +08001255
1256 // Inject event until dispatch out.
Garfield Tandf26e862020-07-01 20:18:19 -07001257 return injectMotionEvent(dispatcher, event);
Arthur Hungb92218b2018-08-14 12:00:21 +08001258}
1259
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001260static InputEventInjectionResult injectMotionDown(const sp<InputDispatcher>& dispatcher,
1261 int32_t source, int32_t displayId,
1262 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001263 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001264}
1265
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001266static InputEventInjectionResult injectMotionUp(const sp<InputDispatcher>& dispatcher,
1267 int32_t source, int32_t displayId,
1268 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001269 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001270}
1271
Jackal Guof9696682018-10-05 12:23:23 +08001272static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1273 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1274 // Define a valid key event.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001275 NotifyKeyArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1276 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A,
1277 KEY_A, AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001278
1279 return args;
1280}
1281
chaviwd1c23182019-12-20 18:44:56 -08001282static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId,
1283 const std::vector<PointF>& points) {
1284 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001285 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1286 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1287 }
1288
chaviwd1c23182019-12-20 18:44:56 -08001289 PointerProperties pointerProperties[pointerCount];
1290 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001291
chaviwd1c23182019-12-20 18:44:56 -08001292 for (size_t i = 0; i < pointerCount; i++) {
1293 pointerProperties[i].clear();
1294 pointerProperties[i].id = i;
1295 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001296
chaviwd1c23182019-12-20 18:44:56 -08001297 pointerCoords[i].clear();
1298 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1299 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1300 }
Jackal Guof9696682018-10-05 12:23:23 +08001301
1302 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1303 // Define a valid motion event.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001304 NotifyMotionArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001305 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1306 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001307 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1308 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001309 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1310 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001311
1312 return args;
1313}
1314
chaviwd1c23182019-12-20 18:44:56 -08001315static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1316 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1317}
1318
Prabir Pradhan99987712020-11-10 18:43:05 -08001319static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(bool enabled) {
1320 return NotifyPointerCaptureChangedArgs(/* id */ 0, systemTime(SYSTEM_TIME_MONOTONIC), enabled);
1321}
1322
Arthur Hungb92218b2018-08-14 12:00:21 +08001323TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001324 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001325 sp<FakeWindowHandle> window =
1326 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001327
Arthur Hung72d8dc32020-03-28 00:48:39 +00001328 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001329 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1330 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1331 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001332
1333 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001334 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001335}
1336
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001337/**
1338 * Calling setInputWindows once with FLAG_NOT_TOUCH_MODAL should not cause any issues.
1339 * To ensure that window receives only events that were directly inside of it, add
1340 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1341 * when finding touched windows.
1342 * This test serves as a sanity check for the next test, where setInputWindows is
1343 * called twice.
1344 */
1345TEST_F(InputDispatcherTest, SetInputWindowOnce_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001346 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001347 sp<FakeWindowHandle> window =
1348 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1349 window->setFrame(Rect(0, 0, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01001350 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001351
1352 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001353 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001354 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1355 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001356 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001357
1358 // Window should receive motion event.
1359 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1360}
1361
1362/**
1363 * Calling setInputWindows twice, with the same info, should not cause any issues.
1364 * To ensure that window receives only events that were directly inside of it, add
1365 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1366 * when finding touched windows.
1367 */
1368TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001369 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001370 sp<FakeWindowHandle> window =
1371 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1372 window->setFrame(Rect(0, 0, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01001373 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001374
1375 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1376 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001377 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001378 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1379 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001380 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001381
1382 // Window should receive motion event.
1383 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1384}
1385
Arthur Hungb92218b2018-08-14 12:00:21 +08001386// The foreground window should receive the first touch down event.
1387TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001388 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001389 sp<FakeWindowHandle> windowTop =
1390 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
1391 sp<FakeWindowHandle> windowSecond =
1392 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001393
Arthur Hung72d8dc32020-03-28 00:48:39 +00001394 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001395 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1396 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1397 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001398
1399 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001400 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001401 windowSecond->assertNoEvents();
1402}
1403
Garfield Tandf26e862020-07-01 20:18:19 -07001404TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07001405 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07001406 sp<FakeWindowHandle> windowLeft =
1407 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1408 windowLeft->setFrame(Rect(0, 0, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001409 windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001410 sp<FakeWindowHandle> windowRight =
1411 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1412 windowRight->setFrame(Rect(600, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001413 windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001414
1415 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1416
1417 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
1418
1419 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001420 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001421 injectMotionEvent(mDispatcher,
1422 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1423 AINPUT_SOURCE_MOUSE)
1424 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1425 .x(900)
1426 .y(400))
1427 .build()));
1428 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1429 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1430 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1431 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1432
1433 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001434 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001435 injectMotionEvent(mDispatcher,
1436 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1437 AINPUT_SOURCE_MOUSE)
1438 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1439 .x(300)
1440 .y(400))
1441 .build()));
1442 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1443 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1444 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1445 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1446 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1447 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1448
1449 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001450 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001451 injectMotionEvent(mDispatcher,
1452 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1453 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1454 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1455 .x(300)
1456 .y(400))
1457 .build()));
1458 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1459
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001460 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001461 injectMotionEvent(mDispatcher,
1462 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
1463 AINPUT_SOURCE_MOUSE)
1464 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1465 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1466 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1467 .x(300)
1468 .y(400))
1469 .build()));
1470 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1471 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1472
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001473 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001474 injectMotionEvent(mDispatcher,
1475 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1476 AINPUT_SOURCE_MOUSE)
1477 .buttonState(0)
1478 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1479 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1480 .x(300)
1481 .y(400))
1482 .build()));
1483 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1484 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1485
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001486 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001487 injectMotionEvent(mDispatcher,
1488 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
1489 .buttonState(0)
1490 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1491 .x(300)
1492 .y(400))
1493 .build()));
1494 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1495
1496 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001497 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001498 injectMotionEvent(mDispatcher,
1499 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1500 AINPUT_SOURCE_MOUSE)
1501 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1502 .x(900)
1503 .y(400))
1504 .build()));
1505 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1506 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1507 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1508 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1509 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1510 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1511}
1512
1513// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
1514// directly in this test.
1515TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07001516 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07001517 sp<FakeWindowHandle> window =
1518 new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1519 window->setFrame(Rect(0, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001520 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001521
1522 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1523
1524 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1525
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001526 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001527 injectMotionEvent(mDispatcher,
1528 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
1529 AINPUT_SOURCE_MOUSE)
1530 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1531 .x(300)
1532 .y(400))
1533 .build()));
1534 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1535 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1536
1537 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001538 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001539 injectMotionEvent(mDispatcher,
1540 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1541 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1542 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1543 .x(300)
1544 .y(400))
1545 .build()));
1546 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1547
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001548 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001549 injectMotionEvent(mDispatcher,
1550 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
1551 AINPUT_SOURCE_MOUSE)
1552 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1553 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1554 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1555 .x(300)
1556 .y(400))
1557 .build()));
1558 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1559 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1560
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001561 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001562 injectMotionEvent(mDispatcher,
1563 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1564 AINPUT_SOURCE_MOUSE)
1565 .buttonState(0)
1566 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1567 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1568 .x(300)
1569 .y(400))
1570 .build()));
1571 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1572 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1573
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001574 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001575 injectMotionEvent(mDispatcher,
1576 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
1577 .buttonState(0)
1578 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1579 .x(300)
1580 .y(400))
1581 .build()));
1582 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1583
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001584 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001585 injectMotionEvent(mDispatcher,
1586 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
1587 AINPUT_SOURCE_MOUSE)
1588 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1589 .x(300)
1590 .y(400))
1591 .build()));
1592 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1593 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1594}
1595
Garfield Tan00f511d2019-06-12 16:55:40 -07001596TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07001597 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07001598
1599 sp<FakeWindowHandle> windowLeft =
1600 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1601 windowLeft->setFrame(Rect(0, 0, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001602 windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07001603 sp<FakeWindowHandle> windowRight =
1604 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1605 windowRight->setFrame(Rect(600, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001606 windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07001607
1608 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1609
Arthur Hung72d8dc32020-03-28 00:48:39 +00001610 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07001611
1612 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
1613 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001614 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07001615 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001616 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001617 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07001618 windowRight->assertNoEvents();
1619}
1620
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001621TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001622 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001623 sp<FakeWindowHandle> window =
1624 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07001625 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001626
Arthur Hung72d8dc32020-03-28 00:48:39 +00001627 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001628 setFocusedWindow(window);
1629
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001630 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001631
1632 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1633 mDispatcher->notifyKey(&keyArgs);
1634
1635 // Window should receive key down event.
1636 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1637
1638 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
1639 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001640 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001641 mDispatcher->notifyDeviceReset(&args);
1642 window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
1643 AKEY_EVENT_FLAG_CANCELED);
1644}
1645
1646TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001647 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001648 sp<FakeWindowHandle> window =
1649 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1650
Arthur Hung72d8dc32020-03-28 00:48:39 +00001651 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001652
1653 NotifyMotionArgs motionArgs =
1654 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1655 ADISPLAY_ID_DEFAULT);
1656 mDispatcher->notifyMotion(&motionArgs);
1657
1658 // Window should receive motion down event.
1659 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1660
1661 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
1662 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001663 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001664 mDispatcher->notifyDeviceReset(&args);
1665 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT,
1666 0 /*expectedFlags*/);
1667}
1668
Svet Ganov5d3bc372020-01-26 23:11:07 -08001669TEST_F(InputDispatcherTest, TransferTouchFocus_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07001670 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001671
1672 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001673 sp<FakeWindowHandle> firstWindow =
1674 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1675 sp<FakeWindowHandle> secondWindow =
1676 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001677
1678 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001679 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001680
1681 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001682 NotifyMotionArgs downMotionArgs =
1683 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1684 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001685 mDispatcher->notifyMotion(&downMotionArgs);
1686 // Only the first window should get the down event
1687 firstWindow->consumeMotionDown();
1688 secondWindow->assertNoEvents();
1689
1690 // Transfer touch focus to the second window
1691 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1692 // The first window gets cancel and the second gets down
1693 firstWindow->consumeMotionCancel();
1694 secondWindow->consumeMotionDown();
1695
1696 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001697 NotifyMotionArgs upMotionArgs =
1698 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1699 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001700 mDispatcher->notifyMotion(&upMotionArgs);
1701 // The first window gets no events and the second gets up
1702 firstWindow->assertNoEvents();
1703 secondWindow->consumeMotionUp();
1704}
1705
1706TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointerNoSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001707 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001708
1709 PointF touchPoint = {10, 10};
1710
1711 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001712 sp<FakeWindowHandle> firstWindow =
1713 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1714 sp<FakeWindowHandle> secondWindow =
1715 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001716
1717 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001718 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001719
1720 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001721 NotifyMotionArgs downMotionArgs =
1722 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1723 ADISPLAY_ID_DEFAULT, {touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001724 mDispatcher->notifyMotion(&downMotionArgs);
1725 // Only the first window should get the down event
1726 firstWindow->consumeMotionDown();
1727 secondWindow->assertNoEvents();
1728
1729 // Send pointer down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001730 NotifyMotionArgs pointerDownMotionArgs =
1731 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1732 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1733 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1734 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001735 mDispatcher->notifyMotion(&pointerDownMotionArgs);
1736 // Only the first window should get the pointer down event
1737 firstWindow->consumeMotionPointerDown(1);
1738 secondWindow->assertNoEvents();
1739
1740 // Transfer touch focus to the second window
1741 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1742 // The first window gets cancel and the second gets down and pointer down
1743 firstWindow->consumeMotionCancel();
1744 secondWindow->consumeMotionDown();
1745 secondWindow->consumeMotionPointerDown(1);
1746
1747 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001748 NotifyMotionArgs pointerUpMotionArgs =
1749 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1750 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1751 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1752 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001753 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1754 // The first window gets nothing and the second gets pointer up
1755 firstWindow->assertNoEvents();
1756 secondWindow->consumeMotionPointerUp(1);
1757
1758 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001759 NotifyMotionArgs upMotionArgs =
1760 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1761 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001762 mDispatcher->notifyMotion(&upMotionArgs);
1763 // The first window gets nothing and the second gets up
1764 firstWindow->assertNoEvents();
1765 secondWindow->consumeMotionUp();
1766}
1767
1768TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001769 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001770
1771 // Create a non touch modal window that supports split touch
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001772 sp<FakeWindowHandle> firstWindow =
1773 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001774 firstWindow->setFrame(Rect(0, 0, 600, 400));
Michael Wright44753b12020-07-08 13:48:11 +01001775 firstWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1776 InputWindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001777
1778 // Create a non touch modal window that supports split touch
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001779 sp<FakeWindowHandle> secondWindow =
1780 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001781 secondWindow->setFrame(Rect(0, 400, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001782 secondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1783 InputWindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001784
1785 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001786 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001787
1788 PointF pointInFirst = {300, 200};
1789 PointF pointInSecond = {300, 600};
1790
1791 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001792 NotifyMotionArgs firstDownMotionArgs =
1793 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1794 ADISPLAY_ID_DEFAULT, {pointInFirst});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001795 mDispatcher->notifyMotion(&firstDownMotionArgs);
1796 // Only the first window should get the down event
1797 firstWindow->consumeMotionDown();
1798 secondWindow->assertNoEvents();
1799
1800 // Send down to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001801 NotifyMotionArgs secondDownMotionArgs =
1802 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1803 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1804 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1805 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001806 mDispatcher->notifyMotion(&secondDownMotionArgs);
1807 // The first window gets a move and the second a down
1808 firstWindow->consumeMotionMove();
1809 secondWindow->consumeMotionDown();
1810
1811 // Transfer touch focus to the second window
1812 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1813 // The first window gets cancel and the new gets pointer down (it already saw down)
1814 firstWindow->consumeMotionCancel();
1815 secondWindow->consumeMotionPointerDown(1);
1816
1817 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001818 NotifyMotionArgs pointerUpMotionArgs =
1819 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1820 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1821 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1822 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001823 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1824 // The first window gets nothing and the second gets pointer up
1825 firstWindow->assertNoEvents();
1826 secondWindow->consumeMotionPointerUp(1);
1827
1828 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001829 NotifyMotionArgs upMotionArgs =
1830 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1831 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001832 mDispatcher->notifyMotion(&upMotionArgs);
1833 // The first window gets nothing and the second gets up
1834 firstWindow->assertNoEvents();
1835 secondWindow->consumeMotionUp();
1836}
1837
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001838TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07001839 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001840 sp<FakeWindowHandle> window =
1841 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1842
Vishnu Nair47074b82020-08-14 11:54:47 -07001843 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001844 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001845 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001846
1847 window->consumeFocusEvent(true);
1848
1849 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1850 mDispatcher->notifyKey(&keyArgs);
1851
1852 // Window should receive key down event.
1853 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1854}
1855
1856TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07001857 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001858 sp<FakeWindowHandle> window =
1859 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1860
Arthur Hung72d8dc32020-03-28 00:48:39 +00001861 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001862
1863 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1864 mDispatcher->notifyKey(&keyArgs);
1865 mDispatcher->waitForIdle();
1866
1867 window->assertNoEvents();
1868}
1869
1870// If a window is touchable, but does not have focus, it should receive motion events, but not keys
1871TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07001872 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001873 sp<FakeWindowHandle> window =
1874 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1875
Arthur Hung72d8dc32020-03-28 00:48:39 +00001876 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001877
1878 // Send key
1879 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1880 mDispatcher->notifyKey(&keyArgs);
1881 // Send motion
1882 NotifyMotionArgs motionArgs =
1883 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1884 ADISPLAY_ID_DEFAULT);
1885 mDispatcher->notifyMotion(&motionArgs);
1886
1887 // Window should receive only the motion event
1888 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1889 window->assertNoEvents(); // Key event or focus event will not be received
1890}
1891
arthurhungea3f4fc2020-12-21 23:18:53 +08001892TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
1893 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1894
1895 // Create first non touch modal window that supports split touch
1896 sp<FakeWindowHandle> firstWindow =
1897 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1898 firstWindow->setFrame(Rect(0, 0, 600, 400));
1899 firstWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1900 InputWindowInfo::Flag::SPLIT_TOUCH);
1901
1902 // Create second non touch modal window that supports split touch
1903 sp<FakeWindowHandle> secondWindow =
1904 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
1905 secondWindow->setFrame(Rect(0, 400, 600, 800));
1906 secondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1907 InputWindowInfo::Flag::SPLIT_TOUCH);
1908
1909 // Add the windows to the dispatcher
1910 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
1911
1912 PointF pointInFirst = {300, 200};
1913 PointF pointInSecond = {300, 600};
1914
1915 // Send down to the first window
1916 NotifyMotionArgs firstDownMotionArgs =
1917 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1918 ADISPLAY_ID_DEFAULT, {pointInFirst});
1919 mDispatcher->notifyMotion(&firstDownMotionArgs);
1920 // Only the first window should get the down event
1921 firstWindow->consumeMotionDown();
1922 secondWindow->assertNoEvents();
1923
1924 // Send down to the second window
1925 NotifyMotionArgs secondDownMotionArgs =
1926 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1927 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1928 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1929 {pointInFirst, pointInSecond});
1930 mDispatcher->notifyMotion(&secondDownMotionArgs);
1931 // The first window gets a move and the second a down
1932 firstWindow->consumeMotionMove();
1933 secondWindow->consumeMotionDown();
1934
1935 // Send pointer cancel to the second window
1936 NotifyMotionArgs pointerUpMotionArgs =
1937 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1938 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1939 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1940 {pointInFirst, pointInSecond});
1941 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
1942 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1943 // The first window gets move and the second gets cancel.
1944 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
1945 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
1946
1947 // Send up event.
1948 NotifyMotionArgs upMotionArgs =
1949 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1950 ADISPLAY_ID_DEFAULT);
1951 mDispatcher->notifyMotion(&upMotionArgs);
1952 // The first window gets up and the second gets nothing.
1953 firstWindow->consumeMotionUp();
1954 secondWindow->assertNoEvents();
1955}
1956
chaviwd1c23182019-12-20 18:44:56 -08001957class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00001958public:
1959 FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name,
chaviwd1c23182019-12-20 18:44:56 -08001960 int32_t displayId, bool isGestureMonitor = false) {
Garfield Tan15601662020-09-22 15:32:38 -07001961 base::Result<std::unique_ptr<InputChannel>> channel =
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +00001962 dispatcher->createInputMonitor(displayId, isGestureMonitor, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07001963 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00001964 }
1965
chaviwd1c23182019-12-20 18:44:56 -08001966 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
1967
1968 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1969 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN,
1970 expectedDisplayId, expectedFlags);
1971 }
1972
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001973 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
1974
1975 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
1976
chaviwd1c23182019-12-20 18:44:56 -08001977 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1978 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
1979 expectedDisplayId, expectedFlags);
1980 }
1981
1982 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1983 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP,
1984 expectedDisplayId, expectedFlags);
1985 }
1986
1987 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
1988
1989private:
1990 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00001991};
1992
1993// Tests for gesture monitors
1994TEST_F(InputDispatcherTest, GestureMonitor_ReceivesMotionEvents) {
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
2010TEST_F(InputDispatcherTest, GestureMonitor_DoesNotReceiveKeyEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07002011 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00002012 sp<FakeWindowHandle> window =
2013 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2014
2015 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002016 window->setFocusable(true);
Michael Wright3a240c42019-12-10 20:53:41 +00002017
Arthur Hung72d8dc32020-03-28 00:48:39 +00002018 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002019 setFocusedWindow(window);
2020
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002021 window->consumeFocusEvent(true);
Michael Wright3a240c42019-12-10 20:53:41 +00002022
chaviwd1c23182019-12-20 18:44:56 -08002023 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
2024 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00002025
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002026 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2027 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00002028 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002029 monitor.assertNoEvents();
Michael Wright3a240c42019-12-10 20:53:41 +00002030}
2031
2032TEST_F(InputDispatcherTest, GestureMonitor_CanPilferAfterWindowIsRemovedMidStream) {
Chris Yea209fde2020-07-22 13:54:51 -07002033 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00002034 sp<FakeWindowHandle> window =
2035 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002036 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00002037
chaviwd1c23182019-12-20 18:44:56 -08002038 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
2039 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00002040
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002041 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00002042 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002043 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00002044 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002045 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00002046
2047 window->releaseChannel();
2048
chaviwd1c23182019-12-20 18:44:56 -08002049 mDispatcher->pilferPointers(monitor.getToken());
Michael Wright3a240c42019-12-10 20:53:41 +00002050
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002051 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00002052 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002053 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08002054 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00002055}
2056
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002057TEST_F(InputDispatcherTest, UnresponsiveGestureMonitor_GetsAnr) {
2058 FakeMonitorReceiver monitor =
2059 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
2060 true /*isGestureMonitor*/);
2061
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002062 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002063 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
2064 std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
2065 ASSERT_TRUE(consumeSeq);
2066
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00002067 mFakePolicy->assertNotifyMonitorUnresponsiveWasCalled(DISPATCHING_TIMEOUT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002068 monitor.finishEvent(*consumeSeq);
2069 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00002070 mFakePolicy->assertNotifyMonitorResponsiveWasCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002071}
2072
chaviw81e2bb92019-12-18 15:03:51 -08002073TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002074 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
chaviw81e2bb92019-12-18 15:03:51 -08002075 sp<FakeWindowHandle> window =
2076 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2077
Arthur Hung72d8dc32020-03-28 00:48:39 +00002078 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08002079
2080 NotifyMotionArgs motionArgs =
2081 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2082 ADISPLAY_ID_DEFAULT);
2083
2084 mDispatcher->notifyMotion(&motionArgs);
2085 // Window should receive motion down event.
2086 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2087
2088 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002089 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08002090 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
2091 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
2092 motionArgs.pointerCoords[0].getX() - 10);
2093
2094 mDispatcher->notifyMotion(&motionArgs);
2095 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
2096 0 /*expectedFlags*/);
2097}
2098
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002099/**
2100 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
2101 * the device default right away. In the test scenario, we check both the default value,
2102 * and the action of enabling / disabling.
2103 */
2104TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07002105 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002106 sp<FakeWindowHandle> window =
2107 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2108
2109 // Set focused application.
2110 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002111 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002112
2113 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00002114 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002115 setFocusedWindow(window);
2116
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002117 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2118
2119 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07002120 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002121 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002122 window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/);
2123
2124 SCOPED_TRACE("Disable touch mode");
2125 mDispatcher->setInTouchMode(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07002126 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002127 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002128 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002129 window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/);
2130
2131 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07002132 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002133 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002134 window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/);
2135
2136 SCOPED_TRACE("Enable touch mode again");
2137 mDispatcher->setInTouchMode(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07002138 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002139 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002140 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002141 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2142
2143 window->assertNoEvents();
2144}
2145
Gang Wange9087892020-01-07 12:17:14 -05002146TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002147 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Gang Wange9087892020-01-07 12:17:14 -05002148 sp<FakeWindowHandle> window =
2149 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2150
2151 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002152 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05002153
Arthur Hung72d8dc32020-03-28 00:48:39 +00002154 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002155 setFocusedWindow(window);
2156
Gang Wange9087892020-01-07 12:17:14 -05002157 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2158
2159 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
2160 mDispatcher->notifyKey(&keyArgs);
2161
2162 InputEvent* event = window->consume();
2163 ASSERT_NE(event, nullptr);
2164
2165 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
2166 ASSERT_NE(verified, nullptr);
2167 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
2168
2169 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
2170 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
2171 ASSERT_EQ(keyArgs.source, verified->source);
2172 ASSERT_EQ(keyArgs.displayId, verified->displayId);
2173
2174 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
2175
2176 ASSERT_EQ(keyArgs.action, verifiedKey.action);
2177 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05002178 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
2179 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
2180 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
2181 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
2182 ASSERT_EQ(0, verifiedKey.repeatCount);
2183}
2184
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002185TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002186 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002187 sp<FakeWindowHandle> window =
2188 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2189
2190 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2191
Arthur Hung72d8dc32020-03-28 00:48:39 +00002192 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002193
2194 NotifyMotionArgs motionArgs =
2195 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2196 ADISPLAY_ID_DEFAULT);
2197 mDispatcher->notifyMotion(&motionArgs);
2198
2199 InputEvent* event = window->consume();
2200 ASSERT_NE(event, nullptr);
2201
2202 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
2203 ASSERT_NE(verified, nullptr);
2204 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
2205
2206 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
2207 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
2208 EXPECT_EQ(motionArgs.source, verified->source);
2209 EXPECT_EQ(motionArgs.displayId, verified->displayId);
2210
2211 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
2212
2213 EXPECT_EQ(motionArgs.pointerCoords[0].getX(), verifiedMotion.rawX);
2214 EXPECT_EQ(motionArgs.pointerCoords[0].getY(), verifiedMotion.rawY);
2215 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
2216 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
2217 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
2218 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
2219 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
2220}
2221
Prabir Pradhanbd527712021-03-09 19:17:09 -08002222TEST_F(InputDispatcherTest, NonPointerMotionEvent_NotTransformed) {
yunho.shinf4a80b82020-11-16 21:13:57 +09002223 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2224 sp<FakeWindowHandle> window =
2225 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2226 const std::string name = window->getName();
2227
2228 // Window gets transformed by offset values.
2229 window->setWindowOffset(500.0f, 500.0f);
2230
2231 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2232 window->setFocusable(true);
2233
2234 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2235
2236 // First, we set focused window so that focusedWindowHandle is not null.
2237 setFocusedWindow(window);
2238
2239 // Second, we consume focus event if it is right or wrong according to onFocusChangedLocked.
2240 window->consumeFocusEvent(true);
2241
Prabir Pradhanbd527712021-03-09 19:17:09 -08002242 constexpr const std::array nonPointerSources = {AINPUT_SOURCE_TRACKBALL,
2243 AINPUT_SOURCE_MOUSE_RELATIVE,
2244 AINPUT_SOURCE_JOYSTICK};
2245 for (const int source : nonPointerSources) {
2246 // Notify motion with a non-pointer source.
2247 NotifyMotionArgs motionArgs =
2248 generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, source, ADISPLAY_ID_DEFAULT);
2249 mDispatcher->notifyMotion(&motionArgs);
yunho.shinf4a80b82020-11-16 21:13:57 +09002250
Prabir Pradhanbd527712021-03-09 19:17:09 -08002251 InputEvent* event = window->consume();
2252 ASSERT_NE(event, nullptr);
2253 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
2254 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
2255 << " event, got " << inputEventTypeToString(event->getType()) << " event";
yunho.shinf4a80b82020-11-16 21:13:57 +09002256
Prabir Pradhanbd527712021-03-09 19:17:09 -08002257 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
2258 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, motionEvent.getAction());
2259 EXPECT_EQ(motionArgs.pointerCount, motionEvent.getPointerCount());
yunho.shinf4a80b82020-11-16 21:13:57 +09002260
Prabir Pradhanbd527712021-03-09 19:17:09 -08002261 float expectedX = motionArgs.pointerCoords[0].getX();
2262 float expectedY = motionArgs.pointerCoords[0].getY();
yunho.shinf4a80b82020-11-16 21:13:57 +09002263
Prabir Pradhanbd527712021-03-09 19:17:09 -08002264 // Ensure the axis values from the final motion event are not transformed.
2265 EXPECT_EQ(expectedX, motionEvent.getX(0))
2266 << "expected " << expectedX << " for x coord of " << name.c_str() << ", got "
2267 << motionEvent.getX(0);
2268 EXPECT_EQ(expectedY, motionEvent.getY(0))
2269 << "expected " << expectedY << " for y coord of " << name.c_str() << ", got "
2270 << motionEvent.getY(0);
2271 // Ensure the raw and transformed axis values for the motion event are the same.
2272 EXPECT_EQ(motionEvent.getRawX(0), motionEvent.getX(0))
2273 << "expected raw and transformed X-axis values to be equal";
2274 EXPECT_EQ(motionEvent.getRawY(0), motionEvent.getY(0))
2275 << "expected raw and transformed Y-axis values to be equal";
2276 }
yunho.shinf4a80b82020-11-16 21:13:57 +09002277}
2278
chaviw09c8d2d2020-08-24 15:48:26 -07002279/**
2280 * Ensure that separate calls to sign the same data are generating the same key.
2281 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
2282 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
2283 * tests.
2284 */
2285TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
2286 KeyEvent event = getTestKeyEvent();
2287 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
2288
2289 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
2290 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
2291 ASSERT_EQ(hmac1, hmac2);
2292}
2293
2294/**
2295 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
2296 */
2297TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
2298 KeyEvent event = getTestKeyEvent();
2299 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
2300 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
2301
2302 verifiedEvent.deviceId += 1;
2303 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2304
2305 verifiedEvent.source += 1;
2306 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2307
2308 verifiedEvent.eventTimeNanos += 1;
2309 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2310
2311 verifiedEvent.displayId += 1;
2312 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2313
2314 verifiedEvent.action += 1;
2315 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2316
2317 verifiedEvent.downTimeNanos += 1;
2318 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2319
2320 verifiedEvent.flags += 1;
2321 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2322
2323 verifiedEvent.keyCode += 1;
2324 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2325
2326 verifiedEvent.scanCode += 1;
2327 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2328
2329 verifiedEvent.metaState += 1;
2330 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2331
2332 verifiedEvent.repeatCount += 1;
2333 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2334}
2335
Vishnu Nair958da932020-08-21 17:12:37 -07002336TEST_F(InputDispatcherTest, SetFocusedWindow) {
2337 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2338 sp<FakeWindowHandle> windowTop =
2339 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2340 sp<FakeWindowHandle> windowSecond =
2341 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2342 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2343
2344 // Top window is also focusable but is not granted focus.
2345 windowTop->setFocusable(true);
2346 windowSecond->setFocusable(true);
2347 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2348 setFocusedWindow(windowSecond);
2349
2350 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002351 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2352 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002353
2354 // Focused window should receive event.
2355 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
2356 windowTop->assertNoEvents();
2357}
2358
2359TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
2360 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2361 sp<FakeWindowHandle> window =
2362 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2363 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2364
2365 window->setFocusable(true);
2366 // Release channel for window is no longer valid.
2367 window->releaseChannel();
2368 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2369 setFocusedWindow(window);
2370
2371 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002372 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2373 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002374
2375 // window channel is invalid, so it should not receive any input event.
2376 window->assertNoEvents();
2377}
2378
2379TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
2380 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2381 sp<FakeWindowHandle> window =
2382 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2383 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2384
2385 // Window is not focusable.
2386 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2387 setFocusedWindow(window);
2388
2389 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002390 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2391 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002392
2393 // window is invalid, so it should not receive any input event.
2394 window->assertNoEvents();
2395}
2396
2397TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
2398 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2399 sp<FakeWindowHandle> windowTop =
2400 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2401 sp<FakeWindowHandle> windowSecond =
2402 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2403 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2404
2405 windowTop->setFocusable(true);
2406 windowSecond->setFocusable(true);
2407 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2408 setFocusedWindow(windowTop);
2409 windowTop->consumeFocusEvent(true);
2410
2411 setFocusedWindow(windowSecond, windowTop);
2412 windowSecond->consumeFocusEvent(true);
2413 windowTop->consumeFocusEvent(false);
2414
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002415 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2416 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002417
2418 // Focused window should receive event.
2419 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
2420}
2421
2422TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) {
2423 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2424 sp<FakeWindowHandle> windowTop =
2425 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2426 sp<FakeWindowHandle> windowSecond =
2427 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2428 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2429
2430 windowTop->setFocusable(true);
2431 windowSecond->setFocusable(true);
2432 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2433 setFocusedWindow(windowSecond, windowTop);
2434
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002435 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2436 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002437
2438 // Event should be dropped.
2439 windowTop->assertNoEvents();
2440 windowSecond->assertNoEvents();
2441}
2442
2443TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
2444 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2445 sp<FakeWindowHandle> window =
2446 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2447 sp<FakeWindowHandle> previousFocusedWindow =
2448 new FakeWindowHandle(application, mDispatcher, "previousFocusedWindow",
2449 ADISPLAY_ID_DEFAULT);
2450 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2451
2452 window->setFocusable(true);
2453 previousFocusedWindow->setFocusable(true);
2454 window->setVisible(false);
2455 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
2456 setFocusedWindow(previousFocusedWindow);
2457 previousFocusedWindow->consumeFocusEvent(true);
2458
2459 // Requesting focus on invisible window takes focus from currently focused window.
2460 setFocusedWindow(window);
2461 previousFocusedWindow->consumeFocusEvent(false);
2462
2463 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002464 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07002465 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002466 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07002467
2468 // Window does not get focus event or key down.
2469 window->assertNoEvents();
2470
2471 // Window becomes visible.
2472 window->setVisible(true);
2473 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2474
2475 // Window receives focus event.
2476 window->consumeFocusEvent(true);
2477 // Focused window receives key down.
2478 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2479}
2480
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002481/**
2482 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
2483 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
2484 * of the 'slipperyEnterWindow'.
2485 *
2486 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
2487 * a way so that the touched location is no longer covered by the top window.
2488 *
2489 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
2490 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
2491 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
2492 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
2493 * with ACTION_DOWN).
2494 * Thus, the touch has been transferred from the top window into the bottom window, because the top
2495 * window moved itself away from the touched location and had Flag::SLIPPERY.
2496 *
2497 * Even though the top window moved away from the touched location, it is still obscuring the bottom
2498 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
2499 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
2500 *
2501 * In this test, we ensure that the event received by the bottom window has
2502 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
2503 */
2504TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
2505 constexpr int32_t SLIPPERY_PID = INJECTOR_PID + 1;
2506 constexpr int32_t SLIPPERY_UID = INJECTOR_UID + 1;
2507
2508 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2509 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2510
2511 sp<FakeWindowHandle> slipperyExitWindow =
2512 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2513 slipperyExitWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
2514 InputWindowInfo::Flag::SLIPPERY);
2515 // Make sure this one overlaps the bottom window
2516 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
2517 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
2518 // one. Windows with the same owner are not considered to be occluding each other.
2519 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
2520
2521 sp<FakeWindowHandle> slipperyEnterWindow =
2522 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2523 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
2524
2525 mDispatcher->setInputWindows(
2526 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
2527
2528 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
2529 NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2530 ADISPLAY_ID_DEFAULT, {{50, 50}});
2531 mDispatcher->notifyMotion(&args);
2532 slipperyExitWindow->consumeMotionDown();
2533 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
2534 mDispatcher->setInputWindows(
2535 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
2536
2537 args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2538 ADISPLAY_ID_DEFAULT, {{51, 51}});
2539 mDispatcher->notifyMotion(&args);
2540
2541 slipperyExitWindow->consumeMotionCancel();
2542
2543 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
2544 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
2545}
2546
Garfield Tan1c7bc862020-01-28 13:24:04 -08002547class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
2548protected:
2549 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
2550 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
2551
Chris Yea209fde2020-07-22 13:54:51 -07002552 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002553 sp<FakeWindowHandle> mWindow;
2554
2555 virtual void SetUp() override {
2556 mFakePolicy = new FakeInputDispatcherPolicy();
2557 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
2558 mDispatcher = new InputDispatcher(mFakePolicy);
2559 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
2560 ASSERT_EQ(OK, mDispatcher->start());
2561
2562 setUpWindow();
2563 }
2564
2565 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07002566 mApp = std::make_shared<FakeApplicationHandle>();
Garfield Tan1c7bc862020-01-28 13:24:04 -08002567 mWindow = new FakeWindowHandle(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2568
Vishnu Nair47074b82020-08-14 11:54:47 -07002569 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002570 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002571 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002572 mWindow->consumeFocusEvent(true);
2573 }
2574
Chris Ye2ad95392020-09-01 13:44:44 -07002575 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08002576 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07002577 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002578 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
2579 mDispatcher->notifyKey(&keyArgs);
2580
2581 // Window should receive key down event.
2582 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2583 }
2584
2585 void expectKeyRepeatOnce(int32_t repeatCount) {
2586 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
2587 InputEvent* repeatEvent = mWindow->consume();
2588 ASSERT_NE(nullptr, repeatEvent);
2589
2590 uint32_t eventType = repeatEvent->getType();
2591 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType);
2592
2593 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
2594 uint32_t eventAction = repeatKeyEvent->getAction();
2595 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
2596 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
2597 }
2598
Chris Ye2ad95392020-09-01 13:44:44 -07002599 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08002600 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07002601 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002602 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
2603 mDispatcher->notifyKey(&keyArgs);
2604
2605 // Window should receive key down event.
2606 mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
2607 0 /*expectedFlags*/);
2608 }
2609};
2610
2611TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Chris Ye2ad95392020-09-01 13:44:44 -07002612 sendAndConsumeKeyDown(1 /* deviceId */);
2613 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2614 expectKeyRepeatOnce(repeatCount);
2615 }
2616}
2617
2618TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
2619 sendAndConsumeKeyDown(1 /* deviceId */);
2620 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2621 expectKeyRepeatOnce(repeatCount);
2622 }
2623 sendAndConsumeKeyDown(2 /* deviceId */);
2624 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08002625 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2626 expectKeyRepeatOnce(repeatCount);
2627 }
2628}
2629
2630TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Chris Ye2ad95392020-09-01 13:44:44 -07002631 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002632 expectKeyRepeatOnce(1 /*repeatCount*/);
Chris Ye2ad95392020-09-01 13:44:44 -07002633 sendAndConsumeKeyUp(1 /* deviceId */);
2634 mWindow->assertNoEvents();
2635}
2636
2637TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
2638 sendAndConsumeKeyDown(1 /* deviceId */);
2639 expectKeyRepeatOnce(1 /*repeatCount*/);
2640 sendAndConsumeKeyDown(2 /* deviceId */);
2641 expectKeyRepeatOnce(1 /*repeatCount*/);
2642 // Stale key up from device 1.
2643 sendAndConsumeKeyUp(1 /* deviceId */);
2644 // Device 2 is still down, keep repeating
2645 expectKeyRepeatOnce(2 /*repeatCount*/);
2646 expectKeyRepeatOnce(3 /*repeatCount*/);
2647 // Device 2 key up
2648 sendAndConsumeKeyUp(2 /* deviceId */);
2649 mWindow->assertNoEvents();
2650}
2651
2652TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
2653 sendAndConsumeKeyDown(1 /* deviceId */);
2654 expectKeyRepeatOnce(1 /*repeatCount*/);
2655 sendAndConsumeKeyDown(2 /* deviceId */);
2656 expectKeyRepeatOnce(1 /*repeatCount*/);
2657 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
2658 sendAndConsumeKeyUp(2 /* deviceId */);
2659 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08002660 mWindow->assertNoEvents();
2661}
2662
2663TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Chris Ye2ad95392020-09-01 13:44:44 -07002664 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002665 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2666 InputEvent* repeatEvent = mWindow->consume();
2667 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2668 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
2669 IdGenerator::getSource(repeatEvent->getId()));
2670 }
2671}
2672
2673TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Chris Ye2ad95392020-09-01 13:44:44 -07002674 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002675
2676 std::unordered_set<int32_t> idSet;
2677 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2678 InputEvent* repeatEvent = mWindow->consume();
2679 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2680 int32_t id = repeatEvent->getId();
2681 EXPECT_EQ(idSet.end(), idSet.find(id));
2682 idSet.insert(id);
2683 }
2684}
2685
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002686/* Test InputDispatcher for MultiDisplay */
2687class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
2688public:
2689 static constexpr int32_t SECOND_DISPLAY_ID = 1;
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002690 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002691 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08002692
Chris Yea209fde2020-07-22 13:54:51 -07002693 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002694 windowInPrimary =
2695 new FakeWindowHandle(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002696
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002697 // Set focus window for primary display, but focused display would be second one.
2698 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07002699 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002700 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002701 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002702 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08002703
Chris Yea209fde2020-07-22 13:54:51 -07002704 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002705 windowInSecondary =
2706 new FakeWindowHandle(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002707 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002708 // Set focus display to second one.
2709 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
2710 // Set focus window for second display.
2711 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07002712 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002713 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002714 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002715 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002716 }
2717
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002718 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002719 InputDispatcherTest::TearDown();
2720
Chris Yea209fde2020-07-22 13:54:51 -07002721 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002722 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07002723 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002724 windowInSecondary.clear();
2725 }
2726
2727protected:
Chris Yea209fde2020-07-22 13:54:51 -07002728 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002729 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07002730 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002731 sp<FakeWindowHandle> windowInSecondary;
2732};
2733
2734TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
2735 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002736 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2737 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2738 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002739 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08002740 windowInSecondary->assertNoEvents();
2741
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002742 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002743 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2744 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
2745 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08002746 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002747 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08002748}
2749
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002750TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08002751 // Test inject a key down with display id specified.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002752 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2753 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002754 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08002755 windowInSecondary->assertNoEvents();
2756
2757 // Test inject a key down without display id specified.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002758 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2759 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08002760 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002761 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08002762
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002763 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00002764 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08002765
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002766 // Old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002767 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
2768 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08002769
2770 // Test inject a key down, should timeout because of no target window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002771 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2772 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08002773 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002774 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08002775 windowInSecondary->assertNoEvents();
2776}
2777
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002778// Test per-display input monitors for motion event.
2779TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08002780 FakeMonitorReceiver monitorInPrimary =
2781 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
2782 FakeMonitorReceiver monitorInSecondary =
2783 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002784
2785 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002786 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2787 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2788 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002789 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002790 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002791 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002792 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002793
2794 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002795 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2796 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
2797 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002798 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002799 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002800 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08002801 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002802
2803 // Test inject a non-pointer motion event.
2804 // If specific a display, it will dispatch to the focused window of particular display,
2805 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002806 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2807 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
2808 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002809 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002810 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002811 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08002812 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002813}
2814
2815// Test per-display input monitors for key event.
2816TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002817 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08002818 FakeMonitorReceiver monitorInPrimary =
2819 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
2820 FakeMonitorReceiver monitorInSecondary =
2821 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002822
2823 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002824 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2825 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002826 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002827 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002828 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08002829 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002830}
2831
Vishnu Nair958da932020-08-21 17:12:37 -07002832TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
2833 sp<FakeWindowHandle> secondWindowInPrimary =
2834 new FakeWindowHandle(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
2835 secondWindowInPrimary->setFocusable(true);
2836 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
2837 setFocusedWindow(secondWindowInPrimary);
2838 windowInPrimary->consumeFocusEvent(false);
2839 secondWindowInPrimary->consumeFocusEvent(true);
2840
2841 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002842 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2843 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002844 windowInPrimary->assertNoEvents();
2845 windowInSecondary->assertNoEvents();
2846 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2847}
2848
Jackal Guof9696682018-10-05 12:23:23 +08002849class InputFilterTest : public InputDispatcherTest {
2850protected:
2851 static constexpr int32_t SECOND_DISPLAY_ID = 1;
2852
2853 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) {
2854 NotifyMotionArgs motionArgs;
2855
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002856 motionArgs =
2857 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08002858 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002859 motionArgs =
2860 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08002861 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002862 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08002863 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08002864 mFakePolicy->assertFilterInputEventWasCalled(motionArgs);
Jackal Guof9696682018-10-05 12:23:23 +08002865 } else {
2866 mFakePolicy->assertFilterInputEventWasNotCalled();
2867 }
2868 }
2869
2870 void testNotifyKey(bool expectToBeFiltered) {
2871 NotifyKeyArgs keyArgs;
2872
2873 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
2874 mDispatcher->notifyKey(&keyArgs);
2875 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
2876 mDispatcher->notifyKey(&keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002877 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08002878
2879 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08002880 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08002881 } else {
2882 mFakePolicy->assertFilterInputEventWasNotCalled();
2883 }
2884 }
2885};
2886
2887// Test InputFilter for MotionEvent
2888TEST_F(InputFilterTest, MotionEvent_InputFilter) {
2889 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
2890 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
2891 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
2892
2893 // Enable InputFilter
2894 mDispatcher->setInputFilterEnabled(true);
2895 // Test touch on both primary and second display, and check if both events are filtered.
2896 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
2897 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
2898
2899 // Disable InputFilter
2900 mDispatcher->setInputFilterEnabled(false);
2901 // Test touch on both primary and second display, and check if both events aren't filtered.
2902 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
2903 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
2904}
2905
2906// Test InputFilter for KeyEvent
2907TEST_F(InputFilterTest, KeyEvent_InputFilter) {
2908 // Since the InputFilter is disabled by default, check if key event aren't filtered.
2909 testNotifyKey(/*expectToBeFiltered*/ false);
2910
2911 // Enable InputFilter
2912 mDispatcher->setInputFilterEnabled(true);
2913 // Send a key event, and check if it is filtered.
2914 testNotifyKey(/*expectToBeFiltered*/ true);
2915
2916 // Disable InputFilter
2917 mDispatcher->setInputFilterEnabled(false);
2918 // Send a key event, and check if it isn't filtered.
2919 testNotifyKey(/*expectToBeFiltered*/ false);
2920}
2921
chaviwfd6d3512019-03-25 13:23:49 -07002922class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002923 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07002924 InputDispatcherTest::SetUp();
2925
Chris Yea209fde2020-07-22 13:54:51 -07002926 std::shared_ptr<FakeApplicationHandle> application =
2927 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002928 mUnfocusedWindow =
2929 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07002930 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
2931 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
2932 // window.
Michael Wright44753b12020-07-08 13:48:11 +01002933 mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07002934
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002935 mFocusedWindow =
2936 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2937 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01002938 mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07002939
2940 // Set focused application.
2941 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002942 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07002943
2944 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00002945 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002946 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002947 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07002948 }
2949
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002950 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07002951 InputDispatcherTest::TearDown();
2952
2953 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002954 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07002955 }
2956
2957protected:
2958 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002959 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002960 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07002961};
2962
2963// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
2964// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
2965// the onPointerDownOutsideFocus callback.
2966TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002967 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002968 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2969 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002970 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002971 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07002972
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002973 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07002974 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
2975}
2976
2977// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
2978// DOWN on the window that doesn't have focus. Ensure no window received the
2979// onPointerDownOutsideFocus callback.
2980TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002981 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002982 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002983 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002984 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07002985
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002986 ASSERT_TRUE(mDispatcher->waitForIdle());
2987 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07002988}
2989
2990// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
2991// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
2992TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002993 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2994 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002995 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07002996
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002997 ASSERT_TRUE(mDispatcher->waitForIdle());
2998 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07002999}
3000
3001// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
3002// DOWN on the window that already has focus. Ensure no window received the
3003// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10003004TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003005 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08003006 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07003007 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003008 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07003009 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07003010
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003011 ASSERT_TRUE(mDispatcher->waitForIdle());
3012 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07003013}
3014
chaviwaf87b3e2019-10-01 16:59:28 -07003015// These tests ensures we can send touch events to a single client when there are multiple input
3016// windows that point to the same client token.
3017class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
3018 virtual void SetUp() override {
3019 InputDispatcherTest::SetUp();
3020
Chris Yea209fde2020-07-22 13:54:51 -07003021 std::shared_ptr<FakeApplicationHandle> application =
3022 std::make_shared<FakeApplicationHandle>();
chaviwaf87b3e2019-10-01 16:59:28 -07003023 mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1",
3024 ADISPLAY_ID_DEFAULT);
3025 // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window.
3026 // 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 +01003027 mWindow1->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3028 InputWindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07003029 mWindow1->setFrame(Rect(0, 0, 100, 100));
3030
3031 mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2",
3032 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
Michael Wright44753b12020-07-08 13:48:11 +01003033 mWindow2->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3034 InputWindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07003035 mWindow2->setFrame(Rect(100, 100, 200, 200));
3036
Arthur Hung72d8dc32020-03-28 00:48:39 +00003037 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07003038 }
3039
3040protected:
3041 sp<FakeWindowHandle> mWindow1;
3042 sp<FakeWindowHandle> mWindow2;
3043
3044 // Helper function to convert the point from screen coordinates into the window's space
3045 static PointF getPointInWindow(const InputWindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07003046 vec2 vals = windowInfo->transform.transform(point.x, point.y);
3047 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07003048 }
3049
3050 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
3051 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003052 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07003053 InputEvent* event = window->consume();
3054
3055 ASSERT_NE(nullptr, event) << name.c_str()
3056 << ": consumer should have returned non-NULL event.";
3057
3058 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
3059 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
3060 << " event, got " << inputEventTypeToString(event->getType()) << " event";
3061
3062 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
3063 EXPECT_EQ(expectedAction, motionEvent.getAction());
3064
3065 for (size_t i = 0; i < points.size(); i++) {
3066 float expectedX = points[i].x;
3067 float expectedY = points[i].y;
3068
3069 EXPECT_EQ(expectedX, motionEvent.getX(i))
3070 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
3071 << ", got " << motionEvent.getX(i);
3072 EXPECT_EQ(expectedY, motionEvent.getY(i))
3073 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
3074 << ", got " << motionEvent.getY(i);
3075 }
3076 }
chaviw9eaa22c2020-07-01 16:21:27 -07003077
3078 void touchAndAssertPositions(int32_t action, std::vector<PointF> touchedPoints,
3079 std::vector<PointF> expectedPoints) {
3080 NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
3081 ADISPLAY_ID_DEFAULT, touchedPoints);
3082 mDispatcher->notifyMotion(&motionArgs);
3083
3084 // Always consume from window1 since it's the window that has the InputReceiver
3085 consumeMotionEvent(mWindow1, action, expectedPoints);
3086 }
chaviwaf87b3e2019-10-01 16:59:28 -07003087};
3088
3089TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
3090 // Touch Window 1
3091 PointF touchedPoint = {10, 10};
3092 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003093 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003094
3095 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07003096 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003097
3098 // Touch Window 2
3099 touchedPoint = {150, 150};
3100 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003101 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003102}
3103
chaviw9eaa22c2020-07-01 16:21:27 -07003104TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
3105 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07003106 mWindow2->setWindowScale(0.5f, 0.5f);
3107
3108 // Touch Window 1
3109 PointF touchedPoint = {10, 10};
3110 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003111 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003112 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07003113 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003114
3115 // Touch Window 2
3116 touchedPoint = {150, 150};
3117 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003118 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
3119 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003120
chaviw9eaa22c2020-07-01 16:21:27 -07003121 // Update the transform so rotation is set
3122 mWindow2->setWindowTransform(0, -1, 1, 0);
3123 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
3124 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003125}
3126
chaviw9eaa22c2020-07-01 16:21:27 -07003127TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003128 mWindow2->setWindowScale(0.5f, 0.5f);
3129
3130 // Touch Window 1
3131 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3132 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003133 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003134
3135 // Touch Window 2
3136 int32_t actionPointerDown =
3137 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003138 touchedPoints.push_back(PointF{150, 150});
3139 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3140 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003141
chaviw9eaa22c2020-07-01 16:21:27 -07003142 // Release Window 2
3143 int32_t actionPointerUp =
3144 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3145 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
3146 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003147
chaviw9eaa22c2020-07-01 16:21:27 -07003148 // Update the transform so rotation is set for Window 2
3149 mWindow2->setWindowTransform(0, -1, 1, 0);
3150 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3151 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003152}
3153
chaviw9eaa22c2020-07-01 16:21:27 -07003154TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003155 mWindow2->setWindowScale(0.5f, 0.5f);
3156
3157 // Touch Window 1
3158 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3159 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003160 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003161
3162 // Touch Window 2
3163 int32_t actionPointerDown =
3164 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003165 touchedPoints.push_back(PointF{150, 150});
3166 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003167
chaviw9eaa22c2020-07-01 16:21:27 -07003168 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003169
3170 // Move both windows
3171 touchedPoints = {{20, 20}, {175, 175}};
3172 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3173 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3174
chaviw9eaa22c2020-07-01 16:21:27 -07003175 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003176
chaviw9eaa22c2020-07-01 16:21:27 -07003177 // Release Window 2
3178 int32_t actionPointerUp =
3179 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3180 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
3181 expectedPoints.pop_back();
3182
3183 // Touch Window 2
3184 mWindow2->setWindowTransform(0, -1, 1, 0);
3185 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3186 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
3187
3188 // Move both windows
3189 touchedPoints = {{20, 20}, {175, 175}};
3190 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3191 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3192
3193 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003194}
3195
3196TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
3197 mWindow1->setWindowScale(0.5f, 0.5f);
3198
3199 // Touch Window 1
3200 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3201 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003202 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003203
3204 // Touch Window 2
3205 int32_t actionPointerDown =
3206 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003207 touchedPoints.push_back(PointF{150, 150});
3208 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003209
chaviw9eaa22c2020-07-01 16:21:27 -07003210 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003211
3212 // Move both windows
3213 touchedPoints = {{20, 20}, {175, 175}};
3214 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3215 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3216
chaviw9eaa22c2020-07-01 16:21:27 -07003217 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003218}
3219
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003220class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
3221 virtual void SetUp() override {
3222 InputDispatcherTest::SetUp();
3223
Chris Yea209fde2020-07-22 13:54:51 -07003224 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003225 mApplication->setDispatchingTimeout(20ms);
3226 mWindow =
3227 new FakeWindowHandle(mApplication, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
3228 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05003229 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07003230 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003231 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3232 // window.
Michael Wright44753b12020-07-08 13:48:11 +01003233 mWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003234
3235 // Set focused application.
3236 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
3237
3238 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003239 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003240 mWindow->consumeFocusEvent(true);
3241 }
3242
3243 virtual void TearDown() override {
3244 InputDispatcherTest::TearDown();
3245 mWindow.clear();
3246 }
3247
3248protected:
Chris Yea209fde2020-07-22 13:54:51 -07003249 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003250 sp<FakeWindowHandle> mWindow;
3251 static constexpr PointF WINDOW_LOCATION = {20, 20};
3252
3253 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003254 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003255 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3256 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003257 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003258 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3259 WINDOW_LOCATION));
3260 }
3261};
3262
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003263// Send a tap and respond, which should not cause an ANR.
3264TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
3265 tapOnWindow();
3266 mWindow->consumeMotionDown();
3267 mWindow->consumeMotionUp();
3268 ASSERT_TRUE(mDispatcher->waitForIdle());
3269 mFakePolicy->assertNotifyAnrWasNotCalled();
3270}
3271
3272// Send a regular key and respond, which should not cause an ANR.
3273TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003274 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003275 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
3276 ASSERT_TRUE(mDispatcher->waitForIdle());
3277 mFakePolicy->assertNotifyAnrWasNotCalled();
3278}
3279
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003280TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
3281 mWindow->setFocusable(false);
3282 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3283 mWindow->consumeFocusEvent(false);
3284
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003285 InputEventInjectionResult result =
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003286 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003287 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
3288 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003289 // Key will not go to window because we have no focused window.
3290 // The 'no focused window' ANR timer should start instead.
3291
3292 // Now, the focused application goes away.
3293 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
3294 // The key should get dropped and there should be no ANR.
3295
3296 ASSERT_TRUE(mDispatcher->waitForIdle());
3297 mFakePolicy->assertNotifyAnrWasNotCalled();
3298}
3299
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003300// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003301// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
3302// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003303TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003304 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003305 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3306 WINDOW_LOCATION));
3307
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003308 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
3309 ASSERT_TRUE(sequenceNum);
3310 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003311 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003312
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003313 mWindow->finishEvent(*sequenceNum);
3314 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3315 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003316 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003317 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003318}
3319
3320// Send a key to the app and have the app not respond right away.
3321TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
3322 // Inject a key, and don't respond - expect that ANR is called.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003323 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003324 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
3325 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003326 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003327 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003328 ASSERT_TRUE(mDispatcher->waitForIdle());
3329}
3330
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003331// We have a focused application, but no focused window
3332TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003333 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003334 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3335 mWindow->consumeFocusEvent(false);
3336
3337 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003338 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003339 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3340 WINDOW_LOCATION));
3341 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
3342 mDispatcher->waitForIdle();
3343 mFakePolicy->assertNotifyAnrWasNotCalled();
3344
3345 // Once a focused event arrives, we get an ANR for this application
3346 // We specify the injection timeout to be smaller than the application timeout, to ensure that
3347 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003348 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003349 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003350 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3351 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003352 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003353 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003354 ASSERT_TRUE(mDispatcher->waitForIdle());
3355}
3356
3357// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003358// Make sure that we don't notify policy twice about the same ANR.
3359TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003360 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003361 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3362 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003363
3364 // Once a focused event arrives, we get an ANR for this application
3365 // We specify the injection timeout to be smaller than the application timeout, to ensure that
3366 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003367 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003368 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003369 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3370 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003371 const std::chrono::duration appTimeout =
3372 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003373 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003374
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003375 std::this_thread::sleep_for(appTimeout);
3376 // ANR should not be raised again. It is up to policy to do that if it desires.
3377 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003378
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003379 // If we now get a focused window, the ANR should stop, but the policy handles that via
3380 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003381 ASSERT_TRUE(mDispatcher->waitForIdle());
3382}
3383
3384// We have a focused application, but no focused window
3385TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003386 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003387 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3388 mWindow->consumeFocusEvent(false);
3389
3390 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003391 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003392 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003393 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3394 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003395
3396 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003397 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003398
3399 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003400 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003401 ASSERT_TRUE(mDispatcher->waitForIdle());
3402 mWindow->assertNoEvents();
3403}
3404
3405/**
3406 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
3407 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
3408 * If we process 1 of the events, but ANR on the second event with the same timestamp,
3409 * the ANR mechanism should still work.
3410 *
3411 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
3412 * DOWN event, while not responding on the second one.
3413 */
3414TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
3415 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
3416 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3417 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
3418 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
3419 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003420 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003421
3422 // Now send ACTION_UP, with identical timestamp
3423 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3424 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
3425 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
3426 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003427 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003428
3429 // We have now sent down and up. Let's consume first event and then ANR on the second.
3430 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3431 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003432 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003433}
3434
3435// If an app is not responding to a key event, gesture monitors should continue to receive
3436// new motion events
3437TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnKey) {
3438 FakeMonitorReceiver monitor =
3439 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
3440 true /*isGestureMonitor*/);
3441
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003442 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3443 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003444 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003445 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003446
3447 // Stuck on the ACTION_UP
3448 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003449 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003450
3451 // New tap will go to the gesture monitor, but not to the window
3452 tapOnWindow();
3453 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3454 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3455
3456 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
3457 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003458 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003459 mWindow->assertNoEvents();
3460 monitor.assertNoEvents();
3461}
3462
3463// If an app is not responding to a motion event, gesture monitors should continue to receive
3464// new motion events
3465TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnMotion) {
3466 FakeMonitorReceiver monitor =
3467 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
3468 true /*isGestureMonitor*/);
3469
3470 tapOnWindow();
3471 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3472 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3473
3474 mWindow->consumeMotionDown();
3475 // Stuck on the ACTION_UP
3476 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003477 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003478
3479 // New tap will go to the gesture monitor, but not to the window
3480 tapOnWindow();
3481 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3482 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3483
3484 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
3485 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003486 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003487 mWindow->assertNoEvents();
3488 monitor.assertNoEvents();
3489}
3490
3491// If a window is unresponsive, then you get anr. if the window later catches up and starts to
3492// process events, you don't get an anr. When the window later becomes unresponsive again, you
3493// get an ANR again.
3494// 1. tap -> block on ACTION_UP -> receive ANR
3495// 2. consume all pending events (= queue becomes healthy again)
3496// 3. tap again -> block on ACTION_UP again -> receive ANR second time
3497TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
3498 tapOnWindow();
3499
3500 mWindow->consumeMotionDown();
3501 // Block on ACTION_UP
3502 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003503 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003504 mWindow->consumeMotionUp(); // Now the connection should be healthy again
3505 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003506 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003507 mWindow->assertNoEvents();
3508
3509 tapOnWindow();
3510 mWindow->consumeMotionDown();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003511 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003512 mWindow->consumeMotionUp();
3513
3514 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003515 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003516 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003517 mWindow->assertNoEvents();
3518}
3519
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003520// If a connection remains unresponsive for a while, make sure policy is only notified once about
3521// it.
3522TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003523 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003524 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3525 WINDOW_LOCATION));
3526
3527 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003528 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003529 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003530 // 'notifyConnectionUnresponsive' should only be called once per connection
3531 mFakePolicy->assertNotifyAnrWasNotCalled();
3532 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003533 mWindow->consumeMotionDown();
3534 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3535 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3536 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003537 mDispatcher->waitForIdle();
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003538 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003539 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003540}
3541
3542/**
3543 * If a window is processing a motion event, and then a key event comes in, the key event should
3544 * not to to the focused window until the motion is processed.
3545 *
3546 * Warning!!!
3547 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
3548 * and the injection timeout that we specify when injecting the key.
3549 * We must have the injection timeout (10ms) be smaller than
3550 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
3551 *
3552 * If that value changes, this test should also change.
3553 */
3554TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
3555 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3556 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3557
3558 tapOnWindow();
3559 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3560 ASSERT_TRUE(downSequenceNum);
3561 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3562 ASSERT_TRUE(upSequenceNum);
3563 // Don't finish the events yet, and send a key
3564 // Injection will "succeed" because we will eventually give up and send the key to the focused
3565 // window even if motions are still being processed. But because the injection timeout is short,
3566 // we will receive INJECTION_TIMED_OUT as the result.
3567
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003568 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003569 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003570 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3571 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003572 // Key will not be sent to the window, yet, because the window is still processing events
3573 // and the key remains pending, waiting for the touch events to be processed
3574 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3575 ASSERT_FALSE(keySequenceNum);
3576
3577 std::this_thread::sleep_for(500ms);
3578 // if we wait long enough though, dispatcher will give up, and still send the key
3579 // to the focused window, even though we have not yet finished the motion event
3580 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3581 mWindow->finishEvent(*downSequenceNum);
3582 mWindow->finishEvent(*upSequenceNum);
3583}
3584
3585/**
3586 * If a window is processing a motion event, and then a key event comes in, the key event should
3587 * not go to the focused window until the motion is processed.
3588 * If then a new motion comes in, then the pending key event should be going to the currently
3589 * focused window right away.
3590 */
3591TEST_F(InputDispatcherSingleWindowAnr,
3592 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
3593 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3594 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3595
3596 tapOnWindow();
3597 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3598 ASSERT_TRUE(downSequenceNum);
3599 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3600 ASSERT_TRUE(upSequenceNum);
3601 // Don't finish the events yet, and send a key
3602 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003603 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003604 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003605 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003606 // At this point, key is still pending, and should not be sent to the application yet.
3607 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3608 ASSERT_FALSE(keySequenceNum);
3609
3610 // Now tap down again. It should cause the pending key to go to the focused window right away.
3611 tapOnWindow();
3612 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
3613 // the other events yet. We can finish events in any order.
3614 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
3615 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
3616 mWindow->consumeMotionDown();
3617 mWindow->consumeMotionUp();
3618 mWindow->assertNoEvents();
3619}
3620
3621class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
3622 virtual void SetUp() override {
3623 InputDispatcherTest::SetUp();
3624
Chris Yea209fde2020-07-22 13:54:51 -07003625 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003626 mApplication->setDispatchingTimeout(10ms);
3627 mUnfocusedWindow =
3628 new FakeWindowHandle(mApplication, mDispatcher, "Unfocused", ADISPLAY_ID_DEFAULT);
3629 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
3630 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3631 // window.
3632 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Michael Wright44753b12020-07-08 13:48:11 +01003633 mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3634 InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH |
3635 InputWindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003636
3637 mFocusedWindow =
3638 new FakeWindowHandle(mApplication, mDispatcher, "Focused", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05003639 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003640 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01003641 mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3642 InputWindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003643
3644 // Set focused application.
3645 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07003646 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003647
3648 // Expect one focus window exist in display.
3649 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003650 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003651 mFocusedWindow->consumeFocusEvent(true);
3652 }
3653
3654 virtual void TearDown() override {
3655 InputDispatcherTest::TearDown();
3656
3657 mUnfocusedWindow.clear();
3658 mFocusedWindow.clear();
3659 }
3660
3661protected:
Chris Yea209fde2020-07-22 13:54:51 -07003662 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003663 sp<FakeWindowHandle> mUnfocusedWindow;
3664 sp<FakeWindowHandle> mFocusedWindow;
3665 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
3666 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
3667 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
3668
3669 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
3670
3671 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
3672
3673private:
3674 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003675 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003676 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3677 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003678 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003679 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3680 location));
3681 }
3682};
3683
3684// If we have 2 windows that are both unresponsive, the one with the shortest timeout
3685// should be ANR'd first.
3686TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003687 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003688 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3689 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003690 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003691 mFocusedWindow->consumeMotionDown();
3692 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3693 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3694 // We consumed all events, so no ANR
3695 ASSERT_TRUE(mDispatcher->waitForIdle());
3696 mFakePolicy->assertNotifyAnrWasNotCalled();
3697
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003698 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003699 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3700 FOCUSED_WINDOW_LOCATION));
3701 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
3702 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003703
3704 const std::chrono::duration timeout =
3705 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003706 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003707 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
3708 // sequence to make it consistent
3709 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003710 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003711 mFocusedWindow->consumeMotionDown();
3712 // This cancel is generated because the connection was unresponsive
3713 mFocusedWindow->consumeMotionCancel();
3714 mFocusedWindow->assertNoEvents();
3715 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003716 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003717 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003718 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003719}
3720
3721// If we have 2 windows with identical timeouts that are both unresponsive,
3722// it doesn't matter which order they should have ANR.
3723// But we should receive ANR for both.
3724TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
3725 // Set the timeout for unfocused window to match the focused window
3726 mUnfocusedWindow->setDispatchingTimeout(10ms);
3727 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
3728
3729 tapOnFocusedWindow();
3730 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003731 sp<IBinder> anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms);
3732 sp<IBinder> anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003733
3734 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003735 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
3736 mFocusedWindow->getToken() == anrConnectionToken2);
3737 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
3738 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003739
3740 ASSERT_TRUE(mDispatcher->waitForIdle());
3741 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003742
3743 mFocusedWindow->consumeMotionDown();
3744 mFocusedWindow->consumeMotionUp();
3745 mUnfocusedWindow->consumeMotionOutside();
3746
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003747 sp<IBinder> responsiveToken1 = mFakePolicy->getResponsiveWindowToken();
3748 sp<IBinder> responsiveToken2 = mFakePolicy->getResponsiveWindowToken();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003749
3750 // Both applications should be marked as responsive, in any order
3751 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
3752 mFocusedWindow->getToken() == responsiveToken2);
3753 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
3754 mUnfocusedWindow->getToken() == responsiveToken2);
3755 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003756}
3757
3758// If a window is already not responding, the second tap on the same window should be ignored.
3759// We should also log an error to account for the dropped event (not tested here).
3760// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
3761TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
3762 tapOnFocusedWindow();
3763 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3764 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3765 // Receive the events, but don't respond
3766 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
3767 ASSERT_TRUE(downEventSequenceNum);
3768 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
3769 ASSERT_TRUE(upEventSequenceNum);
3770 const std::chrono::duration timeout =
3771 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003772 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003773
3774 // Tap once again
3775 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003776 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003777 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3778 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003779 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003780 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3781 FOCUSED_WINDOW_LOCATION));
3782 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
3783 // valid touch target
3784 mUnfocusedWindow->assertNoEvents();
3785
3786 // Consume the first tap
3787 mFocusedWindow->finishEvent(*downEventSequenceNum);
3788 mFocusedWindow->finishEvent(*upEventSequenceNum);
3789 ASSERT_TRUE(mDispatcher->waitForIdle());
3790 // The second tap did not go to the focused window
3791 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003792 // Since all events are finished, connection should be deemed healthy again
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003793 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003794 mFakePolicy->assertNotifyAnrWasNotCalled();
3795}
3796
3797// If you tap outside of all windows, there will not be ANR
3798TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003799 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003800 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3801 LOCATION_OUTSIDE_ALL_WINDOWS));
3802 ASSERT_TRUE(mDispatcher->waitForIdle());
3803 mFakePolicy->assertNotifyAnrWasNotCalled();
3804}
3805
3806// Since the focused window is paused, tapping on it should not produce any events
3807TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
3808 mFocusedWindow->setPaused(true);
3809 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
3810
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003811 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003812 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3813 FOCUSED_WINDOW_LOCATION));
3814
3815 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
3816 ASSERT_TRUE(mDispatcher->waitForIdle());
3817 // Should not ANR because the window is paused, and touches shouldn't go to it
3818 mFakePolicy->assertNotifyAnrWasNotCalled();
3819
3820 mFocusedWindow->assertNoEvents();
3821 mUnfocusedWindow->assertNoEvents();
3822}
3823
3824/**
3825 * If a window is processing a motion event, and then a key event comes in, the key event should
3826 * not to to the focused window until the motion is processed.
3827 * If a different window becomes focused at this time, the key should go to that window instead.
3828 *
3829 * Warning!!!
3830 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
3831 * and the injection timeout that we specify when injecting the key.
3832 * We must have the injection timeout (10ms) be smaller than
3833 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
3834 *
3835 * If that value changes, this test should also change.
3836 */
3837TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
3838 // Set a long ANR timeout to prevent it from triggering
3839 mFocusedWindow->setDispatchingTimeout(2s);
3840 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
3841
3842 tapOnUnfocusedWindow();
3843 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
3844 ASSERT_TRUE(downSequenceNum);
3845 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
3846 ASSERT_TRUE(upSequenceNum);
3847 // Don't finish the events yet, and send a key
3848 // Injection will succeed because we will eventually give up and send the key to the focused
3849 // window even if motions are still being processed.
3850
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003851 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003852 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003853 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
3854 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003855 // Key will not be sent to the window, yet, because the window is still processing events
3856 // and the key remains pending, waiting for the touch events to be processed
3857 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
3858 ASSERT_FALSE(keySequenceNum);
3859
3860 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07003861 mFocusedWindow->setFocusable(false);
3862 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003863 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003864 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003865
3866 // Focus events should precede the key events
3867 mUnfocusedWindow->consumeFocusEvent(true);
3868 mFocusedWindow->consumeFocusEvent(false);
3869
3870 // Finish the tap events, which should unblock dispatcher
3871 mUnfocusedWindow->finishEvent(*downSequenceNum);
3872 mUnfocusedWindow->finishEvent(*upSequenceNum);
3873
3874 // Now that all queues are cleared and no backlog in the connections, the key event
3875 // can finally go to the newly focused "mUnfocusedWindow".
3876 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3877 mFocusedWindow->assertNoEvents();
3878 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003879 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003880}
3881
3882// When the touch stream is split across 2 windows, and one of them does not respond,
3883// then ANR should be raised and the touch should be canceled for the unresponsive window.
3884// The other window should not be affected by that.
3885TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
3886 // Touch Window 1
3887 NotifyMotionArgs motionArgs =
3888 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3889 ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION});
3890 mDispatcher->notifyMotion(&motionArgs);
3891 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3892 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3893
3894 // Touch Window 2
3895 int32_t actionPointerDown =
3896 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3897
3898 motionArgs =
3899 generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3900 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION});
3901 mDispatcher->notifyMotion(&motionArgs);
3902
3903 const std::chrono::duration timeout =
3904 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003905 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003906
3907 mUnfocusedWindow->consumeMotionDown();
3908 mFocusedWindow->consumeMotionDown();
3909 // Focused window may or may not receive ACTION_MOVE
3910 // But it should definitely receive ACTION_CANCEL due to the ANR
3911 InputEvent* event;
3912 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
3913 ASSERT_TRUE(moveOrCancelSequenceNum);
3914 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
3915 ASSERT_NE(nullptr, event);
3916 ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION);
3917 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
3918 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
3919 mFocusedWindow->consumeMotionCancel();
3920 } else {
3921 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
3922 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003923 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00003924 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003925
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003926 mUnfocusedWindow->assertNoEvents();
3927 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003928 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003929}
3930
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05003931/**
3932 * If we have no focused window, and a key comes in, we start the ANR timer.
3933 * The focused application should add a focused window before the timer runs out to prevent ANR.
3934 *
3935 * If the user touches another application during this time, the key should be dropped.
3936 * Next, if a new focused window comes in, without toggling the focused application,
3937 * then no ANR should occur.
3938 *
3939 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
3940 * but in some cases the policy may not update the focused application.
3941 */
3942TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
3943 std::shared_ptr<FakeApplicationHandle> focusedApplication =
3944 std::make_shared<FakeApplicationHandle>();
3945 focusedApplication->setDispatchingTimeout(60ms);
3946 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
3947 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
3948 mFocusedWindow->setFocusable(false);
3949
3950 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
3951 mFocusedWindow->consumeFocusEvent(false);
3952
3953 // Send a key. The ANR timer should start because there is no focused window.
3954 // 'focusedApplication' will get blamed if this timer completes.
3955 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003956 InputEventInjectionResult result =
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05003957 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003958 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
3959 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05003960
3961 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
3962 // then the injected touches won't cause the focused event to get dropped.
3963 // The dispatcher only checks for whether the queue should be pruned upon queueing.
3964 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
3965 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
3966 // For this test, it means that the key would get delivered to the window once it becomes
3967 // focused.
3968 std::this_thread::sleep_for(10ms);
3969
3970 // Touch unfocused window. This should force the pending key to get dropped.
3971 NotifyMotionArgs motionArgs =
3972 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3973 ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION});
3974 mDispatcher->notifyMotion(&motionArgs);
3975
3976 // We do not consume the motion right away, because that would require dispatcher to first
3977 // process (== drop) the key event, and by that time, ANR will be raised.
3978 // Set the focused window first.
3979 mFocusedWindow->setFocusable(true);
3980 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
3981 setFocusedWindow(mFocusedWindow);
3982 mFocusedWindow->consumeFocusEvent(true);
3983 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
3984 // to another application. This could be a bug / behaviour in the policy.
3985
3986 mUnfocusedWindow->consumeMotionDown();
3987
3988 ASSERT_TRUE(mDispatcher->waitForIdle());
3989 // Should not ANR because we actually have a focused window. It was just added too slowly.
3990 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
3991}
3992
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05003993// These tests ensure we cannot send touch events to a window that's positioned behind a window
3994// that has feature NO_INPUT_CHANNEL.
3995// Layout:
3996// Top (closest to user)
3997// mNoInputWindow (above all windows)
3998// mBottomWindow
3999// Bottom (furthest from user)
4000class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
4001 virtual void SetUp() override {
4002 InputDispatcherTest::SetUp();
4003
4004 mApplication = std::make_shared<FakeApplicationHandle>();
4005 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
4006 "Window without input channel", ADISPLAY_ID_DEFAULT,
4007 std::make_optional<sp<IBinder>>(nullptr) /*token*/);
4008
4009 mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL);
4010 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
4011 // It's perfectly valid for this window to not have an associated input channel
4012
4013 mBottomWindow = new FakeWindowHandle(mApplication, mDispatcher, "Bottom window",
4014 ADISPLAY_ID_DEFAULT);
4015 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
4016
4017 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
4018 }
4019
4020protected:
4021 std::shared_ptr<FakeApplicationHandle> mApplication;
4022 sp<FakeWindowHandle> mNoInputWindow;
4023 sp<FakeWindowHandle> mBottomWindow;
4024};
4025
4026TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
4027 PointF touchedPoint = {10, 10};
4028
4029 NotifyMotionArgs motionArgs =
4030 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4031 ADISPLAY_ID_DEFAULT, {touchedPoint});
4032 mDispatcher->notifyMotion(&motionArgs);
4033
4034 mNoInputWindow->assertNoEvents();
4035 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
4036 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
4037 // and therefore should prevent mBottomWindow from receiving touches
4038 mBottomWindow->assertNoEvents();
4039}
4040
4041/**
4042 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
4043 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
4044 */
4045TEST_F(InputDispatcherMultiWindowOcclusionTests,
4046 NoInputChannelFeature_DropsTouchesWithValidChannel) {
4047 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
4048 "Window with input channel and NO_INPUT_CHANNEL",
4049 ADISPLAY_ID_DEFAULT);
4050
4051 mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL);
4052 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
4053 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
4054
4055 PointF touchedPoint = {10, 10};
4056
4057 NotifyMotionArgs motionArgs =
4058 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4059 ADISPLAY_ID_DEFAULT, {touchedPoint});
4060 mDispatcher->notifyMotion(&motionArgs);
4061
4062 mNoInputWindow->assertNoEvents();
4063 mBottomWindow->assertNoEvents();
4064}
4065
Vishnu Nair958da932020-08-21 17:12:37 -07004066class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
4067protected:
4068 std::shared_ptr<FakeApplicationHandle> mApp;
4069 sp<FakeWindowHandle> mWindow;
4070 sp<FakeWindowHandle> mMirror;
4071
4072 virtual void SetUp() override {
4073 InputDispatcherTest::SetUp();
4074 mApp = std::make_shared<FakeApplicationHandle>();
4075 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
4076 mMirror = new FakeWindowHandle(mApp, mDispatcher, "TestWindowMirror", ADISPLAY_ID_DEFAULT,
4077 mWindow->getToken());
4078 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
4079 mWindow->setFocusable(true);
4080 mMirror->setFocusable(true);
4081 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4082 }
4083};
4084
4085TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
4086 // Request focus on a mirrored window
4087 setFocusedWindow(mMirror);
4088
4089 // window gets focused
4090 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004091 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4092 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004093 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
4094}
4095
4096// A focused & mirrored window remains focused only if the window and its mirror are both
4097// focusable.
4098TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
4099 setFocusedWindow(mMirror);
4100
4101 // window gets focused
4102 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004103 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4104 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004105 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004106 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4107 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004108 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4109
4110 mMirror->setFocusable(false);
4111 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4112
4113 // window loses focus since one of the windows associated with the token in not focusable
4114 mWindow->consumeFocusEvent(false);
4115
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004116 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4117 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004118 mWindow->assertNoEvents();
4119}
4120
4121// A focused & mirrored window remains focused until the window and its mirror both become
4122// invisible.
4123TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
4124 setFocusedWindow(mMirror);
4125
4126 // window gets focused
4127 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004128 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4129 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004130 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004131 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4132 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004133 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4134
4135 mMirror->setVisible(false);
4136 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4137
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004138 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4139 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004140 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004141 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4142 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004143 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4144
4145 mWindow->setVisible(false);
4146 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4147
4148 // window loses focus only after all windows associated with the token become invisible.
4149 mWindow->consumeFocusEvent(false);
4150
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004151 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4152 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004153 mWindow->assertNoEvents();
4154}
4155
4156// A focused & mirrored window remains focused until both windows are removed.
4157TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
4158 setFocusedWindow(mMirror);
4159
4160 // window gets focused
4161 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004162 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4163 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004164 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004165 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4166 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004167 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4168
4169 // single window is removed but the window token remains focused
4170 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
4171
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004172 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4173 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004174 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004175 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4176 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004177 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4178
4179 // Both windows are removed
4180 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
4181 mWindow->consumeFocusEvent(false);
4182
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004183 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4184 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004185 mWindow->assertNoEvents();
4186}
4187
4188// Focus request can be pending until one window becomes visible.
4189TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
4190 // Request focus on an invisible mirror.
4191 mWindow->setVisible(false);
4192 mMirror->setVisible(false);
4193 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4194 setFocusedWindow(mMirror);
4195
4196 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004197 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07004198 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004199 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07004200
4201 mMirror->setVisible(true);
4202 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4203
4204 // window gets focused
4205 mWindow->consumeFocusEvent(true);
4206 // window gets the pending key event
4207 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4208}
Prabir Pradhan99987712020-11-10 18:43:05 -08004209
4210class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
4211protected:
4212 std::shared_ptr<FakeApplicationHandle> mApp;
4213 sp<FakeWindowHandle> mWindow;
4214 sp<FakeWindowHandle> mSecondWindow;
4215
4216 void SetUp() override {
4217 InputDispatcherTest::SetUp();
4218 mApp = std::make_shared<FakeApplicationHandle>();
4219 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
4220 mWindow->setFocusable(true);
4221 mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
4222 mSecondWindow->setFocusable(true);
4223
4224 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
4225 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
4226
4227 setFocusedWindow(mWindow);
4228 mWindow->consumeFocusEvent(true);
4229 }
4230
4231 void notifyPointerCaptureChanged(bool enabled) {
4232 const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(enabled);
4233 mDispatcher->notifyPointerCaptureChanged(&args);
4234 }
4235
4236 void requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, bool enabled) {
4237 mDispatcher->requestPointerCapture(window->getToken(), enabled);
4238 mFakePolicy->waitForSetPointerCapture(enabled);
4239 notifyPointerCaptureChanged(enabled);
4240 window->consumeCaptureEvent(enabled);
4241 }
4242};
4243
4244TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
4245 // Ensure that capture cannot be obtained for unfocused windows.
4246 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
4247 mFakePolicy->assertSetPointerCaptureNotCalled();
4248 mSecondWindow->assertNoEvents();
4249
4250 // Ensure that capture can be enabled from the focus window.
4251 requestAndVerifyPointerCapture(mWindow, true);
4252
4253 // Ensure that capture cannot be disabled from a window that does not have capture.
4254 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
4255 mFakePolicy->assertSetPointerCaptureNotCalled();
4256
4257 // Ensure that capture can be disabled from the window with capture.
4258 requestAndVerifyPointerCapture(mWindow, false);
4259}
4260
4261TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
4262 requestAndVerifyPointerCapture(mWindow, true);
4263
4264 setFocusedWindow(mSecondWindow);
4265
4266 // Ensure that the capture disabled event was sent first.
4267 mWindow->consumeCaptureEvent(false);
4268 mWindow->consumeFocusEvent(false);
4269 mSecondWindow->consumeFocusEvent(true);
4270 mFakePolicy->waitForSetPointerCapture(false);
4271
4272 // Ensure that additional state changes from InputReader are not sent to the window.
4273 notifyPointerCaptureChanged(false);
4274 notifyPointerCaptureChanged(true);
4275 notifyPointerCaptureChanged(false);
4276 mWindow->assertNoEvents();
4277 mSecondWindow->assertNoEvents();
4278 mFakePolicy->assertSetPointerCaptureNotCalled();
4279}
4280
4281TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
4282 requestAndVerifyPointerCapture(mWindow, true);
4283
4284 // InputReader unexpectedly disables and enables pointer capture.
4285 notifyPointerCaptureChanged(false);
4286 notifyPointerCaptureChanged(true);
4287
4288 // Ensure that Pointer Capture is disabled.
Prabir Pradhan7d030382020-12-21 07:58:35 -08004289 mFakePolicy->waitForSetPointerCapture(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08004290 mWindow->consumeCaptureEvent(false);
4291 mWindow->assertNoEvents();
4292}
4293
Prabir Pradhan167e6d92021-02-04 16:18:17 -08004294TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
4295 requestAndVerifyPointerCapture(mWindow, true);
4296
4297 // The first window loses focus.
4298 setFocusedWindow(mSecondWindow);
4299 mFakePolicy->waitForSetPointerCapture(false);
4300 mWindow->consumeCaptureEvent(false);
4301
4302 // Request Pointer Capture from the second window before the notification from InputReader
4303 // arrives.
4304 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
4305 mFakePolicy->waitForSetPointerCapture(true);
4306
4307 // InputReader notifies Pointer Capture was disabled (because of the focus change).
4308 notifyPointerCaptureChanged(false);
4309
4310 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
4311 notifyPointerCaptureChanged(true);
4312
4313 mSecondWindow->consumeFocusEvent(true);
4314 mSecondWindow->consumeCaptureEvent(true);
4315}
4316
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004317class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
4318protected:
4319 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00004320
4321 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
4322 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
4323
4324 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
4325 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
4326
4327 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
4328 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
4329 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
4330 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
4331 MAXIMUM_OBSCURING_OPACITY);
4332
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004333 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004334 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004335 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004336
4337 sp<FakeWindowHandle> mTouchWindow;
4338
4339 virtual void SetUp() override {
4340 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004341 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004342 mDispatcher->setBlockUntrustedTouchesMode(android::os::BlockUntrustedTouchesMode::BLOCK);
4343 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
4344 }
4345
4346 virtual void TearDown() override {
4347 InputDispatcherTest::TearDown();
4348 mTouchWindow.clear();
4349 }
4350
4351 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name,
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004352 os::TouchOcclusionMode mode, float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004353 sp<FakeWindowHandle> window = getWindow(uid, name);
4354 window->setFlags(InputWindowInfo::Flag::NOT_TOUCHABLE);
4355 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004356 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004357 return window;
4358 }
4359
4360 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
4361 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
4362 sp<FakeWindowHandle> window =
4363 new FakeWindowHandle(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
4364 // Generate an arbitrary PID based on the UID
4365 window->setOwnerInfo(1777 + (uid % 10000), uid);
4366 return window;
4367 }
4368
4369 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
4370 NotifyMotionArgs args =
4371 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4372 ADISPLAY_ID_DEFAULT, points);
4373 mDispatcher->notifyMotion(&args);
4374 }
4375};
4376
4377TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004378 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004379 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004380 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004381
4382 touch();
4383
4384 mTouchWindow->assertNoEvents();
4385}
4386
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004387TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00004388 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
4389 const sp<FakeWindowHandle>& w =
4390 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
4391 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4392
4393 touch();
4394
4395 mTouchWindow->assertNoEvents();
4396}
4397
4398TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004399 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
4400 const sp<FakeWindowHandle>& w =
4401 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
4402 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4403
4404 touch();
4405
4406 w->assertNoEvents();
4407}
4408
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004409TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004410 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
4411 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004412
4413 touch();
4414
4415 mTouchWindow->consumeAnyMotionDown();
4416}
4417
4418TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004419 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004420 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004421 w->setFrame(Rect(0, 0, 50, 50));
4422 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004423
4424 touch({PointF{100, 100}});
4425
4426 mTouchWindow->consumeAnyMotionDown();
4427}
4428
4429TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004430 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004431 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004432 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4433
4434 touch();
4435
4436 mTouchWindow->consumeAnyMotionDown();
4437}
4438
4439TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
4440 const sp<FakeWindowHandle>& w =
4441 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4442 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004443
4444 touch();
4445
4446 mTouchWindow->consumeAnyMotionDown();
4447}
4448
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00004449TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
4450 const sp<FakeWindowHandle>& w =
4451 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4452 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4453
4454 touch();
4455
4456 w->assertNoEvents();
4457}
4458
4459/**
4460 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
4461 * inside) while letting them pass-through. Note that even though touch passes through the occluding
4462 * window, the occluding window will still receive ACTION_OUTSIDE event.
4463 */
4464TEST_F(InputDispatcherUntrustedTouchesTest,
4465 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
4466 const sp<FakeWindowHandle>& w =
4467 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4468 w->addFlags(InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH);
4469 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4470
4471 touch();
4472
4473 w->consumeMotionOutside();
4474}
4475
4476TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
4477 const sp<FakeWindowHandle>& w =
4478 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
4479 w->addFlags(InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH);
4480 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4481
4482 touch();
4483
4484 InputEvent* event = w->consume();
4485 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType());
4486 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
4487 EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getX());
4488 EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getY());
4489}
4490
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004491TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004492 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004493 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4494 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004495 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4496
4497 touch();
4498
4499 mTouchWindow->consumeAnyMotionDown();
4500}
4501
4502TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
4503 const sp<FakeWindowHandle>& w =
4504 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4505 MAXIMUM_OBSCURING_OPACITY);
4506 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004507
4508 touch();
4509
4510 mTouchWindow->consumeAnyMotionDown();
4511}
4512
4513TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004514 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004515 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4516 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004517 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4518
4519 touch();
4520
4521 mTouchWindow->assertNoEvents();
4522}
4523
4524TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
4525 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
4526 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004527 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
4528 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004529 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004530 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
4531 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004532 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
4533
4534 touch();
4535
4536 mTouchWindow->assertNoEvents();
4537}
4538
4539TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
4540 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
4541 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004542 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
4543 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004544 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004545 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
4546 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004547 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
4548
4549 touch();
4550
4551 mTouchWindow->consumeAnyMotionDown();
4552}
4553
4554TEST_F(InputDispatcherUntrustedTouchesTest,
4555 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
4556 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004557 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4558 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004559 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004560 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
4561 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004562 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
4563
4564 touch();
4565
4566 mTouchWindow->consumeAnyMotionDown();
4567}
4568
4569TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
4570 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004571 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4572 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004573 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004574 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
4575 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00004576 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00004577
4578 touch();
4579
4580 mTouchWindow->assertNoEvents();
4581}
4582
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004583TEST_F(InputDispatcherUntrustedTouchesTest,
4584 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
4585 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004586 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
4587 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004588 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004589 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4590 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004591 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
4592
4593 touch();
4594
4595 mTouchWindow->assertNoEvents();
4596}
4597
4598TEST_F(InputDispatcherUntrustedTouchesTest,
4599 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
4600 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004601 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
4602 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004603 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004604 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4605 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004606 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
4607
4608 touch();
4609
4610 mTouchWindow->consumeAnyMotionDown();
4611}
4612
4613TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
4614 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004615 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
4616 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00004617 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4618
4619 touch();
4620
4621 mTouchWindow->consumeAnyMotionDown();
4622}
4623
4624TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
4625 const sp<FakeWindowHandle>& w =
4626 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
4627 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4628
4629 touch();
4630
4631 mTouchWindow->consumeAnyMotionDown();
4632}
4633
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00004634TEST_F(InputDispatcherUntrustedTouchesTest,
4635 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
4636 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
4637 const sp<FakeWindowHandle>& w =
4638 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
4639 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4640
4641 touch();
4642
4643 mTouchWindow->assertNoEvents();
4644}
4645
4646TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
4647 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
4648 const sp<FakeWindowHandle>& w =
4649 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
4650 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4651
4652 touch();
4653
4654 mTouchWindow->consumeAnyMotionDown();
4655}
4656
4657TEST_F(InputDispatcherUntrustedTouchesTest,
4658 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
4659 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
4660 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00004661 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4662 OPACITY_ABOVE_THRESHOLD);
4663 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4664
4665 touch();
4666
4667 mTouchWindow->consumeAnyMotionDown();
4668}
4669
4670TEST_F(InputDispatcherUntrustedTouchesTest,
4671 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
4672 const sp<FakeWindowHandle>& w1 =
4673 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
4674 OPACITY_BELOW_THRESHOLD);
4675 const sp<FakeWindowHandle>& w2 =
4676 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
4677 OPACITY_BELOW_THRESHOLD);
4678 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
4679
4680 touch();
4681
4682 mTouchWindow->assertNoEvents();
4683}
4684
4685/**
4686 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
4687 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
4688 * (which alone would result in allowing touches) does not affect the blocking behavior.
4689 */
4690TEST_F(InputDispatcherUntrustedTouchesTest,
4691 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
4692 const sp<FakeWindowHandle>& wB =
4693 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
4694 OPACITY_BELOW_THRESHOLD);
4695 const sp<FakeWindowHandle>& wC =
4696 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
4697 OPACITY_BELOW_THRESHOLD);
4698 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
4699
4700 touch();
4701
4702 mTouchWindow->assertNoEvents();
4703}
4704
4705/**
4706 * This test is testing that a window from a different UID but with same application token doesn't
4707 * block the touch. Apps can share the application token for close UI collaboration for example.
4708 */
4709TEST_F(InputDispatcherUntrustedTouchesTest,
4710 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
4711 const sp<FakeWindowHandle>& w =
4712 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
4713 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00004714 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
4715
4716 touch();
4717
4718 mTouchWindow->consumeAnyMotionDown();
4719}
4720
arthurhungb89ccb02020-12-30 16:19:01 +08004721class InputDispatcherDragTests : public InputDispatcherTest {
4722protected:
4723 std::shared_ptr<FakeApplicationHandle> mApp;
4724 sp<FakeWindowHandle> mWindow;
4725 sp<FakeWindowHandle> mSecondWindow;
4726 sp<FakeWindowHandle> mDragWindow;
4727
4728 void SetUp() override {
4729 InputDispatcherTest::SetUp();
4730 mApp = std::make_shared<FakeApplicationHandle>();
4731 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
4732 mWindow->setFrame(Rect(0, 0, 100, 100));
4733 mWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
4734
4735 mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
4736 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
4737 mSecondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
4738
4739 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
4740 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
4741 }
4742
4743 // Start performing drag, we will create a drag window and transfer touch to it.
4744 void performDrag() {
4745 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4746 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4747 {50, 50}))
4748 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4749
4750 // Window should receive motion event.
4751 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4752
4753 // The drag window covers the entire display
4754 mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
4755 mDispatcher->setInputWindows(
4756 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
4757
4758 // Transfer touch focus to the drag window
4759 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
4760 true /* isDragDrop */);
4761 mWindow->consumeMotionCancel();
4762 mDragWindow->consumeMotionDown();
4763 }
4764};
4765
4766TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
4767 performDrag();
4768
4769 // Move on window.
4770 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4771 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4772 ADISPLAY_ID_DEFAULT, {50, 50}))
4773 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4774 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
4775 mWindow->consumeDragEvent(false, 50, 50);
4776 mSecondWindow->assertNoEvents();
4777
4778 // Move to another window.
4779 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4780 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4781 ADISPLAY_ID_DEFAULT, {150, 50}))
4782 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4783 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
4784 mWindow->consumeDragEvent(true, 150, 50);
4785 mSecondWindow->consumeDragEvent(false, 50, 50);
4786
4787 // Move back to original window.
4788 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4789 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4790 ADISPLAY_ID_DEFAULT, {50, 50}))
4791 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4792 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
4793 mWindow->consumeDragEvent(false, 50, 50);
4794 mSecondWindow->consumeDragEvent(true, -50, 50);
4795
4796 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4797 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
4798 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4799 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
4800 mWindow->assertNoEvents();
4801 mSecondWindow->assertNoEvents();
4802}
4803
arthurhungf452d0b2021-01-06 00:19:52 +08004804TEST_F(InputDispatcherDragTests, DragAndDrop) {
4805 performDrag();
4806
4807 // Move on window.
4808 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4809 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4810 ADISPLAY_ID_DEFAULT, {50, 50}))
4811 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4812 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
4813 mWindow->consumeDragEvent(false, 50, 50);
4814 mSecondWindow->assertNoEvents();
4815
4816 // Move to another window.
4817 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4818 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4819 ADISPLAY_ID_DEFAULT, {150, 50}))
4820 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4821 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
4822 mWindow->consumeDragEvent(true, 150, 50);
4823 mSecondWindow->consumeDragEvent(false, 50, 50);
4824
4825 // drop to another window.
4826 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4827 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4828 {150, 50}))
4829 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4830 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
4831 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
4832 mWindow->assertNoEvents();
4833 mSecondWindow->assertNoEvents();
4834}
4835
Garfield Tane84e6f92019-08-29 17:28:41 -07004836} // namespace android::inputdispatcher