blob: 6dbc88f4d6854bb706309ce0a79a7e175867f090 [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;
Michael Wright44753b12020-07-08 13:48:11 +010034using namespace android::flag_operators;
Garfield Tan1c7bc862020-01-28 13:24:04 -080035
Garfield Tane84e6f92019-08-29 17:28:41 -070036namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080037
38// An arbitrary time value.
39static const nsecs_t ARBITRARY_TIME = 1234;
40
41// An arbitrary device id.
42static const int32_t DEVICE_ID = 1;
43
Jeff Brownf086ddb2014-02-11 14:28:48 -080044// An arbitrary display id.
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -080045static const int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
Jeff Brownf086ddb2014-02-11 14:28:48 -080046
Michael Wrightd02c5b62014-02-10 15:10:22 -080047// An arbitrary injector pid / uid pair that has permission to inject events.
48static const int32_t INJECTOR_PID = 999;
49static const int32_t INJECTOR_UID = 1001;
50
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +000051// An arbitrary pid of the gesture monitor window
52static constexpr int32_t MONITOR_PID = 2001;
53
chaviwd1c23182019-12-20 18:44:56 -080054struct PointF {
55 float x;
56 float y;
57};
Michael Wrightd02c5b62014-02-10 15:10:22 -080058
Gang Wang342c9272020-01-13 13:15:04 -050059/**
60 * Return a DOWN key event with KEYCODE_A.
61 */
62static KeyEvent getTestKeyEvent() {
63 KeyEvent event;
64
Garfield Tanfbe732e2020-01-24 11:26:14 -080065 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
66 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
67 ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -050068 return event;
69}
70
Michael Wrightd02c5b62014-02-10 15:10:22 -080071// --- FakeInputDispatcherPolicy ---
72
73class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
74 InputDispatcherConfiguration mConfig;
75
76protected:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100077 virtual ~FakeInputDispatcherPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -080078
79public:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100080 FakeInputDispatcherPolicy() {}
Jackal Guof9696682018-10-05 12:23:23 +080081
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080082 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080083 assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_KEY, args.eventTime, args.action,
84 args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080085 }
86
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080087 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args) {
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080088 assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_MOTION, args.eventTime, args.action,
89 args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080090 }
91
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -070092 void assertFilterInputEventWasNotCalled() {
93 std::scoped_lock lock(mLock);
94 ASSERT_EQ(nullptr, mFilteredEvent);
95 }
Michael Wrightd02c5b62014-02-10 15:10:22 -080096
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -080097 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -070098 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -080099 ASSERT_TRUE(mConfigurationChangedTime)
100 << "Timed out waiting for configuration changed call";
101 ASSERT_EQ(*mConfigurationChangedTime, when);
102 mConfigurationChangedTime = std::nullopt;
103 }
104
105 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700106 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800107 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800108 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800109 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
110 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
111 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
112 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
113 mLastNotifySwitch = std::nullopt;
114 }
115
chaviwfd6d3512019-03-25 13:23:49 -0700116 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700117 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800118 ASSERT_EQ(touchedToken, mOnPointerDownToken);
119 mOnPointerDownToken.clear();
120 }
121
122 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700123 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800124 ASSERT_TRUE(mOnPointerDownToken == nullptr)
125 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700126 }
127
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700128 // This function must be called soon after the expected ANR timer starts,
129 // because we are also checking how much time has passed.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500130 void assertNotifyNoFocusedWindowAnrWasCalled(
Chris Yea209fde2020-07-22 13:54:51 -0700131 std::chrono::nanoseconds timeout,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500132 const std::shared_ptr<InputApplicationHandle>& expectedApplication) {
133 std::shared_ptr<InputApplicationHandle> application;
134 { // acquire lock
135 std::unique_lock lock(mLock);
136 android::base::ScopedLockAssertion assumeLocked(mLock);
137 ASSERT_NO_FATAL_FAILURE(
138 application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock));
139 } // release lock
140 ASSERT_EQ(expectedApplication, application);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700141 }
142
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500143 void assertNotifyConnectionUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
144 const sp<IBinder>& expectedConnectionToken) {
145 sp<IBinder> connectionToken = getUnresponsiveConnectionToken(timeout);
146 ASSERT_EQ(expectedConnectionToken, connectionToken);
147 }
148
149 void assertNotifyConnectionResponsiveWasCalled(const sp<IBinder>& expectedConnectionToken) {
150 sp<IBinder> connectionToken = getResponsiveConnectionToken();
151 ASSERT_EQ(expectedConnectionToken, connectionToken);
152 }
153
154 sp<IBinder> getUnresponsiveConnectionToken(std::chrono::nanoseconds timeout) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700155 std::unique_lock lock(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700156 android::base::ScopedLockAssertion assumeLocked(mLock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500157 return getAnrTokenLockedInterruptible(timeout, mAnrConnectionTokens, lock);
158 }
159
160 sp<IBinder> getResponsiveConnectionToken() {
161 std::unique_lock lock(mLock);
162 android::base::ScopedLockAssertion assumeLocked(mLock);
163 return getAnrTokenLockedInterruptible(0s, mResponsiveConnectionTokens, lock);
164 }
165
166 // All three ANR-related callbacks behave the same way, so we use this generic function to wait
167 // for a specific container to become non-empty. When the container is non-empty, return the
168 // first entry from the container and erase it.
169 template <class T>
170 T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage,
171 std::unique_lock<std::mutex>& lock) REQUIRES(mLock) {
172 const std::chrono::time_point start = std::chrono::steady_clock::now();
173 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700174
175 // If there is an ANR, Dispatcher won't be idle because there are still events
176 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
177 // before checking if ANR was called.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500178 // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need
179 // to provide it some time to act. 100ms seems reasonable.
180 mNotifyAnr.wait_for(lock, timeToWait,
181 [&storage]() REQUIRES(mLock) { return !storage.empty(); });
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700182 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500183 if (storage.empty()) {
184 ADD_FAILURE() << "Did not receive the ANR callback";
185 return nullptr;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700186 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700187 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
188 // the dispatcher started counting before this function was called
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700189 if (std::chrono::abs(timeout - waited) > 100ms) {
190 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
191 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
192 << "ms, but waited "
193 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
194 << "ms instead";
195 }
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500196 T token = storage.front();
197 storage.pop();
198 return token;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700199 }
200
201 void assertNotifyAnrWasNotCalled() {
202 std::scoped_lock lock(mLock);
203 ASSERT_TRUE(mAnrApplications.empty());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500204 ASSERT_TRUE(mAnrConnectionTokens.empty());
205 ASSERT_TRUE(mResponsiveConnectionTokens.empty())
206 << "ANR was not called, but please also consume the 'connection is responsive' "
207 "signal";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700208 }
209
Garfield Tan1c7bc862020-01-28 13:24:04 -0800210 void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) {
211 mConfig.keyRepeatTimeout = timeout;
212 mConfig.keyRepeatDelay = delay;
213 }
214
Prabir Pradhan99987712020-11-10 18:43:05 -0800215 void waitForSetPointerCapture(bool enabled) {
216 std::unique_lock lock(mLock);
217 base::ScopedLockAssertion assumeLocked(mLock);
218
219 if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms,
220 [this, enabled]() REQUIRES(mLock) {
221 return mPointerCaptureEnabled &&
222 *mPointerCaptureEnabled ==
223 enabled;
224 })) {
225 FAIL() << "Timed out waiting for setPointerCapture(" << enabled << ") to be called.";
226 }
227 mPointerCaptureEnabled.reset();
228 }
229
230 void assertSetPointerCaptureNotCalled() {
231 std::unique_lock lock(mLock);
232 base::ScopedLockAssertion assumeLocked(mLock);
233
234 if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) {
235 FAIL() << "Expected setPointerCapture(enabled) to not be called, but was called. "
236 "enabled = "
237 << *mPointerCaptureEnabled;
238 }
239 mPointerCaptureEnabled.reset();
240 }
241
Michael Wrightd02c5b62014-02-10 15:10:22 -0800242private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700243 std::mutex mLock;
244 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
245 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
246 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
247 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800248
Prabir Pradhan99987712020-11-10 18:43:05 -0800249 std::condition_variable mPointerCaptureChangedCondition;
250 std::optional<bool> mPointerCaptureEnabled GUARDED_BY(mLock);
251
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700252 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700253 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500254 std::queue<sp<IBinder>> mAnrConnectionTokens GUARDED_BY(mLock);
255 std::queue<sp<IBinder>> mResponsiveConnectionTokens GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700256 std::condition_variable mNotifyAnr;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700257
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600258 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700259 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800260 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800261 }
262
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500263 void notifyConnectionUnresponsive(const sp<IBinder>& connectionToken,
264 const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700265 std::scoped_lock lock(mLock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500266 mAnrConnectionTokens.push(connectionToken);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700267 mNotifyAnr.notify_all();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500268 }
269
270 void notifyConnectionResponsive(const sp<IBinder>& connectionToken) override {
271 std::scoped_lock lock(mLock);
272 mResponsiveConnectionTokens.push(connectionToken);
273 mNotifyAnr.notify_all();
274 }
275
276 void notifyNoFocusedWindowAnr(
277 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
278 std::scoped_lock lock(mLock);
279 mAnrApplications.push(applicationHandle);
280 mNotifyAnr.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800281 }
282
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600283 void notifyInputChannelBroken(const sp<IBinder>&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800284
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600285 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700286
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600287 void notifyUntrustedTouch(const std::string& obscuringPackage) override {}
Chris Yef59a2f42020-10-16 12:55:26 -0700288 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
289 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
290 const std::vector<float>& values) override {}
291
292 void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType,
293 InputDeviceSensorAccuracy accuracy) override {}
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000294
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600295 void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800296 *outConfig = mConfig;
297 }
298
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600299 bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700300 std::scoped_lock lock(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800301 switch (inputEvent->getType()) {
302 case AINPUT_EVENT_TYPE_KEY: {
303 const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800304 mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800305 break;
306 }
307
308 case AINPUT_EVENT_TYPE_MOTION: {
309 const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800310 mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800311 break;
312 }
313 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800314 return true;
315 }
316
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600317 void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800318
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600319 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800320
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600321 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800322 return 0;
323 }
324
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600325 bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800326 return false;
327 }
328
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600329 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
330 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700331 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800332 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
333 * essentially a passthrough for notifySwitch.
334 */
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800335 mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800336 }
337
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600338 void pokeUserActivity(nsecs_t, int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800339
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600340 bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) override { return false; }
Jackal Guof9696682018-10-05 12:23:23 +0800341
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600342 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700343 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700344 mOnPointerDownToken = newToken;
345 }
346
Prabir Pradhan99987712020-11-10 18:43:05 -0800347 void setPointerCapture(bool enabled) override {
348 std::scoped_lock lock(mLock);
349 mPointerCaptureEnabled = {enabled};
350 mPointerCaptureChangedCondition.notify_all();
351 }
352
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800353 void assertFilterInputEventWasCalled(int type, nsecs_t eventTime, int32_t action,
354 int32_t displayId) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700355 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800356 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
357 ASSERT_EQ(mFilteredEvent->getType(), type);
358
359 if (type == AINPUT_EVENT_TYPE_KEY) {
360 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*mFilteredEvent);
361 EXPECT_EQ(keyEvent.getEventTime(), eventTime);
362 EXPECT_EQ(keyEvent.getAction(), action);
363 EXPECT_EQ(keyEvent.getDisplayId(), displayId);
364 } else if (type == AINPUT_EVENT_TYPE_MOTION) {
365 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*mFilteredEvent);
366 EXPECT_EQ(motionEvent.getEventTime(), eventTime);
367 EXPECT_EQ(motionEvent.getAction(), action);
368 EXPECT_EQ(motionEvent.getDisplayId(), displayId);
369 } else {
370 FAIL() << "Unknown type: " << type;
371 }
372
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800373 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800374 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800375};
376
Michael Wrightd02c5b62014-02-10 15:10:22 -0800377// --- InputDispatcherTest ---
378
379class InputDispatcherTest : public testing::Test {
380protected:
381 sp<FakeInputDispatcherPolicy> mFakePolicy;
382 sp<InputDispatcher> mDispatcher;
383
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700384 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800385 mFakePolicy = new FakeInputDispatcherPolicy();
386 mDispatcher = new InputDispatcher(mFakePolicy);
Arthur Hungb92218b2018-08-14 12:00:21 +0800387 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000388 // Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700389 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800390 }
391
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700392 virtual void TearDown() override {
393 ASSERT_EQ(OK, mDispatcher->stop());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800394 mFakePolicy.clear();
395 mDispatcher.clear();
396 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700397
398 /**
399 * Used for debugging when writing the test
400 */
401 void dumpDispatcherState() {
402 std::string dump;
403 mDispatcher->dump(dump);
404 std::stringstream ss(dump);
405 std::string to;
406
407 while (std::getline(ss, to, '\n')) {
408 ALOGE("%s", to.c_str());
409 }
410 }
Vishnu Nair958da932020-08-21 17:12:37 -0700411
412 void setFocusedWindow(const sp<InputWindowHandle>& window,
413 const sp<InputWindowHandle>& focusedWindow = nullptr) {
414 FocusRequest request;
415 request.token = window->getToken();
416 if (focusedWindow) {
417 request.focusedToken = focusedWindow->getToken();
418 }
419 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
420 request.displayId = window->getInfo()->displayId;
421 mDispatcher->setFocusedWindow(request);
422 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800423};
424
Michael Wrightd02c5b62014-02-10 15:10:22 -0800425TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
426 KeyEvent event;
427
428 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800429 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
430 INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600431 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
432 ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800433 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700434 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800435 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800436 << "Should reject key events with undefined action.";
437
438 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800439 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
440 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600441 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800442 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700443 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800444 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800445 << "Should reject key events with ACTION_MULTIPLE.";
446}
447
448TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
449 MotionEvent event;
450 PointerProperties pointerProperties[MAX_POINTERS + 1];
451 PointerCoords pointerCoords[MAX_POINTERS + 1];
452 for (int i = 0; i <= MAX_POINTERS; i++) {
453 pointerProperties[i].clear();
454 pointerProperties[i].id = i;
455 pointerCoords[i].clear();
456 }
457
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800458 // Some constants commonly used below
459 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
460 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
461 constexpr int32_t metaState = AMETA_NONE;
462 constexpr MotionClassification classification = MotionClassification::NONE;
463
chaviw9eaa22c2020-07-01 16:21:27 -0700464 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800465 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800466 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
chaviw9eaa22c2020-07-01 16:21:27 -0700467 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification,
468 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600469 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700470 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800471 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700472 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800473 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800474 << "Should reject motion events with undefined action.";
475
476 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800477 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700478 AMOTION_EVENT_ACTION_POINTER_DOWN |
479 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700480 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
481 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
482 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
483 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800484 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700485 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800486 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800487 << "Should reject motion events with pointer down index too large.";
488
Garfield Tanfbe732e2020-01-24 11:26:14 -0800489 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700490 AMOTION_EVENT_ACTION_POINTER_DOWN |
491 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700492 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
493 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
494 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
495 pointerCoords);
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 motion events with pointer down index too small.";
500
501 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800502 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700503 AMOTION_EVENT_ACTION_POINTER_UP |
504 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700505 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
506 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
507 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
508 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800509 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700510 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800511 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800512 << "Should reject motion events with pointer up index too large.";
513
Garfield Tanfbe732e2020-01-24 11:26:14 -0800514 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700515 AMOTION_EVENT_ACTION_POINTER_UP |
516 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700517 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
518 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
519 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
520 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800521 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700522 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800523 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800524 << "Should reject motion events with pointer up index too small.";
525
526 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800527 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
528 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700529 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
530 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700531 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800532 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700533 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800534 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800535 << "Should reject motion events with 0 pointers.";
536
Garfield Tanfbe732e2020-01-24 11:26:14 -0800537 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
538 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700539 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
540 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700541 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800542 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700543 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800544 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800545 << "Should reject motion events with more than MAX_POINTERS pointers.";
546
547 // Rejects motion events with invalid pointer ids.
548 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800549 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
550 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700551 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
552 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700553 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800554 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700555 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800556 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800557 << "Should reject motion events with pointer ids less than 0.";
558
559 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800560 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
561 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700562 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
563 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700564 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800565 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700566 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800567 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800568 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
569
570 // Rejects motion events with duplicate pointer ids.
571 pointerProperties[0].id = 1;
572 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800573 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
574 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700575 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
576 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700577 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800578 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700579 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800580 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800581 << "Should reject motion events with duplicate pointer ids.";
582}
583
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800584/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
585
586TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
587 constexpr nsecs_t eventTime = 20;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800588 NotifyConfigurationChangedArgs args(10 /*id*/, eventTime);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800589 mDispatcher->notifyConfigurationChanged(&args);
590 ASSERT_TRUE(mDispatcher->waitForIdle());
591
592 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
593}
594
595TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800596 NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/,
597 2 /*switchMask*/);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800598 mDispatcher->notifySwitch(&args);
599
600 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
601 args.policyFlags |= POLICY_FLAG_TRUSTED;
602 mFakePolicy->assertNotifySwitchWasCalled(args);
603}
604
Arthur Hungb92218b2018-08-14 12:00:21 +0800605// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700606static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700607static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s;
Arthur Hungb92218b2018-08-14 12:00:21 +0800608
609class FakeApplicationHandle : public InputApplicationHandle {
610public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700611 FakeApplicationHandle() {
612 mInfo.name = "Fake Application";
613 mInfo.token = new BBinder();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500614 mInfo.dispatchingTimeoutMillis =
615 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700616 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800617 virtual ~FakeApplicationHandle() {}
618
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000619 virtual bool updateInfo() override { return true; }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700620
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500621 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
622 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700623 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800624};
625
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800626class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800627public:
Garfield Tan15601662020-09-22 15:32:38 -0700628 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800629 : mName(name) {
Garfield Tan15601662020-09-22 15:32:38 -0700630 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800631 }
632
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800633 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700634 InputEvent* event;
635 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
636 if (!consumeSeq) {
637 return nullptr;
638 }
639 finishEvent(*consumeSeq);
640 return event;
641 }
642
643 /**
644 * Receive an event without acknowledging it.
645 * Return the sequence number that could later be used to send finished signal.
646 */
647 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800648 uint32_t consumeSeq;
649 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800650
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800651 std::chrono::time_point start = std::chrono::steady_clock::now();
652 status_t status = WOULD_BLOCK;
653 while (status == WOULD_BLOCK) {
chaviw81e2bb92019-12-18 15:03:51 -0800654 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800655 &event);
656 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
657 if (elapsed > 100ms) {
658 break;
659 }
660 }
661
662 if (status == WOULD_BLOCK) {
663 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700664 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800665 }
666
667 if (status != OK) {
668 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700669 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800670 }
671 if (event == nullptr) {
672 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700673 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800674 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700675 if (outEvent != nullptr) {
676 *outEvent = event;
677 }
678 return consumeSeq;
679 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800680
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700681 /**
682 * To be used together with "receiveEvent" to complete the consumption of an event.
683 */
684 void finishEvent(uint32_t consumeSeq) {
685 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
686 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800687 }
688
689 void consumeEvent(int32_t expectedEventType, int32_t expectedAction, int32_t expectedDisplayId,
690 int32_t expectedFlags) {
691 InputEvent* event = consume();
692
693 ASSERT_NE(nullptr, event) << mName.c_str()
694 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800695 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700696 << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType)
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800697 << " event, got " << inputEventTypeToString(event->getType()) << " event";
Arthur Hungb92218b2018-08-14 12:00:21 +0800698
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800699 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
Tiger Huang8664f8c2018-10-11 19:14:35 +0800700
Tiger Huang8664f8c2018-10-11 19:14:35 +0800701 switch (expectedEventType) {
702 case AINPUT_EVENT_TYPE_KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800703 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
704 EXPECT_EQ(expectedAction, keyEvent.getAction());
705 EXPECT_EQ(expectedFlags, keyEvent.getFlags());
Tiger Huang8664f8c2018-10-11 19:14:35 +0800706 break;
707 }
708 case AINPUT_EVENT_TYPE_MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800709 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
710 EXPECT_EQ(expectedAction, motionEvent.getAction());
711 EXPECT_EQ(expectedFlags, motionEvent.getFlags());
Tiger Huang8664f8c2018-10-11 19:14:35 +0800712 break;
713 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100714 case AINPUT_EVENT_TYPE_FOCUS: {
715 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
716 }
Prabir Pradhan99987712020-11-10 18:43:05 -0800717 case AINPUT_EVENT_TYPE_CAPTURE: {
718 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
719 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800720 default: {
721 FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType;
722 }
723 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800724 }
725
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100726 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
727 InputEvent* event = consume();
728 ASSERT_NE(nullptr, event) << mName.c_str()
729 << ": consumer should have returned non-NULL event.";
730 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
731 << "Got " << inputEventTypeToString(event->getType())
732 << " event instead of FOCUS event";
733
734 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
735 << mName.c_str() << ": event displayId should always be NONE.";
736
737 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
738 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
739 EXPECT_EQ(inTouchMode, focusEvent->getInTouchMode());
740 }
741
Prabir Pradhan99987712020-11-10 18:43:05 -0800742 void consumeCaptureEvent(bool hasCapture) {
743 const InputEvent* event = consume();
744 ASSERT_NE(nullptr, event) << mName.c_str()
745 << ": consumer should have returned non-NULL event.";
746 ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType())
747 << "Got " << inputEventTypeToString(event->getType())
748 << " event instead of CAPTURE event";
749
750 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
751 << mName.c_str() << ": event displayId should always be NONE.";
752
753 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
754 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
755 }
756
chaviwd1c23182019-12-20 18:44:56 -0800757 void assertNoEvents() {
758 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700759 if (event == nullptr) {
760 return;
761 }
762 if (event->getType() == AINPUT_EVENT_TYPE_KEY) {
763 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
764 ADD_FAILURE() << "Received key event "
765 << KeyEvent::actionToString(keyEvent.getAction());
766 } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) {
767 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
768 ADD_FAILURE() << "Received motion event "
769 << MotionEvent::actionToString(motionEvent.getAction());
770 } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) {
771 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
772 ADD_FAILURE() << "Received focus event, hasFocus = "
773 << (focusEvent.getHasFocus() ? "true" : "false");
Prabir Pradhan99987712020-11-10 18:43:05 -0800774 } else if (event->getType() == AINPUT_EVENT_TYPE_CAPTURE) {
775 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
776 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
777 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700778 }
779 FAIL() << mName.c_str()
780 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -0800781 }
782
783 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
784
785protected:
786 std::unique_ptr<InputConsumer> mConsumer;
787 PreallocatedInputEventFactory mEventFactory;
788
789 std::string mName;
790};
791
792class FakeWindowHandle : public InputWindowHandle {
793public:
794 static const int32_t WIDTH = 600;
795 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -0800796
Chris Yea209fde2020-07-22 13:54:51 -0700797 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
chaviwd1c23182019-12-20 18:44:56 -0800798 const sp<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500799 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -0800800 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500801 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -0700802 base::Result<std::unique_ptr<InputChannel>> channel =
803 dispatcher->createInputChannel(name);
804 token = (*channel)->getConnectionToken();
805 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -0800806 }
807
808 inputApplicationHandle->updateInfo();
809 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
810
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500811 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700812 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -0800813 mInfo.name = name;
Michael Wright44753b12020-07-08 13:48:11 +0100814 mInfo.type = InputWindowInfo::Type::APPLICATION;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500815 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
chaviwd1c23182019-12-20 18:44:56 -0800816 mInfo.frameLeft = 0;
817 mInfo.frameTop = 0;
818 mInfo.frameRight = WIDTH;
819 mInfo.frameBottom = HEIGHT;
chaviw1ff3d1e2020-07-01 15:53:47 -0700820 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -0800821 mInfo.globalScaleFactor = 1.0;
822 mInfo.touchableRegion.clear();
823 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
824 mInfo.visible = true;
Vishnu Nair47074b82020-08-14 11:54:47 -0700825 mInfo.focusable = false;
chaviwd1c23182019-12-20 18:44:56 -0800826 mInfo.hasWallpaper = false;
827 mInfo.paused = false;
chaviwd1c23182019-12-20 18:44:56 -0800828 mInfo.ownerPid = INJECTOR_PID;
829 mInfo.ownerUid = INJECTOR_UID;
chaviwd1c23182019-12-20 18:44:56 -0800830 mInfo.displayId = displayId;
831 }
832
833 virtual bool updateInfo() { return true; }
834
Vishnu Nair47074b82020-08-14 11:54:47 -0700835 void setFocusable(bool focusable) { mInfo.focusable = focusable; }
chaviwd1c23182019-12-20 18:44:56 -0800836
Vishnu Nair958da932020-08-21 17:12:37 -0700837 void setVisible(bool visible) { mInfo.visible = visible; }
838
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700839 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500840 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700841 }
842
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700843 void setPaused(bool paused) { mInfo.paused = paused; }
844
chaviwd1c23182019-12-20 18:44:56 -0800845 void setFrame(const Rect& frame) {
846 mInfo.frameLeft = frame.left;
847 mInfo.frameTop = frame.top;
848 mInfo.frameRight = frame.right;
849 mInfo.frameBottom = frame.bottom;
chaviw1ff3d1e2020-07-01 15:53:47 -0700850 mInfo.transform.set(frame.left, frame.top);
chaviwd1c23182019-12-20 18:44:56 -0800851 mInfo.touchableRegion.clear();
852 mInfo.addTouchableRegion(frame);
853 }
854
Michael Wright44753b12020-07-08 13:48:11 +0100855 void setFlags(Flags<InputWindowInfo::Flag> flags) { mInfo.flags = flags; }
chaviwd1c23182019-12-20 18:44:56 -0800856
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500857 void setInputFeatures(InputWindowInfo::Feature features) { mInfo.inputFeatures = features; }
858
chaviw9eaa22c2020-07-01 16:21:27 -0700859 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
860 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
861 }
862
863 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -0700864
yunho.shinf4a80b82020-11-16 21:13:57 +0900865 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
866
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800867 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
868 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
869 expectedFlags);
870 }
871
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700872 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
873 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
874 }
875
Svet Ganov5d3bc372020-01-26 23:11:07 -0800876 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000877 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800878 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId,
879 expectedFlags);
880 }
881
882 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000883 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800884 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId,
885 expectedFlags);
886 }
887
888 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000889 int32_t expectedFlags = 0) {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800890 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
891 expectedFlags);
892 }
893
Svet Ganov5d3bc372020-01-26 23:11:07 -0800894 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000895 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
896 int32_t expectedFlags = 0) {
897 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
898 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -0800899 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
900 }
901
902 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000903 int32_t expectedFlags = 0) {
904 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
905 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -0800906 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
907 }
908
909 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000910 int32_t expectedFlags = 0) {
Michael Wright3a240c42019-12-10 20:53:41 +0000911 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
912 expectedFlags);
913 }
914
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500915 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
916 int32_t expectedFlags = 0) {
917 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
918 expectedFlags);
919 }
920
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100921 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
922 ASSERT_NE(mInputReceiver, nullptr)
923 << "Cannot consume events from a window with no receiver";
924 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
925 }
926
Prabir Pradhan99987712020-11-10 18:43:05 -0800927 void consumeCaptureEvent(bool hasCapture) {
928 ASSERT_NE(mInputReceiver, nullptr)
929 << "Cannot consume events from a window with no receiver";
930 mInputReceiver->consumeCaptureEvent(hasCapture);
931 }
932
chaviwd1c23182019-12-20 18:44:56 -0800933 void consumeEvent(int32_t expectedEventType, int32_t expectedAction, int32_t expectedDisplayId,
934 int32_t expectedFlags) {
935 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
936 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
937 expectedFlags);
938 }
939
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700940 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700941 if (mInputReceiver == nullptr) {
942 ADD_FAILURE() << "Invalid receive event on window with no receiver";
943 return std::nullopt;
944 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700945 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700946 }
947
948 void finishEvent(uint32_t sequenceNum) {
949 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
950 mInputReceiver->finishEvent(sequenceNum);
951 }
952
chaviwaf87b3e2019-10-01 16:59:28 -0700953 InputEvent* consume() {
954 if (mInputReceiver == nullptr) {
955 return nullptr;
956 }
957 return mInputReceiver->consume();
958 }
959
Arthur Hungb92218b2018-08-14 12:00:21 +0800960 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500961 if (mInputReceiver == nullptr &&
962 mInfo.inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL)) {
963 return; // Can't receive events if the window does not have input channel
964 }
965 ASSERT_NE(nullptr, mInputReceiver)
966 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -0800967 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +0800968 }
969
chaviwaf87b3e2019-10-01 16:59:28 -0700970 sp<IBinder> getToken() { return mInfo.token; }
971
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100972 const std::string& getName() { return mName; }
973
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000974 void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) {
975 mInfo.ownerPid = ownerPid;
976 mInfo.ownerUid = ownerUid;
977 }
978
chaviwd1c23182019-12-20 18:44:56 -0800979private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100980 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -0800981 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700982 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800983};
984
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700985std::atomic<int32_t> FakeWindowHandle::sId{1};
986
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800987static InputEventInjectionResult injectKey(
988 const sp<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
989 int32_t displayId = ADISPLAY_ID_NONE,
990 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
991 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800992 KeyEvent event;
993 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
994
995 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800996 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700997 INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE,
998 repeatCount, currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +0800999
1000 // Inject event until dispatch out.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001001 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, syncMode,
1002 injectionTimeout,
1003 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
Arthur Hungb92218b2018-08-14 12:00:21 +08001004}
1005
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001006static InputEventInjectionResult injectKeyDown(const sp<InputDispatcher>& dispatcher,
1007 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001008 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId);
1009}
1010
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001011static InputEventInjectionResult injectKeyUp(const sp<InputDispatcher>& dispatcher,
1012 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001013 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId);
1014}
1015
Garfield Tandf26e862020-07-01 20:18:19 -07001016class PointerBuilder {
1017public:
1018 PointerBuilder(int32_t id, int32_t toolType) {
1019 mProperties.clear();
1020 mProperties.id = id;
1021 mProperties.toolType = toolType;
1022 mCoords.clear();
1023 }
1024
1025 PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); }
1026
1027 PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); }
1028
1029 PointerBuilder& axis(int32_t axis, float value) {
1030 mCoords.setAxisValue(axis, value);
1031 return *this;
1032 }
1033
1034 PointerProperties buildProperties() const { return mProperties; }
1035
1036 PointerCoords buildCoords() const { return mCoords; }
1037
1038private:
1039 PointerProperties mProperties;
1040 PointerCoords mCoords;
1041};
1042
1043class MotionEventBuilder {
1044public:
1045 MotionEventBuilder(int32_t action, int32_t source) {
1046 mAction = action;
1047 mSource = source;
1048 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1049 }
1050
1051 MotionEventBuilder& eventTime(nsecs_t eventTime) {
1052 mEventTime = eventTime;
1053 return *this;
1054 }
1055
1056 MotionEventBuilder& displayId(int32_t displayId) {
1057 mDisplayId = displayId;
1058 return *this;
1059 }
1060
1061 MotionEventBuilder& actionButton(int32_t actionButton) {
1062 mActionButton = actionButton;
1063 return *this;
1064 }
1065
1066 MotionEventBuilder& buttonState(int32_t actionButton) {
1067 mActionButton = actionButton;
1068 return *this;
1069 }
1070
1071 MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) {
1072 mRawXCursorPosition = rawXCursorPosition;
1073 return *this;
1074 }
1075
1076 MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) {
1077 mRawYCursorPosition = rawYCursorPosition;
1078 return *this;
1079 }
1080
1081 MotionEventBuilder& pointer(PointerBuilder pointer) {
1082 mPointers.push_back(pointer);
1083 return *this;
1084 }
1085
1086 MotionEvent build() {
1087 std::vector<PointerProperties> pointerProperties;
1088 std::vector<PointerCoords> pointerCoords;
1089 for (const PointerBuilder& pointer : mPointers) {
1090 pointerProperties.push_back(pointer.buildProperties());
1091 pointerCoords.push_back(pointer.buildCoords());
1092 }
1093
1094 // Set mouse cursor position for the most common cases to avoid boilerplate.
1095 if (mSource == AINPUT_SOURCE_MOUSE &&
1096 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) &&
1097 mPointers.size() == 1) {
1098 mRawXCursorPosition = pointerCoords[0].getX();
1099 mRawYCursorPosition = pointerCoords[0].getY();
1100 }
1101
1102 MotionEvent event;
chaviw9eaa22c2020-07-01 16:21:27 -07001103 ui::Transform identityTransform;
Garfield Tandf26e862020-07-01 20:18:19 -07001104 event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC,
1105 mAction, mActionButton, /* flags */ 0, /* edgeFlags */ 0, AMETA_NONE,
chaviw9eaa22c2020-07-01 16:21:27 -07001106 mButtonState, MotionClassification::NONE, identityTransform,
1107 /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition,
1108 mRawYCursorPosition, mEventTime, mEventTime, mPointers.size(),
1109 pointerProperties.data(), pointerCoords.data());
Garfield Tandf26e862020-07-01 20:18:19 -07001110
1111 return event;
1112 }
1113
1114private:
1115 int32_t mAction;
1116 int32_t mSource;
1117 nsecs_t mEventTime;
1118 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1119 int32_t mActionButton{0};
1120 int32_t mButtonState{0};
1121 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1122 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1123
1124 std::vector<PointerBuilder> mPointers;
1125};
1126
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001127static InputEventInjectionResult injectMotionEvent(
Garfield Tandf26e862020-07-01 20:18:19 -07001128 const sp<InputDispatcher>& dispatcher, const MotionEvent& event,
1129 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001130 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT) {
Garfield Tandf26e862020-07-01 20:18:19 -07001131 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, injectionMode,
1132 injectionTimeout,
1133 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
1134}
1135
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001136static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001137 const sp<InputDispatcher>& dispatcher, int32_t action, int32_t source, int32_t displayId,
1138 const PointF& position,
1139 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001140 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1141 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001142 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001143 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC)) {
Garfield Tandf26e862020-07-01 20:18:19 -07001144 MotionEvent event = MotionEventBuilder(action, source)
1145 .displayId(displayId)
1146 .eventTime(eventTime)
1147 .rawXCursorPosition(cursorPosition.x)
1148 .rawYCursorPosition(cursorPosition.y)
1149 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1150 .x(position.x)
1151 .y(position.y))
1152 .build();
Arthur Hungb92218b2018-08-14 12:00:21 +08001153
1154 // Inject event until dispatch out.
Garfield Tandf26e862020-07-01 20:18:19 -07001155 return injectMotionEvent(dispatcher, event);
Arthur Hungb92218b2018-08-14 12:00:21 +08001156}
1157
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001158static InputEventInjectionResult injectMotionDown(const sp<InputDispatcher>& dispatcher,
1159 int32_t source, int32_t displayId,
1160 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001161 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001162}
1163
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001164static InputEventInjectionResult injectMotionUp(const sp<InputDispatcher>& dispatcher,
1165 int32_t source, int32_t displayId,
1166 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001167 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001168}
1169
Jackal Guof9696682018-10-05 12:23:23 +08001170static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1171 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1172 // Define a valid key event.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001173 NotifyKeyArgs args(/* id */ 0, currentTime, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
1174 POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, KEY_A,
1175 AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001176
1177 return args;
1178}
1179
chaviwd1c23182019-12-20 18:44:56 -08001180static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId,
1181 const std::vector<PointF>& points) {
1182 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001183 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1184 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1185 }
1186
chaviwd1c23182019-12-20 18:44:56 -08001187 PointerProperties pointerProperties[pointerCount];
1188 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001189
chaviwd1c23182019-12-20 18:44:56 -08001190 for (size_t i = 0; i < pointerCount; i++) {
1191 pointerProperties[i].clear();
1192 pointerProperties[i].id = i;
1193 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001194
chaviwd1c23182019-12-20 18:44:56 -08001195 pointerCoords[i].clear();
1196 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1197 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1198 }
Jackal Guof9696682018-10-05 12:23:23 +08001199
1200 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1201 // Define a valid motion event.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001202 NotifyMotionArgs args(/* id */ 0, currentTime, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001203 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1204 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001205 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1206 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001207 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1208 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001209
1210 return args;
1211}
1212
chaviwd1c23182019-12-20 18:44:56 -08001213static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1214 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1215}
1216
Prabir Pradhan99987712020-11-10 18:43:05 -08001217static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(bool enabled) {
1218 return NotifyPointerCaptureChangedArgs(/* id */ 0, systemTime(SYSTEM_TIME_MONOTONIC), enabled);
1219}
1220
Arthur Hungb92218b2018-08-14 12:00:21 +08001221TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001222 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001223 sp<FakeWindowHandle> window =
1224 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001225
Arthur Hung72d8dc32020-03-28 00:48:39 +00001226 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001227 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1228 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1229 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001230
1231 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001232 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001233}
1234
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001235/**
1236 * Calling setInputWindows once with FLAG_NOT_TOUCH_MODAL should not cause any issues.
1237 * To ensure that window receives only events that were directly inside of it, add
1238 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1239 * when finding touched windows.
1240 * This test serves as a sanity check for the next test, where setInputWindows is
1241 * called twice.
1242 */
1243TEST_F(InputDispatcherTest, SetInputWindowOnce_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001244 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001245 sp<FakeWindowHandle> window =
1246 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1247 window->setFrame(Rect(0, 0, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01001248 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001249
1250 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001251 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001252 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1253 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001254 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001255
1256 // Window should receive motion event.
1257 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1258}
1259
1260/**
1261 * Calling setInputWindows twice, with the same info, should not cause any issues.
1262 * To ensure that window receives only events that were directly inside of it, add
1263 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1264 * when finding touched windows.
1265 */
1266TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001267 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001268 sp<FakeWindowHandle> window =
1269 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1270 window->setFrame(Rect(0, 0, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01001271 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001272
1273 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1274 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001275 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001276 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1277 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001278 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001279
1280 // Window should receive motion event.
1281 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1282}
1283
Arthur Hungb92218b2018-08-14 12:00:21 +08001284// The foreground window should receive the first touch down event.
1285TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001286 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001287 sp<FakeWindowHandle> windowTop =
1288 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
1289 sp<FakeWindowHandle> windowSecond =
1290 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001291
Arthur Hung72d8dc32020-03-28 00:48:39 +00001292 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001293 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1294 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1295 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001296
1297 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001298 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001299 windowSecond->assertNoEvents();
1300}
1301
Garfield Tandf26e862020-07-01 20:18:19 -07001302TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07001303 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07001304 sp<FakeWindowHandle> windowLeft =
1305 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1306 windowLeft->setFrame(Rect(0, 0, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001307 windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001308 sp<FakeWindowHandle> windowRight =
1309 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1310 windowRight->setFrame(Rect(600, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001311 windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001312
1313 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1314
1315 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
1316
1317 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001318 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001319 injectMotionEvent(mDispatcher,
1320 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1321 AINPUT_SOURCE_MOUSE)
1322 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1323 .x(900)
1324 .y(400))
1325 .build()));
1326 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1327 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1328 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1329 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1330
1331 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001332 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001333 injectMotionEvent(mDispatcher,
1334 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1335 AINPUT_SOURCE_MOUSE)
1336 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1337 .x(300)
1338 .y(400))
1339 .build()));
1340 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1341 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1342 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1343 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1344 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1345 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1346
1347 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001348 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001349 injectMotionEvent(mDispatcher,
1350 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1351 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1352 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1353 .x(300)
1354 .y(400))
1355 .build()));
1356 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1357
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001358 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001359 injectMotionEvent(mDispatcher,
1360 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
1361 AINPUT_SOURCE_MOUSE)
1362 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1363 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1364 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1365 .x(300)
1366 .y(400))
1367 .build()));
1368 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1369 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1370
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001371 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001372 injectMotionEvent(mDispatcher,
1373 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1374 AINPUT_SOURCE_MOUSE)
1375 .buttonState(0)
1376 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1377 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1378 .x(300)
1379 .y(400))
1380 .build()));
1381 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1382 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1383
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001384 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001385 injectMotionEvent(mDispatcher,
1386 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
1387 .buttonState(0)
1388 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1389 .x(300)
1390 .y(400))
1391 .build()));
1392 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1393
1394 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001395 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001396 injectMotionEvent(mDispatcher,
1397 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1398 AINPUT_SOURCE_MOUSE)
1399 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1400 .x(900)
1401 .y(400))
1402 .build()));
1403 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1404 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1405 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1406 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1407 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1408 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1409}
1410
1411// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
1412// directly in this test.
1413TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07001414 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07001415 sp<FakeWindowHandle> window =
1416 new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1417 window->setFrame(Rect(0, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001418 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001419
1420 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1421
1422 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1423
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001424 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001425 injectMotionEvent(mDispatcher,
1426 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
1427 AINPUT_SOURCE_MOUSE)
1428 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1429 .x(300)
1430 .y(400))
1431 .build()));
1432 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1433 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1434
1435 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001436 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001437 injectMotionEvent(mDispatcher,
1438 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1439 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1440 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1441 .x(300)
1442 .y(400))
1443 .build()));
1444 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1445
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001446 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001447 injectMotionEvent(mDispatcher,
1448 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
1449 AINPUT_SOURCE_MOUSE)
1450 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1451 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1452 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1453 .x(300)
1454 .y(400))
1455 .build()));
1456 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1457 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1458
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001459 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001460 injectMotionEvent(mDispatcher,
1461 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1462 AINPUT_SOURCE_MOUSE)
1463 .buttonState(0)
1464 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1465 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1466 .x(300)
1467 .y(400))
1468 .build()));
1469 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1470 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1471
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001472 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001473 injectMotionEvent(mDispatcher,
1474 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
1475 .buttonState(0)
1476 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1477 .x(300)
1478 .y(400))
1479 .build()));
1480 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1481
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001482 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001483 injectMotionEvent(mDispatcher,
1484 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
1485 AINPUT_SOURCE_MOUSE)
1486 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1487 .x(300)
1488 .y(400))
1489 .build()));
1490 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1491 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1492}
1493
Garfield Tan00f511d2019-06-12 16:55:40 -07001494TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07001495 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07001496
1497 sp<FakeWindowHandle> windowLeft =
1498 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1499 windowLeft->setFrame(Rect(0, 0, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001500 windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07001501 sp<FakeWindowHandle> windowRight =
1502 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1503 windowRight->setFrame(Rect(600, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001504 windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07001505
1506 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1507
Arthur Hung72d8dc32020-03-28 00:48:39 +00001508 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07001509
1510 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
1511 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001512 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07001513 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001514 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001515 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07001516 windowRight->assertNoEvents();
1517}
1518
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001519TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001520 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001521 sp<FakeWindowHandle> window =
1522 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07001523 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001524
Arthur Hung72d8dc32020-03-28 00:48:39 +00001525 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001526 setFocusedWindow(window);
1527
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001528 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001529
1530 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1531 mDispatcher->notifyKey(&keyArgs);
1532
1533 // Window should receive key down event.
1534 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1535
1536 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
1537 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001538 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001539 mDispatcher->notifyDeviceReset(&args);
1540 window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
1541 AKEY_EVENT_FLAG_CANCELED);
1542}
1543
1544TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001545 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001546 sp<FakeWindowHandle> window =
1547 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1548
Arthur Hung72d8dc32020-03-28 00:48:39 +00001549 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001550
1551 NotifyMotionArgs motionArgs =
1552 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1553 ADISPLAY_ID_DEFAULT);
1554 mDispatcher->notifyMotion(&motionArgs);
1555
1556 // Window should receive motion down event.
1557 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1558
1559 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
1560 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001561 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001562 mDispatcher->notifyDeviceReset(&args);
1563 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT,
1564 0 /*expectedFlags*/);
1565}
1566
Svet Ganov5d3bc372020-01-26 23:11:07 -08001567TEST_F(InputDispatcherTest, TransferTouchFocus_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07001568 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001569
1570 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001571 sp<FakeWindowHandle> firstWindow =
1572 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1573 sp<FakeWindowHandle> secondWindow =
1574 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001575
1576 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001577 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001578
1579 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001580 NotifyMotionArgs downMotionArgs =
1581 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1582 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001583 mDispatcher->notifyMotion(&downMotionArgs);
1584 // Only the first window should get the down event
1585 firstWindow->consumeMotionDown();
1586 secondWindow->assertNoEvents();
1587
1588 // Transfer touch focus to the second window
1589 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1590 // The first window gets cancel and the second gets down
1591 firstWindow->consumeMotionCancel();
1592 secondWindow->consumeMotionDown();
1593
1594 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001595 NotifyMotionArgs upMotionArgs =
1596 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1597 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001598 mDispatcher->notifyMotion(&upMotionArgs);
1599 // The first window gets no events and the second gets up
1600 firstWindow->assertNoEvents();
1601 secondWindow->consumeMotionUp();
1602}
1603
1604TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointerNoSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001605 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001606
1607 PointF touchPoint = {10, 10};
1608
1609 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001610 sp<FakeWindowHandle> firstWindow =
1611 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1612 sp<FakeWindowHandle> secondWindow =
1613 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001614
1615 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001616 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001617
1618 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001619 NotifyMotionArgs downMotionArgs =
1620 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1621 ADISPLAY_ID_DEFAULT, {touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001622 mDispatcher->notifyMotion(&downMotionArgs);
1623 // Only the first window should get the down event
1624 firstWindow->consumeMotionDown();
1625 secondWindow->assertNoEvents();
1626
1627 // Send pointer down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001628 NotifyMotionArgs pointerDownMotionArgs =
1629 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1630 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1631 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1632 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001633 mDispatcher->notifyMotion(&pointerDownMotionArgs);
1634 // Only the first window should get the pointer down event
1635 firstWindow->consumeMotionPointerDown(1);
1636 secondWindow->assertNoEvents();
1637
1638 // Transfer touch focus to the second window
1639 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1640 // The first window gets cancel and the second gets down and pointer down
1641 firstWindow->consumeMotionCancel();
1642 secondWindow->consumeMotionDown();
1643 secondWindow->consumeMotionPointerDown(1);
1644
1645 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001646 NotifyMotionArgs pointerUpMotionArgs =
1647 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1648 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1649 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1650 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001651 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1652 // The first window gets nothing and the second gets pointer up
1653 firstWindow->assertNoEvents();
1654 secondWindow->consumeMotionPointerUp(1);
1655
1656 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001657 NotifyMotionArgs upMotionArgs =
1658 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1659 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001660 mDispatcher->notifyMotion(&upMotionArgs);
1661 // The first window gets nothing and the second gets up
1662 firstWindow->assertNoEvents();
1663 secondWindow->consumeMotionUp();
1664}
1665
1666TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001667 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001668
1669 // Create a non touch modal window that supports split touch
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001670 sp<FakeWindowHandle> firstWindow =
1671 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001672 firstWindow->setFrame(Rect(0, 0, 600, 400));
Michael Wright44753b12020-07-08 13:48:11 +01001673 firstWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1674 InputWindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001675
1676 // Create a non touch modal window that supports split touch
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001677 sp<FakeWindowHandle> secondWindow =
1678 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001679 secondWindow->setFrame(Rect(0, 400, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001680 secondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1681 InputWindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001682
1683 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001684 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001685
1686 PointF pointInFirst = {300, 200};
1687 PointF pointInSecond = {300, 600};
1688
1689 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001690 NotifyMotionArgs firstDownMotionArgs =
1691 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1692 ADISPLAY_ID_DEFAULT, {pointInFirst});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001693 mDispatcher->notifyMotion(&firstDownMotionArgs);
1694 // Only the first window should get the down event
1695 firstWindow->consumeMotionDown();
1696 secondWindow->assertNoEvents();
1697
1698 // Send down to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001699 NotifyMotionArgs secondDownMotionArgs =
1700 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1701 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1702 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1703 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001704 mDispatcher->notifyMotion(&secondDownMotionArgs);
1705 // The first window gets a move and the second a down
1706 firstWindow->consumeMotionMove();
1707 secondWindow->consumeMotionDown();
1708
1709 // Transfer touch focus to the second window
1710 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1711 // The first window gets cancel and the new gets pointer down (it already saw down)
1712 firstWindow->consumeMotionCancel();
1713 secondWindow->consumeMotionPointerDown(1);
1714
1715 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001716 NotifyMotionArgs pointerUpMotionArgs =
1717 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1718 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1719 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1720 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001721 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1722 // The first window gets nothing and the second gets pointer up
1723 firstWindow->assertNoEvents();
1724 secondWindow->consumeMotionPointerUp(1);
1725
1726 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001727 NotifyMotionArgs upMotionArgs =
1728 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1729 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001730 mDispatcher->notifyMotion(&upMotionArgs);
1731 // The first window gets nothing and the second gets up
1732 firstWindow->assertNoEvents();
1733 secondWindow->consumeMotionUp();
1734}
1735
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001736TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07001737 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001738 sp<FakeWindowHandle> window =
1739 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1740
Vishnu Nair47074b82020-08-14 11:54:47 -07001741 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001742 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001743 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001744
1745 window->consumeFocusEvent(true);
1746
1747 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1748 mDispatcher->notifyKey(&keyArgs);
1749
1750 // Window should receive key down event.
1751 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1752}
1753
1754TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07001755 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001756 sp<FakeWindowHandle> window =
1757 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1758
Arthur Hung72d8dc32020-03-28 00:48:39 +00001759 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001760
1761 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1762 mDispatcher->notifyKey(&keyArgs);
1763 mDispatcher->waitForIdle();
1764
1765 window->assertNoEvents();
1766}
1767
1768// If a window is touchable, but does not have focus, it should receive motion events, but not keys
1769TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07001770 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001771 sp<FakeWindowHandle> window =
1772 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1773
Arthur Hung72d8dc32020-03-28 00:48:39 +00001774 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001775
1776 // Send key
1777 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1778 mDispatcher->notifyKey(&keyArgs);
1779 // Send motion
1780 NotifyMotionArgs motionArgs =
1781 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1782 ADISPLAY_ID_DEFAULT);
1783 mDispatcher->notifyMotion(&motionArgs);
1784
1785 // Window should receive only the motion event
1786 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1787 window->assertNoEvents(); // Key event or focus event will not be received
1788}
1789
arthurhungea3f4fc2020-12-21 23:18:53 +08001790TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
1791 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1792
1793 // Create first non touch modal window that supports split touch
1794 sp<FakeWindowHandle> firstWindow =
1795 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1796 firstWindow->setFrame(Rect(0, 0, 600, 400));
1797 firstWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1798 InputWindowInfo::Flag::SPLIT_TOUCH);
1799
1800 // Create second non touch modal window that supports split touch
1801 sp<FakeWindowHandle> secondWindow =
1802 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
1803 secondWindow->setFrame(Rect(0, 400, 600, 800));
1804 secondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1805 InputWindowInfo::Flag::SPLIT_TOUCH);
1806
1807 // Add the windows to the dispatcher
1808 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
1809
1810 PointF pointInFirst = {300, 200};
1811 PointF pointInSecond = {300, 600};
1812
1813 // Send down to the first window
1814 NotifyMotionArgs firstDownMotionArgs =
1815 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1816 ADISPLAY_ID_DEFAULT, {pointInFirst});
1817 mDispatcher->notifyMotion(&firstDownMotionArgs);
1818 // Only the first window should get the down event
1819 firstWindow->consumeMotionDown();
1820 secondWindow->assertNoEvents();
1821
1822 // Send down to the second window
1823 NotifyMotionArgs secondDownMotionArgs =
1824 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1825 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1826 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1827 {pointInFirst, pointInSecond});
1828 mDispatcher->notifyMotion(&secondDownMotionArgs);
1829 // The first window gets a move and the second a down
1830 firstWindow->consumeMotionMove();
1831 secondWindow->consumeMotionDown();
1832
1833 // Send pointer cancel to the second window
1834 NotifyMotionArgs pointerUpMotionArgs =
1835 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1836 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1837 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1838 {pointInFirst, pointInSecond});
1839 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
1840 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1841 // The first window gets move and the second gets cancel.
1842 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
1843 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
1844
1845 // Send up event.
1846 NotifyMotionArgs upMotionArgs =
1847 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1848 ADISPLAY_ID_DEFAULT);
1849 mDispatcher->notifyMotion(&upMotionArgs);
1850 // The first window gets up and the second gets nothing.
1851 firstWindow->consumeMotionUp();
1852 secondWindow->assertNoEvents();
1853}
1854
chaviwd1c23182019-12-20 18:44:56 -08001855class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00001856public:
1857 FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name,
chaviwd1c23182019-12-20 18:44:56 -08001858 int32_t displayId, bool isGestureMonitor = false) {
Garfield Tan15601662020-09-22 15:32:38 -07001859 base::Result<std::unique_ptr<InputChannel>> channel =
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +00001860 dispatcher->createInputMonitor(displayId, isGestureMonitor, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07001861 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00001862 }
1863
chaviwd1c23182019-12-20 18:44:56 -08001864 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
1865
1866 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1867 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN,
1868 expectedDisplayId, expectedFlags);
1869 }
1870
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001871 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
1872
1873 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
1874
chaviwd1c23182019-12-20 18:44:56 -08001875 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1876 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
1877 expectedDisplayId, expectedFlags);
1878 }
1879
1880 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1881 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP,
1882 expectedDisplayId, expectedFlags);
1883 }
1884
1885 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
1886
1887private:
1888 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00001889};
1890
1891// Tests for gesture monitors
1892TEST_F(InputDispatcherTest, GestureMonitor_ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07001893 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00001894 sp<FakeWindowHandle> window =
1895 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001896 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00001897
chaviwd1c23182019-12-20 18:44:56 -08001898 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
1899 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00001900
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001901 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00001902 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001903 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00001904 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001905 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00001906}
1907
1908TEST_F(InputDispatcherTest, GestureMonitor_DoesNotReceiveKeyEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07001909 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00001910 sp<FakeWindowHandle> window =
1911 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1912
1913 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07001914 window->setFocusable(true);
Michael Wright3a240c42019-12-10 20:53:41 +00001915
Arthur Hung72d8dc32020-03-28 00:48:39 +00001916 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001917 setFocusedWindow(window);
1918
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001919 window->consumeFocusEvent(true);
Michael Wright3a240c42019-12-10 20:53:41 +00001920
chaviwd1c23182019-12-20 18:44:56 -08001921 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
1922 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00001923
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001924 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
1925 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00001926 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001927 monitor.assertNoEvents();
Michael Wright3a240c42019-12-10 20:53:41 +00001928}
1929
1930TEST_F(InputDispatcherTest, GestureMonitor_CanPilferAfterWindowIsRemovedMidStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001931 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00001932 sp<FakeWindowHandle> window =
1933 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001934 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00001935
chaviwd1c23182019-12-20 18:44:56 -08001936 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
1937 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00001938
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001939 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00001940 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001941 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00001942 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001943 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00001944
1945 window->releaseChannel();
1946
chaviwd1c23182019-12-20 18:44:56 -08001947 mDispatcher->pilferPointers(monitor.getToken());
Michael Wright3a240c42019-12-10 20:53:41 +00001948
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001949 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00001950 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001951 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08001952 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00001953}
1954
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001955TEST_F(InputDispatcherTest, UnresponsiveGestureMonitor_GetsAnr) {
1956 FakeMonitorReceiver monitor =
1957 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
1958 true /*isGestureMonitor*/);
1959
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001960 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001961 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
1962 std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
1963 ASSERT_TRUE(consumeSeq);
1964
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001965 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(DISPATCHING_TIMEOUT,
1966 monitor.getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001967 monitor.finishEvent(*consumeSeq);
1968 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001969 mFakePolicy->assertNotifyConnectionResponsiveWasCalled(monitor.getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001970}
1971
chaviw81e2bb92019-12-18 15:03:51 -08001972TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07001973 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
chaviw81e2bb92019-12-18 15:03:51 -08001974 sp<FakeWindowHandle> window =
1975 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1976
Arthur Hung72d8dc32020-03-28 00:48:39 +00001977 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08001978
1979 NotifyMotionArgs motionArgs =
1980 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1981 ADISPLAY_ID_DEFAULT);
1982
1983 mDispatcher->notifyMotion(&motionArgs);
1984 // Window should receive motion down event.
1985 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1986
1987 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001988 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08001989 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1990 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
1991 motionArgs.pointerCoords[0].getX() - 10);
1992
1993 mDispatcher->notifyMotion(&motionArgs);
1994 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
1995 0 /*expectedFlags*/);
1996}
1997
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001998/**
1999 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
2000 * the device default right away. In the test scenario, we check both the default value,
2001 * and the action of enabling / disabling.
2002 */
2003TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07002004 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002005 sp<FakeWindowHandle> window =
2006 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2007
2008 // Set focused application.
2009 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002010 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002011
2012 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00002013 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002014 setFocusedWindow(window);
2015
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002016 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2017
2018 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07002019 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002020 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002021 window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/);
2022
2023 SCOPED_TRACE("Disable touch mode");
2024 mDispatcher->setInTouchMode(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07002025 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002026 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002027 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002028 window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/);
2029
2030 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07002031 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002032 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002033 window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/);
2034
2035 SCOPED_TRACE("Enable touch mode again");
2036 mDispatcher->setInTouchMode(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07002037 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002038 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002039 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002040 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2041
2042 window->assertNoEvents();
2043}
2044
Gang Wange9087892020-01-07 12:17:14 -05002045TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002046 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Gang Wange9087892020-01-07 12:17:14 -05002047 sp<FakeWindowHandle> window =
2048 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2049
2050 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002051 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05002052
Arthur Hung72d8dc32020-03-28 00:48:39 +00002053 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002054 setFocusedWindow(window);
2055
Gang Wange9087892020-01-07 12:17:14 -05002056 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
2057
2058 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
2059 mDispatcher->notifyKey(&keyArgs);
2060
2061 InputEvent* event = window->consume();
2062 ASSERT_NE(event, nullptr);
2063
2064 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
2065 ASSERT_NE(verified, nullptr);
2066 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
2067
2068 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
2069 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
2070 ASSERT_EQ(keyArgs.source, verified->source);
2071 ASSERT_EQ(keyArgs.displayId, verified->displayId);
2072
2073 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
2074
2075 ASSERT_EQ(keyArgs.action, verifiedKey.action);
2076 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05002077 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
2078 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
2079 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
2080 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
2081 ASSERT_EQ(0, verifiedKey.repeatCount);
2082}
2083
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002084TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002085 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002086 sp<FakeWindowHandle> window =
2087 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2088
2089 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2090
Arthur Hung72d8dc32020-03-28 00:48:39 +00002091 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002092
2093 NotifyMotionArgs motionArgs =
2094 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2095 ADISPLAY_ID_DEFAULT);
2096 mDispatcher->notifyMotion(&motionArgs);
2097
2098 InputEvent* event = window->consume();
2099 ASSERT_NE(event, nullptr);
2100
2101 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
2102 ASSERT_NE(verified, nullptr);
2103 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
2104
2105 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
2106 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
2107 EXPECT_EQ(motionArgs.source, verified->source);
2108 EXPECT_EQ(motionArgs.displayId, verified->displayId);
2109
2110 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
2111
2112 EXPECT_EQ(motionArgs.pointerCoords[0].getX(), verifiedMotion.rawX);
2113 EXPECT_EQ(motionArgs.pointerCoords[0].getY(), verifiedMotion.rawY);
2114 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
2115 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
2116 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
2117 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
2118 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
2119}
2120
yunho.shinf4a80b82020-11-16 21:13:57 +09002121TEST_F(InputDispatcherTest, NonPointerMotionEvent_JoystickNotTransformed) {
2122 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2123 sp<FakeWindowHandle> window =
2124 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2125 const std::string name = window->getName();
2126
2127 // Window gets transformed by offset values.
2128 window->setWindowOffset(500.0f, 500.0f);
2129
2130 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2131 window->setFocusable(true);
2132
2133 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2134
2135 // First, we set focused window so that focusedWindowHandle is not null.
2136 setFocusedWindow(window);
2137
2138 // Second, we consume focus event if it is right or wrong according to onFocusChangedLocked.
2139 window->consumeFocusEvent(true);
2140
2141 NotifyMotionArgs motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE,
2142 AINPUT_SOURCE_JOYSTICK, ADISPLAY_ID_DEFAULT);
2143 mDispatcher->notifyMotion(&motionArgs);
2144
2145 // Third, we consume motion event.
2146 InputEvent* event = window->consume();
2147 ASSERT_NE(event, nullptr);
2148 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
2149 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
2150 << " event, got " << inputEventTypeToString(event->getType()) << " event";
2151
2152 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
2153 EXPECT_EQ(AINPUT_EVENT_TYPE_MOTION, motionEvent.getAction());
2154
2155 float expectedX = motionArgs.pointerCoords[0].getX();
2156 float expectedY = motionArgs.pointerCoords[0].getY();
2157
2158 // Finally we test if the axis values from the final motion event are not transformed
2159 EXPECT_EQ(expectedX, motionEvent.getX(0)) << "expected " << expectedX << " for x coord of "
2160 << name.c_str() << ", got " << motionEvent.getX(0);
2161 EXPECT_EQ(expectedY, motionEvent.getY(0)) << "expected " << expectedY << " for y coord of "
2162 << name.c_str() << ", got " << motionEvent.getY(0);
2163}
2164
chaviw09c8d2d2020-08-24 15:48:26 -07002165/**
2166 * Ensure that separate calls to sign the same data are generating the same key.
2167 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
2168 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
2169 * tests.
2170 */
2171TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
2172 KeyEvent event = getTestKeyEvent();
2173 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
2174
2175 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
2176 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
2177 ASSERT_EQ(hmac1, hmac2);
2178}
2179
2180/**
2181 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
2182 */
2183TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
2184 KeyEvent event = getTestKeyEvent();
2185 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
2186 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
2187
2188 verifiedEvent.deviceId += 1;
2189 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2190
2191 verifiedEvent.source += 1;
2192 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2193
2194 verifiedEvent.eventTimeNanos += 1;
2195 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2196
2197 verifiedEvent.displayId += 1;
2198 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2199
2200 verifiedEvent.action += 1;
2201 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2202
2203 verifiedEvent.downTimeNanos += 1;
2204 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2205
2206 verifiedEvent.flags += 1;
2207 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2208
2209 verifiedEvent.keyCode += 1;
2210 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2211
2212 verifiedEvent.scanCode += 1;
2213 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2214
2215 verifiedEvent.metaState += 1;
2216 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2217
2218 verifiedEvent.repeatCount += 1;
2219 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2220}
2221
Vishnu Nair958da932020-08-21 17:12:37 -07002222TEST_F(InputDispatcherTest, SetFocusedWindow) {
2223 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2224 sp<FakeWindowHandle> windowTop =
2225 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2226 sp<FakeWindowHandle> windowSecond =
2227 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2228 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2229
2230 // Top window is also focusable but is not granted focus.
2231 windowTop->setFocusable(true);
2232 windowSecond->setFocusable(true);
2233 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2234 setFocusedWindow(windowSecond);
2235
2236 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002237 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2238 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002239
2240 // Focused window should receive event.
2241 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
2242 windowTop->assertNoEvents();
2243}
2244
2245TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
2246 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2247 sp<FakeWindowHandle> window =
2248 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2249 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2250
2251 window->setFocusable(true);
2252 // Release channel for window is no longer valid.
2253 window->releaseChannel();
2254 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2255 setFocusedWindow(window);
2256
2257 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002258 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2259 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002260
2261 // window channel is invalid, so it should not receive any input event.
2262 window->assertNoEvents();
2263}
2264
2265TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
2266 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2267 sp<FakeWindowHandle> window =
2268 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2269 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2270
2271 // Window is not focusable.
2272 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2273 setFocusedWindow(window);
2274
2275 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002276 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2277 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002278
2279 // window is invalid, so it should not receive any input event.
2280 window->assertNoEvents();
2281}
2282
2283TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
2284 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2285 sp<FakeWindowHandle> windowTop =
2286 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2287 sp<FakeWindowHandle> windowSecond =
2288 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2289 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2290
2291 windowTop->setFocusable(true);
2292 windowSecond->setFocusable(true);
2293 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2294 setFocusedWindow(windowTop);
2295 windowTop->consumeFocusEvent(true);
2296
2297 setFocusedWindow(windowSecond, windowTop);
2298 windowSecond->consumeFocusEvent(true);
2299 windowTop->consumeFocusEvent(false);
2300
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002301 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2302 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002303
2304 // Focused window should receive event.
2305 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
2306}
2307
2308TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) {
2309 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2310 sp<FakeWindowHandle> windowTop =
2311 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2312 sp<FakeWindowHandle> windowSecond =
2313 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2314 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2315
2316 windowTop->setFocusable(true);
2317 windowSecond->setFocusable(true);
2318 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2319 setFocusedWindow(windowSecond, windowTop);
2320
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002321 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2322 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002323
2324 // Event should be dropped.
2325 windowTop->assertNoEvents();
2326 windowSecond->assertNoEvents();
2327}
2328
2329TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
2330 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2331 sp<FakeWindowHandle> window =
2332 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2333 sp<FakeWindowHandle> previousFocusedWindow =
2334 new FakeWindowHandle(application, mDispatcher, "previousFocusedWindow",
2335 ADISPLAY_ID_DEFAULT);
2336 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2337
2338 window->setFocusable(true);
2339 previousFocusedWindow->setFocusable(true);
2340 window->setVisible(false);
2341 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
2342 setFocusedWindow(previousFocusedWindow);
2343 previousFocusedWindow->consumeFocusEvent(true);
2344
2345 // Requesting focus on invisible window takes focus from currently focused window.
2346 setFocusedWindow(window);
2347 previousFocusedWindow->consumeFocusEvent(false);
2348
2349 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002350 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07002351 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002352 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07002353
2354 // Window does not get focus event or key down.
2355 window->assertNoEvents();
2356
2357 // Window becomes visible.
2358 window->setVisible(true);
2359 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2360
2361 // Window receives focus event.
2362 window->consumeFocusEvent(true);
2363 // Focused window receives key down.
2364 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2365}
2366
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002367/**
2368 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
2369 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
2370 * of the 'slipperyEnterWindow'.
2371 *
2372 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
2373 * a way so that the touched location is no longer covered by the top window.
2374 *
2375 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
2376 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
2377 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
2378 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
2379 * with ACTION_DOWN).
2380 * Thus, the touch has been transferred from the top window into the bottom window, because the top
2381 * window moved itself away from the touched location and had Flag::SLIPPERY.
2382 *
2383 * Even though the top window moved away from the touched location, it is still obscuring the bottom
2384 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
2385 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
2386 *
2387 * In this test, we ensure that the event received by the bottom window has
2388 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
2389 */
2390TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
2391 constexpr int32_t SLIPPERY_PID = INJECTOR_PID + 1;
2392 constexpr int32_t SLIPPERY_UID = INJECTOR_UID + 1;
2393
2394 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2395 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2396
2397 sp<FakeWindowHandle> slipperyExitWindow =
2398 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2399 slipperyExitWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
2400 InputWindowInfo::Flag::SLIPPERY);
2401 // Make sure this one overlaps the bottom window
2402 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
2403 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
2404 // one. Windows with the same owner are not considered to be occluding each other.
2405 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
2406
2407 sp<FakeWindowHandle> slipperyEnterWindow =
2408 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2409 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
2410
2411 mDispatcher->setInputWindows(
2412 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
2413
2414 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
2415 NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2416 ADISPLAY_ID_DEFAULT, {{50, 50}});
2417 mDispatcher->notifyMotion(&args);
2418 slipperyExitWindow->consumeMotionDown();
2419 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
2420 mDispatcher->setInputWindows(
2421 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
2422
2423 args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2424 ADISPLAY_ID_DEFAULT, {{51, 51}});
2425 mDispatcher->notifyMotion(&args);
2426
2427 slipperyExitWindow->consumeMotionCancel();
2428
2429 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
2430 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
2431}
2432
Garfield Tan1c7bc862020-01-28 13:24:04 -08002433class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
2434protected:
2435 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
2436 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
2437
Chris Yea209fde2020-07-22 13:54:51 -07002438 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002439 sp<FakeWindowHandle> mWindow;
2440
2441 virtual void SetUp() override {
2442 mFakePolicy = new FakeInputDispatcherPolicy();
2443 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
2444 mDispatcher = new InputDispatcher(mFakePolicy);
2445 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
2446 ASSERT_EQ(OK, mDispatcher->start());
2447
2448 setUpWindow();
2449 }
2450
2451 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07002452 mApp = std::make_shared<FakeApplicationHandle>();
Garfield Tan1c7bc862020-01-28 13:24:04 -08002453 mWindow = new FakeWindowHandle(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2454
Vishnu Nair47074b82020-08-14 11:54:47 -07002455 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002456 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002457 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002458 mWindow->consumeFocusEvent(true);
2459 }
2460
Chris Ye2ad95392020-09-01 13:44:44 -07002461 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08002462 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07002463 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002464 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
2465 mDispatcher->notifyKey(&keyArgs);
2466
2467 // Window should receive key down event.
2468 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2469 }
2470
2471 void expectKeyRepeatOnce(int32_t repeatCount) {
2472 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
2473 InputEvent* repeatEvent = mWindow->consume();
2474 ASSERT_NE(nullptr, repeatEvent);
2475
2476 uint32_t eventType = repeatEvent->getType();
2477 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType);
2478
2479 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
2480 uint32_t eventAction = repeatKeyEvent->getAction();
2481 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
2482 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
2483 }
2484
Chris Ye2ad95392020-09-01 13:44:44 -07002485 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08002486 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07002487 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002488 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
2489 mDispatcher->notifyKey(&keyArgs);
2490
2491 // Window should receive key down event.
2492 mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
2493 0 /*expectedFlags*/);
2494 }
2495};
2496
2497TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Chris Ye2ad95392020-09-01 13:44:44 -07002498 sendAndConsumeKeyDown(1 /* deviceId */);
2499 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2500 expectKeyRepeatOnce(repeatCount);
2501 }
2502}
2503
2504TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
2505 sendAndConsumeKeyDown(1 /* deviceId */);
2506 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2507 expectKeyRepeatOnce(repeatCount);
2508 }
2509 sendAndConsumeKeyDown(2 /* deviceId */);
2510 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08002511 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2512 expectKeyRepeatOnce(repeatCount);
2513 }
2514}
2515
2516TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Chris Ye2ad95392020-09-01 13:44:44 -07002517 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002518 expectKeyRepeatOnce(1 /*repeatCount*/);
Chris Ye2ad95392020-09-01 13:44:44 -07002519 sendAndConsumeKeyUp(1 /* deviceId */);
2520 mWindow->assertNoEvents();
2521}
2522
2523TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
2524 sendAndConsumeKeyDown(1 /* deviceId */);
2525 expectKeyRepeatOnce(1 /*repeatCount*/);
2526 sendAndConsumeKeyDown(2 /* deviceId */);
2527 expectKeyRepeatOnce(1 /*repeatCount*/);
2528 // Stale key up from device 1.
2529 sendAndConsumeKeyUp(1 /* deviceId */);
2530 // Device 2 is still down, keep repeating
2531 expectKeyRepeatOnce(2 /*repeatCount*/);
2532 expectKeyRepeatOnce(3 /*repeatCount*/);
2533 // Device 2 key up
2534 sendAndConsumeKeyUp(2 /* deviceId */);
2535 mWindow->assertNoEvents();
2536}
2537
2538TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
2539 sendAndConsumeKeyDown(1 /* deviceId */);
2540 expectKeyRepeatOnce(1 /*repeatCount*/);
2541 sendAndConsumeKeyDown(2 /* deviceId */);
2542 expectKeyRepeatOnce(1 /*repeatCount*/);
2543 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
2544 sendAndConsumeKeyUp(2 /* deviceId */);
2545 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08002546 mWindow->assertNoEvents();
2547}
2548
2549TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Chris Ye2ad95392020-09-01 13:44:44 -07002550 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002551 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2552 InputEvent* repeatEvent = mWindow->consume();
2553 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2554 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
2555 IdGenerator::getSource(repeatEvent->getId()));
2556 }
2557}
2558
2559TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Chris Ye2ad95392020-09-01 13:44:44 -07002560 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002561
2562 std::unordered_set<int32_t> idSet;
2563 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2564 InputEvent* repeatEvent = mWindow->consume();
2565 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2566 int32_t id = repeatEvent->getId();
2567 EXPECT_EQ(idSet.end(), idSet.find(id));
2568 idSet.insert(id);
2569 }
2570}
2571
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002572/* Test InputDispatcher for MultiDisplay */
2573class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
2574public:
2575 static constexpr int32_t SECOND_DISPLAY_ID = 1;
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002576 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002577 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08002578
Chris Yea209fde2020-07-22 13:54:51 -07002579 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002580 windowInPrimary =
2581 new FakeWindowHandle(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002582
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002583 // Set focus window for primary display, but focused display would be second one.
2584 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07002585 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002586 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002587 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002588 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08002589
Chris Yea209fde2020-07-22 13:54:51 -07002590 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002591 windowInSecondary =
2592 new FakeWindowHandle(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002593 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002594 // Set focus display to second one.
2595 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
2596 // Set focus window for second display.
2597 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07002598 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002599 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002600 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002601 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002602 }
2603
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002604 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002605 InputDispatcherTest::TearDown();
2606
Chris Yea209fde2020-07-22 13:54:51 -07002607 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002608 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07002609 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002610 windowInSecondary.clear();
2611 }
2612
2613protected:
Chris Yea209fde2020-07-22 13:54:51 -07002614 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002615 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07002616 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002617 sp<FakeWindowHandle> windowInSecondary;
2618};
2619
2620TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
2621 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002622 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2623 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2624 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002625 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08002626 windowInSecondary->assertNoEvents();
2627
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002628 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002629 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2630 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
2631 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08002632 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002633 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08002634}
2635
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002636TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08002637 // Test inject a key down with display id specified.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002638 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2639 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002640 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08002641 windowInSecondary->assertNoEvents();
2642
2643 // Test inject a key down without display id specified.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002644 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2645 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08002646 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002647 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08002648
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002649 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00002650 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08002651
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002652 // Old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002653 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
2654 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08002655
2656 // Test inject a key down, should timeout because of no target window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002657 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2658 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08002659 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002660 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08002661 windowInSecondary->assertNoEvents();
2662}
2663
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002664// Test per-display input monitors for motion event.
2665TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08002666 FakeMonitorReceiver monitorInPrimary =
2667 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
2668 FakeMonitorReceiver monitorInSecondary =
2669 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002670
2671 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002672 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2673 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2674 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002675 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002676 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002677 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002678 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002679
2680 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002681 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2682 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
2683 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002684 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002685 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002686 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08002687 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002688
2689 // Test inject a non-pointer motion event.
2690 // If specific a display, it will dispatch to the focused window of particular display,
2691 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002692 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2693 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
2694 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002695 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002696 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002697 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08002698 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002699}
2700
2701// Test per-display input monitors for key event.
2702TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002703 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08002704 FakeMonitorReceiver monitorInPrimary =
2705 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
2706 FakeMonitorReceiver monitorInSecondary =
2707 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002708
2709 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002710 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2711 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002712 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002713 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002714 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08002715 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002716}
2717
Vishnu Nair958da932020-08-21 17:12:37 -07002718TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
2719 sp<FakeWindowHandle> secondWindowInPrimary =
2720 new FakeWindowHandle(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
2721 secondWindowInPrimary->setFocusable(true);
2722 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
2723 setFocusedWindow(secondWindowInPrimary);
2724 windowInPrimary->consumeFocusEvent(false);
2725 secondWindowInPrimary->consumeFocusEvent(true);
2726
2727 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002728 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2729 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002730 windowInPrimary->assertNoEvents();
2731 windowInSecondary->assertNoEvents();
2732 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2733}
2734
Jackal Guof9696682018-10-05 12:23:23 +08002735class InputFilterTest : public InputDispatcherTest {
2736protected:
2737 static constexpr int32_t SECOND_DISPLAY_ID = 1;
2738
2739 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) {
2740 NotifyMotionArgs motionArgs;
2741
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002742 motionArgs =
2743 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08002744 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002745 motionArgs =
2746 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08002747 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002748 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08002749 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08002750 mFakePolicy->assertFilterInputEventWasCalled(motionArgs);
Jackal Guof9696682018-10-05 12:23:23 +08002751 } else {
2752 mFakePolicy->assertFilterInputEventWasNotCalled();
2753 }
2754 }
2755
2756 void testNotifyKey(bool expectToBeFiltered) {
2757 NotifyKeyArgs keyArgs;
2758
2759 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
2760 mDispatcher->notifyKey(&keyArgs);
2761 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
2762 mDispatcher->notifyKey(&keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002763 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08002764
2765 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08002766 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08002767 } else {
2768 mFakePolicy->assertFilterInputEventWasNotCalled();
2769 }
2770 }
2771};
2772
2773// Test InputFilter for MotionEvent
2774TEST_F(InputFilterTest, MotionEvent_InputFilter) {
2775 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
2776 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
2777 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
2778
2779 // Enable InputFilter
2780 mDispatcher->setInputFilterEnabled(true);
2781 // Test touch on both primary and second display, and check if both events are filtered.
2782 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
2783 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
2784
2785 // Disable InputFilter
2786 mDispatcher->setInputFilterEnabled(false);
2787 // Test touch on both primary and second display, and check if both events aren't filtered.
2788 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
2789 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
2790}
2791
2792// Test InputFilter for KeyEvent
2793TEST_F(InputFilterTest, KeyEvent_InputFilter) {
2794 // Since the InputFilter is disabled by default, check if key event aren't filtered.
2795 testNotifyKey(/*expectToBeFiltered*/ false);
2796
2797 // Enable InputFilter
2798 mDispatcher->setInputFilterEnabled(true);
2799 // Send a key event, and check if it is filtered.
2800 testNotifyKey(/*expectToBeFiltered*/ true);
2801
2802 // Disable InputFilter
2803 mDispatcher->setInputFilterEnabled(false);
2804 // Send a key event, and check if it isn't filtered.
2805 testNotifyKey(/*expectToBeFiltered*/ false);
2806}
2807
chaviwfd6d3512019-03-25 13:23:49 -07002808class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002809 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07002810 InputDispatcherTest::SetUp();
2811
Chris Yea209fde2020-07-22 13:54:51 -07002812 std::shared_ptr<FakeApplicationHandle> application =
2813 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002814 mUnfocusedWindow =
2815 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07002816 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
2817 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
2818 // window.
Michael Wright44753b12020-07-08 13:48:11 +01002819 mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07002820
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002821 mFocusedWindow =
2822 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2823 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01002824 mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07002825
2826 // Set focused application.
2827 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002828 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07002829
2830 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00002831 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002832 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002833 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07002834 }
2835
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002836 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07002837 InputDispatcherTest::TearDown();
2838
2839 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002840 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07002841 }
2842
2843protected:
2844 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002845 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002846 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07002847};
2848
2849// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
2850// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
2851// the onPointerDownOutsideFocus callback.
2852TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002853 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002854 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2855 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002856 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002857 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07002858
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002859 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07002860 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
2861}
2862
2863// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
2864// DOWN on the window that doesn't have focus. Ensure no window received the
2865// onPointerDownOutsideFocus callback.
2866TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002867 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002868 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002869 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002870 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07002871
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002872 ASSERT_TRUE(mDispatcher->waitForIdle());
2873 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07002874}
2875
2876// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
2877// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
2878TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002879 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2880 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002881 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07002882
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002883 ASSERT_TRUE(mDispatcher->waitForIdle());
2884 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07002885}
2886
2887// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
2888// DOWN on the window that already has focus. Ensure no window received the
2889// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002890TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002891 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002892 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002893 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002894 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002895 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07002896
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002897 ASSERT_TRUE(mDispatcher->waitForIdle());
2898 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07002899}
2900
chaviwaf87b3e2019-10-01 16:59:28 -07002901// These tests ensures we can send touch events to a single client when there are multiple input
2902// windows that point to the same client token.
2903class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
2904 virtual void SetUp() override {
2905 InputDispatcherTest::SetUp();
2906
Chris Yea209fde2020-07-22 13:54:51 -07002907 std::shared_ptr<FakeApplicationHandle> application =
2908 std::make_shared<FakeApplicationHandle>();
chaviwaf87b3e2019-10-01 16:59:28 -07002909 mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1",
2910 ADISPLAY_ID_DEFAULT);
2911 // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window.
2912 // 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 +01002913 mWindow1->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
2914 InputWindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07002915 mWindow1->setFrame(Rect(0, 0, 100, 100));
2916
2917 mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2",
2918 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
Michael Wright44753b12020-07-08 13:48:11 +01002919 mWindow2->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
2920 InputWindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07002921 mWindow2->setFrame(Rect(100, 100, 200, 200));
2922
Arthur Hung72d8dc32020-03-28 00:48:39 +00002923 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07002924 }
2925
2926protected:
2927 sp<FakeWindowHandle> mWindow1;
2928 sp<FakeWindowHandle> mWindow2;
2929
2930 // Helper function to convert the point from screen coordinates into the window's space
2931 static PointF getPointInWindow(const InputWindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07002932 vec2 vals = windowInfo->transform.transform(point.x, point.y);
2933 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07002934 }
2935
2936 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
2937 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002938 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07002939 InputEvent* event = window->consume();
2940
2941 ASSERT_NE(nullptr, event) << name.c_str()
2942 << ": consumer should have returned non-NULL event.";
2943
2944 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
2945 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
2946 << " event, got " << inputEventTypeToString(event->getType()) << " event";
2947
2948 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
2949 EXPECT_EQ(expectedAction, motionEvent.getAction());
2950
2951 for (size_t i = 0; i < points.size(); i++) {
2952 float expectedX = points[i].x;
2953 float expectedY = points[i].y;
2954
2955 EXPECT_EQ(expectedX, motionEvent.getX(i))
2956 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
2957 << ", got " << motionEvent.getX(i);
2958 EXPECT_EQ(expectedY, motionEvent.getY(i))
2959 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
2960 << ", got " << motionEvent.getY(i);
2961 }
2962 }
chaviw9eaa22c2020-07-01 16:21:27 -07002963
2964 void touchAndAssertPositions(int32_t action, std::vector<PointF> touchedPoints,
2965 std::vector<PointF> expectedPoints) {
2966 NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
2967 ADISPLAY_ID_DEFAULT, touchedPoints);
2968 mDispatcher->notifyMotion(&motionArgs);
2969
2970 // Always consume from window1 since it's the window that has the InputReceiver
2971 consumeMotionEvent(mWindow1, action, expectedPoints);
2972 }
chaviwaf87b3e2019-10-01 16:59:28 -07002973};
2974
2975TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
2976 // Touch Window 1
2977 PointF touchedPoint = {10, 10};
2978 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07002979 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07002980
2981 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07002982 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07002983
2984 // Touch Window 2
2985 touchedPoint = {150, 150};
2986 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07002987 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07002988}
2989
chaviw9eaa22c2020-07-01 16:21:27 -07002990TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
2991 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07002992 mWindow2->setWindowScale(0.5f, 0.5f);
2993
2994 // Touch Window 1
2995 PointF touchedPoint = {10, 10};
2996 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07002997 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07002998 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07002999 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003000
3001 // Touch Window 2
3002 touchedPoint = {150, 150};
3003 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07003004 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
3005 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003006
chaviw9eaa22c2020-07-01 16:21:27 -07003007 // Update the transform so rotation is set
3008 mWindow2->setWindowTransform(0, -1, 1, 0);
3009 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
3010 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07003011}
3012
chaviw9eaa22c2020-07-01 16:21:27 -07003013TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003014 mWindow2->setWindowScale(0.5f, 0.5f);
3015
3016 // Touch Window 1
3017 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3018 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003019 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003020
3021 // Touch Window 2
3022 int32_t actionPointerDown =
3023 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003024 touchedPoints.push_back(PointF{150, 150});
3025 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3026 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003027
chaviw9eaa22c2020-07-01 16:21:27 -07003028 // Release Window 2
3029 int32_t actionPointerUp =
3030 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3031 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
3032 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003033
chaviw9eaa22c2020-07-01 16:21:27 -07003034 // Update the transform so rotation is set for Window 2
3035 mWindow2->setWindowTransform(0, -1, 1, 0);
3036 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3037 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003038}
3039
chaviw9eaa22c2020-07-01 16:21:27 -07003040TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003041 mWindow2->setWindowScale(0.5f, 0.5f);
3042
3043 // Touch Window 1
3044 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3045 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003046 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003047
3048 // Touch Window 2
3049 int32_t actionPointerDown =
3050 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003051 touchedPoints.push_back(PointF{150, 150});
3052 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003053
chaviw9eaa22c2020-07-01 16:21:27 -07003054 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003055
3056 // Move both windows
3057 touchedPoints = {{20, 20}, {175, 175}};
3058 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3059 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3060
chaviw9eaa22c2020-07-01 16:21:27 -07003061 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003062
chaviw9eaa22c2020-07-01 16:21:27 -07003063 // Release Window 2
3064 int32_t actionPointerUp =
3065 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3066 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
3067 expectedPoints.pop_back();
3068
3069 // Touch Window 2
3070 mWindow2->setWindowTransform(0, -1, 1, 0);
3071 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3072 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
3073
3074 // Move both windows
3075 touchedPoints = {{20, 20}, {175, 175}};
3076 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3077 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3078
3079 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003080}
3081
3082TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
3083 mWindow1->setWindowScale(0.5f, 0.5f);
3084
3085 // Touch Window 1
3086 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3087 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003088 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003089
3090 // Touch Window 2
3091 int32_t actionPointerDown =
3092 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003093 touchedPoints.push_back(PointF{150, 150});
3094 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003095
chaviw9eaa22c2020-07-01 16:21:27 -07003096 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003097
3098 // Move both windows
3099 touchedPoints = {{20, 20}, {175, 175}};
3100 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3101 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3102
chaviw9eaa22c2020-07-01 16:21:27 -07003103 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003104}
3105
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003106class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
3107 virtual void SetUp() override {
3108 InputDispatcherTest::SetUp();
3109
Chris Yea209fde2020-07-22 13:54:51 -07003110 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003111 mApplication->setDispatchingTimeout(20ms);
3112 mWindow =
3113 new FakeWindowHandle(mApplication, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
3114 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05003115 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07003116 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003117 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3118 // window.
Michael Wright44753b12020-07-08 13:48:11 +01003119 mWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003120
3121 // Set focused application.
3122 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
3123
3124 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003125 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003126 mWindow->consumeFocusEvent(true);
3127 }
3128
3129 virtual void TearDown() override {
3130 InputDispatcherTest::TearDown();
3131 mWindow.clear();
3132 }
3133
3134protected:
Chris Yea209fde2020-07-22 13:54:51 -07003135 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003136 sp<FakeWindowHandle> mWindow;
3137 static constexpr PointF WINDOW_LOCATION = {20, 20};
3138
3139 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003140 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003141 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3142 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003143 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003144 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3145 WINDOW_LOCATION));
3146 }
3147};
3148
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003149// Send a tap and respond, which should not cause an ANR.
3150TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
3151 tapOnWindow();
3152 mWindow->consumeMotionDown();
3153 mWindow->consumeMotionUp();
3154 ASSERT_TRUE(mDispatcher->waitForIdle());
3155 mFakePolicy->assertNotifyAnrWasNotCalled();
3156}
3157
3158// Send a regular key and respond, which should not cause an ANR.
3159TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003160 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003161 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
3162 ASSERT_TRUE(mDispatcher->waitForIdle());
3163 mFakePolicy->assertNotifyAnrWasNotCalled();
3164}
3165
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003166TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
3167 mWindow->setFocusable(false);
3168 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3169 mWindow->consumeFocusEvent(false);
3170
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003171 InputEventInjectionResult result =
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003172 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003173 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
3174 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003175 // Key will not go to window because we have no focused window.
3176 // The 'no focused window' ANR timer should start instead.
3177
3178 // Now, the focused application goes away.
3179 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
3180 // The key should get dropped and there should be no ANR.
3181
3182 ASSERT_TRUE(mDispatcher->waitForIdle());
3183 mFakePolicy->assertNotifyAnrWasNotCalled();
3184}
3185
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003186// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003187// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
3188// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003189TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003190 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003191 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3192 WINDOW_LOCATION));
3193
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003194 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
3195 ASSERT_TRUE(sequenceNum);
3196 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003197 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003198
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003199 mWindow->finishEvent(*sequenceNum);
3200 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3201 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003202 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003203 mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003204}
3205
3206// Send a key to the app and have the app not respond right away.
3207TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
3208 // Inject a key, and don't respond - expect that ANR is called.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003209 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003210 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
3211 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003212 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003213 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003214 ASSERT_TRUE(mDispatcher->waitForIdle());
3215}
3216
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003217// We have a focused application, but no focused window
3218TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003219 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003220 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3221 mWindow->consumeFocusEvent(false);
3222
3223 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003224 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003225 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3226 WINDOW_LOCATION));
3227 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
3228 mDispatcher->waitForIdle();
3229 mFakePolicy->assertNotifyAnrWasNotCalled();
3230
3231 // Once a focused event arrives, we get an ANR for this application
3232 // We specify the injection timeout to be smaller than the application timeout, to ensure that
3233 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003234 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003235 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003236 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3237 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003238 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003239 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003240 ASSERT_TRUE(mDispatcher->waitForIdle());
3241}
3242
3243// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003244// Make sure that we don't notify policy twice about the same ANR.
3245TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003246 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003247 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3248 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003249
3250 // Once a focused event arrives, we get an ANR for this application
3251 // We specify the injection timeout to be smaller than the application timeout, to ensure that
3252 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003253 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003254 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003255 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3256 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003257 const std::chrono::duration appTimeout =
3258 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003259 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003260
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003261 std::this_thread::sleep_for(appTimeout);
3262 // ANR should not be raised again. It is up to policy to do that if it desires.
3263 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003264
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003265 // If we now get a focused window, the ANR should stop, but the policy handles that via
3266 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003267 ASSERT_TRUE(mDispatcher->waitForIdle());
3268}
3269
3270// We have a focused application, but no focused window
3271TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003272 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003273 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3274 mWindow->consumeFocusEvent(false);
3275
3276 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003277 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003278 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003279 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3280 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003281
3282 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003283 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003284
3285 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003286 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003287 ASSERT_TRUE(mDispatcher->waitForIdle());
3288 mWindow->assertNoEvents();
3289}
3290
3291/**
3292 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
3293 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
3294 * If we process 1 of the events, but ANR on the second event with the same timestamp,
3295 * the ANR mechanism should still work.
3296 *
3297 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
3298 * DOWN event, while not responding on the second one.
3299 */
3300TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
3301 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
3302 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3303 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
3304 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
3305 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003306 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003307
3308 // Now send ACTION_UP, with identical timestamp
3309 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3310 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
3311 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
3312 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003313 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003314
3315 // We have now sent down and up. Let's consume first event and then ANR on the second.
3316 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3317 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003318 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003319}
3320
3321// If an app is not responding to a key event, gesture monitors should continue to receive
3322// new motion events
3323TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnKey) {
3324 FakeMonitorReceiver monitor =
3325 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
3326 true /*isGestureMonitor*/);
3327
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003328 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3329 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003330 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003331 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003332
3333 // Stuck on the ACTION_UP
3334 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003335 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003336
3337 // New tap will go to the gesture monitor, but not to the window
3338 tapOnWindow();
3339 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3340 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3341
3342 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
3343 mDispatcher->waitForIdle();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003344 mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003345 mWindow->assertNoEvents();
3346 monitor.assertNoEvents();
3347}
3348
3349// If an app is not responding to a motion event, gesture monitors should continue to receive
3350// new motion events
3351TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnMotion) {
3352 FakeMonitorReceiver monitor =
3353 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
3354 true /*isGestureMonitor*/);
3355
3356 tapOnWindow();
3357 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3358 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3359
3360 mWindow->consumeMotionDown();
3361 // Stuck on the ACTION_UP
3362 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003363 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003364
3365 // New tap will go to the gesture monitor, but not to the window
3366 tapOnWindow();
3367 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3368 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3369
3370 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
3371 mDispatcher->waitForIdle();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003372 mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003373 mWindow->assertNoEvents();
3374 monitor.assertNoEvents();
3375}
3376
3377// If a window is unresponsive, then you get anr. if the window later catches up and starts to
3378// process events, you don't get an anr. When the window later becomes unresponsive again, you
3379// get an ANR again.
3380// 1. tap -> block on ACTION_UP -> receive ANR
3381// 2. consume all pending events (= queue becomes healthy again)
3382// 3. tap again -> block on ACTION_UP again -> receive ANR second time
3383TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
3384 tapOnWindow();
3385
3386 mWindow->consumeMotionDown();
3387 // Block on ACTION_UP
3388 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003389 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003390 mWindow->consumeMotionUp(); // Now the connection should be healthy again
3391 mDispatcher->waitForIdle();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003392 mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003393 mWindow->assertNoEvents();
3394
3395 tapOnWindow();
3396 mWindow->consumeMotionDown();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003397 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003398 mWindow->consumeMotionUp();
3399
3400 mDispatcher->waitForIdle();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003401 mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mWindow->getToken());
3402 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003403 mWindow->assertNoEvents();
3404}
3405
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003406// If a connection remains unresponsive for a while, make sure policy is only notified once about
3407// it.
3408TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003409 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003410 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3411 WINDOW_LOCATION));
3412
3413 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003414 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(windowTimeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003415 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003416 // 'notifyConnectionUnresponsive' should only be called once per connection
3417 mFakePolicy->assertNotifyAnrWasNotCalled();
3418 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003419 mWindow->consumeMotionDown();
3420 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3421 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3422 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003423 mDispatcher->waitForIdle();
3424 mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mWindow->getToken());
3425 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003426}
3427
3428/**
3429 * If a window is processing a motion event, and then a key event comes in, the key event should
3430 * not to to the focused window until the motion is processed.
3431 *
3432 * Warning!!!
3433 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
3434 * and the injection timeout that we specify when injecting the key.
3435 * We must have the injection timeout (10ms) be smaller than
3436 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
3437 *
3438 * If that value changes, this test should also change.
3439 */
3440TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
3441 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3442 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3443
3444 tapOnWindow();
3445 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3446 ASSERT_TRUE(downSequenceNum);
3447 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3448 ASSERT_TRUE(upSequenceNum);
3449 // Don't finish the events yet, and send a key
3450 // Injection will "succeed" because we will eventually give up and send the key to the focused
3451 // window even if motions are still being processed. But because the injection timeout is short,
3452 // we will receive INJECTION_TIMED_OUT as the result.
3453
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003454 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003455 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003456 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3457 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003458 // Key will not be sent to the window, yet, because the window is still processing events
3459 // and the key remains pending, waiting for the touch events to be processed
3460 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3461 ASSERT_FALSE(keySequenceNum);
3462
3463 std::this_thread::sleep_for(500ms);
3464 // if we wait long enough though, dispatcher will give up, and still send the key
3465 // to the focused window, even though we have not yet finished the motion event
3466 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3467 mWindow->finishEvent(*downSequenceNum);
3468 mWindow->finishEvent(*upSequenceNum);
3469}
3470
3471/**
3472 * If a window is processing a motion event, and then a key event comes in, the key event should
3473 * not go to the focused window until the motion is processed.
3474 * If then a new motion comes in, then the pending key event should be going to the currently
3475 * focused window right away.
3476 */
3477TEST_F(InputDispatcherSingleWindowAnr,
3478 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
3479 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3480 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3481
3482 tapOnWindow();
3483 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3484 ASSERT_TRUE(downSequenceNum);
3485 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3486 ASSERT_TRUE(upSequenceNum);
3487 // Don't finish the events yet, and send a key
3488 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003489 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003490 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003491 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003492 // At this point, key is still pending, and should not be sent to the application yet.
3493 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3494 ASSERT_FALSE(keySequenceNum);
3495
3496 // Now tap down again. It should cause the pending key to go to the focused window right away.
3497 tapOnWindow();
3498 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
3499 // the other events yet. We can finish events in any order.
3500 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
3501 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
3502 mWindow->consumeMotionDown();
3503 mWindow->consumeMotionUp();
3504 mWindow->assertNoEvents();
3505}
3506
3507class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
3508 virtual void SetUp() override {
3509 InputDispatcherTest::SetUp();
3510
Chris Yea209fde2020-07-22 13:54:51 -07003511 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003512 mApplication->setDispatchingTimeout(10ms);
3513 mUnfocusedWindow =
3514 new FakeWindowHandle(mApplication, mDispatcher, "Unfocused", ADISPLAY_ID_DEFAULT);
3515 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
3516 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3517 // window.
3518 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Michael Wright44753b12020-07-08 13:48:11 +01003519 mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3520 InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH |
3521 InputWindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003522
3523 mFocusedWindow =
3524 new FakeWindowHandle(mApplication, mDispatcher, "Focused", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05003525 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003526 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01003527 mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3528 InputWindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003529
3530 // Set focused application.
3531 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07003532 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003533
3534 // Expect one focus window exist in display.
3535 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003536 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003537 mFocusedWindow->consumeFocusEvent(true);
3538 }
3539
3540 virtual void TearDown() override {
3541 InputDispatcherTest::TearDown();
3542
3543 mUnfocusedWindow.clear();
3544 mFocusedWindow.clear();
3545 }
3546
3547protected:
Chris Yea209fde2020-07-22 13:54:51 -07003548 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003549 sp<FakeWindowHandle> mUnfocusedWindow;
3550 sp<FakeWindowHandle> mFocusedWindow;
3551 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
3552 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
3553 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
3554
3555 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
3556
3557 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
3558
3559private:
3560 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003561 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003562 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3563 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003564 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003565 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3566 location));
3567 }
3568};
3569
3570// If we have 2 windows that are both unresponsive, the one with the shortest timeout
3571// should be ANR'd first.
3572TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003573 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003574 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3575 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003576 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003577 mFocusedWindow->consumeMotionDown();
3578 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3579 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3580 // We consumed all events, so no ANR
3581 ASSERT_TRUE(mDispatcher->waitForIdle());
3582 mFakePolicy->assertNotifyAnrWasNotCalled();
3583
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003584 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003585 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3586 FOCUSED_WINDOW_LOCATION));
3587 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
3588 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003589
3590 const std::chrono::duration timeout =
3591 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003592 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
3593 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
3594 // sequence to make it consistent
3595 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003596 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003597 mFocusedWindow->consumeMotionDown();
3598 // This cancel is generated because the connection was unresponsive
3599 mFocusedWindow->consumeMotionCancel();
3600 mFocusedWindow->assertNoEvents();
3601 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003602 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003603 mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mFocusedWindow->getToken());
3604 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003605}
3606
3607// If we have 2 windows with identical timeouts that are both unresponsive,
3608// it doesn't matter which order they should have ANR.
3609// But we should receive ANR for both.
3610TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
3611 // Set the timeout for unfocused window to match the focused window
3612 mUnfocusedWindow->setDispatchingTimeout(10ms);
3613 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
3614
3615 tapOnFocusedWindow();
3616 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003617 sp<IBinder> anrConnectionToken1 = mFakePolicy->getUnresponsiveConnectionToken(10ms);
3618 sp<IBinder> anrConnectionToken2 = mFakePolicy->getUnresponsiveConnectionToken(0ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003619
3620 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003621 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
3622 mFocusedWindow->getToken() == anrConnectionToken2);
3623 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
3624 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003625
3626 ASSERT_TRUE(mDispatcher->waitForIdle());
3627 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003628
3629 mFocusedWindow->consumeMotionDown();
3630 mFocusedWindow->consumeMotionUp();
3631 mUnfocusedWindow->consumeMotionOutside();
3632
3633 sp<IBinder> responsiveToken1 = mFakePolicy->getResponsiveConnectionToken();
3634 sp<IBinder> responsiveToken2 = mFakePolicy->getResponsiveConnectionToken();
3635
3636 // Both applications should be marked as responsive, in any order
3637 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
3638 mFocusedWindow->getToken() == responsiveToken2);
3639 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
3640 mUnfocusedWindow->getToken() == responsiveToken2);
3641 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003642}
3643
3644// If a window is already not responding, the second tap on the same window should be ignored.
3645// We should also log an error to account for the dropped event (not tested here).
3646// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
3647TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
3648 tapOnFocusedWindow();
3649 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3650 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3651 // Receive the events, but don't respond
3652 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
3653 ASSERT_TRUE(downEventSequenceNum);
3654 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
3655 ASSERT_TRUE(upEventSequenceNum);
3656 const std::chrono::duration timeout =
3657 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003658 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003659
3660 // Tap once again
3661 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003662 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003663 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3664 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003665 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003666 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3667 FOCUSED_WINDOW_LOCATION));
3668 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
3669 // valid touch target
3670 mUnfocusedWindow->assertNoEvents();
3671
3672 // Consume the first tap
3673 mFocusedWindow->finishEvent(*downEventSequenceNum);
3674 mFocusedWindow->finishEvent(*upEventSequenceNum);
3675 ASSERT_TRUE(mDispatcher->waitForIdle());
3676 // The second tap did not go to the focused window
3677 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003678 // Since all events are finished, connection should be deemed healthy again
3679 mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003680 mFakePolicy->assertNotifyAnrWasNotCalled();
3681}
3682
3683// If you tap outside of all windows, there will not be ANR
3684TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003685 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003686 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3687 LOCATION_OUTSIDE_ALL_WINDOWS));
3688 ASSERT_TRUE(mDispatcher->waitForIdle());
3689 mFakePolicy->assertNotifyAnrWasNotCalled();
3690}
3691
3692// Since the focused window is paused, tapping on it should not produce any events
3693TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
3694 mFocusedWindow->setPaused(true);
3695 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
3696
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003697 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003698 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3699 FOCUSED_WINDOW_LOCATION));
3700
3701 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
3702 ASSERT_TRUE(mDispatcher->waitForIdle());
3703 // Should not ANR because the window is paused, and touches shouldn't go to it
3704 mFakePolicy->assertNotifyAnrWasNotCalled();
3705
3706 mFocusedWindow->assertNoEvents();
3707 mUnfocusedWindow->assertNoEvents();
3708}
3709
3710/**
3711 * If a window is processing a motion event, and then a key event comes in, the key event should
3712 * not to to the focused window until the motion is processed.
3713 * If a different window becomes focused at this time, the key should go to that window instead.
3714 *
3715 * Warning!!!
3716 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
3717 * and the injection timeout that we specify when injecting the key.
3718 * We must have the injection timeout (10ms) be smaller than
3719 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
3720 *
3721 * If that value changes, this test should also change.
3722 */
3723TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
3724 // Set a long ANR timeout to prevent it from triggering
3725 mFocusedWindow->setDispatchingTimeout(2s);
3726 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
3727
3728 tapOnUnfocusedWindow();
3729 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
3730 ASSERT_TRUE(downSequenceNum);
3731 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
3732 ASSERT_TRUE(upSequenceNum);
3733 // Don't finish the events yet, and send a key
3734 // Injection will succeed because we will eventually give up and send the key to the focused
3735 // window even if motions are still being processed.
3736
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003737 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003738 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003739 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
3740 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003741 // Key will not be sent to the window, yet, because the window is still processing events
3742 // and the key remains pending, waiting for the touch events to be processed
3743 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
3744 ASSERT_FALSE(keySequenceNum);
3745
3746 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07003747 mFocusedWindow->setFocusable(false);
3748 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003749 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003750 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003751
3752 // Focus events should precede the key events
3753 mUnfocusedWindow->consumeFocusEvent(true);
3754 mFocusedWindow->consumeFocusEvent(false);
3755
3756 // Finish the tap events, which should unblock dispatcher
3757 mUnfocusedWindow->finishEvent(*downSequenceNum);
3758 mUnfocusedWindow->finishEvent(*upSequenceNum);
3759
3760 // Now that all queues are cleared and no backlog in the connections, the key event
3761 // can finally go to the newly focused "mUnfocusedWindow".
3762 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3763 mFocusedWindow->assertNoEvents();
3764 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003765 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003766}
3767
3768// When the touch stream is split across 2 windows, and one of them does not respond,
3769// then ANR should be raised and the touch should be canceled for the unresponsive window.
3770// The other window should not be affected by that.
3771TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
3772 // Touch Window 1
3773 NotifyMotionArgs motionArgs =
3774 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3775 ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION});
3776 mDispatcher->notifyMotion(&motionArgs);
3777 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3778 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3779
3780 // Touch Window 2
3781 int32_t actionPointerDown =
3782 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3783
3784 motionArgs =
3785 generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3786 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION});
3787 mDispatcher->notifyMotion(&motionArgs);
3788
3789 const std::chrono::duration timeout =
3790 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003791 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003792
3793 mUnfocusedWindow->consumeMotionDown();
3794 mFocusedWindow->consumeMotionDown();
3795 // Focused window may or may not receive ACTION_MOVE
3796 // But it should definitely receive ACTION_CANCEL due to the ANR
3797 InputEvent* event;
3798 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
3799 ASSERT_TRUE(moveOrCancelSequenceNum);
3800 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
3801 ASSERT_NE(nullptr, event);
3802 ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION);
3803 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
3804 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
3805 mFocusedWindow->consumeMotionCancel();
3806 } else {
3807 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
3808 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003809 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003810 mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mFocusedWindow->getToken());
3811
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003812 mUnfocusedWindow->assertNoEvents();
3813 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003814 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003815}
3816
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05003817/**
3818 * If we have no focused window, and a key comes in, we start the ANR timer.
3819 * The focused application should add a focused window before the timer runs out to prevent ANR.
3820 *
3821 * If the user touches another application during this time, the key should be dropped.
3822 * Next, if a new focused window comes in, without toggling the focused application,
3823 * then no ANR should occur.
3824 *
3825 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
3826 * but in some cases the policy may not update the focused application.
3827 */
3828TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
3829 std::shared_ptr<FakeApplicationHandle> focusedApplication =
3830 std::make_shared<FakeApplicationHandle>();
3831 focusedApplication->setDispatchingTimeout(60ms);
3832 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
3833 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
3834 mFocusedWindow->setFocusable(false);
3835
3836 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
3837 mFocusedWindow->consumeFocusEvent(false);
3838
3839 // Send a key. The ANR timer should start because there is no focused window.
3840 // 'focusedApplication' will get blamed if this timer completes.
3841 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003842 InputEventInjectionResult result =
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05003843 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003844 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
3845 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05003846
3847 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
3848 // then the injected touches won't cause the focused event to get dropped.
3849 // The dispatcher only checks for whether the queue should be pruned upon queueing.
3850 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
3851 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
3852 // For this test, it means that the key would get delivered to the window once it becomes
3853 // focused.
3854 std::this_thread::sleep_for(10ms);
3855
3856 // Touch unfocused window. This should force the pending key to get dropped.
3857 NotifyMotionArgs motionArgs =
3858 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3859 ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION});
3860 mDispatcher->notifyMotion(&motionArgs);
3861
3862 // We do not consume the motion right away, because that would require dispatcher to first
3863 // process (== drop) the key event, and by that time, ANR will be raised.
3864 // Set the focused window first.
3865 mFocusedWindow->setFocusable(true);
3866 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
3867 setFocusedWindow(mFocusedWindow);
3868 mFocusedWindow->consumeFocusEvent(true);
3869 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
3870 // to another application. This could be a bug / behaviour in the policy.
3871
3872 mUnfocusedWindow->consumeMotionDown();
3873
3874 ASSERT_TRUE(mDispatcher->waitForIdle());
3875 // Should not ANR because we actually have a focused window. It was just added too slowly.
3876 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
3877}
3878
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05003879// These tests ensure we cannot send touch events to a window that's positioned behind a window
3880// that has feature NO_INPUT_CHANNEL.
3881// Layout:
3882// Top (closest to user)
3883// mNoInputWindow (above all windows)
3884// mBottomWindow
3885// Bottom (furthest from user)
3886class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
3887 virtual void SetUp() override {
3888 InputDispatcherTest::SetUp();
3889
3890 mApplication = std::make_shared<FakeApplicationHandle>();
3891 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
3892 "Window without input channel", ADISPLAY_ID_DEFAULT,
3893 std::make_optional<sp<IBinder>>(nullptr) /*token*/);
3894
3895 mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL);
3896 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
3897 // It's perfectly valid for this window to not have an associated input channel
3898
3899 mBottomWindow = new FakeWindowHandle(mApplication, mDispatcher, "Bottom window",
3900 ADISPLAY_ID_DEFAULT);
3901 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
3902
3903 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
3904 }
3905
3906protected:
3907 std::shared_ptr<FakeApplicationHandle> mApplication;
3908 sp<FakeWindowHandle> mNoInputWindow;
3909 sp<FakeWindowHandle> mBottomWindow;
3910};
3911
3912TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
3913 PointF touchedPoint = {10, 10};
3914
3915 NotifyMotionArgs motionArgs =
3916 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3917 ADISPLAY_ID_DEFAULT, {touchedPoint});
3918 mDispatcher->notifyMotion(&motionArgs);
3919
3920 mNoInputWindow->assertNoEvents();
3921 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
3922 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
3923 // and therefore should prevent mBottomWindow from receiving touches
3924 mBottomWindow->assertNoEvents();
3925}
3926
3927/**
3928 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
3929 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
3930 */
3931TEST_F(InputDispatcherMultiWindowOcclusionTests,
3932 NoInputChannelFeature_DropsTouchesWithValidChannel) {
3933 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
3934 "Window with input channel and NO_INPUT_CHANNEL",
3935 ADISPLAY_ID_DEFAULT);
3936
3937 mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL);
3938 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
3939 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
3940
3941 PointF touchedPoint = {10, 10};
3942
3943 NotifyMotionArgs motionArgs =
3944 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3945 ADISPLAY_ID_DEFAULT, {touchedPoint});
3946 mDispatcher->notifyMotion(&motionArgs);
3947
3948 mNoInputWindow->assertNoEvents();
3949 mBottomWindow->assertNoEvents();
3950}
3951
Vishnu Nair958da932020-08-21 17:12:37 -07003952class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
3953protected:
3954 std::shared_ptr<FakeApplicationHandle> mApp;
3955 sp<FakeWindowHandle> mWindow;
3956 sp<FakeWindowHandle> mMirror;
3957
3958 virtual void SetUp() override {
3959 InputDispatcherTest::SetUp();
3960 mApp = std::make_shared<FakeApplicationHandle>();
3961 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
3962 mMirror = new FakeWindowHandle(mApp, mDispatcher, "TestWindowMirror", ADISPLAY_ID_DEFAULT,
3963 mWindow->getToken());
3964 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
3965 mWindow->setFocusable(true);
3966 mMirror->setFocusable(true);
3967 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
3968 }
3969};
3970
3971TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
3972 // Request focus on a mirrored window
3973 setFocusedWindow(mMirror);
3974
3975 // window gets focused
3976 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003977 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3978 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003979 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
3980}
3981
3982// A focused & mirrored window remains focused only if the window and its mirror are both
3983// focusable.
3984TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
3985 setFocusedWindow(mMirror);
3986
3987 // window gets focused
3988 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003989 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3990 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003991 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003992 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
3993 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003994 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
3995
3996 mMirror->setFocusable(false);
3997 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
3998
3999 // window loses focus since one of the windows associated with the token in not focusable
4000 mWindow->consumeFocusEvent(false);
4001
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004002 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4003 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004004 mWindow->assertNoEvents();
4005}
4006
4007// A focused & mirrored window remains focused until the window and its mirror both become
4008// invisible.
4009TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
4010 setFocusedWindow(mMirror);
4011
4012 // window gets focused
4013 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004014 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4015 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004016 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004017 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4018 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004019 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4020
4021 mMirror->setVisible(false);
4022 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4023
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004024 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4025 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004026 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004027 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4028 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004029 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4030
4031 mWindow->setVisible(false);
4032 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4033
4034 // window loses focus only after all windows associated with the token become invisible.
4035 mWindow->consumeFocusEvent(false);
4036
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004037 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4038 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004039 mWindow->assertNoEvents();
4040}
4041
4042// A focused & mirrored window remains focused until both windows are removed.
4043TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
4044 setFocusedWindow(mMirror);
4045
4046 // window gets focused
4047 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004048 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4049 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004050 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004051 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4052 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004053 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4054
4055 // single window is removed but the window token remains focused
4056 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
4057
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004058 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
4059 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004060 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004061 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
4062 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07004063 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
4064
4065 // Both windows are removed
4066 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
4067 mWindow->consumeFocusEvent(false);
4068
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004069 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4070 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004071 mWindow->assertNoEvents();
4072}
4073
4074// Focus request can be pending until one window becomes visible.
4075TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
4076 // Request focus on an invisible mirror.
4077 mWindow->setVisible(false);
4078 mMirror->setVisible(false);
4079 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4080 setFocusedWindow(mMirror);
4081
4082 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004083 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07004084 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004085 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07004086
4087 mMirror->setVisible(true);
4088 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4089
4090 // window gets focused
4091 mWindow->consumeFocusEvent(true);
4092 // window gets the pending key event
4093 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4094}
Prabir Pradhan99987712020-11-10 18:43:05 -08004095
4096class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
4097protected:
4098 std::shared_ptr<FakeApplicationHandle> mApp;
4099 sp<FakeWindowHandle> mWindow;
4100 sp<FakeWindowHandle> mSecondWindow;
4101
4102 void SetUp() override {
4103 InputDispatcherTest::SetUp();
4104 mApp = std::make_shared<FakeApplicationHandle>();
4105 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
4106 mWindow->setFocusable(true);
4107 mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
4108 mSecondWindow->setFocusable(true);
4109
4110 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
4111 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
4112
4113 setFocusedWindow(mWindow);
4114 mWindow->consumeFocusEvent(true);
4115 }
4116
4117 void notifyPointerCaptureChanged(bool enabled) {
4118 const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(enabled);
4119 mDispatcher->notifyPointerCaptureChanged(&args);
4120 }
4121
4122 void requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, bool enabled) {
4123 mDispatcher->requestPointerCapture(window->getToken(), enabled);
4124 mFakePolicy->waitForSetPointerCapture(enabled);
4125 notifyPointerCaptureChanged(enabled);
4126 window->consumeCaptureEvent(enabled);
4127 }
4128};
4129
4130TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
4131 // Ensure that capture cannot be obtained for unfocused windows.
4132 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
4133 mFakePolicy->assertSetPointerCaptureNotCalled();
4134 mSecondWindow->assertNoEvents();
4135
4136 // Ensure that capture can be enabled from the focus window.
4137 requestAndVerifyPointerCapture(mWindow, true);
4138
4139 // Ensure that capture cannot be disabled from a window that does not have capture.
4140 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
4141 mFakePolicy->assertSetPointerCaptureNotCalled();
4142
4143 // Ensure that capture can be disabled from the window with capture.
4144 requestAndVerifyPointerCapture(mWindow, false);
4145}
4146
4147TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
4148 requestAndVerifyPointerCapture(mWindow, true);
4149
4150 setFocusedWindow(mSecondWindow);
4151
4152 // Ensure that the capture disabled event was sent first.
4153 mWindow->consumeCaptureEvent(false);
4154 mWindow->consumeFocusEvent(false);
4155 mSecondWindow->consumeFocusEvent(true);
4156 mFakePolicy->waitForSetPointerCapture(false);
4157
4158 // Ensure that additional state changes from InputReader are not sent to the window.
4159 notifyPointerCaptureChanged(false);
4160 notifyPointerCaptureChanged(true);
4161 notifyPointerCaptureChanged(false);
4162 mWindow->assertNoEvents();
4163 mSecondWindow->assertNoEvents();
4164 mFakePolicy->assertSetPointerCaptureNotCalled();
4165}
4166
4167TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
4168 requestAndVerifyPointerCapture(mWindow, true);
4169
4170 // InputReader unexpectedly disables and enables pointer capture.
4171 notifyPointerCaptureChanged(false);
4172 notifyPointerCaptureChanged(true);
4173
4174 // Ensure that Pointer Capture is disabled.
4175 mWindow->consumeCaptureEvent(false);
4176 mWindow->assertNoEvents();
4177}
4178
Garfield Tane84e6f92019-08-29 17:28:41 -07004179} // namespace android::inputdispatcher