blob: f0d2edea6a63c1d218abecc2fc7d5857b2f09372 [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
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800865 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
866 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
867 expectedFlags);
868 }
869
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700870 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
871 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
872 }
873
Svet Ganov5d3bc372020-01-26 23:11:07 -0800874 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000875 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800876 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId,
877 expectedFlags);
878 }
879
880 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000881 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800882 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId,
883 expectedFlags);
884 }
885
886 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000887 int32_t expectedFlags = 0) {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800888 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
889 expectedFlags);
890 }
891
Svet Ganov5d3bc372020-01-26 23:11:07 -0800892 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000893 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
894 int32_t expectedFlags = 0) {
895 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
896 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -0800897 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
898 }
899
900 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000901 int32_t expectedFlags = 0) {
902 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
903 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -0800904 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
905 }
906
907 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000908 int32_t expectedFlags = 0) {
Michael Wright3a240c42019-12-10 20:53:41 +0000909 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
910 expectedFlags);
911 }
912
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500913 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
914 int32_t expectedFlags = 0) {
915 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
916 expectedFlags);
917 }
918
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100919 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
920 ASSERT_NE(mInputReceiver, nullptr)
921 << "Cannot consume events from a window with no receiver";
922 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
923 }
924
Prabir Pradhan99987712020-11-10 18:43:05 -0800925 void consumeCaptureEvent(bool hasCapture) {
926 ASSERT_NE(mInputReceiver, nullptr)
927 << "Cannot consume events from a window with no receiver";
928 mInputReceiver->consumeCaptureEvent(hasCapture);
929 }
930
chaviwd1c23182019-12-20 18:44:56 -0800931 void consumeEvent(int32_t expectedEventType, int32_t expectedAction, int32_t expectedDisplayId,
932 int32_t expectedFlags) {
933 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
934 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
935 expectedFlags);
936 }
937
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700938 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700939 if (mInputReceiver == nullptr) {
940 ADD_FAILURE() << "Invalid receive event on window with no receiver";
941 return std::nullopt;
942 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700943 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700944 }
945
946 void finishEvent(uint32_t sequenceNum) {
947 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
948 mInputReceiver->finishEvent(sequenceNum);
949 }
950
chaviwaf87b3e2019-10-01 16:59:28 -0700951 InputEvent* consume() {
952 if (mInputReceiver == nullptr) {
953 return nullptr;
954 }
955 return mInputReceiver->consume();
956 }
957
Arthur Hungb92218b2018-08-14 12:00:21 +0800958 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500959 if (mInputReceiver == nullptr &&
960 mInfo.inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL)) {
961 return; // Can't receive events if the window does not have input channel
962 }
963 ASSERT_NE(nullptr, mInputReceiver)
964 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -0800965 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +0800966 }
967
chaviwaf87b3e2019-10-01 16:59:28 -0700968 sp<IBinder> getToken() { return mInfo.token; }
969
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100970 const std::string& getName() { return mName; }
971
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000972 void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) {
973 mInfo.ownerPid = ownerPid;
974 mInfo.ownerUid = ownerUid;
975 }
976
chaviwd1c23182019-12-20 18:44:56 -0800977private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100978 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -0800979 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700980 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800981};
982
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700983std::atomic<int32_t> FakeWindowHandle::sId{1};
984
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800985static InputEventInjectionResult injectKey(
986 const sp<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
987 int32_t displayId = ADISPLAY_ID_NONE,
988 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
989 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800990 KeyEvent event;
991 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
992
993 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800994 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700995 INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE,
996 repeatCount, currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +0800997
998 // Inject event until dispatch out.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700999 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, syncMode,
1000 injectionTimeout,
1001 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
Arthur Hungb92218b2018-08-14 12:00:21 +08001002}
1003
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001004static InputEventInjectionResult injectKeyDown(const sp<InputDispatcher>& dispatcher,
1005 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001006 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId);
1007}
1008
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001009static InputEventInjectionResult injectKeyUp(const sp<InputDispatcher>& dispatcher,
1010 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001011 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId);
1012}
1013
Garfield Tandf26e862020-07-01 20:18:19 -07001014class PointerBuilder {
1015public:
1016 PointerBuilder(int32_t id, int32_t toolType) {
1017 mProperties.clear();
1018 mProperties.id = id;
1019 mProperties.toolType = toolType;
1020 mCoords.clear();
1021 }
1022
1023 PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); }
1024
1025 PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); }
1026
1027 PointerBuilder& axis(int32_t axis, float value) {
1028 mCoords.setAxisValue(axis, value);
1029 return *this;
1030 }
1031
1032 PointerProperties buildProperties() const { return mProperties; }
1033
1034 PointerCoords buildCoords() const { return mCoords; }
1035
1036private:
1037 PointerProperties mProperties;
1038 PointerCoords mCoords;
1039};
1040
1041class MotionEventBuilder {
1042public:
1043 MotionEventBuilder(int32_t action, int32_t source) {
1044 mAction = action;
1045 mSource = source;
1046 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1047 }
1048
1049 MotionEventBuilder& eventTime(nsecs_t eventTime) {
1050 mEventTime = eventTime;
1051 return *this;
1052 }
1053
1054 MotionEventBuilder& displayId(int32_t displayId) {
1055 mDisplayId = displayId;
1056 return *this;
1057 }
1058
1059 MotionEventBuilder& actionButton(int32_t actionButton) {
1060 mActionButton = actionButton;
1061 return *this;
1062 }
1063
1064 MotionEventBuilder& buttonState(int32_t actionButton) {
1065 mActionButton = actionButton;
1066 return *this;
1067 }
1068
1069 MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) {
1070 mRawXCursorPosition = rawXCursorPosition;
1071 return *this;
1072 }
1073
1074 MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) {
1075 mRawYCursorPosition = rawYCursorPosition;
1076 return *this;
1077 }
1078
1079 MotionEventBuilder& pointer(PointerBuilder pointer) {
1080 mPointers.push_back(pointer);
1081 return *this;
1082 }
1083
1084 MotionEvent build() {
1085 std::vector<PointerProperties> pointerProperties;
1086 std::vector<PointerCoords> pointerCoords;
1087 for (const PointerBuilder& pointer : mPointers) {
1088 pointerProperties.push_back(pointer.buildProperties());
1089 pointerCoords.push_back(pointer.buildCoords());
1090 }
1091
1092 // Set mouse cursor position for the most common cases to avoid boilerplate.
1093 if (mSource == AINPUT_SOURCE_MOUSE &&
1094 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) &&
1095 mPointers.size() == 1) {
1096 mRawXCursorPosition = pointerCoords[0].getX();
1097 mRawYCursorPosition = pointerCoords[0].getY();
1098 }
1099
1100 MotionEvent event;
chaviw9eaa22c2020-07-01 16:21:27 -07001101 ui::Transform identityTransform;
Garfield Tandf26e862020-07-01 20:18:19 -07001102 event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC,
1103 mAction, mActionButton, /* flags */ 0, /* edgeFlags */ 0, AMETA_NONE,
chaviw9eaa22c2020-07-01 16:21:27 -07001104 mButtonState, MotionClassification::NONE, identityTransform,
1105 /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition,
1106 mRawYCursorPosition, mEventTime, mEventTime, mPointers.size(),
1107 pointerProperties.data(), pointerCoords.data());
Garfield Tandf26e862020-07-01 20:18:19 -07001108
1109 return event;
1110 }
1111
1112private:
1113 int32_t mAction;
1114 int32_t mSource;
1115 nsecs_t mEventTime;
1116 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1117 int32_t mActionButton{0};
1118 int32_t mButtonState{0};
1119 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1120 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1121
1122 std::vector<PointerBuilder> mPointers;
1123};
1124
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001125static InputEventInjectionResult injectMotionEvent(
Garfield Tandf26e862020-07-01 20:18:19 -07001126 const sp<InputDispatcher>& dispatcher, const MotionEvent& event,
1127 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001128 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT) {
Garfield Tandf26e862020-07-01 20:18:19 -07001129 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, injectionMode,
1130 injectionTimeout,
1131 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
1132}
1133
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001134static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001135 const sp<InputDispatcher>& dispatcher, int32_t action, int32_t source, int32_t displayId,
1136 const PointF& position,
1137 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001138 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1139 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001140 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001141 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC)) {
Garfield Tandf26e862020-07-01 20:18:19 -07001142 MotionEvent event = MotionEventBuilder(action, source)
1143 .displayId(displayId)
1144 .eventTime(eventTime)
1145 .rawXCursorPosition(cursorPosition.x)
1146 .rawYCursorPosition(cursorPosition.y)
1147 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1148 .x(position.x)
1149 .y(position.y))
1150 .build();
Arthur Hungb92218b2018-08-14 12:00:21 +08001151
1152 // Inject event until dispatch out.
Garfield Tandf26e862020-07-01 20:18:19 -07001153 return injectMotionEvent(dispatcher, event);
Arthur Hungb92218b2018-08-14 12:00:21 +08001154}
1155
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001156static InputEventInjectionResult injectMotionDown(const sp<InputDispatcher>& dispatcher,
1157 int32_t source, int32_t displayId,
1158 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001159 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001160}
1161
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001162static InputEventInjectionResult injectMotionUp(const sp<InputDispatcher>& dispatcher,
1163 int32_t source, int32_t displayId,
1164 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001165 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001166}
1167
Jackal Guof9696682018-10-05 12:23:23 +08001168static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1169 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1170 // Define a valid key event.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001171 NotifyKeyArgs args(/* id */ 0, currentTime, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
1172 POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, KEY_A,
1173 AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001174
1175 return args;
1176}
1177
chaviwd1c23182019-12-20 18:44:56 -08001178static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId,
1179 const std::vector<PointF>& points) {
1180 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001181 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1182 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1183 }
1184
chaviwd1c23182019-12-20 18:44:56 -08001185 PointerProperties pointerProperties[pointerCount];
1186 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001187
chaviwd1c23182019-12-20 18:44:56 -08001188 for (size_t i = 0; i < pointerCount; i++) {
1189 pointerProperties[i].clear();
1190 pointerProperties[i].id = i;
1191 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001192
chaviwd1c23182019-12-20 18:44:56 -08001193 pointerCoords[i].clear();
1194 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1195 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1196 }
Jackal Guof9696682018-10-05 12:23:23 +08001197
1198 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1199 // Define a valid motion event.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001200 NotifyMotionArgs args(/* id */ 0, currentTime, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001201 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1202 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001203 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1204 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001205 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1206 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001207
1208 return args;
1209}
1210
chaviwd1c23182019-12-20 18:44:56 -08001211static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1212 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1213}
1214
Prabir Pradhan99987712020-11-10 18:43:05 -08001215static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(bool enabled) {
1216 return NotifyPointerCaptureChangedArgs(/* id */ 0, systemTime(SYSTEM_TIME_MONOTONIC), enabled);
1217}
1218
Arthur Hungb92218b2018-08-14 12:00:21 +08001219TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001220 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001221 sp<FakeWindowHandle> window =
1222 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001223
Arthur Hung72d8dc32020-03-28 00:48:39 +00001224 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001225 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1226 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1227 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001228
1229 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001230 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001231}
1232
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001233/**
1234 * Calling setInputWindows once with FLAG_NOT_TOUCH_MODAL should not cause any issues.
1235 * To ensure that window receives only events that were directly inside of it, add
1236 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1237 * when finding touched windows.
1238 * This test serves as a sanity check for the next test, where setInputWindows is
1239 * called twice.
1240 */
1241TEST_F(InputDispatcherTest, SetInputWindowOnce_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001242 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001243 sp<FakeWindowHandle> window =
1244 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1245 window->setFrame(Rect(0, 0, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01001246 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001247
1248 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001249 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001250 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1251 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001252 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001253
1254 // Window should receive motion event.
1255 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1256}
1257
1258/**
1259 * Calling setInputWindows twice, with the same info, should not cause any issues.
1260 * To ensure that window receives only events that were directly inside of it, add
1261 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1262 * when finding touched windows.
1263 */
1264TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001265 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001266 sp<FakeWindowHandle> window =
1267 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1268 window->setFrame(Rect(0, 0, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01001269 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001270
1271 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1272 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001273 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001274 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1275 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001276 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001277
1278 // Window should receive motion event.
1279 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1280}
1281
Arthur Hungb92218b2018-08-14 12:00:21 +08001282// The foreground window should receive the first touch down event.
1283TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001284 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001285 sp<FakeWindowHandle> windowTop =
1286 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
1287 sp<FakeWindowHandle> windowSecond =
1288 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001289
Arthur Hung72d8dc32020-03-28 00:48:39 +00001290 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001291 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1292 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1293 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001294
1295 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001296 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001297 windowSecond->assertNoEvents();
1298}
1299
Garfield Tandf26e862020-07-01 20:18:19 -07001300TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07001301 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07001302 sp<FakeWindowHandle> windowLeft =
1303 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1304 windowLeft->setFrame(Rect(0, 0, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001305 windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001306 sp<FakeWindowHandle> windowRight =
1307 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1308 windowRight->setFrame(Rect(600, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001309 windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001310
1311 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1312
1313 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
1314
1315 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001316 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001317 injectMotionEvent(mDispatcher,
1318 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1319 AINPUT_SOURCE_MOUSE)
1320 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1321 .x(900)
1322 .y(400))
1323 .build()));
1324 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1325 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1326 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1327 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1328
1329 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001330 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001331 injectMotionEvent(mDispatcher,
1332 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1333 AINPUT_SOURCE_MOUSE)
1334 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1335 .x(300)
1336 .y(400))
1337 .build()));
1338 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1339 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1340 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1341 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1342 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1343 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1344
1345 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001346 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001347 injectMotionEvent(mDispatcher,
1348 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1349 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1350 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1351 .x(300)
1352 .y(400))
1353 .build()));
1354 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1355
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001356 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001357 injectMotionEvent(mDispatcher,
1358 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
1359 AINPUT_SOURCE_MOUSE)
1360 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1361 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1362 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1363 .x(300)
1364 .y(400))
1365 .build()));
1366 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1367 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1368
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001369 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001370 injectMotionEvent(mDispatcher,
1371 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1372 AINPUT_SOURCE_MOUSE)
1373 .buttonState(0)
1374 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1375 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1376 .x(300)
1377 .y(400))
1378 .build()));
1379 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1380 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1381
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001382 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001383 injectMotionEvent(mDispatcher,
1384 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
1385 .buttonState(0)
1386 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1387 .x(300)
1388 .y(400))
1389 .build()));
1390 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1391
1392 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001393 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001394 injectMotionEvent(mDispatcher,
1395 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1396 AINPUT_SOURCE_MOUSE)
1397 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1398 .x(900)
1399 .y(400))
1400 .build()));
1401 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1402 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1403 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1404 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1405 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1406 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1407}
1408
1409// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
1410// directly in this test.
1411TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07001412 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07001413 sp<FakeWindowHandle> window =
1414 new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1415 window->setFrame(Rect(0, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001416 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001417
1418 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1419
1420 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1421
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001422 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001423 injectMotionEvent(mDispatcher,
1424 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
1425 AINPUT_SOURCE_MOUSE)
1426 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1427 .x(300)
1428 .y(400))
1429 .build()));
1430 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1431 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1432
1433 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001434 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001435 injectMotionEvent(mDispatcher,
1436 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1437 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1438 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1439 .x(300)
1440 .y(400))
1441 .build()));
1442 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1443
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001444 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001445 injectMotionEvent(mDispatcher,
1446 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
1447 AINPUT_SOURCE_MOUSE)
1448 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1449 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1450 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1451 .x(300)
1452 .y(400))
1453 .build()));
1454 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1455 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1456
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001457 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001458 injectMotionEvent(mDispatcher,
1459 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1460 AINPUT_SOURCE_MOUSE)
1461 .buttonState(0)
1462 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1463 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1464 .x(300)
1465 .y(400))
1466 .build()));
1467 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1468 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1469
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001470 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001471 injectMotionEvent(mDispatcher,
1472 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
1473 .buttonState(0)
1474 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1475 .x(300)
1476 .y(400))
1477 .build()));
1478 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1479
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001480 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001481 injectMotionEvent(mDispatcher,
1482 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
1483 AINPUT_SOURCE_MOUSE)
1484 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1485 .x(300)
1486 .y(400))
1487 .build()));
1488 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1489 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1490}
1491
Garfield Tan00f511d2019-06-12 16:55:40 -07001492TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07001493 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07001494
1495 sp<FakeWindowHandle> windowLeft =
1496 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1497 windowLeft->setFrame(Rect(0, 0, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001498 windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07001499 sp<FakeWindowHandle> windowRight =
1500 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1501 windowRight->setFrame(Rect(600, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001502 windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07001503
1504 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1505
Arthur Hung72d8dc32020-03-28 00:48:39 +00001506 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07001507
1508 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
1509 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001510 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07001511 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001512 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001513 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07001514 windowRight->assertNoEvents();
1515}
1516
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001517TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001518 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001519 sp<FakeWindowHandle> window =
1520 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07001521 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001522
Arthur Hung72d8dc32020-03-28 00:48:39 +00001523 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001524 setFocusedWindow(window);
1525
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001526 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001527
1528 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1529 mDispatcher->notifyKey(&keyArgs);
1530
1531 // Window should receive key down event.
1532 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1533
1534 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
1535 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001536 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001537 mDispatcher->notifyDeviceReset(&args);
1538 window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
1539 AKEY_EVENT_FLAG_CANCELED);
1540}
1541
1542TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001543 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001544 sp<FakeWindowHandle> window =
1545 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1546
Arthur Hung72d8dc32020-03-28 00:48:39 +00001547 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001548
1549 NotifyMotionArgs motionArgs =
1550 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1551 ADISPLAY_ID_DEFAULT);
1552 mDispatcher->notifyMotion(&motionArgs);
1553
1554 // Window should receive motion down event.
1555 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1556
1557 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
1558 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001559 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001560 mDispatcher->notifyDeviceReset(&args);
1561 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT,
1562 0 /*expectedFlags*/);
1563}
1564
Svet Ganov5d3bc372020-01-26 23:11:07 -08001565TEST_F(InputDispatcherTest, TransferTouchFocus_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07001566 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001567
1568 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001569 sp<FakeWindowHandle> firstWindow =
1570 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1571 sp<FakeWindowHandle> secondWindow =
1572 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001573
1574 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001575 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001576
1577 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001578 NotifyMotionArgs downMotionArgs =
1579 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1580 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001581 mDispatcher->notifyMotion(&downMotionArgs);
1582 // Only the first window should get the down event
1583 firstWindow->consumeMotionDown();
1584 secondWindow->assertNoEvents();
1585
1586 // Transfer touch focus to the second window
1587 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1588 // The first window gets cancel and the second gets down
1589 firstWindow->consumeMotionCancel();
1590 secondWindow->consumeMotionDown();
1591
1592 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001593 NotifyMotionArgs upMotionArgs =
1594 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1595 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001596 mDispatcher->notifyMotion(&upMotionArgs);
1597 // The first window gets no events and the second gets up
1598 firstWindow->assertNoEvents();
1599 secondWindow->consumeMotionUp();
1600}
1601
1602TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointerNoSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001603 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001604
1605 PointF touchPoint = {10, 10};
1606
1607 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001608 sp<FakeWindowHandle> firstWindow =
1609 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1610 sp<FakeWindowHandle> secondWindow =
1611 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001612
1613 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001614 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001615
1616 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001617 NotifyMotionArgs downMotionArgs =
1618 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1619 ADISPLAY_ID_DEFAULT, {touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001620 mDispatcher->notifyMotion(&downMotionArgs);
1621 // Only the first window should get the down event
1622 firstWindow->consumeMotionDown();
1623 secondWindow->assertNoEvents();
1624
1625 // Send pointer down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001626 NotifyMotionArgs pointerDownMotionArgs =
1627 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1628 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1629 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1630 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001631 mDispatcher->notifyMotion(&pointerDownMotionArgs);
1632 // Only the first window should get the pointer down event
1633 firstWindow->consumeMotionPointerDown(1);
1634 secondWindow->assertNoEvents();
1635
1636 // Transfer touch focus to the second window
1637 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1638 // The first window gets cancel and the second gets down and pointer down
1639 firstWindow->consumeMotionCancel();
1640 secondWindow->consumeMotionDown();
1641 secondWindow->consumeMotionPointerDown(1);
1642
1643 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001644 NotifyMotionArgs pointerUpMotionArgs =
1645 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1646 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1647 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1648 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001649 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1650 // The first window gets nothing and the second gets pointer up
1651 firstWindow->assertNoEvents();
1652 secondWindow->consumeMotionPointerUp(1);
1653
1654 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001655 NotifyMotionArgs upMotionArgs =
1656 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1657 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001658 mDispatcher->notifyMotion(&upMotionArgs);
1659 // The first window gets nothing and the second gets up
1660 firstWindow->assertNoEvents();
1661 secondWindow->consumeMotionUp();
1662}
1663
1664TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001665 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001666
1667 // Create a non touch modal window that supports split touch
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001668 sp<FakeWindowHandle> firstWindow =
1669 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001670 firstWindow->setFrame(Rect(0, 0, 600, 400));
Michael Wright44753b12020-07-08 13:48:11 +01001671 firstWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1672 InputWindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001673
1674 // Create a non touch modal window that supports split touch
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001675 sp<FakeWindowHandle> secondWindow =
1676 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001677 secondWindow->setFrame(Rect(0, 400, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001678 secondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1679 InputWindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001680
1681 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001682 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001683
1684 PointF pointInFirst = {300, 200};
1685 PointF pointInSecond = {300, 600};
1686
1687 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001688 NotifyMotionArgs firstDownMotionArgs =
1689 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1690 ADISPLAY_ID_DEFAULT, {pointInFirst});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001691 mDispatcher->notifyMotion(&firstDownMotionArgs);
1692 // Only the first window should get the down event
1693 firstWindow->consumeMotionDown();
1694 secondWindow->assertNoEvents();
1695
1696 // Send down to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001697 NotifyMotionArgs secondDownMotionArgs =
1698 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1699 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1700 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1701 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001702 mDispatcher->notifyMotion(&secondDownMotionArgs);
1703 // The first window gets a move and the second a down
1704 firstWindow->consumeMotionMove();
1705 secondWindow->consumeMotionDown();
1706
1707 // Transfer touch focus to the second window
1708 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1709 // The first window gets cancel and the new gets pointer down (it already saw down)
1710 firstWindow->consumeMotionCancel();
1711 secondWindow->consumeMotionPointerDown(1);
1712
1713 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001714 NotifyMotionArgs pointerUpMotionArgs =
1715 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1716 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1717 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1718 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001719 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1720 // The first window gets nothing and the second gets pointer up
1721 firstWindow->assertNoEvents();
1722 secondWindow->consumeMotionPointerUp(1);
1723
1724 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001725 NotifyMotionArgs upMotionArgs =
1726 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1727 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001728 mDispatcher->notifyMotion(&upMotionArgs);
1729 // The first window gets nothing and the second gets up
1730 firstWindow->assertNoEvents();
1731 secondWindow->consumeMotionUp();
1732}
1733
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001734TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07001735 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001736 sp<FakeWindowHandle> window =
1737 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1738
Vishnu Nair47074b82020-08-14 11:54:47 -07001739 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001740 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001741 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001742
1743 window->consumeFocusEvent(true);
1744
1745 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1746 mDispatcher->notifyKey(&keyArgs);
1747
1748 // Window should receive key down event.
1749 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1750}
1751
1752TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07001753 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001754 sp<FakeWindowHandle> window =
1755 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1756
Arthur Hung72d8dc32020-03-28 00:48:39 +00001757 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001758
1759 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1760 mDispatcher->notifyKey(&keyArgs);
1761 mDispatcher->waitForIdle();
1762
1763 window->assertNoEvents();
1764}
1765
1766// If a window is touchable, but does not have focus, it should receive motion events, but not keys
1767TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07001768 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001769 sp<FakeWindowHandle> window =
1770 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1771
Arthur Hung72d8dc32020-03-28 00:48:39 +00001772 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001773
1774 // Send key
1775 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1776 mDispatcher->notifyKey(&keyArgs);
1777 // Send motion
1778 NotifyMotionArgs motionArgs =
1779 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1780 ADISPLAY_ID_DEFAULT);
1781 mDispatcher->notifyMotion(&motionArgs);
1782
1783 // Window should receive only the motion event
1784 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1785 window->assertNoEvents(); // Key event or focus event will not be received
1786}
1787
chaviwd1c23182019-12-20 18:44:56 -08001788class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00001789public:
1790 FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name,
chaviwd1c23182019-12-20 18:44:56 -08001791 int32_t displayId, bool isGestureMonitor = false) {
Garfield Tan15601662020-09-22 15:32:38 -07001792 base::Result<std::unique_ptr<InputChannel>> channel =
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +00001793 dispatcher->createInputMonitor(displayId, isGestureMonitor, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07001794 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00001795 }
1796
chaviwd1c23182019-12-20 18:44:56 -08001797 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
1798
1799 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1800 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN,
1801 expectedDisplayId, expectedFlags);
1802 }
1803
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001804 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
1805
1806 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
1807
chaviwd1c23182019-12-20 18:44:56 -08001808 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1809 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
1810 expectedDisplayId, expectedFlags);
1811 }
1812
1813 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1814 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP,
1815 expectedDisplayId, expectedFlags);
1816 }
1817
1818 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
1819
1820private:
1821 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00001822};
1823
1824// Tests for gesture monitors
1825TEST_F(InputDispatcherTest, GestureMonitor_ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07001826 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00001827 sp<FakeWindowHandle> window =
1828 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001829 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00001830
chaviwd1c23182019-12-20 18:44:56 -08001831 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
1832 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00001833
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001834 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00001835 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001836 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00001837 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001838 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00001839}
1840
1841TEST_F(InputDispatcherTest, GestureMonitor_DoesNotReceiveKeyEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07001842 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00001843 sp<FakeWindowHandle> window =
1844 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1845
1846 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07001847 window->setFocusable(true);
Michael Wright3a240c42019-12-10 20:53:41 +00001848
Arthur Hung72d8dc32020-03-28 00:48:39 +00001849 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001850 setFocusedWindow(window);
1851
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001852 window->consumeFocusEvent(true);
Michael Wright3a240c42019-12-10 20:53:41 +00001853
chaviwd1c23182019-12-20 18:44:56 -08001854 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
1855 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00001856
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001857 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
1858 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00001859 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001860 monitor.assertNoEvents();
Michael Wright3a240c42019-12-10 20:53:41 +00001861}
1862
1863TEST_F(InputDispatcherTest, GestureMonitor_CanPilferAfterWindowIsRemovedMidStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001864 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00001865 sp<FakeWindowHandle> window =
1866 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001867 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00001868
chaviwd1c23182019-12-20 18:44:56 -08001869 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
1870 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00001871
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001872 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00001873 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001874 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00001875 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001876 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00001877
1878 window->releaseChannel();
1879
chaviwd1c23182019-12-20 18:44:56 -08001880 mDispatcher->pilferPointers(monitor.getToken());
Michael Wright3a240c42019-12-10 20:53:41 +00001881
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001882 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00001883 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001884 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08001885 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00001886}
1887
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001888TEST_F(InputDispatcherTest, UnresponsiveGestureMonitor_GetsAnr) {
1889 FakeMonitorReceiver monitor =
1890 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
1891 true /*isGestureMonitor*/);
1892
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001893 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001894 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
1895 std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
1896 ASSERT_TRUE(consumeSeq);
1897
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001898 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(DISPATCHING_TIMEOUT,
1899 monitor.getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001900 monitor.finishEvent(*consumeSeq);
1901 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001902 mFakePolicy->assertNotifyConnectionResponsiveWasCalled(monitor.getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001903}
1904
chaviw81e2bb92019-12-18 15:03:51 -08001905TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07001906 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
chaviw81e2bb92019-12-18 15:03:51 -08001907 sp<FakeWindowHandle> window =
1908 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1909
Arthur Hung72d8dc32020-03-28 00:48:39 +00001910 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08001911
1912 NotifyMotionArgs motionArgs =
1913 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1914 ADISPLAY_ID_DEFAULT);
1915
1916 mDispatcher->notifyMotion(&motionArgs);
1917 // Window should receive motion down event.
1918 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1919
1920 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001921 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08001922 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1923 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
1924 motionArgs.pointerCoords[0].getX() - 10);
1925
1926 mDispatcher->notifyMotion(&motionArgs);
1927 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
1928 0 /*expectedFlags*/);
1929}
1930
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001931/**
1932 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
1933 * the device default right away. In the test scenario, we check both the default value,
1934 * and the action of enabling / disabling.
1935 */
1936TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07001937 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001938 sp<FakeWindowHandle> window =
1939 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
1940
1941 // Set focused application.
1942 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07001943 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001944
1945 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00001946 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001947 setFocusedWindow(window);
1948
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001949 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
1950
1951 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07001952 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001953 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001954 window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/);
1955
1956 SCOPED_TRACE("Disable touch mode");
1957 mDispatcher->setInTouchMode(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07001958 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001959 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001960 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001961 window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/);
1962
1963 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07001964 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001965 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001966 window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/);
1967
1968 SCOPED_TRACE("Enable touch mode again");
1969 mDispatcher->setInTouchMode(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07001970 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001971 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001972 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001973 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
1974
1975 window->assertNoEvents();
1976}
1977
Gang Wange9087892020-01-07 12:17:14 -05001978TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07001979 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Gang Wange9087892020-01-07 12:17:14 -05001980 sp<FakeWindowHandle> window =
1981 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
1982
1983 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07001984 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05001985
Arthur Hung72d8dc32020-03-28 00:48:39 +00001986 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001987 setFocusedWindow(window);
1988
Gang Wange9087892020-01-07 12:17:14 -05001989 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
1990
1991 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
1992 mDispatcher->notifyKey(&keyArgs);
1993
1994 InputEvent* event = window->consume();
1995 ASSERT_NE(event, nullptr);
1996
1997 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
1998 ASSERT_NE(verified, nullptr);
1999 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
2000
2001 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
2002 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
2003 ASSERT_EQ(keyArgs.source, verified->source);
2004 ASSERT_EQ(keyArgs.displayId, verified->displayId);
2005
2006 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
2007
2008 ASSERT_EQ(keyArgs.action, verifiedKey.action);
2009 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05002010 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
2011 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
2012 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
2013 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
2014 ASSERT_EQ(0, verifiedKey.repeatCount);
2015}
2016
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002017TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002018 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002019 sp<FakeWindowHandle> window =
2020 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2021
2022 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2023
Arthur Hung72d8dc32020-03-28 00:48:39 +00002024 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002025
2026 NotifyMotionArgs motionArgs =
2027 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2028 ADISPLAY_ID_DEFAULT);
2029 mDispatcher->notifyMotion(&motionArgs);
2030
2031 InputEvent* event = window->consume();
2032 ASSERT_NE(event, nullptr);
2033
2034 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
2035 ASSERT_NE(verified, nullptr);
2036 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
2037
2038 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
2039 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
2040 EXPECT_EQ(motionArgs.source, verified->source);
2041 EXPECT_EQ(motionArgs.displayId, verified->displayId);
2042
2043 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
2044
2045 EXPECT_EQ(motionArgs.pointerCoords[0].getX(), verifiedMotion.rawX);
2046 EXPECT_EQ(motionArgs.pointerCoords[0].getY(), verifiedMotion.rawY);
2047 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
2048 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
2049 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
2050 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
2051 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
2052}
2053
chaviw09c8d2d2020-08-24 15:48:26 -07002054/**
2055 * Ensure that separate calls to sign the same data are generating the same key.
2056 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
2057 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
2058 * tests.
2059 */
2060TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
2061 KeyEvent event = getTestKeyEvent();
2062 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
2063
2064 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
2065 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
2066 ASSERT_EQ(hmac1, hmac2);
2067}
2068
2069/**
2070 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
2071 */
2072TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
2073 KeyEvent event = getTestKeyEvent();
2074 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
2075 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
2076
2077 verifiedEvent.deviceId += 1;
2078 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2079
2080 verifiedEvent.source += 1;
2081 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2082
2083 verifiedEvent.eventTimeNanos += 1;
2084 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2085
2086 verifiedEvent.displayId += 1;
2087 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2088
2089 verifiedEvent.action += 1;
2090 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2091
2092 verifiedEvent.downTimeNanos += 1;
2093 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2094
2095 verifiedEvent.flags += 1;
2096 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2097
2098 verifiedEvent.keyCode += 1;
2099 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2100
2101 verifiedEvent.scanCode += 1;
2102 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2103
2104 verifiedEvent.metaState += 1;
2105 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2106
2107 verifiedEvent.repeatCount += 1;
2108 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2109}
2110
Vishnu Nair958da932020-08-21 17:12:37 -07002111TEST_F(InputDispatcherTest, SetFocusedWindow) {
2112 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2113 sp<FakeWindowHandle> windowTop =
2114 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2115 sp<FakeWindowHandle> windowSecond =
2116 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2117 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2118
2119 // Top window is also focusable but is not granted focus.
2120 windowTop->setFocusable(true);
2121 windowSecond->setFocusable(true);
2122 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2123 setFocusedWindow(windowSecond);
2124
2125 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002126 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2127 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002128
2129 // Focused window should receive event.
2130 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
2131 windowTop->assertNoEvents();
2132}
2133
2134TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
2135 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2136 sp<FakeWindowHandle> window =
2137 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2138 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2139
2140 window->setFocusable(true);
2141 // Release channel for window is no longer valid.
2142 window->releaseChannel();
2143 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2144 setFocusedWindow(window);
2145
2146 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002147 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2148 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002149
2150 // window channel is invalid, so it should not receive any input event.
2151 window->assertNoEvents();
2152}
2153
2154TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
2155 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2156 sp<FakeWindowHandle> window =
2157 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2158 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2159
2160 // Window is not focusable.
2161 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2162 setFocusedWindow(window);
2163
2164 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002165 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2166 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002167
2168 // window is invalid, so it should not receive any input event.
2169 window->assertNoEvents();
2170}
2171
2172TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
2173 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2174 sp<FakeWindowHandle> windowTop =
2175 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2176 sp<FakeWindowHandle> windowSecond =
2177 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2178 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2179
2180 windowTop->setFocusable(true);
2181 windowSecond->setFocusable(true);
2182 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2183 setFocusedWindow(windowTop);
2184 windowTop->consumeFocusEvent(true);
2185
2186 setFocusedWindow(windowSecond, windowTop);
2187 windowSecond->consumeFocusEvent(true);
2188 windowTop->consumeFocusEvent(false);
2189
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002190 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2191 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002192
2193 // Focused window should receive event.
2194 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
2195}
2196
2197TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) {
2198 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2199 sp<FakeWindowHandle> windowTop =
2200 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2201 sp<FakeWindowHandle> windowSecond =
2202 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2203 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2204
2205 windowTop->setFocusable(true);
2206 windowSecond->setFocusable(true);
2207 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2208 setFocusedWindow(windowSecond, windowTop);
2209
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002210 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2211 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002212
2213 // Event should be dropped.
2214 windowTop->assertNoEvents();
2215 windowSecond->assertNoEvents();
2216}
2217
2218TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
2219 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2220 sp<FakeWindowHandle> window =
2221 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2222 sp<FakeWindowHandle> previousFocusedWindow =
2223 new FakeWindowHandle(application, mDispatcher, "previousFocusedWindow",
2224 ADISPLAY_ID_DEFAULT);
2225 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2226
2227 window->setFocusable(true);
2228 previousFocusedWindow->setFocusable(true);
2229 window->setVisible(false);
2230 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
2231 setFocusedWindow(previousFocusedWindow);
2232 previousFocusedWindow->consumeFocusEvent(true);
2233
2234 // Requesting focus on invisible window takes focus from currently focused window.
2235 setFocusedWindow(window);
2236 previousFocusedWindow->consumeFocusEvent(false);
2237
2238 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002239 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07002240 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002241 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07002242
2243 // Window does not get focus event or key down.
2244 window->assertNoEvents();
2245
2246 // Window becomes visible.
2247 window->setVisible(true);
2248 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2249
2250 // Window receives focus event.
2251 window->consumeFocusEvent(true);
2252 // Focused window receives key down.
2253 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2254}
2255
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002256/**
2257 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
2258 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
2259 * of the 'slipperyEnterWindow'.
2260 *
2261 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
2262 * a way so that the touched location is no longer covered by the top window.
2263 *
2264 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
2265 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
2266 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
2267 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
2268 * with ACTION_DOWN).
2269 * Thus, the touch has been transferred from the top window into the bottom window, because the top
2270 * window moved itself away from the touched location and had Flag::SLIPPERY.
2271 *
2272 * Even though the top window moved away from the touched location, it is still obscuring the bottom
2273 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
2274 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
2275 *
2276 * In this test, we ensure that the event received by the bottom window has
2277 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
2278 */
2279TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
2280 constexpr int32_t SLIPPERY_PID = INJECTOR_PID + 1;
2281 constexpr int32_t SLIPPERY_UID = INJECTOR_UID + 1;
2282
2283 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2284 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2285
2286 sp<FakeWindowHandle> slipperyExitWindow =
2287 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2288 slipperyExitWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
2289 InputWindowInfo::Flag::SLIPPERY);
2290 // Make sure this one overlaps the bottom window
2291 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
2292 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
2293 // one. Windows with the same owner are not considered to be occluding each other.
2294 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
2295
2296 sp<FakeWindowHandle> slipperyEnterWindow =
2297 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2298 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
2299
2300 mDispatcher->setInputWindows(
2301 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
2302
2303 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
2304 NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2305 ADISPLAY_ID_DEFAULT, {{50, 50}});
2306 mDispatcher->notifyMotion(&args);
2307 slipperyExitWindow->consumeMotionDown();
2308 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
2309 mDispatcher->setInputWindows(
2310 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
2311
2312 args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2313 ADISPLAY_ID_DEFAULT, {{51, 51}});
2314 mDispatcher->notifyMotion(&args);
2315
2316 slipperyExitWindow->consumeMotionCancel();
2317
2318 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
2319 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
2320}
2321
Garfield Tan1c7bc862020-01-28 13:24:04 -08002322class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
2323protected:
2324 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
2325 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
2326
Chris Yea209fde2020-07-22 13:54:51 -07002327 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002328 sp<FakeWindowHandle> mWindow;
2329
2330 virtual void SetUp() override {
2331 mFakePolicy = new FakeInputDispatcherPolicy();
2332 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
2333 mDispatcher = new InputDispatcher(mFakePolicy);
2334 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
2335 ASSERT_EQ(OK, mDispatcher->start());
2336
2337 setUpWindow();
2338 }
2339
2340 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07002341 mApp = std::make_shared<FakeApplicationHandle>();
Garfield Tan1c7bc862020-01-28 13:24:04 -08002342 mWindow = new FakeWindowHandle(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2343
Vishnu Nair47074b82020-08-14 11:54:47 -07002344 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002345 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002346 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002347 mWindow->consumeFocusEvent(true);
2348 }
2349
Chris Ye2ad95392020-09-01 13:44:44 -07002350 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08002351 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07002352 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002353 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
2354 mDispatcher->notifyKey(&keyArgs);
2355
2356 // Window should receive key down event.
2357 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2358 }
2359
2360 void expectKeyRepeatOnce(int32_t repeatCount) {
2361 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
2362 InputEvent* repeatEvent = mWindow->consume();
2363 ASSERT_NE(nullptr, repeatEvent);
2364
2365 uint32_t eventType = repeatEvent->getType();
2366 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType);
2367
2368 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
2369 uint32_t eventAction = repeatKeyEvent->getAction();
2370 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
2371 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
2372 }
2373
Chris Ye2ad95392020-09-01 13:44:44 -07002374 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08002375 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07002376 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002377 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
2378 mDispatcher->notifyKey(&keyArgs);
2379
2380 // Window should receive key down event.
2381 mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
2382 0 /*expectedFlags*/);
2383 }
2384};
2385
2386TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Chris Ye2ad95392020-09-01 13:44:44 -07002387 sendAndConsumeKeyDown(1 /* deviceId */);
2388 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2389 expectKeyRepeatOnce(repeatCount);
2390 }
2391}
2392
2393TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
2394 sendAndConsumeKeyDown(1 /* deviceId */);
2395 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2396 expectKeyRepeatOnce(repeatCount);
2397 }
2398 sendAndConsumeKeyDown(2 /* deviceId */);
2399 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08002400 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2401 expectKeyRepeatOnce(repeatCount);
2402 }
2403}
2404
2405TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Chris Ye2ad95392020-09-01 13:44:44 -07002406 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002407 expectKeyRepeatOnce(1 /*repeatCount*/);
Chris Ye2ad95392020-09-01 13:44:44 -07002408 sendAndConsumeKeyUp(1 /* deviceId */);
2409 mWindow->assertNoEvents();
2410}
2411
2412TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
2413 sendAndConsumeKeyDown(1 /* deviceId */);
2414 expectKeyRepeatOnce(1 /*repeatCount*/);
2415 sendAndConsumeKeyDown(2 /* deviceId */);
2416 expectKeyRepeatOnce(1 /*repeatCount*/);
2417 // Stale key up from device 1.
2418 sendAndConsumeKeyUp(1 /* deviceId */);
2419 // Device 2 is still down, keep repeating
2420 expectKeyRepeatOnce(2 /*repeatCount*/);
2421 expectKeyRepeatOnce(3 /*repeatCount*/);
2422 // Device 2 key up
2423 sendAndConsumeKeyUp(2 /* deviceId */);
2424 mWindow->assertNoEvents();
2425}
2426
2427TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
2428 sendAndConsumeKeyDown(1 /* deviceId */);
2429 expectKeyRepeatOnce(1 /*repeatCount*/);
2430 sendAndConsumeKeyDown(2 /* deviceId */);
2431 expectKeyRepeatOnce(1 /*repeatCount*/);
2432 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
2433 sendAndConsumeKeyUp(2 /* deviceId */);
2434 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08002435 mWindow->assertNoEvents();
2436}
2437
2438TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Chris Ye2ad95392020-09-01 13:44:44 -07002439 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002440 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2441 InputEvent* repeatEvent = mWindow->consume();
2442 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2443 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
2444 IdGenerator::getSource(repeatEvent->getId()));
2445 }
2446}
2447
2448TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Chris Ye2ad95392020-09-01 13:44:44 -07002449 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002450
2451 std::unordered_set<int32_t> idSet;
2452 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2453 InputEvent* repeatEvent = mWindow->consume();
2454 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2455 int32_t id = repeatEvent->getId();
2456 EXPECT_EQ(idSet.end(), idSet.find(id));
2457 idSet.insert(id);
2458 }
2459}
2460
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002461/* Test InputDispatcher for MultiDisplay */
2462class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
2463public:
2464 static constexpr int32_t SECOND_DISPLAY_ID = 1;
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002465 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002466 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08002467
Chris Yea209fde2020-07-22 13:54:51 -07002468 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002469 windowInPrimary =
2470 new FakeWindowHandle(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002471
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002472 // Set focus window for primary display, but focused display would be second one.
2473 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07002474 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002475 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002476 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002477 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08002478
Chris Yea209fde2020-07-22 13:54:51 -07002479 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002480 windowInSecondary =
2481 new FakeWindowHandle(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002482 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002483 // Set focus display to second one.
2484 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
2485 // Set focus window for second display.
2486 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07002487 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002488 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002489 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002490 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002491 }
2492
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002493 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002494 InputDispatcherTest::TearDown();
2495
Chris Yea209fde2020-07-22 13:54:51 -07002496 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002497 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07002498 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002499 windowInSecondary.clear();
2500 }
2501
2502protected:
Chris Yea209fde2020-07-22 13:54:51 -07002503 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002504 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07002505 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002506 sp<FakeWindowHandle> windowInSecondary;
2507};
2508
2509TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
2510 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002511 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2512 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2513 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002514 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08002515 windowInSecondary->assertNoEvents();
2516
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002517 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002518 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2519 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
2520 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08002521 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002522 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08002523}
2524
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002525TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08002526 // Test inject a key down with display id specified.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002527 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2528 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002529 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08002530 windowInSecondary->assertNoEvents();
2531
2532 // Test inject a key down without display id specified.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002533 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2534 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08002535 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002536 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08002537
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002538 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00002539 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08002540
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002541 // Old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002542 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
2543 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08002544
2545 // Test inject a key down, should timeout because of no target window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002546 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2547 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08002548 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002549 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08002550 windowInSecondary->assertNoEvents();
2551}
2552
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002553// Test per-display input monitors for motion event.
2554TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08002555 FakeMonitorReceiver monitorInPrimary =
2556 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
2557 FakeMonitorReceiver monitorInSecondary =
2558 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002559
2560 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002561 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2562 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2563 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002564 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002565 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002566 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002567 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002568
2569 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002570 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2571 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
2572 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002573 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002574 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002575 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08002576 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002577
2578 // Test inject a non-pointer motion event.
2579 // If specific a display, it will dispatch to the focused window of particular display,
2580 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002581 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2582 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
2583 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002584 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002585 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002586 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08002587 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002588}
2589
2590// Test per-display input monitors for key event.
2591TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002592 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08002593 FakeMonitorReceiver monitorInPrimary =
2594 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
2595 FakeMonitorReceiver monitorInSecondary =
2596 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002597
2598 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002599 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2600 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002601 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002602 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002603 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08002604 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002605}
2606
Vishnu Nair958da932020-08-21 17:12:37 -07002607TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
2608 sp<FakeWindowHandle> secondWindowInPrimary =
2609 new FakeWindowHandle(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
2610 secondWindowInPrimary->setFocusable(true);
2611 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
2612 setFocusedWindow(secondWindowInPrimary);
2613 windowInPrimary->consumeFocusEvent(false);
2614 secondWindowInPrimary->consumeFocusEvent(true);
2615
2616 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002617 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2618 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002619 windowInPrimary->assertNoEvents();
2620 windowInSecondary->assertNoEvents();
2621 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2622}
2623
Jackal Guof9696682018-10-05 12:23:23 +08002624class InputFilterTest : public InputDispatcherTest {
2625protected:
2626 static constexpr int32_t SECOND_DISPLAY_ID = 1;
2627
2628 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) {
2629 NotifyMotionArgs motionArgs;
2630
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002631 motionArgs =
2632 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08002633 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002634 motionArgs =
2635 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08002636 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002637 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08002638 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08002639 mFakePolicy->assertFilterInputEventWasCalled(motionArgs);
Jackal Guof9696682018-10-05 12:23:23 +08002640 } else {
2641 mFakePolicy->assertFilterInputEventWasNotCalled();
2642 }
2643 }
2644
2645 void testNotifyKey(bool expectToBeFiltered) {
2646 NotifyKeyArgs keyArgs;
2647
2648 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
2649 mDispatcher->notifyKey(&keyArgs);
2650 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
2651 mDispatcher->notifyKey(&keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002652 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08002653
2654 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08002655 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08002656 } else {
2657 mFakePolicy->assertFilterInputEventWasNotCalled();
2658 }
2659 }
2660};
2661
2662// Test InputFilter for MotionEvent
2663TEST_F(InputFilterTest, MotionEvent_InputFilter) {
2664 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
2665 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
2666 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
2667
2668 // Enable InputFilter
2669 mDispatcher->setInputFilterEnabled(true);
2670 // Test touch on both primary and second display, and check if both events are filtered.
2671 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
2672 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
2673
2674 // Disable InputFilter
2675 mDispatcher->setInputFilterEnabled(false);
2676 // Test touch on both primary and second display, and check if both events aren't filtered.
2677 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
2678 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
2679}
2680
2681// Test InputFilter for KeyEvent
2682TEST_F(InputFilterTest, KeyEvent_InputFilter) {
2683 // Since the InputFilter is disabled by default, check if key event aren't filtered.
2684 testNotifyKey(/*expectToBeFiltered*/ false);
2685
2686 // Enable InputFilter
2687 mDispatcher->setInputFilterEnabled(true);
2688 // Send a key event, and check if it is filtered.
2689 testNotifyKey(/*expectToBeFiltered*/ true);
2690
2691 // Disable InputFilter
2692 mDispatcher->setInputFilterEnabled(false);
2693 // Send a key event, and check if it isn't filtered.
2694 testNotifyKey(/*expectToBeFiltered*/ false);
2695}
2696
chaviwfd6d3512019-03-25 13:23:49 -07002697class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002698 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07002699 InputDispatcherTest::SetUp();
2700
Chris Yea209fde2020-07-22 13:54:51 -07002701 std::shared_ptr<FakeApplicationHandle> application =
2702 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002703 mUnfocusedWindow =
2704 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07002705 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
2706 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
2707 // window.
Michael Wright44753b12020-07-08 13:48:11 +01002708 mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07002709
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002710 mFocusedWindow =
2711 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2712 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01002713 mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07002714
2715 // Set focused application.
2716 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002717 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07002718
2719 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00002720 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002721 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002722 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07002723 }
2724
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002725 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07002726 InputDispatcherTest::TearDown();
2727
2728 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002729 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07002730 }
2731
2732protected:
2733 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002734 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002735 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07002736};
2737
2738// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
2739// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
2740// the onPointerDownOutsideFocus callback.
2741TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002742 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002743 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2744 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002745 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002746 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07002747
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002748 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07002749 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
2750}
2751
2752// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
2753// DOWN on the window that doesn't have focus. Ensure no window received the
2754// onPointerDownOutsideFocus callback.
2755TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002756 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002757 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002758 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002759 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07002760
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002761 ASSERT_TRUE(mDispatcher->waitForIdle());
2762 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07002763}
2764
2765// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
2766// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
2767TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002768 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2769 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002770 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07002771
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002772 ASSERT_TRUE(mDispatcher->waitForIdle());
2773 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07002774}
2775
2776// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
2777// DOWN on the window that already has focus. Ensure no window received the
2778// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002779TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002780 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002781 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002782 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002783 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002784 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07002785
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002786 ASSERT_TRUE(mDispatcher->waitForIdle());
2787 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07002788}
2789
chaviwaf87b3e2019-10-01 16:59:28 -07002790// These tests ensures we can send touch events to a single client when there are multiple input
2791// windows that point to the same client token.
2792class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
2793 virtual void SetUp() override {
2794 InputDispatcherTest::SetUp();
2795
Chris Yea209fde2020-07-22 13:54:51 -07002796 std::shared_ptr<FakeApplicationHandle> application =
2797 std::make_shared<FakeApplicationHandle>();
chaviwaf87b3e2019-10-01 16:59:28 -07002798 mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1",
2799 ADISPLAY_ID_DEFAULT);
2800 // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window.
2801 // 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 +01002802 mWindow1->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
2803 InputWindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07002804 mWindow1->setFrame(Rect(0, 0, 100, 100));
2805
2806 mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2",
2807 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
Michael Wright44753b12020-07-08 13:48:11 +01002808 mWindow2->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
2809 InputWindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07002810 mWindow2->setFrame(Rect(100, 100, 200, 200));
2811
Arthur Hung72d8dc32020-03-28 00:48:39 +00002812 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07002813 }
2814
2815protected:
2816 sp<FakeWindowHandle> mWindow1;
2817 sp<FakeWindowHandle> mWindow2;
2818
2819 // Helper function to convert the point from screen coordinates into the window's space
2820 static PointF getPointInWindow(const InputWindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07002821 vec2 vals = windowInfo->transform.transform(point.x, point.y);
2822 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07002823 }
2824
2825 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
2826 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002827 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07002828 InputEvent* event = window->consume();
2829
2830 ASSERT_NE(nullptr, event) << name.c_str()
2831 << ": consumer should have returned non-NULL event.";
2832
2833 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
2834 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
2835 << " event, got " << inputEventTypeToString(event->getType()) << " event";
2836
2837 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
2838 EXPECT_EQ(expectedAction, motionEvent.getAction());
2839
2840 for (size_t i = 0; i < points.size(); i++) {
2841 float expectedX = points[i].x;
2842 float expectedY = points[i].y;
2843
2844 EXPECT_EQ(expectedX, motionEvent.getX(i))
2845 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
2846 << ", got " << motionEvent.getX(i);
2847 EXPECT_EQ(expectedY, motionEvent.getY(i))
2848 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
2849 << ", got " << motionEvent.getY(i);
2850 }
2851 }
chaviw9eaa22c2020-07-01 16:21:27 -07002852
2853 void touchAndAssertPositions(int32_t action, std::vector<PointF> touchedPoints,
2854 std::vector<PointF> expectedPoints) {
2855 NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
2856 ADISPLAY_ID_DEFAULT, touchedPoints);
2857 mDispatcher->notifyMotion(&motionArgs);
2858
2859 // Always consume from window1 since it's the window that has the InputReceiver
2860 consumeMotionEvent(mWindow1, action, expectedPoints);
2861 }
chaviwaf87b3e2019-10-01 16:59:28 -07002862};
2863
2864TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
2865 // Touch Window 1
2866 PointF touchedPoint = {10, 10};
2867 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07002868 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07002869
2870 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07002871 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07002872
2873 // Touch Window 2
2874 touchedPoint = {150, 150};
2875 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07002876 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07002877}
2878
chaviw9eaa22c2020-07-01 16:21:27 -07002879TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
2880 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07002881 mWindow2->setWindowScale(0.5f, 0.5f);
2882
2883 // Touch Window 1
2884 PointF touchedPoint = {10, 10};
2885 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07002886 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07002887 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07002888 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07002889
2890 // Touch Window 2
2891 touchedPoint = {150, 150};
2892 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07002893 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
2894 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07002895
chaviw9eaa22c2020-07-01 16:21:27 -07002896 // Update the transform so rotation is set
2897 mWindow2->setWindowTransform(0, -1, 1, 0);
2898 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
2899 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07002900}
2901
chaviw9eaa22c2020-07-01 16:21:27 -07002902TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002903 mWindow2->setWindowScale(0.5f, 0.5f);
2904
2905 // Touch Window 1
2906 std::vector<PointF> touchedPoints = {PointF{10, 10}};
2907 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07002908 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002909
2910 // Touch Window 2
2911 int32_t actionPointerDown =
2912 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07002913 touchedPoints.push_back(PointF{150, 150});
2914 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
2915 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002916
chaviw9eaa22c2020-07-01 16:21:27 -07002917 // Release Window 2
2918 int32_t actionPointerUp =
2919 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
2920 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
2921 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002922
chaviw9eaa22c2020-07-01 16:21:27 -07002923 // Update the transform so rotation is set for Window 2
2924 mWindow2->setWindowTransform(0, -1, 1, 0);
2925 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
2926 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002927}
2928
chaviw9eaa22c2020-07-01 16:21:27 -07002929TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002930 mWindow2->setWindowScale(0.5f, 0.5f);
2931
2932 // Touch Window 1
2933 std::vector<PointF> touchedPoints = {PointF{10, 10}};
2934 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07002935 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002936
2937 // Touch Window 2
2938 int32_t actionPointerDown =
2939 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07002940 touchedPoints.push_back(PointF{150, 150});
2941 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002942
chaviw9eaa22c2020-07-01 16:21:27 -07002943 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002944
2945 // Move both windows
2946 touchedPoints = {{20, 20}, {175, 175}};
2947 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
2948 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
2949
chaviw9eaa22c2020-07-01 16:21:27 -07002950 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002951
chaviw9eaa22c2020-07-01 16:21:27 -07002952 // Release Window 2
2953 int32_t actionPointerUp =
2954 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
2955 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
2956 expectedPoints.pop_back();
2957
2958 // Touch Window 2
2959 mWindow2->setWindowTransform(0, -1, 1, 0);
2960 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
2961 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
2962
2963 // Move both windows
2964 touchedPoints = {{20, 20}, {175, 175}};
2965 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
2966 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
2967
2968 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002969}
2970
2971TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
2972 mWindow1->setWindowScale(0.5f, 0.5f);
2973
2974 // Touch Window 1
2975 std::vector<PointF> touchedPoints = {PointF{10, 10}};
2976 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07002977 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002978
2979 // Touch Window 2
2980 int32_t actionPointerDown =
2981 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07002982 touchedPoints.push_back(PointF{150, 150});
2983 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002984
chaviw9eaa22c2020-07-01 16:21:27 -07002985 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002986
2987 // Move both windows
2988 touchedPoints = {{20, 20}, {175, 175}};
2989 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
2990 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
2991
chaviw9eaa22c2020-07-01 16:21:27 -07002992 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002993}
2994
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002995class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
2996 virtual void SetUp() override {
2997 InputDispatcherTest::SetUp();
2998
Chris Yea209fde2020-07-22 13:54:51 -07002999 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003000 mApplication->setDispatchingTimeout(20ms);
3001 mWindow =
3002 new FakeWindowHandle(mApplication, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
3003 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05003004 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07003005 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003006 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3007 // window.
Michael Wright44753b12020-07-08 13:48:11 +01003008 mWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003009
3010 // Set focused application.
3011 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
3012
3013 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003014 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003015 mWindow->consumeFocusEvent(true);
3016 }
3017
3018 virtual void TearDown() override {
3019 InputDispatcherTest::TearDown();
3020 mWindow.clear();
3021 }
3022
3023protected:
Chris Yea209fde2020-07-22 13:54:51 -07003024 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003025 sp<FakeWindowHandle> mWindow;
3026 static constexpr PointF WINDOW_LOCATION = {20, 20};
3027
3028 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003029 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003030 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3031 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003032 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003033 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3034 WINDOW_LOCATION));
3035 }
3036};
3037
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003038// Send a tap and respond, which should not cause an ANR.
3039TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
3040 tapOnWindow();
3041 mWindow->consumeMotionDown();
3042 mWindow->consumeMotionUp();
3043 ASSERT_TRUE(mDispatcher->waitForIdle());
3044 mFakePolicy->assertNotifyAnrWasNotCalled();
3045}
3046
3047// Send a regular key and respond, which should not cause an ANR.
3048TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003049 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003050 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
3051 ASSERT_TRUE(mDispatcher->waitForIdle());
3052 mFakePolicy->assertNotifyAnrWasNotCalled();
3053}
3054
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003055TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
3056 mWindow->setFocusable(false);
3057 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3058 mWindow->consumeFocusEvent(false);
3059
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003060 InputEventInjectionResult result =
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003061 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003062 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
3063 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003064 // Key will not go to window because we have no focused window.
3065 // The 'no focused window' ANR timer should start instead.
3066
3067 // Now, the focused application goes away.
3068 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
3069 // The key should get dropped and there should be no ANR.
3070
3071 ASSERT_TRUE(mDispatcher->waitForIdle());
3072 mFakePolicy->assertNotifyAnrWasNotCalled();
3073}
3074
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003075// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003076// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
3077// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003078TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003079 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003080 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3081 WINDOW_LOCATION));
3082
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003083 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
3084 ASSERT_TRUE(sequenceNum);
3085 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003086 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003087
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003088 mWindow->finishEvent(*sequenceNum);
3089 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3090 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003091 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003092 mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003093}
3094
3095// Send a key to the app and have the app not respond right away.
3096TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
3097 // Inject a key, and don't respond - expect that ANR is called.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003098 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003099 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
3100 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003101 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003102 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003103 ASSERT_TRUE(mDispatcher->waitForIdle());
3104}
3105
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003106// We have a focused application, but no focused window
3107TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003108 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003109 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3110 mWindow->consumeFocusEvent(false);
3111
3112 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003113 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003114 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3115 WINDOW_LOCATION));
3116 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
3117 mDispatcher->waitForIdle();
3118 mFakePolicy->assertNotifyAnrWasNotCalled();
3119
3120 // Once a focused event arrives, we get an ANR for this application
3121 // We specify the injection timeout to be smaller than the application timeout, to ensure that
3122 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003123 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003124 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003125 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3126 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003127 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003128 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003129 ASSERT_TRUE(mDispatcher->waitForIdle());
3130}
3131
3132// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003133// Make sure that we don't notify policy twice about the same ANR.
3134TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003135 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003136 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3137 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003138
3139 // Once a focused event arrives, we get an ANR for this application
3140 // We specify the injection timeout to be smaller than the application timeout, to ensure that
3141 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003142 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003143 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003144 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3145 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003146 const std::chrono::duration appTimeout =
3147 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003148 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003149
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003150 std::this_thread::sleep_for(appTimeout);
3151 // ANR should not be raised again. It is up to policy to do that if it desires.
3152 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003153
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003154 // If we now get a focused window, the ANR should stop, but the policy handles that via
3155 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003156 ASSERT_TRUE(mDispatcher->waitForIdle());
3157}
3158
3159// We have a focused application, but no focused window
3160TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003161 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003162 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3163 mWindow->consumeFocusEvent(false);
3164
3165 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003166 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003167 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003168 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3169 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003170
3171 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003172 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003173
3174 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003175 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003176 ASSERT_TRUE(mDispatcher->waitForIdle());
3177 mWindow->assertNoEvents();
3178}
3179
3180/**
3181 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
3182 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
3183 * If we process 1 of the events, but ANR on the second event with the same timestamp,
3184 * the ANR mechanism should still work.
3185 *
3186 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
3187 * DOWN event, while not responding on the second one.
3188 */
3189TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
3190 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
3191 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3192 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
3193 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
3194 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003195 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003196
3197 // Now send ACTION_UP, with identical timestamp
3198 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3199 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
3200 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
3201 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003202 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003203
3204 // We have now sent down and up. Let's consume first event and then ANR on the second.
3205 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3206 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003207 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003208}
3209
3210// If an app is not responding to a key event, gesture monitors should continue to receive
3211// new motion events
3212TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnKey) {
3213 FakeMonitorReceiver monitor =
3214 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
3215 true /*isGestureMonitor*/);
3216
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003217 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3218 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003219 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003220 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003221
3222 // Stuck on the ACTION_UP
3223 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003224 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003225
3226 // New tap will go to the gesture monitor, but not to the window
3227 tapOnWindow();
3228 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3229 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3230
3231 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
3232 mDispatcher->waitForIdle();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003233 mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003234 mWindow->assertNoEvents();
3235 monitor.assertNoEvents();
3236}
3237
3238// If an app is not responding to a motion event, gesture monitors should continue to receive
3239// new motion events
3240TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnMotion) {
3241 FakeMonitorReceiver monitor =
3242 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
3243 true /*isGestureMonitor*/);
3244
3245 tapOnWindow();
3246 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3247 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3248
3249 mWindow->consumeMotionDown();
3250 // Stuck on the ACTION_UP
3251 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003252 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003253
3254 // New tap will go to the gesture monitor, but not to the window
3255 tapOnWindow();
3256 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3257 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3258
3259 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
3260 mDispatcher->waitForIdle();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003261 mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003262 mWindow->assertNoEvents();
3263 monitor.assertNoEvents();
3264}
3265
3266// If a window is unresponsive, then you get anr. if the window later catches up and starts to
3267// process events, you don't get an anr. When the window later becomes unresponsive again, you
3268// get an ANR again.
3269// 1. tap -> block on ACTION_UP -> receive ANR
3270// 2. consume all pending events (= queue becomes healthy again)
3271// 3. tap again -> block on ACTION_UP again -> receive ANR second time
3272TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
3273 tapOnWindow();
3274
3275 mWindow->consumeMotionDown();
3276 // Block on ACTION_UP
3277 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003278 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003279 mWindow->consumeMotionUp(); // Now the connection should be healthy again
3280 mDispatcher->waitForIdle();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003281 mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003282 mWindow->assertNoEvents();
3283
3284 tapOnWindow();
3285 mWindow->consumeMotionDown();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003286 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003287 mWindow->consumeMotionUp();
3288
3289 mDispatcher->waitForIdle();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003290 mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mWindow->getToken());
3291 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003292 mWindow->assertNoEvents();
3293}
3294
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003295// If a connection remains unresponsive for a while, make sure policy is only notified once about
3296// it.
3297TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003298 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003299 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3300 WINDOW_LOCATION));
3301
3302 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003303 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(windowTimeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003304 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003305 // 'notifyConnectionUnresponsive' should only be called once per connection
3306 mFakePolicy->assertNotifyAnrWasNotCalled();
3307 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003308 mWindow->consumeMotionDown();
3309 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3310 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3311 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003312 mDispatcher->waitForIdle();
3313 mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mWindow->getToken());
3314 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003315}
3316
3317/**
3318 * If a window is processing a motion event, and then a key event comes in, the key event should
3319 * not to to the focused window until the motion is processed.
3320 *
3321 * Warning!!!
3322 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
3323 * and the injection timeout that we specify when injecting the key.
3324 * We must have the injection timeout (10ms) be smaller than
3325 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
3326 *
3327 * If that value changes, this test should also change.
3328 */
3329TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
3330 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3331 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3332
3333 tapOnWindow();
3334 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3335 ASSERT_TRUE(downSequenceNum);
3336 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3337 ASSERT_TRUE(upSequenceNum);
3338 // Don't finish the events yet, and send a key
3339 // Injection will "succeed" because we will eventually give up and send the key to the focused
3340 // window even if motions are still being processed. But because the injection timeout is short,
3341 // we will receive INJECTION_TIMED_OUT as the result.
3342
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003343 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003344 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003345 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3346 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003347 // Key will not be sent to the window, yet, because the window is still processing events
3348 // and the key remains pending, waiting for the touch events to be processed
3349 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3350 ASSERT_FALSE(keySequenceNum);
3351
3352 std::this_thread::sleep_for(500ms);
3353 // if we wait long enough though, dispatcher will give up, and still send the key
3354 // to the focused window, even though we have not yet finished the motion event
3355 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3356 mWindow->finishEvent(*downSequenceNum);
3357 mWindow->finishEvent(*upSequenceNum);
3358}
3359
3360/**
3361 * If a window is processing a motion event, and then a key event comes in, the key event should
3362 * not go to the focused window until the motion is processed.
3363 * If then a new motion comes in, then the pending key event should be going to the currently
3364 * focused window right away.
3365 */
3366TEST_F(InputDispatcherSingleWindowAnr,
3367 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
3368 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3369 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3370
3371 tapOnWindow();
3372 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3373 ASSERT_TRUE(downSequenceNum);
3374 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3375 ASSERT_TRUE(upSequenceNum);
3376 // Don't finish the events yet, and send a key
3377 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003378 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003379 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003380 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003381 // At this point, key is still pending, and should not be sent to the application yet.
3382 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3383 ASSERT_FALSE(keySequenceNum);
3384
3385 // Now tap down again. It should cause the pending key to go to the focused window right away.
3386 tapOnWindow();
3387 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
3388 // the other events yet. We can finish events in any order.
3389 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
3390 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
3391 mWindow->consumeMotionDown();
3392 mWindow->consumeMotionUp();
3393 mWindow->assertNoEvents();
3394}
3395
3396class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
3397 virtual void SetUp() override {
3398 InputDispatcherTest::SetUp();
3399
Chris Yea209fde2020-07-22 13:54:51 -07003400 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003401 mApplication->setDispatchingTimeout(10ms);
3402 mUnfocusedWindow =
3403 new FakeWindowHandle(mApplication, mDispatcher, "Unfocused", ADISPLAY_ID_DEFAULT);
3404 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
3405 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3406 // window.
3407 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Michael Wright44753b12020-07-08 13:48:11 +01003408 mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3409 InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH |
3410 InputWindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003411
3412 mFocusedWindow =
3413 new FakeWindowHandle(mApplication, mDispatcher, "Focused", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05003414 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003415 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01003416 mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3417 InputWindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003418
3419 // Set focused application.
3420 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07003421 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003422
3423 // Expect one focus window exist in display.
3424 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003425 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003426 mFocusedWindow->consumeFocusEvent(true);
3427 }
3428
3429 virtual void TearDown() override {
3430 InputDispatcherTest::TearDown();
3431
3432 mUnfocusedWindow.clear();
3433 mFocusedWindow.clear();
3434 }
3435
3436protected:
Chris Yea209fde2020-07-22 13:54:51 -07003437 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003438 sp<FakeWindowHandle> mUnfocusedWindow;
3439 sp<FakeWindowHandle> mFocusedWindow;
3440 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
3441 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
3442 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
3443
3444 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
3445
3446 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
3447
3448private:
3449 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003450 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003451 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3452 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003453 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003454 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3455 location));
3456 }
3457};
3458
3459// If we have 2 windows that are both unresponsive, the one with the shortest timeout
3460// should be ANR'd first.
3461TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003462 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003463 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3464 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003465 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003466 mFocusedWindow->consumeMotionDown();
3467 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3468 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3469 // We consumed all events, so no ANR
3470 ASSERT_TRUE(mDispatcher->waitForIdle());
3471 mFakePolicy->assertNotifyAnrWasNotCalled();
3472
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003473 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003474 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3475 FOCUSED_WINDOW_LOCATION));
3476 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
3477 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003478
3479 const std::chrono::duration timeout =
3480 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003481 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
3482 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
3483 // sequence to make it consistent
3484 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003485 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003486 mFocusedWindow->consumeMotionDown();
3487 // This cancel is generated because the connection was unresponsive
3488 mFocusedWindow->consumeMotionCancel();
3489 mFocusedWindow->assertNoEvents();
3490 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003491 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003492 mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mFocusedWindow->getToken());
3493 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003494}
3495
3496// If we have 2 windows with identical timeouts that are both unresponsive,
3497// it doesn't matter which order they should have ANR.
3498// But we should receive ANR for both.
3499TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
3500 // Set the timeout for unfocused window to match the focused window
3501 mUnfocusedWindow->setDispatchingTimeout(10ms);
3502 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
3503
3504 tapOnFocusedWindow();
3505 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003506 sp<IBinder> anrConnectionToken1 = mFakePolicy->getUnresponsiveConnectionToken(10ms);
3507 sp<IBinder> anrConnectionToken2 = mFakePolicy->getUnresponsiveConnectionToken(0ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003508
3509 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003510 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
3511 mFocusedWindow->getToken() == anrConnectionToken2);
3512 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
3513 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003514
3515 ASSERT_TRUE(mDispatcher->waitForIdle());
3516 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003517
3518 mFocusedWindow->consumeMotionDown();
3519 mFocusedWindow->consumeMotionUp();
3520 mUnfocusedWindow->consumeMotionOutside();
3521
3522 sp<IBinder> responsiveToken1 = mFakePolicy->getResponsiveConnectionToken();
3523 sp<IBinder> responsiveToken2 = mFakePolicy->getResponsiveConnectionToken();
3524
3525 // Both applications should be marked as responsive, in any order
3526 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
3527 mFocusedWindow->getToken() == responsiveToken2);
3528 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
3529 mUnfocusedWindow->getToken() == responsiveToken2);
3530 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003531}
3532
3533// If a window is already not responding, the second tap on the same window should be ignored.
3534// We should also log an error to account for the dropped event (not tested here).
3535// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
3536TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
3537 tapOnFocusedWindow();
3538 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3539 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3540 // Receive the events, but don't respond
3541 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
3542 ASSERT_TRUE(downEventSequenceNum);
3543 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
3544 ASSERT_TRUE(upEventSequenceNum);
3545 const std::chrono::duration timeout =
3546 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003547 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003548
3549 // Tap once again
3550 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003551 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003552 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3553 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003554 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003555 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3556 FOCUSED_WINDOW_LOCATION));
3557 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
3558 // valid touch target
3559 mUnfocusedWindow->assertNoEvents();
3560
3561 // Consume the first tap
3562 mFocusedWindow->finishEvent(*downEventSequenceNum);
3563 mFocusedWindow->finishEvent(*upEventSequenceNum);
3564 ASSERT_TRUE(mDispatcher->waitForIdle());
3565 // The second tap did not go to the focused window
3566 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003567 // Since all events are finished, connection should be deemed healthy again
3568 mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003569 mFakePolicy->assertNotifyAnrWasNotCalled();
3570}
3571
3572// If you tap outside of all windows, there will not be ANR
3573TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003574 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003575 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3576 LOCATION_OUTSIDE_ALL_WINDOWS));
3577 ASSERT_TRUE(mDispatcher->waitForIdle());
3578 mFakePolicy->assertNotifyAnrWasNotCalled();
3579}
3580
3581// Since the focused window is paused, tapping on it should not produce any events
3582TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
3583 mFocusedWindow->setPaused(true);
3584 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
3585
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003586 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003587 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3588 FOCUSED_WINDOW_LOCATION));
3589
3590 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
3591 ASSERT_TRUE(mDispatcher->waitForIdle());
3592 // Should not ANR because the window is paused, and touches shouldn't go to it
3593 mFakePolicy->assertNotifyAnrWasNotCalled();
3594
3595 mFocusedWindow->assertNoEvents();
3596 mUnfocusedWindow->assertNoEvents();
3597}
3598
3599/**
3600 * If a window is processing a motion event, and then a key event comes in, the key event should
3601 * not to to the focused window until the motion is processed.
3602 * If a different window becomes focused at this time, the key should go to that window instead.
3603 *
3604 * Warning!!!
3605 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
3606 * and the injection timeout that we specify when injecting the key.
3607 * We must have the injection timeout (10ms) be smaller than
3608 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
3609 *
3610 * If that value changes, this test should also change.
3611 */
3612TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
3613 // Set a long ANR timeout to prevent it from triggering
3614 mFocusedWindow->setDispatchingTimeout(2s);
3615 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
3616
3617 tapOnUnfocusedWindow();
3618 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
3619 ASSERT_TRUE(downSequenceNum);
3620 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
3621 ASSERT_TRUE(upSequenceNum);
3622 // Don't finish the events yet, and send a key
3623 // Injection will succeed because we will eventually give up and send the key to the focused
3624 // window even if motions are still being processed.
3625
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003626 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003627 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003628 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
3629 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003630 // Key will not be sent to the window, yet, because the window is still processing events
3631 // and the key remains pending, waiting for the touch events to be processed
3632 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
3633 ASSERT_FALSE(keySequenceNum);
3634
3635 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07003636 mFocusedWindow->setFocusable(false);
3637 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003638 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003639 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003640
3641 // Focus events should precede the key events
3642 mUnfocusedWindow->consumeFocusEvent(true);
3643 mFocusedWindow->consumeFocusEvent(false);
3644
3645 // Finish the tap events, which should unblock dispatcher
3646 mUnfocusedWindow->finishEvent(*downSequenceNum);
3647 mUnfocusedWindow->finishEvent(*upSequenceNum);
3648
3649 // Now that all queues are cleared and no backlog in the connections, the key event
3650 // can finally go to the newly focused "mUnfocusedWindow".
3651 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3652 mFocusedWindow->assertNoEvents();
3653 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003654 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003655}
3656
3657// When the touch stream is split across 2 windows, and one of them does not respond,
3658// then ANR should be raised and the touch should be canceled for the unresponsive window.
3659// The other window should not be affected by that.
3660TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
3661 // Touch Window 1
3662 NotifyMotionArgs motionArgs =
3663 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3664 ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION});
3665 mDispatcher->notifyMotion(&motionArgs);
3666 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3667 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3668
3669 // Touch Window 2
3670 int32_t actionPointerDown =
3671 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3672
3673 motionArgs =
3674 generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3675 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION});
3676 mDispatcher->notifyMotion(&motionArgs);
3677
3678 const std::chrono::duration timeout =
3679 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003680 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003681
3682 mUnfocusedWindow->consumeMotionDown();
3683 mFocusedWindow->consumeMotionDown();
3684 // Focused window may or may not receive ACTION_MOVE
3685 // But it should definitely receive ACTION_CANCEL due to the ANR
3686 InputEvent* event;
3687 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
3688 ASSERT_TRUE(moveOrCancelSequenceNum);
3689 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
3690 ASSERT_NE(nullptr, event);
3691 ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION);
3692 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
3693 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
3694 mFocusedWindow->consumeMotionCancel();
3695 } else {
3696 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
3697 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003698 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003699 mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mFocusedWindow->getToken());
3700
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003701 mUnfocusedWindow->assertNoEvents();
3702 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003703 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003704}
3705
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05003706/**
3707 * If we have no focused window, and a key comes in, we start the ANR timer.
3708 * The focused application should add a focused window before the timer runs out to prevent ANR.
3709 *
3710 * If the user touches another application during this time, the key should be dropped.
3711 * Next, if a new focused window comes in, without toggling the focused application,
3712 * then no ANR should occur.
3713 *
3714 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
3715 * but in some cases the policy may not update the focused application.
3716 */
3717TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
3718 std::shared_ptr<FakeApplicationHandle> focusedApplication =
3719 std::make_shared<FakeApplicationHandle>();
3720 focusedApplication->setDispatchingTimeout(60ms);
3721 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
3722 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
3723 mFocusedWindow->setFocusable(false);
3724
3725 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
3726 mFocusedWindow->consumeFocusEvent(false);
3727
3728 // Send a key. The ANR timer should start because there is no focused window.
3729 // 'focusedApplication' will get blamed if this timer completes.
3730 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003731 InputEventInjectionResult result =
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05003732 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003733 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
3734 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05003735
3736 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
3737 // then the injected touches won't cause the focused event to get dropped.
3738 // The dispatcher only checks for whether the queue should be pruned upon queueing.
3739 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
3740 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
3741 // For this test, it means that the key would get delivered to the window once it becomes
3742 // focused.
3743 std::this_thread::sleep_for(10ms);
3744
3745 // Touch unfocused window. This should force the pending key to get dropped.
3746 NotifyMotionArgs motionArgs =
3747 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3748 ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION});
3749 mDispatcher->notifyMotion(&motionArgs);
3750
3751 // We do not consume the motion right away, because that would require dispatcher to first
3752 // process (== drop) the key event, and by that time, ANR will be raised.
3753 // Set the focused window first.
3754 mFocusedWindow->setFocusable(true);
3755 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
3756 setFocusedWindow(mFocusedWindow);
3757 mFocusedWindow->consumeFocusEvent(true);
3758 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
3759 // to another application. This could be a bug / behaviour in the policy.
3760
3761 mUnfocusedWindow->consumeMotionDown();
3762
3763 ASSERT_TRUE(mDispatcher->waitForIdle());
3764 // Should not ANR because we actually have a focused window. It was just added too slowly.
3765 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
3766}
3767
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05003768// These tests ensure we cannot send touch events to a window that's positioned behind a window
3769// that has feature NO_INPUT_CHANNEL.
3770// Layout:
3771// Top (closest to user)
3772// mNoInputWindow (above all windows)
3773// mBottomWindow
3774// Bottom (furthest from user)
3775class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
3776 virtual void SetUp() override {
3777 InputDispatcherTest::SetUp();
3778
3779 mApplication = std::make_shared<FakeApplicationHandle>();
3780 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
3781 "Window without input channel", ADISPLAY_ID_DEFAULT,
3782 std::make_optional<sp<IBinder>>(nullptr) /*token*/);
3783
3784 mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL);
3785 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
3786 // It's perfectly valid for this window to not have an associated input channel
3787
3788 mBottomWindow = new FakeWindowHandle(mApplication, mDispatcher, "Bottom window",
3789 ADISPLAY_ID_DEFAULT);
3790 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
3791
3792 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
3793 }
3794
3795protected:
3796 std::shared_ptr<FakeApplicationHandle> mApplication;
3797 sp<FakeWindowHandle> mNoInputWindow;
3798 sp<FakeWindowHandle> mBottomWindow;
3799};
3800
3801TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
3802 PointF touchedPoint = {10, 10};
3803
3804 NotifyMotionArgs motionArgs =
3805 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3806 ADISPLAY_ID_DEFAULT, {touchedPoint});
3807 mDispatcher->notifyMotion(&motionArgs);
3808
3809 mNoInputWindow->assertNoEvents();
3810 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
3811 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
3812 // and therefore should prevent mBottomWindow from receiving touches
3813 mBottomWindow->assertNoEvents();
3814}
3815
3816/**
3817 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
3818 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
3819 */
3820TEST_F(InputDispatcherMultiWindowOcclusionTests,
3821 NoInputChannelFeature_DropsTouchesWithValidChannel) {
3822 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
3823 "Window with input channel and NO_INPUT_CHANNEL",
3824 ADISPLAY_ID_DEFAULT);
3825
3826 mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL);
3827 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
3828 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
3829
3830 PointF touchedPoint = {10, 10};
3831
3832 NotifyMotionArgs motionArgs =
3833 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3834 ADISPLAY_ID_DEFAULT, {touchedPoint});
3835 mDispatcher->notifyMotion(&motionArgs);
3836
3837 mNoInputWindow->assertNoEvents();
3838 mBottomWindow->assertNoEvents();
3839}
3840
Vishnu Nair958da932020-08-21 17:12:37 -07003841class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
3842protected:
3843 std::shared_ptr<FakeApplicationHandle> mApp;
3844 sp<FakeWindowHandle> mWindow;
3845 sp<FakeWindowHandle> mMirror;
3846
3847 virtual void SetUp() override {
3848 InputDispatcherTest::SetUp();
3849 mApp = std::make_shared<FakeApplicationHandle>();
3850 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
3851 mMirror = new FakeWindowHandle(mApp, mDispatcher, "TestWindowMirror", ADISPLAY_ID_DEFAULT,
3852 mWindow->getToken());
3853 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
3854 mWindow->setFocusable(true);
3855 mMirror->setFocusable(true);
3856 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
3857 }
3858};
3859
3860TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
3861 // Request focus on a mirrored window
3862 setFocusedWindow(mMirror);
3863
3864 // window gets focused
3865 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003866 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3867 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003868 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
3869}
3870
3871// A focused & mirrored window remains focused only if the window and its mirror are both
3872// focusable.
3873TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
3874 setFocusedWindow(mMirror);
3875
3876 // window gets focused
3877 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003878 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3879 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003880 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003881 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
3882 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003883 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
3884
3885 mMirror->setFocusable(false);
3886 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
3887
3888 // window loses focus since one of the windows associated with the token in not focusable
3889 mWindow->consumeFocusEvent(false);
3890
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003891 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
3892 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07003893 mWindow->assertNoEvents();
3894}
3895
3896// A focused & mirrored window remains focused until the window and its mirror both become
3897// invisible.
3898TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
3899 setFocusedWindow(mMirror);
3900
3901 // window gets focused
3902 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003903 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3904 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003905 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003906 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
3907 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003908 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
3909
3910 mMirror->setVisible(false);
3911 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
3912
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003913 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3914 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003915 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003916 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
3917 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003918 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
3919
3920 mWindow->setVisible(false);
3921 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
3922
3923 // window loses focus only after all windows associated with the token become invisible.
3924 mWindow->consumeFocusEvent(false);
3925
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003926 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
3927 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07003928 mWindow->assertNoEvents();
3929}
3930
3931// A focused & mirrored window remains focused until both windows are removed.
3932TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
3933 setFocusedWindow(mMirror);
3934
3935 // window gets focused
3936 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003937 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3938 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003939 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003940 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
3941 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003942 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
3943
3944 // single window is removed but the window token remains focused
3945 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
3946
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003947 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3948 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003949 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003950 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
3951 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003952 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
3953
3954 // Both windows are removed
3955 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
3956 mWindow->consumeFocusEvent(false);
3957
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003958 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
3959 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07003960 mWindow->assertNoEvents();
3961}
3962
3963// Focus request can be pending until one window becomes visible.
3964TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
3965 // Request focus on an invisible mirror.
3966 mWindow->setVisible(false);
3967 mMirror->setVisible(false);
3968 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
3969 setFocusedWindow(mMirror);
3970
3971 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003972 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07003973 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003974 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07003975
3976 mMirror->setVisible(true);
3977 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
3978
3979 // window gets focused
3980 mWindow->consumeFocusEvent(true);
3981 // window gets the pending key event
3982 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3983}
Prabir Pradhan99987712020-11-10 18:43:05 -08003984
3985class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
3986protected:
3987 std::shared_ptr<FakeApplicationHandle> mApp;
3988 sp<FakeWindowHandle> mWindow;
3989 sp<FakeWindowHandle> mSecondWindow;
3990
3991 void SetUp() override {
3992 InputDispatcherTest::SetUp();
3993 mApp = std::make_shared<FakeApplicationHandle>();
3994 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
3995 mWindow->setFocusable(true);
3996 mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
3997 mSecondWindow->setFocusable(true);
3998
3999 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
4000 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
4001
4002 setFocusedWindow(mWindow);
4003 mWindow->consumeFocusEvent(true);
4004 }
4005
4006 void notifyPointerCaptureChanged(bool enabled) {
4007 const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(enabled);
4008 mDispatcher->notifyPointerCaptureChanged(&args);
4009 }
4010
4011 void requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, bool enabled) {
4012 mDispatcher->requestPointerCapture(window->getToken(), enabled);
4013 mFakePolicy->waitForSetPointerCapture(enabled);
4014 notifyPointerCaptureChanged(enabled);
4015 window->consumeCaptureEvent(enabled);
4016 }
4017};
4018
4019TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
4020 // Ensure that capture cannot be obtained for unfocused windows.
4021 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
4022 mFakePolicy->assertSetPointerCaptureNotCalled();
4023 mSecondWindow->assertNoEvents();
4024
4025 // Ensure that capture can be enabled from the focus window.
4026 requestAndVerifyPointerCapture(mWindow, true);
4027
4028 // Ensure that capture cannot be disabled from a window that does not have capture.
4029 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
4030 mFakePolicy->assertSetPointerCaptureNotCalled();
4031
4032 // Ensure that capture can be disabled from the window with capture.
4033 requestAndVerifyPointerCapture(mWindow, false);
4034}
4035
4036TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
4037 requestAndVerifyPointerCapture(mWindow, true);
4038
4039 setFocusedWindow(mSecondWindow);
4040
4041 // Ensure that the capture disabled event was sent first.
4042 mWindow->consumeCaptureEvent(false);
4043 mWindow->consumeFocusEvent(false);
4044 mSecondWindow->consumeFocusEvent(true);
4045 mFakePolicy->waitForSetPointerCapture(false);
4046
4047 // Ensure that additional state changes from InputReader are not sent to the window.
4048 notifyPointerCaptureChanged(false);
4049 notifyPointerCaptureChanged(true);
4050 notifyPointerCaptureChanged(false);
4051 mWindow->assertNoEvents();
4052 mSecondWindow->assertNoEvents();
4053 mFakePolicy->assertSetPointerCaptureNotCalled();
4054}
4055
4056TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
4057 requestAndVerifyPointerCapture(mWindow, true);
4058
4059 // InputReader unexpectedly disables and enables pointer capture.
4060 notifyPointerCaptureChanged(false);
4061 notifyPointerCaptureChanged(true);
4062
4063 // Ensure that Pointer Capture is disabled.
Prabir Pradhan7d030382020-12-21 07:58:35 -08004064 mFakePolicy->waitForSetPointerCapture(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08004065 mWindow->consumeCaptureEvent(false);
4066 mWindow->assertNoEvents();
4067}
4068
Garfield Tane84e6f92019-08-29 17:28:41 -07004069} // namespace android::inputdispatcher