blob: 9d8a1bbdff0d5023d004884627548f014e52fb47 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Garfield Tan0fc2fa72019-08-29 17:22:15 -070017#include "../dispatcher/InputDispatcher.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080018
Garfield Tan1c7bc862020-01-28 13:24:04 -080019#include <android-base/stringprintf.h>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070020#include <android-base/thread_annotations.h>
Robert Carr803535b2018-08-02 16:38:15 -070021#include <binder/Binder.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080022#include <gtest/gtest.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100023#include <input/Input.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080024#include <linux/input.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100025
Garfield Tan1c7bc862020-01-28 13:24:04 -080026#include <cinttypes>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070027#include <thread>
Garfield Tan1c7bc862020-01-28 13:24:04 -080028#include <unordered_set>
chaviwd1c23182019-12-20 18:44:56 -080029#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080030
Garfield Tan1c7bc862020-01-28 13:24:04 -080031using android::base::StringPrintf;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080032using android::os::InputEventInjectionResult;
33using android::os::InputEventInjectionSync;
Michael Wright44753b12020-07-08 13:48:11 +010034using namespace android::flag_operators;
Garfield Tan1c7bc862020-01-28 13:24:04 -080035
Garfield Tane84e6f92019-08-29 17:28:41 -070036namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080037
38// An arbitrary time value.
39static const nsecs_t ARBITRARY_TIME = 1234;
40
41// An arbitrary device id.
42static const int32_t DEVICE_ID = 1;
43
Jeff Brownf086ddb2014-02-11 14:28:48 -080044// An arbitrary display id.
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -080045static const int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
Jeff Brownf086ddb2014-02-11 14:28:48 -080046
Michael Wrightd02c5b62014-02-10 15:10:22 -080047// An arbitrary injector pid / uid pair that has permission to inject events.
48static const int32_t INJECTOR_PID = 999;
49static const int32_t INJECTOR_UID = 1001;
50
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +000051// An arbitrary pid of the gesture monitor window
52static constexpr int32_t MONITOR_PID = 2001;
53
chaviwd1c23182019-12-20 18:44:56 -080054struct PointF {
55 float x;
56 float y;
57};
Michael Wrightd02c5b62014-02-10 15:10:22 -080058
Gang Wang342c9272020-01-13 13:15:04 -050059/**
60 * Return a DOWN key event with KEYCODE_A.
61 */
62static KeyEvent getTestKeyEvent() {
63 KeyEvent event;
64
Garfield Tanfbe732e2020-01-24 11:26:14 -080065 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
66 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
67 ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -050068 return event;
69}
70
Michael Wrightd02c5b62014-02-10 15:10:22 -080071// --- FakeInputDispatcherPolicy ---
72
73class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
74 InputDispatcherConfiguration mConfig;
75
76protected:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100077 virtual ~FakeInputDispatcherPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -080078
79public:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100080 FakeInputDispatcherPolicy() {}
Jackal Guof9696682018-10-05 12:23:23 +080081
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080082 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080083 assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_KEY, args.eventTime, args.action,
84 args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080085 }
86
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080087 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args) {
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080088 assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_MOTION, args.eventTime, args.action,
89 args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080090 }
91
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -070092 void assertFilterInputEventWasNotCalled() {
93 std::scoped_lock lock(mLock);
94 ASSERT_EQ(nullptr, mFilteredEvent);
95 }
Michael Wrightd02c5b62014-02-10 15:10:22 -080096
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -080097 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -070098 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -080099 ASSERT_TRUE(mConfigurationChangedTime)
100 << "Timed out waiting for configuration changed call";
101 ASSERT_EQ(*mConfigurationChangedTime, when);
102 mConfigurationChangedTime = std::nullopt;
103 }
104
105 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700106 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800107 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800108 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800109 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
110 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
111 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
112 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
113 mLastNotifySwitch = std::nullopt;
114 }
115
chaviwfd6d3512019-03-25 13:23:49 -0700116 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700117 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800118 ASSERT_EQ(touchedToken, mOnPointerDownToken);
119 mOnPointerDownToken.clear();
120 }
121
122 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700123 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800124 ASSERT_TRUE(mOnPointerDownToken == nullptr)
125 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700126 }
127
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700128 // This function must be called soon after the expected ANR timer starts,
129 // because we are also checking how much time has passed.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500130 void assertNotifyNoFocusedWindowAnrWasCalled(
Chris Yea209fde2020-07-22 13:54:51 -0700131 std::chrono::nanoseconds timeout,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500132 const std::shared_ptr<InputApplicationHandle>& expectedApplication) {
133 std::shared_ptr<InputApplicationHandle> application;
134 { // acquire lock
135 std::unique_lock lock(mLock);
136 android::base::ScopedLockAssertion assumeLocked(mLock);
137 ASSERT_NO_FATAL_FAILURE(
138 application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock));
139 } // release lock
140 ASSERT_EQ(expectedApplication, application);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700141 }
142
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500143 void assertNotifyConnectionUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
144 const sp<IBinder>& expectedConnectionToken) {
145 sp<IBinder> connectionToken = getUnresponsiveConnectionToken(timeout);
146 ASSERT_EQ(expectedConnectionToken, connectionToken);
147 }
148
149 void assertNotifyConnectionResponsiveWasCalled(const sp<IBinder>& expectedConnectionToken) {
150 sp<IBinder> connectionToken = getResponsiveConnectionToken();
151 ASSERT_EQ(expectedConnectionToken, connectionToken);
152 }
153
154 sp<IBinder> getUnresponsiveConnectionToken(std::chrono::nanoseconds timeout) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700155 std::unique_lock lock(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700156 android::base::ScopedLockAssertion assumeLocked(mLock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500157 return getAnrTokenLockedInterruptible(timeout, mAnrConnectionTokens, lock);
158 }
159
160 sp<IBinder> getResponsiveConnectionToken() {
161 std::unique_lock lock(mLock);
162 android::base::ScopedLockAssertion assumeLocked(mLock);
163 return getAnrTokenLockedInterruptible(0s, mResponsiveConnectionTokens, lock);
164 }
165
166 // All three ANR-related callbacks behave the same way, so we use this generic function to wait
167 // for a specific container to become non-empty. When the container is non-empty, return the
168 // first entry from the container and erase it.
169 template <class T>
170 T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage,
171 std::unique_lock<std::mutex>& lock) REQUIRES(mLock) {
172 const std::chrono::time_point start = std::chrono::steady_clock::now();
173 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700174
175 // If there is an ANR, Dispatcher won't be idle because there are still events
176 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
177 // before checking if ANR was called.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500178 // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need
179 // to provide it some time to act. 100ms seems reasonable.
180 mNotifyAnr.wait_for(lock, timeToWait,
181 [&storage]() REQUIRES(mLock) { return !storage.empty(); });
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700182 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500183 if (storage.empty()) {
184 ADD_FAILURE() << "Did not receive the ANR callback";
185 return nullptr;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700186 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700187 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
188 // the dispatcher started counting before this function was called
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700189 if (std::chrono::abs(timeout - waited) > 100ms) {
190 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
191 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
192 << "ms, but waited "
193 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
194 << "ms instead";
195 }
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500196 T token = storage.front();
197 storage.pop();
198 return token;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700199 }
200
201 void assertNotifyAnrWasNotCalled() {
202 std::scoped_lock lock(mLock);
203 ASSERT_TRUE(mAnrApplications.empty());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500204 ASSERT_TRUE(mAnrConnectionTokens.empty());
205 ASSERT_TRUE(mResponsiveConnectionTokens.empty())
206 << "ANR was not called, but please also consume the 'connection is responsive' "
207 "signal";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700208 }
209
Garfield Tan1c7bc862020-01-28 13:24:04 -0800210 void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) {
211 mConfig.keyRepeatTimeout = timeout;
212 mConfig.keyRepeatDelay = delay;
213 }
214
Prabir Pradhan99987712020-11-10 18:43:05 -0800215 void waitForSetPointerCapture(bool enabled) {
216 std::unique_lock lock(mLock);
217 base::ScopedLockAssertion assumeLocked(mLock);
218
219 if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms,
220 [this, enabled]() REQUIRES(mLock) {
221 return mPointerCaptureEnabled &&
222 *mPointerCaptureEnabled ==
223 enabled;
224 })) {
225 FAIL() << "Timed out waiting for setPointerCapture(" << enabled << ") to be called.";
226 }
227 mPointerCaptureEnabled.reset();
228 }
229
230 void assertSetPointerCaptureNotCalled() {
231 std::unique_lock lock(mLock);
232 base::ScopedLockAssertion assumeLocked(mLock);
233
234 if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) {
235 FAIL() << "Expected setPointerCapture(enabled) to not be called, but was called. "
236 "enabled = "
237 << *mPointerCaptureEnabled;
238 }
239 mPointerCaptureEnabled.reset();
240 }
241
Michael Wrightd02c5b62014-02-10 15:10:22 -0800242private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700243 std::mutex mLock;
244 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
245 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
246 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
247 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800248
Prabir Pradhan99987712020-11-10 18:43:05 -0800249 std::condition_variable mPointerCaptureChangedCondition;
250 std::optional<bool> mPointerCaptureEnabled GUARDED_BY(mLock);
251
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700252 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700253 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500254 std::queue<sp<IBinder>> mAnrConnectionTokens GUARDED_BY(mLock);
255 std::queue<sp<IBinder>> mResponsiveConnectionTokens GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700256 std::condition_variable mNotifyAnr;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700257
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600258 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700259 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800260 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800261 }
262
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500263 void notifyConnectionUnresponsive(const sp<IBinder>& connectionToken,
264 const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700265 std::scoped_lock lock(mLock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500266 mAnrConnectionTokens.push(connectionToken);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700267 mNotifyAnr.notify_all();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500268 }
269
270 void notifyConnectionResponsive(const sp<IBinder>& connectionToken) override {
271 std::scoped_lock lock(mLock);
272 mResponsiveConnectionTokens.push(connectionToken);
273 mNotifyAnr.notify_all();
274 }
275
276 void notifyNoFocusedWindowAnr(
277 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
278 std::scoped_lock lock(mLock);
279 mAnrApplications.push(applicationHandle);
280 mNotifyAnr.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800281 }
282
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600283 void notifyInputChannelBroken(const sp<IBinder>&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800284
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600285 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700286
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600287 void notifyUntrustedTouch(const std::string& obscuringPackage) override {}
Chris Yef59a2f42020-10-16 12:55:26 -0700288 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
289 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
290 const std::vector<float>& values) override {}
291
292 void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType,
293 InputDeviceSensorAccuracy accuracy) override {}
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000294
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600295 void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800296 *outConfig = mConfig;
297 }
298
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600299 bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700300 std::scoped_lock lock(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800301 switch (inputEvent->getType()) {
302 case AINPUT_EVENT_TYPE_KEY: {
303 const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800304 mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800305 break;
306 }
307
308 case AINPUT_EVENT_TYPE_MOTION: {
309 const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800310 mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800311 break;
312 }
313 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800314 return true;
315 }
316
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600317 void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800318
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600319 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800320
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600321 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800322 return 0;
323 }
324
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600325 bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800326 return false;
327 }
328
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600329 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
330 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700331 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800332 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
333 * essentially a passthrough for notifySwitch.
334 */
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800335 mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800336 }
337
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600338 void pokeUserActivity(nsecs_t, int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800339
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600340 bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) override { return false; }
Jackal Guof9696682018-10-05 12:23:23 +0800341
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600342 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700343 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700344 mOnPointerDownToken = newToken;
345 }
346
Prabir Pradhan99987712020-11-10 18:43:05 -0800347 void setPointerCapture(bool enabled) override {
348 std::scoped_lock lock(mLock);
349 mPointerCaptureEnabled = {enabled};
350 mPointerCaptureChangedCondition.notify_all();
351 }
352
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800353 void assertFilterInputEventWasCalled(int type, nsecs_t eventTime, int32_t action,
354 int32_t displayId) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700355 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800356 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
357 ASSERT_EQ(mFilteredEvent->getType(), type);
358
359 if (type == AINPUT_EVENT_TYPE_KEY) {
360 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*mFilteredEvent);
361 EXPECT_EQ(keyEvent.getEventTime(), eventTime);
362 EXPECT_EQ(keyEvent.getAction(), action);
363 EXPECT_EQ(keyEvent.getDisplayId(), displayId);
364 } else if (type == AINPUT_EVENT_TYPE_MOTION) {
365 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*mFilteredEvent);
366 EXPECT_EQ(motionEvent.getEventTime(), eventTime);
367 EXPECT_EQ(motionEvent.getAction(), action);
368 EXPECT_EQ(motionEvent.getDisplayId(), displayId);
369 } else {
370 FAIL() << "Unknown type: " << type;
371 }
372
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800373 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800374 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800375};
376
Michael Wrightd02c5b62014-02-10 15:10:22 -0800377// --- InputDispatcherTest ---
378
379class InputDispatcherTest : public testing::Test {
380protected:
381 sp<FakeInputDispatcherPolicy> mFakePolicy;
382 sp<InputDispatcher> mDispatcher;
383
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700384 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800385 mFakePolicy = new FakeInputDispatcherPolicy();
386 mDispatcher = new InputDispatcher(mFakePolicy);
Arthur Hungb92218b2018-08-14 12:00:21 +0800387 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000388 // Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700389 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800390 }
391
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700392 virtual void TearDown() override {
393 ASSERT_EQ(OK, mDispatcher->stop());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800394 mFakePolicy.clear();
395 mDispatcher.clear();
396 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700397
398 /**
399 * Used for debugging when writing the test
400 */
401 void dumpDispatcherState() {
402 std::string dump;
403 mDispatcher->dump(dump);
404 std::stringstream ss(dump);
405 std::string to;
406
407 while (std::getline(ss, to, '\n')) {
408 ALOGE("%s", to.c_str());
409 }
410 }
Vishnu Nair958da932020-08-21 17:12:37 -0700411
412 void setFocusedWindow(const sp<InputWindowHandle>& window,
413 const sp<InputWindowHandle>& focusedWindow = nullptr) {
414 FocusRequest request;
415 request.token = window->getToken();
416 if (focusedWindow) {
417 request.focusedToken = focusedWindow->getToken();
418 }
419 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
420 request.displayId = window->getInfo()->displayId;
421 mDispatcher->setFocusedWindow(request);
422 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800423};
424
Michael Wrightd02c5b62014-02-10 15:10:22 -0800425TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
426 KeyEvent event;
427
428 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800429 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
430 INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600431 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
432 ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800433 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700434 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800435 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800436 << "Should reject key events with undefined action.";
437
438 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800439 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
440 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600441 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800442 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700443 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800444 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800445 << "Should reject key events with ACTION_MULTIPLE.";
446}
447
448TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
449 MotionEvent event;
450 PointerProperties pointerProperties[MAX_POINTERS + 1];
451 PointerCoords pointerCoords[MAX_POINTERS + 1];
452 for (int i = 0; i <= MAX_POINTERS; i++) {
453 pointerProperties[i].clear();
454 pointerProperties[i].id = i;
455 pointerCoords[i].clear();
456 }
457
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800458 // Some constants commonly used below
459 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
460 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
461 constexpr int32_t metaState = AMETA_NONE;
462 constexpr MotionClassification classification = MotionClassification::NONE;
463
chaviw9eaa22c2020-07-01 16:21:27 -0700464 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800465 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800466 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
chaviw9eaa22c2020-07-01 16:21:27 -0700467 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification,
468 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600469 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700470 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800471 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700472 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800473 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800474 << "Should reject motion events with undefined action.";
475
476 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800477 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700478 AMOTION_EVENT_ACTION_POINTER_DOWN |
479 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700480 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
481 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
482 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
483 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800484 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700485 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800486 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800487 << "Should reject motion events with pointer down index too large.";
488
Garfield Tanfbe732e2020-01-24 11:26:14 -0800489 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700490 AMOTION_EVENT_ACTION_POINTER_DOWN |
491 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700492 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
493 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
494 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
495 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800496 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700497 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800498 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800499 << "Should reject motion events with pointer down index too small.";
500
501 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800502 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700503 AMOTION_EVENT_ACTION_POINTER_UP |
504 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700505 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
506 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
507 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
508 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800509 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700510 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800511 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800512 << "Should reject motion events with pointer up index too large.";
513
Garfield Tanfbe732e2020-01-24 11:26:14 -0800514 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700515 AMOTION_EVENT_ACTION_POINTER_UP |
516 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700517 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
518 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
519 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
520 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800521 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700522 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800523 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800524 << "Should reject motion events with pointer up index too small.";
525
526 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800527 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
528 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700529 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
530 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700531 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800532 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700533 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800534 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800535 << "Should reject motion events with 0 pointers.";
536
Garfield Tanfbe732e2020-01-24 11:26:14 -0800537 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
538 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700539 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
540 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700541 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800542 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700543 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800544 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800545 << "Should reject motion events with more than MAX_POINTERS pointers.";
546
547 // Rejects motion events with invalid pointer ids.
548 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800549 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
550 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700551 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
552 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700553 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800554 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700555 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800556 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800557 << "Should reject motion events with pointer ids less than 0.";
558
559 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800560 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
561 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700562 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
563 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700564 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800565 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700566 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800567 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800568 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
569
570 // Rejects motion events with duplicate pointer ids.
571 pointerProperties[0].id = 1;
572 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800573 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
574 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700575 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
576 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700577 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800578 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700579 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800580 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800581 << "Should reject motion events with duplicate pointer ids.";
582}
583
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800584/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
585
586TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
587 constexpr nsecs_t eventTime = 20;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800588 NotifyConfigurationChangedArgs args(10 /*id*/, eventTime);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800589 mDispatcher->notifyConfigurationChanged(&args);
590 ASSERT_TRUE(mDispatcher->waitForIdle());
591
592 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
593}
594
595TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800596 NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/,
597 2 /*switchMask*/);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800598 mDispatcher->notifySwitch(&args);
599
600 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
601 args.policyFlags |= POLICY_FLAG_TRUSTED;
602 mFakePolicy->assertNotifySwitchWasCalled(args);
603}
604
Arthur Hungb92218b2018-08-14 12:00:21 +0800605// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700606static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700607static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s;
Arthur Hungb92218b2018-08-14 12:00:21 +0800608
609class FakeApplicationHandle : public InputApplicationHandle {
610public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700611 FakeApplicationHandle() {
612 mInfo.name = "Fake Application";
613 mInfo.token = new BBinder();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500614 mInfo.dispatchingTimeoutMillis =
615 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700616 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800617 virtual ~FakeApplicationHandle() {}
618
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000619 virtual bool updateInfo() override { return true; }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700620
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500621 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
622 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700623 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800624};
625
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800626class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800627public:
Garfield Tan15601662020-09-22 15:32:38 -0700628 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800629 : mName(name) {
Garfield Tan15601662020-09-22 15:32:38 -0700630 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800631 }
632
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800633 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700634 InputEvent* event;
635 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
636 if (!consumeSeq) {
637 return nullptr;
638 }
639 finishEvent(*consumeSeq);
640 return event;
641 }
642
643 /**
644 * Receive an event without acknowledging it.
645 * Return the sequence number that could later be used to send finished signal.
646 */
647 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800648 uint32_t consumeSeq;
649 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800650
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800651 std::chrono::time_point start = std::chrono::steady_clock::now();
652 status_t status = WOULD_BLOCK;
653 while (status == WOULD_BLOCK) {
chaviw81e2bb92019-12-18 15:03:51 -0800654 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800655 &event);
656 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
657 if (elapsed > 100ms) {
658 break;
659 }
660 }
661
662 if (status == WOULD_BLOCK) {
663 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700664 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800665 }
666
667 if (status != OK) {
668 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700669 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800670 }
671 if (event == nullptr) {
672 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700673 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800674 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700675 if (outEvent != nullptr) {
676 *outEvent = event;
677 }
678 return consumeSeq;
679 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800680
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700681 /**
682 * To be used together with "receiveEvent" to complete the consumption of an event.
683 */
684 void finishEvent(uint32_t consumeSeq) {
685 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
686 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800687 }
688
689 void consumeEvent(int32_t expectedEventType, int32_t expectedAction, int32_t expectedDisplayId,
690 int32_t expectedFlags) {
691 InputEvent* event = consume();
692
693 ASSERT_NE(nullptr, event) << mName.c_str()
694 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800695 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700696 << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType)
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800697 << " event, got " << inputEventTypeToString(event->getType()) << " event";
Arthur Hungb92218b2018-08-14 12:00:21 +0800698
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800699 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
Tiger Huang8664f8c2018-10-11 19:14:35 +0800700
Tiger Huang8664f8c2018-10-11 19:14:35 +0800701 switch (expectedEventType) {
702 case AINPUT_EVENT_TYPE_KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800703 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
704 EXPECT_EQ(expectedAction, keyEvent.getAction());
705 EXPECT_EQ(expectedFlags, keyEvent.getFlags());
Tiger Huang8664f8c2018-10-11 19:14:35 +0800706 break;
707 }
708 case AINPUT_EVENT_TYPE_MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800709 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
710 EXPECT_EQ(expectedAction, motionEvent.getAction());
711 EXPECT_EQ(expectedFlags, motionEvent.getFlags());
Tiger Huang8664f8c2018-10-11 19:14:35 +0800712 break;
713 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100714 case AINPUT_EVENT_TYPE_FOCUS: {
715 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
716 }
Prabir Pradhan99987712020-11-10 18:43:05 -0800717 case AINPUT_EVENT_TYPE_CAPTURE: {
718 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
719 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800720 default: {
721 FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType;
722 }
723 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800724 }
725
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100726 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
727 InputEvent* event = consume();
728 ASSERT_NE(nullptr, event) << mName.c_str()
729 << ": consumer should have returned non-NULL event.";
730 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
731 << "Got " << inputEventTypeToString(event->getType())
732 << " event instead of FOCUS event";
733
734 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
735 << mName.c_str() << ": event displayId should always be NONE.";
736
737 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
738 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
739 EXPECT_EQ(inTouchMode, focusEvent->getInTouchMode());
740 }
741
Prabir Pradhan99987712020-11-10 18:43:05 -0800742 void consumeCaptureEvent(bool hasCapture) {
743 const InputEvent* event = consume();
744 ASSERT_NE(nullptr, event) << mName.c_str()
745 << ": consumer should have returned non-NULL event.";
746 ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType())
747 << "Got " << inputEventTypeToString(event->getType())
748 << " event instead of CAPTURE event";
749
750 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
751 << mName.c_str() << ": event displayId should always be NONE.";
752
753 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
754 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
755 }
756
chaviwd1c23182019-12-20 18:44:56 -0800757 void assertNoEvents() {
758 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700759 if (event == nullptr) {
760 return;
761 }
762 if (event->getType() == AINPUT_EVENT_TYPE_KEY) {
763 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
764 ADD_FAILURE() << "Received key event "
765 << KeyEvent::actionToString(keyEvent.getAction());
766 } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) {
767 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
768 ADD_FAILURE() << "Received motion event "
769 << MotionEvent::actionToString(motionEvent.getAction());
770 } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) {
771 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
772 ADD_FAILURE() << "Received focus event, hasFocus = "
773 << (focusEvent.getHasFocus() ? "true" : "false");
Prabir Pradhan99987712020-11-10 18:43:05 -0800774 } else if (event->getType() == AINPUT_EVENT_TYPE_CAPTURE) {
775 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
776 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
777 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700778 }
779 FAIL() << mName.c_str()
780 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -0800781 }
782
783 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
784
785protected:
786 std::unique_ptr<InputConsumer> mConsumer;
787 PreallocatedInputEventFactory mEventFactory;
788
789 std::string mName;
790};
791
792class FakeWindowHandle : public InputWindowHandle {
793public:
794 static const int32_t WIDTH = 600;
795 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -0800796
Chris Yea209fde2020-07-22 13:54:51 -0700797 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
chaviwd1c23182019-12-20 18:44:56 -0800798 const sp<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500799 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -0800800 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500801 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -0700802 base::Result<std::unique_ptr<InputChannel>> channel =
803 dispatcher->createInputChannel(name);
804 token = (*channel)->getConnectionToken();
805 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -0800806 }
807
808 inputApplicationHandle->updateInfo();
809 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
810
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500811 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700812 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -0800813 mInfo.name = name;
Michael Wright44753b12020-07-08 13:48:11 +0100814 mInfo.type = InputWindowInfo::Type::APPLICATION;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500815 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
chaviwd1c23182019-12-20 18:44:56 -0800816 mInfo.frameLeft = 0;
817 mInfo.frameTop = 0;
818 mInfo.frameRight = WIDTH;
819 mInfo.frameBottom = HEIGHT;
chaviw1ff3d1e2020-07-01 15:53:47 -0700820 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -0800821 mInfo.globalScaleFactor = 1.0;
822 mInfo.touchableRegion.clear();
823 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
824 mInfo.visible = true;
Vishnu Nair47074b82020-08-14 11:54:47 -0700825 mInfo.focusable = false;
chaviwd1c23182019-12-20 18:44:56 -0800826 mInfo.hasWallpaper = false;
827 mInfo.paused = false;
chaviwd1c23182019-12-20 18:44:56 -0800828 mInfo.ownerPid = INJECTOR_PID;
829 mInfo.ownerUid = INJECTOR_UID;
chaviwd1c23182019-12-20 18:44:56 -0800830 mInfo.displayId = displayId;
831 }
832
833 virtual bool updateInfo() { return true; }
834
Vishnu Nair47074b82020-08-14 11:54:47 -0700835 void setFocusable(bool focusable) { mInfo.focusable = focusable; }
chaviwd1c23182019-12-20 18:44:56 -0800836
Vishnu Nair958da932020-08-21 17:12:37 -0700837 void setVisible(bool visible) { mInfo.visible = visible; }
838
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700839 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500840 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700841 }
842
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700843 void setPaused(bool paused) { mInfo.paused = paused; }
844
chaviwd1c23182019-12-20 18:44:56 -0800845 void setFrame(const Rect& frame) {
846 mInfo.frameLeft = frame.left;
847 mInfo.frameTop = frame.top;
848 mInfo.frameRight = frame.right;
849 mInfo.frameBottom = frame.bottom;
chaviw1ff3d1e2020-07-01 15:53:47 -0700850 mInfo.transform.set(frame.left, frame.top);
chaviwd1c23182019-12-20 18:44:56 -0800851 mInfo.touchableRegion.clear();
852 mInfo.addTouchableRegion(frame);
853 }
854
Michael Wright44753b12020-07-08 13:48:11 +0100855 void setFlags(Flags<InputWindowInfo::Flag> flags) { mInfo.flags = flags; }
chaviwd1c23182019-12-20 18:44:56 -0800856
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500857 void setInputFeatures(InputWindowInfo::Feature features) { mInfo.inputFeatures = features; }
858
chaviw9eaa22c2020-07-01 16:21:27 -0700859 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
860 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
861 }
862
863 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -0700864
yunho.shinf4a80b82020-11-16 21:13:57 +0900865 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
866
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800867 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
868 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
869 expectedFlags);
870 }
871
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700872 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
873 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
874 }
875
Svet Ganov5d3bc372020-01-26 23:11:07 -0800876 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000877 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800878 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId,
879 expectedFlags);
880 }
881
882 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000883 int32_t expectedFlags = 0) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800884 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId,
885 expectedFlags);
886 }
887
888 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000889 int32_t expectedFlags = 0) {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800890 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
891 expectedFlags);
892 }
893
Svet Ganov5d3bc372020-01-26 23:11:07 -0800894 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000895 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
896 int32_t expectedFlags = 0) {
897 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
898 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -0800899 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
900 }
901
902 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000903 int32_t expectedFlags = 0) {
904 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
905 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -0800906 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
907 }
908
909 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000910 int32_t expectedFlags = 0) {
Michael Wright3a240c42019-12-10 20:53:41 +0000911 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
912 expectedFlags);
913 }
914
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500915 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
916 int32_t expectedFlags = 0) {
917 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
918 expectedFlags);
919 }
920
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100921 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
922 ASSERT_NE(mInputReceiver, nullptr)
923 << "Cannot consume events from a window with no receiver";
924 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
925 }
926
Prabir Pradhan99987712020-11-10 18:43:05 -0800927 void consumeCaptureEvent(bool hasCapture) {
928 ASSERT_NE(mInputReceiver, nullptr)
929 << "Cannot consume events from a window with no receiver";
930 mInputReceiver->consumeCaptureEvent(hasCapture);
931 }
932
chaviwd1c23182019-12-20 18:44:56 -0800933 void consumeEvent(int32_t expectedEventType, int32_t expectedAction, int32_t expectedDisplayId,
934 int32_t expectedFlags) {
935 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
936 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
937 expectedFlags);
938 }
939
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700940 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700941 if (mInputReceiver == nullptr) {
942 ADD_FAILURE() << "Invalid receive event on window with no receiver";
943 return std::nullopt;
944 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700945 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700946 }
947
948 void finishEvent(uint32_t sequenceNum) {
949 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
950 mInputReceiver->finishEvent(sequenceNum);
951 }
952
chaviwaf87b3e2019-10-01 16:59:28 -0700953 InputEvent* consume() {
954 if (mInputReceiver == nullptr) {
955 return nullptr;
956 }
957 return mInputReceiver->consume();
958 }
959
Arthur Hungb92218b2018-08-14 12:00:21 +0800960 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500961 if (mInputReceiver == nullptr &&
962 mInfo.inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL)) {
963 return; // Can't receive events if the window does not have input channel
964 }
965 ASSERT_NE(nullptr, mInputReceiver)
966 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -0800967 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +0800968 }
969
chaviwaf87b3e2019-10-01 16:59:28 -0700970 sp<IBinder> getToken() { return mInfo.token; }
971
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100972 const std::string& getName() { return mName; }
973
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000974 void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) {
975 mInfo.ownerPid = ownerPid;
976 mInfo.ownerUid = ownerUid;
977 }
978
chaviwd1c23182019-12-20 18:44:56 -0800979private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100980 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -0800981 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700982 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800983};
984
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700985std::atomic<int32_t> FakeWindowHandle::sId{1};
986
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800987static InputEventInjectionResult injectKey(
988 const sp<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
989 int32_t displayId = ADISPLAY_ID_NONE,
990 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
991 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800992 KeyEvent event;
993 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
994
995 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800996 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700997 INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE,
998 repeatCount, currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +0800999
1000 // Inject event until dispatch out.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001001 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, syncMode,
1002 injectionTimeout,
1003 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
Arthur Hungb92218b2018-08-14 12:00:21 +08001004}
1005
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001006static InputEventInjectionResult injectKeyDown(const sp<InputDispatcher>& dispatcher,
1007 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001008 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId);
1009}
1010
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001011static InputEventInjectionResult injectKeyUp(const sp<InputDispatcher>& dispatcher,
1012 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001013 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId);
1014}
1015
Garfield Tandf26e862020-07-01 20:18:19 -07001016class PointerBuilder {
1017public:
1018 PointerBuilder(int32_t id, int32_t toolType) {
1019 mProperties.clear();
1020 mProperties.id = id;
1021 mProperties.toolType = toolType;
1022 mCoords.clear();
1023 }
1024
1025 PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); }
1026
1027 PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); }
1028
1029 PointerBuilder& axis(int32_t axis, float value) {
1030 mCoords.setAxisValue(axis, value);
1031 return *this;
1032 }
1033
1034 PointerProperties buildProperties() const { return mProperties; }
1035
1036 PointerCoords buildCoords() const { return mCoords; }
1037
1038private:
1039 PointerProperties mProperties;
1040 PointerCoords mCoords;
1041};
1042
1043class MotionEventBuilder {
1044public:
1045 MotionEventBuilder(int32_t action, int32_t source) {
1046 mAction = action;
1047 mSource = source;
1048 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1049 }
1050
1051 MotionEventBuilder& eventTime(nsecs_t eventTime) {
1052 mEventTime = eventTime;
1053 return *this;
1054 }
1055
1056 MotionEventBuilder& displayId(int32_t displayId) {
1057 mDisplayId = displayId;
1058 return *this;
1059 }
1060
1061 MotionEventBuilder& actionButton(int32_t actionButton) {
1062 mActionButton = actionButton;
1063 return *this;
1064 }
1065
1066 MotionEventBuilder& buttonState(int32_t actionButton) {
1067 mActionButton = actionButton;
1068 return *this;
1069 }
1070
1071 MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) {
1072 mRawXCursorPosition = rawXCursorPosition;
1073 return *this;
1074 }
1075
1076 MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) {
1077 mRawYCursorPosition = rawYCursorPosition;
1078 return *this;
1079 }
1080
1081 MotionEventBuilder& pointer(PointerBuilder pointer) {
1082 mPointers.push_back(pointer);
1083 return *this;
1084 }
1085
1086 MotionEvent build() {
1087 std::vector<PointerProperties> pointerProperties;
1088 std::vector<PointerCoords> pointerCoords;
1089 for (const PointerBuilder& pointer : mPointers) {
1090 pointerProperties.push_back(pointer.buildProperties());
1091 pointerCoords.push_back(pointer.buildCoords());
1092 }
1093
1094 // Set mouse cursor position for the most common cases to avoid boilerplate.
1095 if (mSource == AINPUT_SOURCE_MOUSE &&
1096 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) &&
1097 mPointers.size() == 1) {
1098 mRawXCursorPosition = pointerCoords[0].getX();
1099 mRawYCursorPosition = pointerCoords[0].getY();
1100 }
1101
1102 MotionEvent event;
chaviw9eaa22c2020-07-01 16:21:27 -07001103 ui::Transform identityTransform;
Garfield Tandf26e862020-07-01 20:18:19 -07001104 event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC,
1105 mAction, mActionButton, /* flags */ 0, /* edgeFlags */ 0, AMETA_NONE,
chaviw9eaa22c2020-07-01 16:21:27 -07001106 mButtonState, MotionClassification::NONE, identityTransform,
1107 /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition,
1108 mRawYCursorPosition, mEventTime, mEventTime, mPointers.size(),
1109 pointerProperties.data(), pointerCoords.data());
Garfield Tandf26e862020-07-01 20:18:19 -07001110
1111 return event;
1112 }
1113
1114private:
1115 int32_t mAction;
1116 int32_t mSource;
1117 nsecs_t mEventTime;
1118 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1119 int32_t mActionButton{0};
1120 int32_t mButtonState{0};
1121 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1122 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1123
1124 std::vector<PointerBuilder> mPointers;
1125};
1126
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001127static InputEventInjectionResult injectMotionEvent(
Garfield Tandf26e862020-07-01 20:18:19 -07001128 const sp<InputDispatcher>& dispatcher, const MotionEvent& event,
1129 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001130 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT) {
Garfield Tandf26e862020-07-01 20:18:19 -07001131 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, injectionMode,
1132 injectionTimeout,
1133 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
1134}
1135
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001136static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001137 const sp<InputDispatcher>& dispatcher, int32_t action, int32_t source, int32_t displayId,
1138 const PointF& position,
1139 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001140 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1141 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001142 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001143 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC)) {
Garfield Tandf26e862020-07-01 20:18:19 -07001144 MotionEvent event = MotionEventBuilder(action, source)
1145 .displayId(displayId)
1146 .eventTime(eventTime)
1147 .rawXCursorPosition(cursorPosition.x)
1148 .rawYCursorPosition(cursorPosition.y)
1149 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1150 .x(position.x)
1151 .y(position.y))
1152 .build();
Arthur Hungb92218b2018-08-14 12:00:21 +08001153
1154 // Inject event until dispatch out.
Garfield Tandf26e862020-07-01 20:18:19 -07001155 return injectMotionEvent(dispatcher, event);
Arthur Hungb92218b2018-08-14 12:00:21 +08001156}
1157
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001158static InputEventInjectionResult injectMotionDown(const sp<InputDispatcher>& dispatcher,
1159 int32_t source, int32_t displayId,
1160 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001161 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001162}
1163
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001164static InputEventInjectionResult injectMotionUp(const sp<InputDispatcher>& dispatcher,
1165 int32_t source, int32_t displayId,
1166 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001167 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001168}
1169
Jackal Guof9696682018-10-05 12:23:23 +08001170static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1171 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1172 // Define a valid key event.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001173 NotifyKeyArgs args(/* id */ 0, currentTime, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
1174 POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, KEY_A,
1175 AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001176
1177 return args;
1178}
1179
chaviwd1c23182019-12-20 18:44:56 -08001180static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId,
1181 const std::vector<PointF>& points) {
1182 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001183 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1184 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1185 }
1186
chaviwd1c23182019-12-20 18:44:56 -08001187 PointerProperties pointerProperties[pointerCount];
1188 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001189
chaviwd1c23182019-12-20 18:44:56 -08001190 for (size_t i = 0; i < pointerCount; i++) {
1191 pointerProperties[i].clear();
1192 pointerProperties[i].id = i;
1193 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001194
chaviwd1c23182019-12-20 18:44:56 -08001195 pointerCoords[i].clear();
1196 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1197 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1198 }
Jackal Guof9696682018-10-05 12:23:23 +08001199
1200 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1201 // Define a valid motion event.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001202 NotifyMotionArgs args(/* id */ 0, currentTime, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001203 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1204 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001205 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1206 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001207 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1208 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001209
1210 return args;
1211}
1212
chaviwd1c23182019-12-20 18:44:56 -08001213static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1214 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1215}
1216
Prabir Pradhan99987712020-11-10 18:43:05 -08001217static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(bool enabled) {
1218 return NotifyPointerCaptureChangedArgs(/* id */ 0, systemTime(SYSTEM_TIME_MONOTONIC), enabled);
1219}
1220
Arthur Hungb92218b2018-08-14 12:00:21 +08001221TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001222 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001223 sp<FakeWindowHandle> window =
1224 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001225
Arthur Hung72d8dc32020-03-28 00:48:39 +00001226 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001227 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1228 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1229 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001230
1231 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001232 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001233}
1234
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001235/**
1236 * Calling setInputWindows once with FLAG_NOT_TOUCH_MODAL should not cause any issues.
1237 * To ensure that window receives only events that were directly inside of it, add
1238 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1239 * when finding touched windows.
1240 * This test serves as a sanity check for the next test, where setInputWindows is
1241 * called twice.
1242 */
1243TEST_F(InputDispatcherTest, SetInputWindowOnce_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001244 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001245 sp<FakeWindowHandle> window =
1246 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1247 window->setFrame(Rect(0, 0, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01001248 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001249
1250 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001251 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001252 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1253 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001254 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001255
1256 // Window should receive motion event.
1257 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1258}
1259
1260/**
1261 * Calling setInputWindows twice, with the same info, should not cause any issues.
1262 * To ensure that window receives only events that were directly inside of it, add
1263 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1264 * when finding touched windows.
1265 */
1266TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001267 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001268 sp<FakeWindowHandle> window =
1269 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1270 window->setFrame(Rect(0, 0, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01001271 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001272
1273 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1274 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001275 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001276 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1277 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001278 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001279
1280 // Window should receive motion event.
1281 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1282}
1283
Arthur Hungb92218b2018-08-14 12:00:21 +08001284// The foreground window should receive the first touch down event.
1285TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001286 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001287 sp<FakeWindowHandle> windowTop =
1288 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
1289 sp<FakeWindowHandle> windowSecond =
1290 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001291
Arthur Hung72d8dc32020-03-28 00:48:39 +00001292 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001293 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1294 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1295 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001296
1297 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001298 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001299 windowSecond->assertNoEvents();
1300}
1301
Garfield Tandf26e862020-07-01 20:18:19 -07001302TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07001303 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07001304 sp<FakeWindowHandle> windowLeft =
1305 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1306 windowLeft->setFrame(Rect(0, 0, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001307 windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001308 sp<FakeWindowHandle> windowRight =
1309 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1310 windowRight->setFrame(Rect(600, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001311 windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001312
1313 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1314
1315 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
1316
1317 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001318 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001319 injectMotionEvent(mDispatcher,
1320 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1321 AINPUT_SOURCE_MOUSE)
1322 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1323 .x(900)
1324 .y(400))
1325 .build()));
1326 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1327 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1328 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1329 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1330
1331 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001332 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001333 injectMotionEvent(mDispatcher,
1334 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1335 AINPUT_SOURCE_MOUSE)
1336 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1337 .x(300)
1338 .y(400))
1339 .build()));
1340 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1341 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1342 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1343 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1344 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1345 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1346
1347 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001348 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001349 injectMotionEvent(mDispatcher,
1350 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1351 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1352 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1353 .x(300)
1354 .y(400))
1355 .build()));
1356 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1357
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001358 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001359 injectMotionEvent(mDispatcher,
1360 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
1361 AINPUT_SOURCE_MOUSE)
1362 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1363 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1364 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1365 .x(300)
1366 .y(400))
1367 .build()));
1368 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1369 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1370
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001371 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001372 injectMotionEvent(mDispatcher,
1373 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1374 AINPUT_SOURCE_MOUSE)
1375 .buttonState(0)
1376 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1377 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1378 .x(300)
1379 .y(400))
1380 .build()));
1381 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1382 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1383
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001384 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001385 injectMotionEvent(mDispatcher,
1386 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
1387 .buttonState(0)
1388 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1389 .x(300)
1390 .y(400))
1391 .build()));
1392 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1393
1394 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001395 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001396 injectMotionEvent(mDispatcher,
1397 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1398 AINPUT_SOURCE_MOUSE)
1399 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1400 .x(900)
1401 .y(400))
1402 .build()));
1403 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1404 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1405 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1406 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1407 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1408 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1409}
1410
1411// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
1412// directly in this test.
1413TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07001414 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07001415 sp<FakeWindowHandle> window =
1416 new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1417 window->setFrame(Rect(0, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001418 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001419
1420 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1421
1422 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1423
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001424 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001425 injectMotionEvent(mDispatcher,
1426 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
1427 AINPUT_SOURCE_MOUSE)
1428 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1429 .x(300)
1430 .y(400))
1431 .build()));
1432 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1433 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1434
1435 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001436 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001437 injectMotionEvent(mDispatcher,
1438 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1439 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1440 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1441 .x(300)
1442 .y(400))
1443 .build()));
1444 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1445
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001446 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001447 injectMotionEvent(mDispatcher,
1448 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
1449 AINPUT_SOURCE_MOUSE)
1450 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1451 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1452 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1453 .x(300)
1454 .y(400))
1455 .build()));
1456 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1457 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1458
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001459 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001460 injectMotionEvent(mDispatcher,
1461 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1462 AINPUT_SOURCE_MOUSE)
1463 .buttonState(0)
1464 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1465 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1466 .x(300)
1467 .y(400))
1468 .build()));
1469 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1470 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1471
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001472 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001473 injectMotionEvent(mDispatcher,
1474 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
1475 .buttonState(0)
1476 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1477 .x(300)
1478 .y(400))
1479 .build()));
1480 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1481
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001482 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001483 injectMotionEvent(mDispatcher,
1484 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
1485 AINPUT_SOURCE_MOUSE)
1486 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1487 .x(300)
1488 .y(400))
1489 .build()));
1490 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1491 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1492}
1493
Garfield Tan00f511d2019-06-12 16:55:40 -07001494TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07001495 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07001496
1497 sp<FakeWindowHandle> windowLeft =
1498 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1499 windowLeft->setFrame(Rect(0, 0, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001500 windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07001501 sp<FakeWindowHandle> windowRight =
1502 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1503 windowRight->setFrame(Rect(600, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001504 windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07001505
1506 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1507
Arthur Hung72d8dc32020-03-28 00:48:39 +00001508 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07001509
1510 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
1511 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001512 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07001513 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001514 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001515 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07001516 windowRight->assertNoEvents();
1517}
1518
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001519TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001520 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001521 sp<FakeWindowHandle> window =
1522 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07001523 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001524
Arthur Hung72d8dc32020-03-28 00:48:39 +00001525 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001526 setFocusedWindow(window);
1527
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001528 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001529
1530 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1531 mDispatcher->notifyKey(&keyArgs);
1532
1533 // Window should receive key down event.
1534 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1535
1536 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
1537 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001538 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001539 mDispatcher->notifyDeviceReset(&args);
1540 window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
1541 AKEY_EVENT_FLAG_CANCELED);
1542}
1543
1544TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001545 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001546 sp<FakeWindowHandle> window =
1547 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1548
Arthur Hung72d8dc32020-03-28 00:48:39 +00001549 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001550
1551 NotifyMotionArgs motionArgs =
1552 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1553 ADISPLAY_ID_DEFAULT);
1554 mDispatcher->notifyMotion(&motionArgs);
1555
1556 // Window should receive motion down event.
1557 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1558
1559 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
1560 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001561 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001562 mDispatcher->notifyDeviceReset(&args);
1563 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT,
1564 0 /*expectedFlags*/);
1565}
1566
Svet Ganov5d3bc372020-01-26 23:11:07 -08001567TEST_F(InputDispatcherTest, TransferTouchFocus_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07001568 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001569
1570 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001571 sp<FakeWindowHandle> firstWindow =
1572 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1573 sp<FakeWindowHandle> secondWindow =
1574 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001575
1576 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001577 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001578
1579 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001580 NotifyMotionArgs downMotionArgs =
1581 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1582 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001583 mDispatcher->notifyMotion(&downMotionArgs);
1584 // Only the first window should get the down event
1585 firstWindow->consumeMotionDown();
1586 secondWindow->assertNoEvents();
1587
1588 // Transfer touch focus to the second window
1589 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1590 // The first window gets cancel and the second gets down
1591 firstWindow->consumeMotionCancel();
1592 secondWindow->consumeMotionDown();
1593
1594 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001595 NotifyMotionArgs upMotionArgs =
1596 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1597 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001598 mDispatcher->notifyMotion(&upMotionArgs);
1599 // The first window gets no events and the second gets up
1600 firstWindow->assertNoEvents();
1601 secondWindow->consumeMotionUp();
1602}
1603
1604TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointerNoSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001605 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001606
1607 PointF touchPoint = {10, 10};
1608
1609 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001610 sp<FakeWindowHandle> firstWindow =
1611 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
1612 sp<FakeWindowHandle> secondWindow =
1613 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001614
1615 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001616 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001617
1618 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001619 NotifyMotionArgs downMotionArgs =
1620 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1621 ADISPLAY_ID_DEFAULT, {touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001622 mDispatcher->notifyMotion(&downMotionArgs);
1623 // Only the first window should get the down event
1624 firstWindow->consumeMotionDown();
1625 secondWindow->assertNoEvents();
1626
1627 // Send pointer down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001628 NotifyMotionArgs pointerDownMotionArgs =
1629 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1630 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1631 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1632 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001633 mDispatcher->notifyMotion(&pointerDownMotionArgs);
1634 // Only the first window should get the pointer down event
1635 firstWindow->consumeMotionPointerDown(1);
1636 secondWindow->assertNoEvents();
1637
1638 // Transfer touch focus to the second window
1639 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1640 // The first window gets cancel and the second gets down and pointer down
1641 firstWindow->consumeMotionCancel();
1642 secondWindow->consumeMotionDown();
1643 secondWindow->consumeMotionPointerDown(1);
1644
1645 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001646 NotifyMotionArgs pointerUpMotionArgs =
1647 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1648 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1649 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1650 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001651 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1652 // The first window gets nothing and the second gets pointer up
1653 firstWindow->assertNoEvents();
1654 secondWindow->consumeMotionPointerUp(1);
1655
1656 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001657 NotifyMotionArgs upMotionArgs =
1658 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1659 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001660 mDispatcher->notifyMotion(&upMotionArgs);
1661 // The first window gets nothing and the second gets up
1662 firstWindow->assertNoEvents();
1663 secondWindow->consumeMotionUp();
1664}
1665
1666TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001667 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001668
1669 // Create a non touch modal window that supports split touch
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001670 sp<FakeWindowHandle> firstWindow =
1671 new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001672 firstWindow->setFrame(Rect(0, 0, 600, 400));
Michael Wright44753b12020-07-08 13:48:11 +01001673 firstWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1674 InputWindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001675
1676 // Create a non touch modal window that supports split touch
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001677 sp<FakeWindowHandle> secondWindow =
1678 new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001679 secondWindow->setFrame(Rect(0, 400, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001680 secondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1681 InputWindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001682
1683 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001684 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001685
1686 PointF pointInFirst = {300, 200};
1687 PointF pointInSecond = {300, 600};
1688
1689 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001690 NotifyMotionArgs firstDownMotionArgs =
1691 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1692 ADISPLAY_ID_DEFAULT, {pointInFirst});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001693 mDispatcher->notifyMotion(&firstDownMotionArgs);
1694 // Only the first window should get the down event
1695 firstWindow->consumeMotionDown();
1696 secondWindow->assertNoEvents();
1697
1698 // Send down to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001699 NotifyMotionArgs secondDownMotionArgs =
1700 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
1701 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1702 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1703 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001704 mDispatcher->notifyMotion(&secondDownMotionArgs);
1705 // The first window gets a move and the second a down
1706 firstWindow->consumeMotionMove();
1707 secondWindow->consumeMotionDown();
1708
1709 // Transfer touch focus to the second window
1710 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1711 // The first window gets cancel and the new gets pointer down (it already saw down)
1712 firstWindow->consumeMotionCancel();
1713 secondWindow->consumeMotionPointerDown(1);
1714
1715 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001716 NotifyMotionArgs pointerUpMotionArgs =
1717 generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
1718 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1719 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1720 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001721 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1722 // The first window gets nothing and the second gets pointer up
1723 firstWindow->assertNoEvents();
1724 secondWindow->consumeMotionPointerUp(1);
1725
1726 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001727 NotifyMotionArgs upMotionArgs =
1728 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1729 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001730 mDispatcher->notifyMotion(&upMotionArgs);
1731 // The first window gets nothing and the second gets up
1732 firstWindow->assertNoEvents();
1733 secondWindow->consumeMotionUp();
1734}
1735
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001736TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07001737 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001738 sp<FakeWindowHandle> window =
1739 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1740
Vishnu Nair47074b82020-08-14 11:54:47 -07001741 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001742 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001743 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001744
1745 window->consumeFocusEvent(true);
1746
1747 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1748 mDispatcher->notifyKey(&keyArgs);
1749
1750 // Window should receive key down event.
1751 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1752}
1753
1754TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07001755 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001756 sp<FakeWindowHandle> window =
1757 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1758
Arthur Hung72d8dc32020-03-28 00:48:39 +00001759 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001760
1761 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1762 mDispatcher->notifyKey(&keyArgs);
1763 mDispatcher->waitForIdle();
1764
1765 window->assertNoEvents();
1766}
1767
1768// If a window is touchable, but does not have focus, it should receive motion events, but not keys
1769TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07001770 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001771 sp<FakeWindowHandle> window =
1772 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1773
Arthur Hung72d8dc32020-03-28 00:48:39 +00001774 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001775
1776 // Send key
1777 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1778 mDispatcher->notifyKey(&keyArgs);
1779 // Send motion
1780 NotifyMotionArgs motionArgs =
1781 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1782 ADISPLAY_ID_DEFAULT);
1783 mDispatcher->notifyMotion(&motionArgs);
1784
1785 // Window should receive only the motion event
1786 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1787 window->assertNoEvents(); // Key event or focus event will not be received
1788}
1789
chaviwd1c23182019-12-20 18:44:56 -08001790class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00001791public:
1792 FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name,
chaviwd1c23182019-12-20 18:44:56 -08001793 int32_t displayId, bool isGestureMonitor = false) {
Garfield Tan15601662020-09-22 15:32:38 -07001794 base::Result<std::unique_ptr<InputChannel>> channel =
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +00001795 dispatcher->createInputMonitor(displayId, isGestureMonitor, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07001796 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00001797 }
1798
chaviwd1c23182019-12-20 18:44:56 -08001799 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
1800
1801 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1802 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN,
1803 expectedDisplayId, expectedFlags);
1804 }
1805
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001806 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
1807
1808 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
1809
chaviwd1c23182019-12-20 18:44:56 -08001810 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1811 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
1812 expectedDisplayId, expectedFlags);
1813 }
1814
1815 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1816 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP,
1817 expectedDisplayId, expectedFlags);
1818 }
1819
1820 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
1821
1822private:
1823 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00001824};
1825
1826// Tests for gesture monitors
1827TEST_F(InputDispatcherTest, GestureMonitor_ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07001828 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00001829 sp<FakeWindowHandle> window =
1830 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001831 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00001832
chaviwd1c23182019-12-20 18:44:56 -08001833 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
1834 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00001835
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001836 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00001837 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001838 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00001839 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001840 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00001841}
1842
1843TEST_F(InputDispatcherTest, GestureMonitor_DoesNotReceiveKeyEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07001844 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00001845 sp<FakeWindowHandle> window =
1846 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1847
1848 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07001849 window->setFocusable(true);
Michael Wright3a240c42019-12-10 20:53:41 +00001850
Arthur Hung72d8dc32020-03-28 00:48:39 +00001851 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001852 setFocusedWindow(window);
1853
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001854 window->consumeFocusEvent(true);
Michael Wright3a240c42019-12-10 20:53:41 +00001855
chaviwd1c23182019-12-20 18:44:56 -08001856 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
1857 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00001858
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001859 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
1860 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00001861 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001862 monitor.assertNoEvents();
Michael Wright3a240c42019-12-10 20:53:41 +00001863}
1864
1865TEST_F(InputDispatcherTest, GestureMonitor_CanPilferAfterWindowIsRemovedMidStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001866 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00001867 sp<FakeWindowHandle> window =
1868 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001869 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00001870
chaviwd1c23182019-12-20 18:44:56 -08001871 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
1872 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00001873
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001874 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00001875 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001876 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00001877 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001878 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00001879
1880 window->releaseChannel();
1881
chaviwd1c23182019-12-20 18:44:56 -08001882 mDispatcher->pilferPointers(monitor.getToken());
Michael Wright3a240c42019-12-10 20:53:41 +00001883
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001884 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00001885 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001886 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08001887 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00001888}
1889
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001890TEST_F(InputDispatcherTest, UnresponsiveGestureMonitor_GetsAnr) {
1891 FakeMonitorReceiver monitor =
1892 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
1893 true /*isGestureMonitor*/);
1894
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001895 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001896 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
1897 std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
1898 ASSERT_TRUE(consumeSeq);
1899
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001900 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(DISPATCHING_TIMEOUT,
1901 monitor.getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001902 monitor.finishEvent(*consumeSeq);
1903 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001904 mFakePolicy->assertNotifyConnectionResponsiveWasCalled(monitor.getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001905}
1906
chaviw81e2bb92019-12-18 15:03:51 -08001907TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07001908 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
chaviw81e2bb92019-12-18 15:03:51 -08001909 sp<FakeWindowHandle> window =
1910 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1911
Arthur Hung72d8dc32020-03-28 00:48:39 +00001912 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08001913
1914 NotifyMotionArgs motionArgs =
1915 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1916 ADISPLAY_ID_DEFAULT);
1917
1918 mDispatcher->notifyMotion(&motionArgs);
1919 // Window should receive motion down event.
1920 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1921
1922 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001923 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08001924 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1925 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
1926 motionArgs.pointerCoords[0].getX() - 10);
1927
1928 mDispatcher->notifyMotion(&motionArgs);
1929 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
1930 0 /*expectedFlags*/);
1931}
1932
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001933/**
1934 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
1935 * the device default right away. In the test scenario, we check both the default value,
1936 * and the action of enabling / disabling.
1937 */
1938TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07001939 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001940 sp<FakeWindowHandle> window =
1941 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
1942
1943 // Set focused application.
1944 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07001945 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001946
1947 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00001948 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001949 setFocusedWindow(window);
1950
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001951 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
1952
1953 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07001954 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001955 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001956 window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/);
1957
1958 SCOPED_TRACE("Disable touch mode");
1959 mDispatcher->setInTouchMode(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07001960 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001961 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001962 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001963 window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/);
1964
1965 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07001966 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001967 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001968 window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/);
1969
1970 SCOPED_TRACE("Enable touch mode again");
1971 mDispatcher->setInTouchMode(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07001972 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001973 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001974 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001975 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
1976
1977 window->assertNoEvents();
1978}
1979
Gang Wange9087892020-01-07 12:17:14 -05001980TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07001981 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Gang Wange9087892020-01-07 12:17:14 -05001982 sp<FakeWindowHandle> window =
1983 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
1984
1985 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07001986 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05001987
Arthur Hung72d8dc32020-03-28 00:48:39 +00001988 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001989 setFocusedWindow(window);
1990
Gang Wange9087892020-01-07 12:17:14 -05001991 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
1992
1993 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
1994 mDispatcher->notifyKey(&keyArgs);
1995
1996 InputEvent* event = window->consume();
1997 ASSERT_NE(event, nullptr);
1998
1999 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
2000 ASSERT_NE(verified, nullptr);
2001 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
2002
2003 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
2004 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
2005 ASSERT_EQ(keyArgs.source, verified->source);
2006 ASSERT_EQ(keyArgs.displayId, verified->displayId);
2007
2008 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
2009
2010 ASSERT_EQ(keyArgs.action, verifiedKey.action);
2011 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05002012 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
2013 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
2014 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
2015 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
2016 ASSERT_EQ(0, verifiedKey.repeatCount);
2017}
2018
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002019TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07002020 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002021 sp<FakeWindowHandle> window =
2022 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2023
2024 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2025
Arthur Hung72d8dc32020-03-28 00:48:39 +00002026 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08002027
2028 NotifyMotionArgs motionArgs =
2029 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2030 ADISPLAY_ID_DEFAULT);
2031 mDispatcher->notifyMotion(&motionArgs);
2032
2033 InputEvent* event = window->consume();
2034 ASSERT_NE(event, nullptr);
2035
2036 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
2037 ASSERT_NE(verified, nullptr);
2038 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
2039
2040 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
2041 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
2042 EXPECT_EQ(motionArgs.source, verified->source);
2043 EXPECT_EQ(motionArgs.displayId, verified->displayId);
2044
2045 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
2046
2047 EXPECT_EQ(motionArgs.pointerCoords[0].getX(), verifiedMotion.rawX);
2048 EXPECT_EQ(motionArgs.pointerCoords[0].getY(), verifiedMotion.rawY);
2049 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
2050 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
2051 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
2052 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
2053 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
2054}
2055
yunho.shinf4a80b82020-11-16 21:13:57 +09002056TEST_F(InputDispatcherTest, NonPointerMotionEvent_JoystickNotTransformed) {
2057 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2058 sp<FakeWindowHandle> window =
2059 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
2060 const std::string name = window->getName();
2061
2062 // Window gets transformed by offset values.
2063 window->setWindowOffset(500.0f, 500.0f);
2064
2065 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2066 window->setFocusable(true);
2067
2068 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2069
2070 // First, we set focused window so that focusedWindowHandle is not null.
2071 setFocusedWindow(window);
2072
2073 // Second, we consume focus event if it is right or wrong according to onFocusChangedLocked.
2074 window->consumeFocusEvent(true);
2075
2076 NotifyMotionArgs motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE,
2077 AINPUT_SOURCE_JOYSTICK, ADISPLAY_ID_DEFAULT);
2078 mDispatcher->notifyMotion(&motionArgs);
2079
2080 // Third, we consume motion event.
2081 InputEvent* event = window->consume();
2082 ASSERT_NE(event, nullptr);
2083 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
2084 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
2085 << " event, got " << inputEventTypeToString(event->getType()) << " event";
2086
2087 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
2088 EXPECT_EQ(AINPUT_EVENT_TYPE_MOTION, motionEvent.getAction());
2089
2090 float expectedX = motionArgs.pointerCoords[0].getX();
2091 float expectedY = motionArgs.pointerCoords[0].getY();
2092
2093 // Finally we test if the axis values from the final motion event are not transformed
2094 EXPECT_EQ(expectedX, motionEvent.getX(0)) << "expected " << expectedX << " for x coord of "
2095 << name.c_str() << ", got " << motionEvent.getX(0);
2096 EXPECT_EQ(expectedY, motionEvent.getY(0)) << "expected " << expectedY << " for y coord of "
2097 << name.c_str() << ", got " << motionEvent.getY(0);
2098}
2099
chaviw09c8d2d2020-08-24 15:48:26 -07002100/**
2101 * Ensure that separate calls to sign the same data are generating the same key.
2102 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
2103 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
2104 * tests.
2105 */
2106TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
2107 KeyEvent event = getTestKeyEvent();
2108 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
2109
2110 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
2111 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
2112 ASSERT_EQ(hmac1, hmac2);
2113}
2114
2115/**
2116 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
2117 */
2118TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
2119 KeyEvent event = getTestKeyEvent();
2120 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
2121 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
2122
2123 verifiedEvent.deviceId += 1;
2124 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2125
2126 verifiedEvent.source += 1;
2127 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2128
2129 verifiedEvent.eventTimeNanos += 1;
2130 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2131
2132 verifiedEvent.displayId += 1;
2133 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2134
2135 verifiedEvent.action += 1;
2136 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2137
2138 verifiedEvent.downTimeNanos += 1;
2139 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2140
2141 verifiedEvent.flags += 1;
2142 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2143
2144 verifiedEvent.keyCode += 1;
2145 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2146
2147 verifiedEvent.scanCode += 1;
2148 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2149
2150 verifiedEvent.metaState += 1;
2151 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2152
2153 verifiedEvent.repeatCount += 1;
2154 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
2155}
2156
Vishnu Nair958da932020-08-21 17:12:37 -07002157TEST_F(InputDispatcherTest, SetFocusedWindow) {
2158 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2159 sp<FakeWindowHandle> windowTop =
2160 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2161 sp<FakeWindowHandle> windowSecond =
2162 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2163 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2164
2165 // Top window is also focusable but is not granted focus.
2166 windowTop->setFocusable(true);
2167 windowSecond->setFocusable(true);
2168 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2169 setFocusedWindow(windowSecond);
2170
2171 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002172 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2173 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002174
2175 // Focused window should receive event.
2176 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
2177 windowTop->assertNoEvents();
2178}
2179
2180TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
2181 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2182 sp<FakeWindowHandle> window =
2183 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2184 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2185
2186 window->setFocusable(true);
2187 // Release channel for window is no longer valid.
2188 window->releaseChannel();
2189 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2190 setFocusedWindow(window);
2191
2192 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002193 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2194 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002195
2196 // window channel is invalid, so it should not receive any input event.
2197 window->assertNoEvents();
2198}
2199
2200TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
2201 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2202 sp<FakeWindowHandle> window =
2203 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2204 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2205
2206 // Window is not focusable.
2207 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2208 setFocusedWindow(window);
2209
2210 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002211 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2212 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002213
2214 // window is invalid, so it should not receive any input event.
2215 window->assertNoEvents();
2216}
2217
2218TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
2219 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2220 sp<FakeWindowHandle> windowTop =
2221 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2222 sp<FakeWindowHandle> windowSecond =
2223 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2224 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2225
2226 windowTop->setFocusable(true);
2227 windowSecond->setFocusable(true);
2228 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2229 setFocusedWindow(windowTop);
2230 windowTop->consumeFocusEvent(true);
2231
2232 setFocusedWindow(windowSecond, windowTop);
2233 windowSecond->consumeFocusEvent(true);
2234 windowTop->consumeFocusEvent(false);
2235
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002236 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2237 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002238
2239 // Focused window should receive event.
2240 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
2241}
2242
2243TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) {
2244 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2245 sp<FakeWindowHandle> windowTop =
2246 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2247 sp<FakeWindowHandle> windowSecond =
2248 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2249 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2250
2251 windowTop->setFocusable(true);
2252 windowSecond->setFocusable(true);
2253 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2254 setFocusedWindow(windowSecond, windowTop);
2255
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002256 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2257 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002258
2259 // Event should be dropped.
2260 windowTop->assertNoEvents();
2261 windowSecond->assertNoEvents();
2262}
2263
2264TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
2265 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2266 sp<FakeWindowHandle> window =
2267 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2268 sp<FakeWindowHandle> previousFocusedWindow =
2269 new FakeWindowHandle(application, mDispatcher, "previousFocusedWindow",
2270 ADISPLAY_ID_DEFAULT);
2271 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2272
2273 window->setFocusable(true);
2274 previousFocusedWindow->setFocusable(true);
2275 window->setVisible(false);
2276 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
2277 setFocusedWindow(previousFocusedWindow);
2278 previousFocusedWindow->consumeFocusEvent(true);
2279
2280 // Requesting focus on invisible window takes focus from currently focused window.
2281 setFocusedWindow(window);
2282 previousFocusedWindow->consumeFocusEvent(false);
2283
2284 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002285 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07002286 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002287 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07002288
2289 // Window does not get focus event or key down.
2290 window->assertNoEvents();
2291
2292 // Window becomes visible.
2293 window->setVisible(true);
2294 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2295
2296 // Window receives focus event.
2297 window->consumeFocusEvent(true);
2298 // Focused window receives key down.
2299 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2300}
2301
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002302/**
2303 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
2304 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
2305 * of the 'slipperyEnterWindow'.
2306 *
2307 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
2308 * a way so that the touched location is no longer covered by the top window.
2309 *
2310 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
2311 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
2312 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
2313 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
2314 * with ACTION_DOWN).
2315 * Thus, the touch has been transferred from the top window into the bottom window, because the top
2316 * window moved itself away from the touched location and had Flag::SLIPPERY.
2317 *
2318 * Even though the top window moved away from the touched location, it is still obscuring the bottom
2319 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
2320 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
2321 *
2322 * In this test, we ensure that the event received by the bottom window has
2323 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
2324 */
2325TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
2326 constexpr int32_t SLIPPERY_PID = INJECTOR_PID + 1;
2327 constexpr int32_t SLIPPERY_UID = INJECTOR_UID + 1;
2328
2329 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2330 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2331
2332 sp<FakeWindowHandle> slipperyExitWindow =
2333 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2334 slipperyExitWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
2335 InputWindowInfo::Flag::SLIPPERY);
2336 // Make sure this one overlaps the bottom window
2337 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
2338 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
2339 // one. Windows with the same owner are not considered to be occluding each other.
2340 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
2341
2342 sp<FakeWindowHandle> slipperyEnterWindow =
2343 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2344 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
2345
2346 mDispatcher->setInputWindows(
2347 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
2348
2349 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
2350 NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2351 ADISPLAY_ID_DEFAULT, {{50, 50}});
2352 mDispatcher->notifyMotion(&args);
2353 slipperyExitWindow->consumeMotionDown();
2354 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
2355 mDispatcher->setInputWindows(
2356 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
2357
2358 args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2359 ADISPLAY_ID_DEFAULT, {{51, 51}});
2360 mDispatcher->notifyMotion(&args);
2361
2362 slipperyExitWindow->consumeMotionCancel();
2363
2364 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
2365 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
2366}
2367
Garfield Tan1c7bc862020-01-28 13:24:04 -08002368class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
2369protected:
2370 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
2371 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
2372
Chris Yea209fde2020-07-22 13:54:51 -07002373 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002374 sp<FakeWindowHandle> mWindow;
2375
2376 virtual void SetUp() override {
2377 mFakePolicy = new FakeInputDispatcherPolicy();
2378 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
2379 mDispatcher = new InputDispatcher(mFakePolicy);
2380 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
2381 ASSERT_EQ(OK, mDispatcher->start());
2382
2383 setUpWindow();
2384 }
2385
2386 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07002387 mApp = std::make_shared<FakeApplicationHandle>();
Garfield Tan1c7bc862020-01-28 13:24:04 -08002388 mWindow = new FakeWindowHandle(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2389
Vishnu Nair47074b82020-08-14 11:54:47 -07002390 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002391 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002392 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002393 mWindow->consumeFocusEvent(true);
2394 }
2395
Chris Ye2ad95392020-09-01 13:44:44 -07002396 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08002397 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07002398 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002399 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
2400 mDispatcher->notifyKey(&keyArgs);
2401
2402 // Window should receive key down event.
2403 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2404 }
2405
2406 void expectKeyRepeatOnce(int32_t repeatCount) {
2407 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
2408 InputEvent* repeatEvent = mWindow->consume();
2409 ASSERT_NE(nullptr, repeatEvent);
2410
2411 uint32_t eventType = repeatEvent->getType();
2412 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType);
2413
2414 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
2415 uint32_t eventAction = repeatKeyEvent->getAction();
2416 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
2417 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
2418 }
2419
Chris Ye2ad95392020-09-01 13:44:44 -07002420 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08002421 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07002422 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002423 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
2424 mDispatcher->notifyKey(&keyArgs);
2425
2426 // Window should receive key down event.
2427 mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
2428 0 /*expectedFlags*/);
2429 }
2430};
2431
2432TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Chris Ye2ad95392020-09-01 13:44:44 -07002433 sendAndConsumeKeyDown(1 /* deviceId */);
2434 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2435 expectKeyRepeatOnce(repeatCount);
2436 }
2437}
2438
2439TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
2440 sendAndConsumeKeyDown(1 /* deviceId */);
2441 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2442 expectKeyRepeatOnce(repeatCount);
2443 }
2444 sendAndConsumeKeyDown(2 /* deviceId */);
2445 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08002446 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2447 expectKeyRepeatOnce(repeatCount);
2448 }
2449}
2450
2451TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Chris Ye2ad95392020-09-01 13:44:44 -07002452 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002453 expectKeyRepeatOnce(1 /*repeatCount*/);
Chris Ye2ad95392020-09-01 13:44:44 -07002454 sendAndConsumeKeyUp(1 /* deviceId */);
2455 mWindow->assertNoEvents();
2456}
2457
2458TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
2459 sendAndConsumeKeyDown(1 /* deviceId */);
2460 expectKeyRepeatOnce(1 /*repeatCount*/);
2461 sendAndConsumeKeyDown(2 /* deviceId */);
2462 expectKeyRepeatOnce(1 /*repeatCount*/);
2463 // Stale key up from device 1.
2464 sendAndConsumeKeyUp(1 /* deviceId */);
2465 // Device 2 is still down, keep repeating
2466 expectKeyRepeatOnce(2 /*repeatCount*/);
2467 expectKeyRepeatOnce(3 /*repeatCount*/);
2468 // Device 2 key up
2469 sendAndConsumeKeyUp(2 /* deviceId */);
2470 mWindow->assertNoEvents();
2471}
2472
2473TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
2474 sendAndConsumeKeyDown(1 /* deviceId */);
2475 expectKeyRepeatOnce(1 /*repeatCount*/);
2476 sendAndConsumeKeyDown(2 /* deviceId */);
2477 expectKeyRepeatOnce(1 /*repeatCount*/);
2478 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
2479 sendAndConsumeKeyUp(2 /* deviceId */);
2480 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08002481 mWindow->assertNoEvents();
2482}
2483
2484TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Chris Ye2ad95392020-09-01 13:44:44 -07002485 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002486 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2487 InputEvent* repeatEvent = mWindow->consume();
2488 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2489 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
2490 IdGenerator::getSource(repeatEvent->getId()));
2491 }
2492}
2493
2494TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Chris Ye2ad95392020-09-01 13:44:44 -07002495 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002496
2497 std::unordered_set<int32_t> idSet;
2498 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2499 InputEvent* repeatEvent = mWindow->consume();
2500 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2501 int32_t id = repeatEvent->getId();
2502 EXPECT_EQ(idSet.end(), idSet.find(id));
2503 idSet.insert(id);
2504 }
2505}
2506
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002507/* Test InputDispatcher for MultiDisplay */
2508class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
2509public:
2510 static constexpr int32_t SECOND_DISPLAY_ID = 1;
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002511 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002512 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08002513
Chris Yea209fde2020-07-22 13:54:51 -07002514 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002515 windowInPrimary =
2516 new FakeWindowHandle(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002517
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002518 // Set focus window for primary display, but focused display would be second one.
2519 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07002520 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002521 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002522 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002523 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08002524
Chris Yea209fde2020-07-22 13:54:51 -07002525 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002526 windowInSecondary =
2527 new FakeWindowHandle(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002528 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002529 // Set focus display to second one.
2530 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
2531 // Set focus window for second display.
2532 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07002533 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002534 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002535 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002536 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002537 }
2538
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002539 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002540 InputDispatcherTest::TearDown();
2541
Chris Yea209fde2020-07-22 13:54:51 -07002542 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002543 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07002544 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002545 windowInSecondary.clear();
2546 }
2547
2548protected:
Chris Yea209fde2020-07-22 13:54:51 -07002549 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002550 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07002551 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002552 sp<FakeWindowHandle> windowInSecondary;
2553};
2554
2555TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
2556 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002557 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2558 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2559 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002560 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08002561 windowInSecondary->assertNoEvents();
2562
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002563 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002564 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2565 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
2566 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08002567 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002568 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08002569}
2570
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002571TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08002572 // Test inject a key down with display id specified.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002573 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2574 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002575 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08002576 windowInSecondary->assertNoEvents();
2577
2578 // Test inject a key down without display id specified.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002579 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2580 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08002581 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002582 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08002583
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002584 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00002585 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08002586
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002587 // Old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002588 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
2589 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08002590
2591 // Test inject a key down, should timeout because of no target window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002592 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2593 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08002594 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002595 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08002596 windowInSecondary->assertNoEvents();
2597}
2598
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002599// Test per-display input monitors for motion event.
2600TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08002601 FakeMonitorReceiver monitorInPrimary =
2602 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
2603 FakeMonitorReceiver monitorInSecondary =
2604 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002605
2606 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002607 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2608 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2609 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002610 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002611 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002612 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002613 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002614
2615 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002616 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2617 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
2618 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002619 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002620 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002621 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08002622 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002623
2624 // Test inject a non-pointer motion event.
2625 // If specific a display, it will dispatch to the focused window of particular display,
2626 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002627 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2628 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
2629 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002630 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002631 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002632 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08002633 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002634}
2635
2636// Test per-display input monitors for key event.
2637TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002638 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08002639 FakeMonitorReceiver monitorInPrimary =
2640 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
2641 FakeMonitorReceiver monitorInSecondary =
2642 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002643
2644 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002645 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2646 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002647 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002648 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002649 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08002650 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002651}
2652
Vishnu Nair958da932020-08-21 17:12:37 -07002653TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
2654 sp<FakeWindowHandle> secondWindowInPrimary =
2655 new FakeWindowHandle(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
2656 secondWindowInPrimary->setFocusable(true);
2657 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
2658 setFocusedWindow(secondWindowInPrimary);
2659 windowInPrimary->consumeFocusEvent(false);
2660 secondWindowInPrimary->consumeFocusEvent(true);
2661
2662 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002663 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2664 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002665 windowInPrimary->assertNoEvents();
2666 windowInSecondary->assertNoEvents();
2667 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2668}
2669
Jackal Guof9696682018-10-05 12:23:23 +08002670class InputFilterTest : public InputDispatcherTest {
2671protected:
2672 static constexpr int32_t SECOND_DISPLAY_ID = 1;
2673
2674 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) {
2675 NotifyMotionArgs motionArgs;
2676
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002677 motionArgs =
2678 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08002679 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002680 motionArgs =
2681 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08002682 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002683 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08002684 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08002685 mFakePolicy->assertFilterInputEventWasCalled(motionArgs);
Jackal Guof9696682018-10-05 12:23:23 +08002686 } else {
2687 mFakePolicy->assertFilterInputEventWasNotCalled();
2688 }
2689 }
2690
2691 void testNotifyKey(bool expectToBeFiltered) {
2692 NotifyKeyArgs keyArgs;
2693
2694 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
2695 mDispatcher->notifyKey(&keyArgs);
2696 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
2697 mDispatcher->notifyKey(&keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002698 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08002699
2700 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08002701 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08002702 } else {
2703 mFakePolicy->assertFilterInputEventWasNotCalled();
2704 }
2705 }
2706};
2707
2708// Test InputFilter for MotionEvent
2709TEST_F(InputFilterTest, MotionEvent_InputFilter) {
2710 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
2711 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
2712 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
2713
2714 // Enable InputFilter
2715 mDispatcher->setInputFilterEnabled(true);
2716 // Test touch on both primary and second display, and check if both events are filtered.
2717 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
2718 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
2719
2720 // Disable InputFilter
2721 mDispatcher->setInputFilterEnabled(false);
2722 // Test touch on both primary and second display, and check if both events aren't filtered.
2723 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
2724 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
2725}
2726
2727// Test InputFilter for KeyEvent
2728TEST_F(InputFilterTest, KeyEvent_InputFilter) {
2729 // Since the InputFilter is disabled by default, check if key event aren't filtered.
2730 testNotifyKey(/*expectToBeFiltered*/ false);
2731
2732 // Enable InputFilter
2733 mDispatcher->setInputFilterEnabled(true);
2734 // Send a key event, and check if it is filtered.
2735 testNotifyKey(/*expectToBeFiltered*/ true);
2736
2737 // Disable InputFilter
2738 mDispatcher->setInputFilterEnabled(false);
2739 // Send a key event, and check if it isn't filtered.
2740 testNotifyKey(/*expectToBeFiltered*/ false);
2741}
2742
chaviwfd6d3512019-03-25 13:23:49 -07002743class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002744 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07002745 InputDispatcherTest::SetUp();
2746
Chris Yea209fde2020-07-22 13:54:51 -07002747 std::shared_ptr<FakeApplicationHandle> application =
2748 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002749 mUnfocusedWindow =
2750 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07002751 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
2752 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
2753 // window.
Michael Wright44753b12020-07-08 13:48:11 +01002754 mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07002755
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002756 mFocusedWindow =
2757 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2758 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01002759 mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07002760
2761 // Set focused application.
2762 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002763 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07002764
2765 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00002766 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002767 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002768 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07002769 }
2770
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002771 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07002772 InputDispatcherTest::TearDown();
2773
2774 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002775 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07002776 }
2777
2778protected:
2779 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002780 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002781 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07002782};
2783
2784// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
2785// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
2786// the onPointerDownOutsideFocus callback.
2787TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002788 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002789 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2790 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002791 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002792 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07002793
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002794 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07002795 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
2796}
2797
2798// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
2799// DOWN on the window that doesn't have focus. Ensure no window received the
2800// onPointerDownOutsideFocus callback.
2801TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002802 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002803 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002804 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002805 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07002806
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002807 ASSERT_TRUE(mDispatcher->waitForIdle());
2808 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07002809}
2810
2811// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
2812// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
2813TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002814 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2815 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002816 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07002817
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002818 ASSERT_TRUE(mDispatcher->waitForIdle());
2819 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07002820}
2821
2822// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
2823// DOWN on the window that already has focus. Ensure no window received the
2824// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002825TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002826 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002827 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002828 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002829 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002830 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07002831
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002832 ASSERT_TRUE(mDispatcher->waitForIdle());
2833 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07002834}
2835
chaviwaf87b3e2019-10-01 16:59:28 -07002836// These tests ensures we can send touch events to a single client when there are multiple input
2837// windows that point to the same client token.
2838class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
2839 virtual void SetUp() override {
2840 InputDispatcherTest::SetUp();
2841
Chris Yea209fde2020-07-22 13:54:51 -07002842 std::shared_ptr<FakeApplicationHandle> application =
2843 std::make_shared<FakeApplicationHandle>();
chaviwaf87b3e2019-10-01 16:59:28 -07002844 mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1",
2845 ADISPLAY_ID_DEFAULT);
2846 // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window.
2847 // 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 +01002848 mWindow1->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
2849 InputWindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07002850 mWindow1->setFrame(Rect(0, 0, 100, 100));
2851
2852 mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2",
2853 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
Michael Wright44753b12020-07-08 13:48:11 +01002854 mWindow2->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
2855 InputWindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07002856 mWindow2->setFrame(Rect(100, 100, 200, 200));
2857
Arthur Hung72d8dc32020-03-28 00:48:39 +00002858 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07002859 }
2860
2861protected:
2862 sp<FakeWindowHandle> mWindow1;
2863 sp<FakeWindowHandle> mWindow2;
2864
2865 // Helper function to convert the point from screen coordinates into the window's space
2866 static PointF getPointInWindow(const InputWindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07002867 vec2 vals = windowInfo->transform.transform(point.x, point.y);
2868 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07002869 }
2870
2871 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
2872 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002873 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07002874 InputEvent* event = window->consume();
2875
2876 ASSERT_NE(nullptr, event) << name.c_str()
2877 << ": consumer should have returned non-NULL event.";
2878
2879 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
2880 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
2881 << " event, got " << inputEventTypeToString(event->getType()) << " event";
2882
2883 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
2884 EXPECT_EQ(expectedAction, motionEvent.getAction());
2885
2886 for (size_t i = 0; i < points.size(); i++) {
2887 float expectedX = points[i].x;
2888 float expectedY = points[i].y;
2889
2890 EXPECT_EQ(expectedX, motionEvent.getX(i))
2891 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
2892 << ", got " << motionEvent.getX(i);
2893 EXPECT_EQ(expectedY, motionEvent.getY(i))
2894 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
2895 << ", got " << motionEvent.getY(i);
2896 }
2897 }
chaviw9eaa22c2020-07-01 16:21:27 -07002898
2899 void touchAndAssertPositions(int32_t action, std::vector<PointF> touchedPoints,
2900 std::vector<PointF> expectedPoints) {
2901 NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
2902 ADISPLAY_ID_DEFAULT, touchedPoints);
2903 mDispatcher->notifyMotion(&motionArgs);
2904
2905 // Always consume from window1 since it's the window that has the InputReceiver
2906 consumeMotionEvent(mWindow1, action, expectedPoints);
2907 }
chaviwaf87b3e2019-10-01 16:59:28 -07002908};
2909
2910TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
2911 // Touch Window 1
2912 PointF touchedPoint = {10, 10};
2913 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07002914 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07002915
2916 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07002917 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07002918
2919 // Touch Window 2
2920 touchedPoint = {150, 150};
2921 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07002922 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07002923}
2924
chaviw9eaa22c2020-07-01 16:21:27 -07002925TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
2926 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07002927 mWindow2->setWindowScale(0.5f, 0.5f);
2928
2929 // Touch Window 1
2930 PointF touchedPoint = {10, 10};
2931 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07002932 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07002933 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07002934 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07002935
2936 // Touch Window 2
2937 touchedPoint = {150, 150};
2938 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07002939 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
2940 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07002941
chaviw9eaa22c2020-07-01 16:21:27 -07002942 // Update the transform so rotation is set
2943 mWindow2->setWindowTransform(0, -1, 1, 0);
2944 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
2945 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07002946}
2947
chaviw9eaa22c2020-07-01 16:21:27 -07002948TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002949 mWindow2->setWindowScale(0.5f, 0.5f);
2950
2951 // Touch Window 1
2952 std::vector<PointF> touchedPoints = {PointF{10, 10}};
2953 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07002954 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002955
2956 // Touch Window 2
2957 int32_t actionPointerDown =
2958 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07002959 touchedPoints.push_back(PointF{150, 150});
2960 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
2961 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002962
chaviw9eaa22c2020-07-01 16:21:27 -07002963 // Release Window 2
2964 int32_t actionPointerUp =
2965 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
2966 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
2967 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002968
chaviw9eaa22c2020-07-01 16:21:27 -07002969 // Update the transform so rotation is set for Window 2
2970 mWindow2->setWindowTransform(0, -1, 1, 0);
2971 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
2972 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002973}
2974
chaviw9eaa22c2020-07-01 16:21:27 -07002975TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002976 mWindow2->setWindowScale(0.5f, 0.5f);
2977
2978 // Touch Window 1
2979 std::vector<PointF> touchedPoints = {PointF{10, 10}};
2980 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07002981 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002982
2983 // Touch Window 2
2984 int32_t actionPointerDown =
2985 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07002986 touchedPoints.push_back(PointF{150, 150});
2987 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002988
chaviw9eaa22c2020-07-01 16:21:27 -07002989 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002990
2991 // Move both windows
2992 touchedPoints = {{20, 20}, {175, 175}};
2993 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
2994 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
2995
chaviw9eaa22c2020-07-01 16:21:27 -07002996 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002997
chaviw9eaa22c2020-07-01 16:21:27 -07002998 // Release Window 2
2999 int32_t actionPointerUp =
3000 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3001 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
3002 expectedPoints.pop_back();
3003
3004 // Touch Window 2
3005 mWindow2->setWindowTransform(0, -1, 1, 0);
3006 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
3007 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
3008
3009 // Move both windows
3010 touchedPoints = {{20, 20}, {175, 175}};
3011 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3012 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3013
3014 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003015}
3016
3017TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
3018 mWindow1->setWindowScale(0.5f, 0.5f);
3019
3020 // Touch Window 1
3021 std::vector<PointF> touchedPoints = {PointF{10, 10}};
3022 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07003023 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003024
3025 // Touch Window 2
3026 int32_t actionPointerDown =
3027 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07003028 touchedPoints.push_back(PointF{150, 150});
3029 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003030
chaviw9eaa22c2020-07-01 16:21:27 -07003031 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003032
3033 // Move both windows
3034 touchedPoints = {{20, 20}, {175, 175}};
3035 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
3036 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
3037
chaviw9eaa22c2020-07-01 16:21:27 -07003038 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003039}
3040
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003041class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
3042 virtual void SetUp() override {
3043 InputDispatcherTest::SetUp();
3044
Chris Yea209fde2020-07-22 13:54:51 -07003045 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003046 mApplication->setDispatchingTimeout(20ms);
3047 mWindow =
3048 new FakeWindowHandle(mApplication, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
3049 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05003050 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07003051 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003052 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3053 // window.
Michael Wright44753b12020-07-08 13:48:11 +01003054 mWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003055
3056 // Set focused application.
3057 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
3058
3059 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003060 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003061 mWindow->consumeFocusEvent(true);
3062 }
3063
3064 virtual void TearDown() override {
3065 InputDispatcherTest::TearDown();
3066 mWindow.clear();
3067 }
3068
3069protected:
Chris Yea209fde2020-07-22 13:54:51 -07003070 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003071 sp<FakeWindowHandle> mWindow;
3072 static constexpr PointF WINDOW_LOCATION = {20, 20};
3073
3074 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003075 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003076 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3077 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003078 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003079 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3080 WINDOW_LOCATION));
3081 }
3082};
3083
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003084// Send a tap and respond, which should not cause an ANR.
3085TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
3086 tapOnWindow();
3087 mWindow->consumeMotionDown();
3088 mWindow->consumeMotionUp();
3089 ASSERT_TRUE(mDispatcher->waitForIdle());
3090 mFakePolicy->assertNotifyAnrWasNotCalled();
3091}
3092
3093// Send a regular key and respond, which should not cause an ANR.
3094TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003095 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003096 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
3097 ASSERT_TRUE(mDispatcher->waitForIdle());
3098 mFakePolicy->assertNotifyAnrWasNotCalled();
3099}
3100
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003101TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
3102 mWindow->setFocusable(false);
3103 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3104 mWindow->consumeFocusEvent(false);
3105
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003106 InputEventInjectionResult result =
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003107 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003108 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
3109 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05003110 // Key will not go to window because we have no focused window.
3111 // The 'no focused window' ANR timer should start instead.
3112
3113 // Now, the focused application goes away.
3114 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
3115 // The key should get dropped and there should be no ANR.
3116
3117 ASSERT_TRUE(mDispatcher->waitForIdle());
3118 mFakePolicy->assertNotifyAnrWasNotCalled();
3119}
3120
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003121// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003122// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
3123// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003124TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003125 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003126 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3127 WINDOW_LOCATION));
3128
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003129 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
3130 ASSERT_TRUE(sequenceNum);
3131 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003132 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003133
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003134 mWindow->finishEvent(*sequenceNum);
3135 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3136 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003137 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003138 mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003139}
3140
3141// Send a key to the app and have the app not respond right away.
3142TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
3143 // Inject a key, and don't respond - expect that ANR is called.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003144 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003145 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
3146 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003147 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003148 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003149 ASSERT_TRUE(mDispatcher->waitForIdle());
3150}
3151
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003152// We have a focused application, but no focused window
3153TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003154 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003155 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3156 mWindow->consumeFocusEvent(false);
3157
3158 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003159 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003160 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3161 WINDOW_LOCATION));
3162 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
3163 mDispatcher->waitForIdle();
3164 mFakePolicy->assertNotifyAnrWasNotCalled();
3165
3166 // Once a focused event arrives, we get an ANR for this application
3167 // We specify the injection timeout to be smaller than the application timeout, to ensure that
3168 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003169 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003170 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003171 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3172 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003173 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003174 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003175 ASSERT_TRUE(mDispatcher->waitForIdle());
3176}
3177
3178// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003179// Make sure that we don't notify policy twice about the same ANR.
3180TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003181 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003182 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3183 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003184
3185 // Once a focused event arrives, we get an ANR for this application
3186 // We specify the injection timeout to be smaller than the application timeout, to ensure that
3187 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003188 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003189 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003190 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3191 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003192 const std::chrono::duration appTimeout =
3193 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003194 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003195
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003196 std::this_thread::sleep_for(appTimeout);
3197 // ANR should not be raised again. It is up to policy to do that if it desires.
3198 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003199
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003200 // If we now get a focused window, the ANR should stop, but the policy handles that via
3201 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003202 ASSERT_TRUE(mDispatcher->waitForIdle());
3203}
3204
3205// We have a focused application, but no focused window
3206TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07003207 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003208 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3209 mWindow->consumeFocusEvent(false);
3210
3211 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003212 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003213 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003214 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3215 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003216
3217 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003218 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003219
3220 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003221 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003222 ASSERT_TRUE(mDispatcher->waitForIdle());
3223 mWindow->assertNoEvents();
3224}
3225
3226/**
3227 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
3228 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
3229 * If we process 1 of the events, but ANR on the second event with the same timestamp,
3230 * the ANR mechanism should still work.
3231 *
3232 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
3233 * DOWN event, while not responding on the second one.
3234 */
3235TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
3236 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
3237 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3238 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
3239 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
3240 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003241 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003242
3243 // Now send ACTION_UP, with identical timestamp
3244 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3245 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
3246 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
3247 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003248 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003249
3250 // We have now sent down and up. Let's consume first event and then ANR on the second.
3251 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3252 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003253 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003254}
3255
3256// If an app is not responding to a key event, gesture monitors should continue to receive
3257// new motion events
3258TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnKey) {
3259 FakeMonitorReceiver monitor =
3260 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
3261 true /*isGestureMonitor*/);
3262
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003263 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3264 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003265 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003266 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003267
3268 // Stuck on the ACTION_UP
3269 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003270 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003271
3272 // New tap will go to the gesture monitor, but not to the window
3273 tapOnWindow();
3274 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3275 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3276
3277 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
3278 mDispatcher->waitForIdle();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003279 mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003280 mWindow->assertNoEvents();
3281 monitor.assertNoEvents();
3282}
3283
3284// If an app is not responding to a motion event, gesture monitors should continue to receive
3285// new motion events
3286TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnMotion) {
3287 FakeMonitorReceiver monitor =
3288 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
3289 true /*isGestureMonitor*/);
3290
3291 tapOnWindow();
3292 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3293 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3294
3295 mWindow->consumeMotionDown();
3296 // Stuck on the ACTION_UP
3297 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003298 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003299
3300 // New tap will go to the gesture monitor, but not to the window
3301 tapOnWindow();
3302 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3303 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3304
3305 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
3306 mDispatcher->waitForIdle();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003307 mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003308 mWindow->assertNoEvents();
3309 monitor.assertNoEvents();
3310}
3311
3312// If a window is unresponsive, then you get anr. if the window later catches up and starts to
3313// process events, you don't get an anr. When the window later becomes unresponsive again, you
3314// get an ANR again.
3315// 1. tap -> block on ACTION_UP -> receive ANR
3316// 2. consume all pending events (= queue becomes healthy again)
3317// 3. tap again -> block on ACTION_UP again -> receive ANR second time
3318TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
3319 tapOnWindow();
3320
3321 mWindow->consumeMotionDown();
3322 // Block on ACTION_UP
3323 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003324 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003325 mWindow->consumeMotionUp(); // Now the connection should be healthy again
3326 mDispatcher->waitForIdle();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003327 mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003328 mWindow->assertNoEvents();
3329
3330 tapOnWindow();
3331 mWindow->consumeMotionDown();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003332 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003333 mWindow->consumeMotionUp();
3334
3335 mDispatcher->waitForIdle();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003336 mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mWindow->getToken());
3337 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003338 mWindow->assertNoEvents();
3339}
3340
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003341// If a connection remains unresponsive for a while, make sure policy is only notified once about
3342// it.
3343TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003344 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003345 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3346 WINDOW_LOCATION));
3347
3348 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003349 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(windowTimeout, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003350 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003351 // 'notifyConnectionUnresponsive' should only be called once per connection
3352 mFakePolicy->assertNotifyAnrWasNotCalled();
3353 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003354 mWindow->consumeMotionDown();
3355 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3356 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3357 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003358 mDispatcher->waitForIdle();
3359 mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mWindow->getToken());
3360 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003361}
3362
3363/**
3364 * If a window is processing a motion event, and then a key event comes in, the key event should
3365 * not to to the focused window until the motion is processed.
3366 *
3367 * Warning!!!
3368 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
3369 * and the injection timeout that we specify when injecting the key.
3370 * We must have the injection timeout (10ms) be smaller than
3371 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
3372 *
3373 * If that value changes, this test should also change.
3374 */
3375TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
3376 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3377 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3378
3379 tapOnWindow();
3380 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3381 ASSERT_TRUE(downSequenceNum);
3382 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3383 ASSERT_TRUE(upSequenceNum);
3384 // Don't finish the events yet, and send a key
3385 // Injection will "succeed" because we will eventually give up and send the key to the focused
3386 // window even if motions are still being processed. But because the injection timeout is short,
3387 // we will receive INJECTION_TIMED_OUT as the result.
3388
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003389 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003390 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003391 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3392 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003393 // Key will not be sent to the window, yet, because the window is still processing events
3394 // and the key remains pending, waiting for the touch events to be processed
3395 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3396 ASSERT_FALSE(keySequenceNum);
3397
3398 std::this_thread::sleep_for(500ms);
3399 // if we wait long enough though, dispatcher will give up, and still send the key
3400 // to the focused window, even though we have not yet finished the motion event
3401 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3402 mWindow->finishEvent(*downSequenceNum);
3403 mWindow->finishEvent(*upSequenceNum);
3404}
3405
3406/**
3407 * If a window is processing a motion event, and then a key event comes in, the key event should
3408 * not go to the focused window until the motion is processed.
3409 * If then a new motion comes in, then the pending key event should be going to the currently
3410 * focused window right away.
3411 */
3412TEST_F(InputDispatcherSingleWindowAnr,
3413 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
3414 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3415 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3416
3417 tapOnWindow();
3418 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3419 ASSERT_TRUE(downSequenceNum);
3420 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3421 ASSERT_TRUE(upSequenceNum);
3422 // Don't finish the events yet, and send a key
3423 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003424 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003425 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003426 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003427 // At this point, key is still pending, and should not be sent to the application yet.
3428 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3429 ASSERT_FALSE(keySequenceNum);
3430
3431 // Now tap down again. It should cause the pending key to go to the focused window right away.
3432 tapOnWindow();
3433 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
3434 // the other events yet. We can finish events in any order.
3435 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
3436 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
3437 mWindow->consumeMotionDown();
3438 mWindow->consumeMotionUp();
3439 mWindow->assertNoEvents();
3440}
3441
3442class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
3443 virtual void SetUp() override {
3444 InputDispatcherTest::SetUp();
3445
Chris Yea209fde2020-07-22 13:54:51 -07003446 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003447 mApplication->setDispatchingTimeout(10ms);
3448 mUnfocusedWindow =
3449 new FakeWindowHandle(mApplication, mDispatcher, "Unfocused", ADISPLAY_ID_DEFAULT);
3450 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
3451 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3452 // window.
3453 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Michael Wright44753b12020-07-08 13:48:11 +01003454 mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3455 InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH |
3456 InputWindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003457
3458 mFocusedWindow =
3459 new FakeWindowHandle(mApplication, mDispatcher, "Focused", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05003460 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003461 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01003462 mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3463 InputWindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003464
3465 // Set focused application.
3466 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07003467 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003468
3469 // Expect one focus window exist in display.
3470 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003471 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003472 mFocusedWindow->consumeFocusEvent(true);
3473 }
3474
3475 virtual void TearDown() override {
3476 InputDispatcherTest::TearDown();
3477
3478 mUnfocusedWindow.clear();
3479 mFocusedWindow.clear();
3480 }
3481
3482protected:
Chris Yea209fde2020-07-22 13:54:51 -07003483 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003484 sp<FakeWindowHandle> mUnfocusedWindow;
3485 sp<FakeWindowHandle> mFocusedWindow;
3486 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
3487 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
3488 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
3489
3490 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
3491
3492 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
3493
3494private:
3495 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003496 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003497 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3498 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003499 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003500 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3501 location));
3502 }
3503};
3504
3505// If we have 2 windows that are both unresponsive, the one with the shortest timeout
3506// should be ANR'd first.
3507TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003508 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003509 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3510 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003511 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003512 mFocusedWindow->consumeMotionDown();
3513 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3514 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3515 // We consumed all events, so no ANR
3516 ASSERT_TRUE(mDispatcher->waitForIdle());
3517 mFakePolicy->assertNotifyAnrWasNotCalled();
3518
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003519 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003520 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3521 FOCUSED_WINDOW_LOCATION));
3522 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
3523 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003524
3525 const std::chrono::duration timeout =
3526 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003527 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
3528 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
3529 // sequence to make it consistent
3530 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003531 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003532 mFocusedWindow->consumeMotionDown();
3533 // This cancel is generated because the connection was unresponsive
3534 mFocusedWindow->consumeMotionCancel();
3535 mFocusedWindow->assertNoEvents();
3536 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003537 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003538 mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mFocusedWindow->getToken());
3539 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003540}
3541
3542// If we have 2 windows with identical timeouts that are both unresponsive,
3543// it doesn't matter which order they should have ANR.
3544// But we should receive ANR for both.
3545TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
3546 // Set the timeout for unfocused window to match the focused window
3547 mUnfocusedWindow->setDispatchingTimeout(10ms);
3548 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
3549
3550 tapOnFocusedWindow();
3551 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003552 sp<IBinder> anrConnectionToken1 = mFakePolicy->getUnresponsiveConnectionToken(10ms);
3553 sp<IBinder> anrConnectionToken2 = mFakePolicy->getUnresponsiveConnectionToken(0ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003554
3555 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003556 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
3557 mFocusedWindow->getToken() == anrConnectionToken2);
3558 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
3559 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003560
3561 ASSERT_TRUE(mDispatcher->waitForIdle());
3562 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003563
3564 mFocusedWindow->consumeMotionDown();
3565 mFocusedWindow->consumeMotionUp();
3566 mUnfocusedWindow->consumeMotionOutside();
3567
3568 sp<IBinder> responsiveToken1 = mFakePolicy->getResponsiveConnectionToken();
3569 sp<IBinder> responsiveToken2 = mFakePolicy->getResponsiveConnectionToken();
3570
3571 // Both applications should be marked as responsive, in any order
3572 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
3573 mFocusedWindow->getToken() == responsiveToken2);
3574 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
3575 mUnfocusedWindow->getToken() == responsiveToken2);
3576 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003577}
3578
3579// If a window is already not responding, the second tap on the same window should be ignored.
3580// We should also log an error to account for the dropped event (not tested here).
3581// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
3582TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
3583 tapOnFocusedWindow();
3584 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3585 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3586 // Receive the events, but don't respond
3587 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
3588 ASSERT_TRUE(downEventSequenceNum);
3589 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
3590 ASSERT_TRUE(upEventSequenceNum);
3591 const std::chrono::duration timeout =
3592 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003593 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003594
3595 // Tap once again
3596 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003597 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003598 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3599 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003600 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003601 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3602 FOCUSED_WINDOW_LOCATION));
3603 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
3604 // valid touch target
3605 mUnfocusedWindow->assertNoEvents();
3606
3607 // Consume the first tap
3608 mFocusedWindow->finishEvent(*downEventSequenceNum);
3609 mFocusedWindow->finishEvent(*upEventSequenceNum);
3610 ASSERT_TRUE(mDispatcher->waitForIdle());
3611 // The second tap did not go to the focused window
3612 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003613 // Since all events are finished, connection should be deemed healthy again
3614 mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003615 mFakePolicy->assertNotifyAnrWasNotCalled();
3616}
3617
3618// If you tap outside of all windows, there will not be ANR
3619TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003620 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003621 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3622 LOCATION_OUTSIDE_ALL_WINDOWS));
3623 ASSERT_TRUE(mDispatcher->waitForIdle());
3624 mFakePolicy->assertNotifyAnrWasNotCalled();
3625}
3626
3627// Since the focused window is paused, tapping on it should not produce any events
3628TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
3629 mFocusedWindow->setPaused(true);
3630 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
3631
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003632 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003633 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3634 FOCUSED_WINDOW_LOCATION));
3635
3636 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
3637 ASSERT_TRUE(mDispatcher->waitForIdle());
3638 // Should not ANR because the window is paused, and touches shouldn't go to it
3639 mFakePolicy->assertNotifyAnrWasNotCalled();
3640
3641 mFocusedWindow->assertNoEvents();
3642 mUnfocusedWindow->assertNoEvents();
3643}
3644
3645/**
3646 * If a window is processing a motion event, and then a key event comes in, the key event should
3647 * not to to the focused window until the motion is processed.
3648 * If a different window becomes focused at this time, the key should go to that window instead.
3649 *
3650 * Warning!!!
3651 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
3652 * and the injection timeout that we specify when injecting the key.
3653 * We must have the injection timeout (10ms) be smaller than
3654 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
3655 *
3656 * If that value changes, this test should also change.
3657 */
3658TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
3659 // Set a long ANR timeout to prevent it from triggering
3660 mFocusedWindow->setDispatchingTimeout(2s);
3661 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
3662
3663 tapOnUnfocusedWindow();
3664 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
3665 ASSERT_TRUE(downSequenceNum);
3666 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
3667 ASSERT_TRUE(upSequenceNum);
3668 // Don't finish the events yet, and send a key
3669 // Injection will succeed because we will eventually give up and send the key to the focused
3670 // window even if motions are still being processed.
3671
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003672 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003673 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003674 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
3675 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003676 // Key will not be sent to the window, yet, because the window is still processing events
3677 // and the key remains pending, waiting for the touch events to be processed
3678 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
3679 ASSERT_FALSE(keySequenceNum);
3680
3681 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07003682 mFocusedWindow->setFocusable(false);
3683 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003684 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003685 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003686
3687 // Focus events should precede the key events
3688 mUnfocusedWindow->consumeFocusEvent(true);
3689 mFocusedWindow->consumeFocusEvent(false);
3690
3691 // Finish the tap events, which should unblock dispatcher
3692 mUnfocusedWindow->finishEvent(*downSequenceNum);
3693 mUnfocusedWindow->finishEvent(*upSequenceNum);
3694
3695 // Now that all queues are cleared and no backlog in the connections, the key event
3696 // can finally go to the newly focused "mUnfocusedWindow".
3697 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3698 mFocusedWindow->assertNoEvents();
3699 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003700 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003701}
3702
3703// When the touch stream is split across 2 windows, and one of them does not respond,
3704// then ANR should be raised and the touch should be canceled for the unresponsive window.
3705// The other window should not be affected by that.
3706TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
3707 // Touch Window 1
3708 NotifyMotionArgs motionArgs =
3709 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3710 ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION});
3711 mDispatcher->notifyMotion(&motionArgs);
3712 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3713 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3714
3715 // Touch Window 2
3716 int32_t actionPointerDown =
3717 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3718
3719 motionArgs =
3720 generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3721 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION});
3722 mDispatcher->notifyMotion(&motionArgs);
3723
3724 const std::chrono::duration timeout =
3725 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003726 mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mFocusedWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003727
3728 mUnfocusedWindow->consumeMotionDown();
3729 mFocusedWindow->consumeMotionDown();
3730 // Focused window may or may not receive ACTION_MOVE
3731 // But it should definitely receive ACTION_CANCEL due to the ANR
3732 InputEvent* event;
3733 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
3734 ASSERT_TRUE(moveOrCancelSequenceNum);
3735 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
3736 ASSERT_NE(nullptr, event);
3737 ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION);
3738 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
3739 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
3740 mFocusedWindow->consumeMotionCancel();
3741 } else {
3742 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
3743 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003744 ASSERT_TRUE(mDispatcher->waitForIdle());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003745 mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mFocusedWindow->getToken());
3746
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003747 mUnfocusedWindow->assertNoEvents();
3748 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05003749 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003750}
3751
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05003752/**
3753 * If we have no focused window, and a key comes in, we start the ANR timer.
3754 * The focused application should add a focused window before the timer runs out to prevent ANR.
3755 *
3756 * If the user touches another application during this time, the key should be dropped.
3757 * Next, if a new focused window comes in, without toggling the focused application,
3758 * then no ANR should occur.
3759 *
3760 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
3761 * but in some cases the policy may not update the focused application.
3762 */
3763TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
3764 std::shared_ptr<FakeApplicationHandle> focusedApplication =
3765 std::make_shared<FakeApplicationHandle>();
3766 focusedApplication->setDispatchingTimeout(60ms);
3767 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
3768 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
3769 mFocusedWindow->setFocusable(false);
3770
3771 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
3772 mFocusedWindow->consumeFocusEvent(false);
3773
3774 // Send a key. The ANR timer should start because there is no focused window.
3775 // 'focusedApplication' will get blamed if this timer completes.
3776 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003777 InputEventInjectionResult result =
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05003778 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003779 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
3780 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05003781
3782 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
3783 // then the injected touches won't cause the focused event to get dropped.
3784 // The dispatcher only checks for whether the queue should be pruned upon queueing.
3785 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
3786 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
3787 // For this test, it means that the key would get delivered to the window once it becomes
3788 // focused.
3789 std::this_thread::sleep_for(10ms);
3790
3791 // Touch unfocused window. This should force the pending key to get dropped.
3792 NotifyMotionArgs motionArgs =
3793 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3794 ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION});
3795 mDispatcher->notifyMotion(&motionArgs);
3796
3797 // We do not consume the motion right away, because that would require dispatcher to first
3798 // process (== drop) the key event, and by that time, ANR will be raised.
3799 // Set the focused window first.
3800 mFocusedWindow->setFocusable(true);
3801 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
3802 setFocusedWindow(mFocusedWindow);
3803 mFocusedWindow->consumeFocusEvent(true);
3804 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
3805 // to another application. This could be a bug / behaviour in the policy.
3806
3807 mUnfocusedWindow->consumeMotionDown();
3808
3809 ASSERT_TRUE(mDispatcher->waitForIdle());
3810 // Should not ANR because we actually have a focused window. It was just added too slowly.
3811 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
3812}
3813
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05003814// These tests ensure we cannot send touch events to a window that's positioned behind a window
3815// that has feature NO_INPUT_CHANNEL.
3816// Layout:
3817// Top (closest to user)
3818// mNoInputWindow (above all windows)
3819// mBottomWindow
3820// Bottom (furthest from user)
3821class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
3822 virtual void SetUp() override {
3823 InputDispatcherTest::SetUp();
3824
3825 mApplication = std::make_shared<FakeApplicationHandle>();
3826 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
3827 "Window without input channel", ADISPLAY_ID_DEFAULT,
3828 std::make_optional<sp<IBinder>>(nullptr) /*token*/);
3829
3830 mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL);
3831 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
3832 // It's perfectly valid for this window to not have an associated input channel
3833
3834 mBottomWindow = new FakeWindowHandle(mApplication, mDispatcher, "Bottom window",
3835 ADISPLAY_ID_DEFAULT);
3836 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
3837
3838 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
3839 }
3840
3841protected:
3842 std::shared_ptr<FakeApplicationHandle> mApplication;
3843 sp<FakeWindowHandle> mNoInputWindow;
3844 sp<FakeWindowHandle> mBottomWindow;
3845};
3846
3847TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
3848 PointF touchedPoint = {10, 10};
3849
3850 NotifyMotionArgs motionArgs =
3851 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3852 ADISPLAY_ID_DEFAULT, {touchedPoint});
3853 mDispatcher->notifyMotion(&motionArgs);
3854
3855 mNoInputWindow->assertNoEvents();
3856 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
3857 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
3858 // and therefore should prevent mBottomWindow from receiving touches
3859 mBottomWindow->assertNoEvents();
3860}
3861
3862/**
3863 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
3864 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
3865 */
3866TEST_F(InputDispatcherMultiWindowOcclusionTests,
3867 NoInputChannelFeature_DropsTouchesWithValidChannel) {
3868 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
3869 "Window with input channel and NO_INPUT_CHANNEL",
3870 ADISPLAY_ID_DEFAULT);
3871
3872 mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL);
3873 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
3874 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
3875
3876 PointF touchedPoint = {10, 10};
3877
3878 NotifyMotionArgs motionArgs =
3879 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3880 ADISPLAY_ID_DEFAULT, {touchedPoint});
3881 mDispatcher->notifyMotion(&motionArgs);
3882
3883 mNoInputWindow->assertNoEvents();
3884 mBottomWindow->assertNoEvents();
3885}
3886
Vishnu Nair958da932020-08-21 17:12:37 -07003887class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
3888protected:
3889 std::shared_ptr<FakeApplicationHandle> mApp;
3890 sp<FakeWindowHandle> mWindow;
3891 sp<FakeWindowHandle> mMirror;
3892
3893 virtual void SetUp() override {
3894 InputDispatcherTest::SetUp();
3895 mApp = std::make_shared<FakeApplicationHandle>();
3896 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
3897 mMirror = new FakeWindowHandle(mApp, mDispatcher, "TestWindowMirror", ADISPLAY_ID_DEFAULT,
3898 mWindow->getToken());
3899 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
3900 mWindow->setFocusable(true);
3901 mMirror->setFocusable(true);
3902 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
3903 }
3904};
3905
3906TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
3907 // Request focus on a mirrored window
3908 setFocusedWindow(mMirror);
3909
3910 // window gets focused
3911 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003912 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3913 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003914 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
3915}
3916
3917// A focused & mirrored window remains focused only if the window and its mirror are both
3918// focusable.
3919TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
3920 setFocusedWindow(mMirror);
3921
3922 // window gets focused
3923 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003924 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3925 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003926 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003927 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
3928 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003929 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
3930
3931 mMirror->setFocusable(false);
3932 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
3933
3934 // window loses focus since one of the windows associated with the token in not focusable
3935 mWindow->consumeFocusEvent(false);
3936
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003937 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
3938 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07003939 mWindow->assertNoEvents();
3940}
3941
3942// A focused & mirrored window remains focused until the window and its mirror both become
3943// invisible.
3944TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
3945 setFocusedWindow(mMirror);
3946
3947 // window gets focused
3948 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003949 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3950 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003951 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003952 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
3953 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003954 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
3955
3956 mMirror->setVisible(false);
3957 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
3958
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003959 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3960 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003961 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003962 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
3963 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003964 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
3965
3966 mWindow->setVisible(false);
3967 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
3968
3969 // window loses focus only after all windows associated with the token become invisible.
3970 mWindow->consumeFocusEvent(false);
3971
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003972 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
3973 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07003974 mWindow->assertNoEvents();
3975}
3976
3977// A focused & mirrored window remains focused until both windows are removed.
3978TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
3979 setFocusedWindow(mMirror);
3980
3981 // window gets focused
3982 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003983 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3984 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003985 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003986 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
3987 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003988 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
3989
3990 // single window is removed but the window token remains focused
3991 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
3992
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003993 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3994 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003995 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003996 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
3997 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003998 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
3999
4000 // Both windows are removed
4001 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
4002 mWindow->consumeFocusEvent(false);
4003
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004004 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
4005 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07004006 mWindow->assertNoEvents();
4007}
4008
4009// Focus request can be pending until one window becomes visible.
4010TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
4011 // Request focus on an invisible mirror.
4012 mWindow->setVisible(false);
4013 mMirror->setVisible(false);
4014 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4015 setFocusedWindow(mMirror);
4016
4017 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004018 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07004019 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004020 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07004021
4022 mMirror->setVisible(true);
4023 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
4024
4025 // window gets focused
4026 mWindow->consumeFocusEvent(true);
4027 // window gets the pending key event
4028 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4029}
Prabir Pradhan99987712020-11-10 18:43:05 -08004030
4031class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
4032protected:
4033 std::shared_ptr<FakeApplicationHandle> mApp;
4034 sp<FakeWindowHandle> mWindow;
4035 sp<FakeWindowHandle> mSecondWindow;
4036
4037 void SetUp() override {
4038 InputDispatcherTest::SetUp();
4039 mApp = std::make_shared<FakeApplicationHandle>();
4040 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
4041 mWindow->setFocusable(true);
4042 mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
4043 mSecondWindow->setFocusable(true);
4044
4045 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
4046 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
4047
4048 setFocusedWindow(mWindow);
4049 mWindow->consumeFocusEvent(true);
4050 }
4051
4052 void notifyPointerCaptureChanged(bool enabled) {
4053 const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(enabled);
4054 mDispatcher->notifyPointerCaptureChanged(&args);
4055 }
4056
4057 void requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, bool enabled) {
4058 mDispatcher->requestPointerCapture(window->getToken(), enabled);
4059 mFakePolicy->waitForSetPointerCapture(enabled);
4060 notifyPointerCaptureChanged(enabled);
4061 window->consumeCaptureEvent(enabled);
4062 }
4063};
4064
4065TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
4066 // Ensure that capture cannot be obtained for unfocused windows.
4067 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
4068 mFakePolicy->assertSetPointerCaptureNotCalled();
4069 mSecondWindow->assertNoEvents();
4070
4071 // Ensure that capture can be enabled from the focus window.
4072 requestAndVerifyPointerCapture(mWindow, true);
4073
4074 // Ensure that capture cannot be disabled from a window that does not have capture.
4075 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
4076 mFakePolicy->assertSetPointerCaptureNotCalled();
4077
4078 // Ensure that capture can be disabled from the window with capture.
4079 requestAndVerifyPointerCapture(mWindow, false);
4080}
4081
4082TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
4083 requestAndVerifyPointerCapture(mWindow, true);
4084
4085 setFocusedWindow(mSecondWindow);
4086
4087 // Ensure that the capture disabled event was sent first.
4088 mWindow->consumeCaptureEvent(false);
4089 mWindow->consumeFocusEvent(false);
4090 mSecondWindow->consumeFocusEvent(true);
4091 mFakePolicy->waitForSetPointerCapture(false);
4092
4093 // Ensure that additional state changes from InputReader are not sent to the window.
4094 notifyPointerCaptureChanged(false);
4095 notifyPointerCaptureChanged(true);
4096 notifyPointerCaptureChanged(false);
4097 mWindow->assertNoEvents();
4098 mSecondWindow->assertNoEvents();
4099 mFakePolicy->assertSetPointerCaptureNotCalled();
4100}
4101
4102TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
4103 requestAndVerifyPointerCapture(mWindow, true);
4104
4105 // InputReader unexpectedly disables and enables pointer capture.
4106 notifyPointerCaptureChanged(false);
4107 notifyPointerCaptureChanged(true);
4108
4109 // Ensure that Pointer Capture is disabled.
4110 mWindow->consumeCaptureEvent(false);
4111 mWindow->assertNoEvents();
4112}
4113
Garfield Tane84e6f92019-08-29 17:28:41 -07004114} // namespace android::inputdispatcher