blob: c7498061900a1d77dda705054d5bb635b0efe295 [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>
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080022#include <input/Input.h>
Robert Carr803535b2018-08-02 16:38:15 -070023
Michael Wrightd02c5b62014-02-10 15:10:22 -080024#include <gtest/gtest.h>
25#include <linux/input.h>
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;
Michael Wright44753b12020-07-08 13:48:11 +010032using namespace android::flag_operators;
Garfield Tan1c7bc862020-01-28 13:24:04 -080033
Garfield Tane84e6f92019-08-29 17:28:41 -070034namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080035
36// An arbitrary time value.
37static const nsecs_t ARBITRARY_TIME = 1234;
38
39// An arbitrary device id.
40static const int32_t DEVICE_ID = 1;
41
Jeff Brownf086ddb2014-02-11 14:28:48 -080042// An arbitrary display id.
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -080043static const int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
Jeff Brownf086ddb2014-02-11 14:28:48 -080044
Michael Wrightd02c5b62014-02-10 15:10:22 -080045// An arbitrary injector pid / uid pair that has permission to inject events.
46static const int32_t INJECTOR_PID = 999;
47static const int32_t INJECTOR_UID = 1001;
48
chaviwd1c23182019-12-20 18:44:56 -080049struct PointF {
50 float x;
51 float y;
52};
Michael Wrightd02c5b62014-02-10 15:10:22 -080053
Gang Wang342c9272020-01-13 13:15:04 -050054/**
55 * Return a DOWN key event with KEYCODE_A.
56 */
57static KeyEvent getTestKeyEvent() {
58 KeyEvent event;
59
Garfield Tanfbe732e2020-01-24 11:26:14 -080060 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
61 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
62 ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -050063 return event;
64}
65
Michael Wrightd02c5b62014-02-10 15:10:22 -080066// --- FakeInputDispatcherPolicy ---
67
68class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
69 InputDispatcherConfiguration mConfig;
70
71protected:
72 virtual ~FakeInputDispatcherPolicy() {
73 }
74
75public:
76 FakeInputDispatcherPolicy() {
Jackal Guof9696682018-10-05 12:23:23 +080077 }
78
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080079 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080080 assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_KEY, args.eventTime, args.action,
81 args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080082 }
83
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080084 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args) {
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080085 assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_MOTION, args.eventTime, args.action,
86 args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080087 }
88
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -070089 void assertFilterInputEventWasNotCalled() {
90 std::scoped_lock lock(mLock);
91 ASSERT_EQ(nullptr, mFilteredEvent);
92 }
Michael Wrightd02c5b62014-02-10 15:10:22 -080093
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -080094 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -070095 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -080096 ASSERT_TRUE(mConfigurationChangedTime)
97 << "Timed out waiting for configuration changed call";
98 ASSERT_EQ(*mConfigurationChangedTime, when);
99 mConfigurationChangedTime = std::nullopt;
100 }
101
102 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700103 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800104 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800105 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800106 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
107 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
108 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
109 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
110 mLastNotifySwitch = std::nullopt;
111 }
112
chaviwfd6d3512019-03-25 13:23:49 -0700113 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700114 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800115 ASSERT_EQ(touchedToken, mOnPointerDownToken);
116 mOnPointerDownToken.clear();
117 }
118
119 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700120 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800121 ASSERT_TRUE(mOnPointerDownToken == nullptr)
122 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700123 }
124
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700125 // This function must be called soon after the expected ANR timer starts,
126 // because we are also checking how much time has passed.
127 void assertNotifyAnrWasCalled(std::chrono::nanoseconds timeout,
128 const sp<InputApplicationHandle>& expectedApplication,
129 const sp<IBinder>& expectedToken) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700130 std::pair<sp<InputApplicationHandle>, sp<IBinder>> anrData;
131 ASSERT_NO_FATAL_FAILURE(anrData = getNotifyAnrData(timeout));
132 ASSERT_EQ(expectedApplication, anrData.first);
133 ASSERT_EQ(expectedToken, anrData.second);
134 }
135
136 std::pair<sp<InputApplicationHandle>, sp<IBinder>> getNotifyAnrData(
137 std::chrono::nanoseconds timeout) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700138 const std::chrono::time_point start = std::chrono::steady_clock::now();
139 std::unique_lock lock(mLock);
140 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
141 android::base::ScopedLockAssertion assumeLocked(mLock);
142
143 // If there is an ANR, Dispatcher won't be idle because there are still events
144 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
145 // before checking if ANR was called.
146 // Since dispatcher is not guaranteed to call notifyAnr right away, we need to provide
147 // it some time to act. 100ms seems reasonable.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700148 mNotifyAnr.wait_for(lock, timeToWait, [this]() REQUIRES(mLock) {
149 return !mAnrApplications.empty() && !mAnrWindowTokens.empty();
150 });
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700151 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700152 if (mAnrApplications.empty() || mAnrWindowTokens.empty()) {
153 ADD_FAILURE() << "Did not receive ANR callback";
154 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700155 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
156 // the dispatcher started counting before this function was called
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700157 if (std::chrono::abs(timeout - waited) > 100ms) {
158 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
159 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
160 << "ms, but waited "
161 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
162 << "ms instead";
163 }
164 std::pair<sp<InputApplicationHandle>, sp<IBinder>> result =
165 std::make_pair(mAnrApplications.front(), mAnrWindowTokens.front());
166 mAnrApplications.pop();
167 mAnrWindowTokens.pop();
168 return result;
169 }
170
171 void assertNotifyAnrWasNotCalled() {
172 std::scoped_lock lock(mLock);
173 ASSERT_TRUE(mAnrApplications.empty());
174 ASSERT_TRUE(mAnrWindowTokens.empty());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700175 }
176
Garfield Tan1c7bc862020-01-28 13:24:04 -0800177 void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) {
178 mConfig.keyRepeatTimeout = timeout;
179 mConfig.keyRepeatDelay = delay;
180 }
181
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700182 void setAnrTimeout(std::chrono::nanoseconds timeout) { mAnrTimeout = timeout; }
183
Michael Wrightd02c5b62014-02-10 15:10:22 -0800184private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700185 std::mutex mLock;
186 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
187 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
188 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
189 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800190
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700191 // ANR handling
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700192 std::queue<sp<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
193 std::queue<sp<IBinder>> mAnrWindowTokens GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700194 std::condition_variable mNotifyAnr;
195 std::chrono::nanoseconds mAnrTimeout = 0ms;
196
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800197 virtual void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700198 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800199 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800200 }
201
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500202 std::chrono::nanoseconds notifyAnr(const sp<InputApplicationHandle>& application,
203 const sp<IBinder>& windowToken,
204 const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700205 std::scoped_lock lock(mLock);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700206 mAnrApplications.push(application);
207 mAnrWindowTokens.push(windowToken);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700208 mNotifyAnr.notify_all();
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500209 return mAnrTimeout;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800210 }
211
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700212 virtual void notifyInputChannelBroken(const sp<IBinder>&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800213
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700214 virtual void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700215
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700216 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800217 *outConfig = mConfig;
218 }
219
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800220 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700221 std::scoped_lock lock(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800222 switch (inputEvent->getType()) {
223 case AINPUT_EVENT_TYPE_KEY: {
224 const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800225 mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800226 break;
227 }
228
229 case AINPUT_EVENT_TYPE_MOTION: {
230 const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800231 mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800232 break;
233 }
234 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800235 return true;
236 }
237
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700238 virtual void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800239
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700240 virtual void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800241
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700242 virtual nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*,
243 uint32_t) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800244 return 0;
245 }
246
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700247 virtual bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t,
248 KeyEvent*) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800249 return false;
250 }
251
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800252 virtual void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
253 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700254 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800255 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
256 * essentially a passthrough for notifySwitch.
257 */
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800258 mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800259 }
260
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700261 virtual void pokeUserActivity(nsecs_t, int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800262
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700263 virtual bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800264 return false;
265 }
Jackal Guof9696682018-10-05 12:23:23 +0800266
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700267 virtual void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700268 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700269 mOnPointerDownToken = newToken;
270 }
271
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800272 void assertFilterInputEventWasCalled(int type, nsecs_t eventTime, int32_t action,
273 int32_t displayId) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700274 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800275 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
276 ASSERT_EQ(mFilteredEvent->getType(), type);
277
278 if (type == AINPUT_EVENT_TYPE_KEY) {
279 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*mFilteredEvent);
280 EXPECT_EQ(keyEvent.getEventTime(), eventTime);
281 EXPECT_EQ(keyEvent.getAction(), action);
282 EXPECT_EQ(keyEvent.getDisplayId(), displayId);
283 } else if (type == AINPUT_EVENT_TYPE_MOTION) {
284 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*mFilteredEvent);
285 EXPECT_EQ(motionEvent.getEventTime(), eventTime);
286 EXPECT_EQ(motionEvent.getAction(), action);
287 EXPECT_EQ(motionEvent.getDisplayId(), displayId);
288 } else {
289 FAIL() << "Unknown type: " << type;
290 }
291
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800292 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800293 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800294};
295
Gang Wang342c9272020-01-13 13:15:04 -0500296// --- HmacKeyManagerTest ---
297
298class HmacKeyManagerTest : public testing::Test {
299protected:
300 HmacKeyManager mHmacKeyManager;
301};
302
303/**
304 * Ensure that separate calls to sign the same data are generating the same key.
305 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
306 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
307 * tests.
308 */
309TEST_F(HmacKeyManagerTest, GeneratedHmac_IsConsistent) {
310 KeyEvent event = getTestKeyEvent();
311 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
312
313 std::array<uint8_t, 32> hmac1 = mHmacKeyManager.sign(verifiedEvent);
314 std::array<uint8_t, 32> hmac2 = mHmacKeyManager.sign(verifiedEvent);
315 ASSERT_EQ(hmac1, hmac2);
316}
317
318/**
319 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
320 */
321TEST_F(HmacKeyManagerTest, GeneratedHmac_ChangesWhenFieldsChange) {
322 KeyEvent event = getTestKeyEvent();
323 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
324 std::array<uint8_t, 32> initialHmac = mHmacKeyManager.sign(verifiedEvent);
325
326 verifiedEvent.deviceId += 1;
327 ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent));
328
329 verifiedEvent.source += 1;
330 ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent));
331
332 verifiedEvent.eventTimeNanos += 1;
333 ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent));
334
335 verifiedEvent.displayId += 1;
336 ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent));
337
338 verifiedEvent.action += 1;
339 ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent));
340
341 verifiedEvent.downTimeNanos += 1;
342 ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent));
343
344 verifiedEvent.flags += 1;
345 ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent));
346
347 verifiedEvent.keyCode += 1;
348 ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent));
349
350 verifiedEvent.scanCode += 1;
351 ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent));
352
353 verifiedEvent.metaState += 1;
354 ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent));
355
356 verifiedEvent.repeatCount += 1;
357 ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent));
358}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800359
360// --- InputDispatcherTest ---
361
362class InputDispatcherTest : public testing::Test {
363protected:
364 sp<FakeInputDispatcherPolicy> mFakePolicy;
365 sp<InputDispatcher> mDispatcher;
366
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700367 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800368 mFakePolicy = new FakeInputDispatcherPolicy();
369 mDispatcher = new InputDispatcher(mFakePolicy);
Arthur Hungb92218b2018-08-14 12:00:21 +0800370 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
371 //Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700372 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800373 }
374
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700375 virtual void TearDown() override {
376 ASSERT_EQ(OK, mDispatcher->stop());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800377 mFakePolicy.clear();
378 mDispatcher.clear();
379 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700380
381 /**
382 * Used for debugging when writing the test
383 */
384 void dumpDispatcherState() {
385 std::string dump;
386 mDispatcher->dump(dump);
387 std::stringstream ss(dump);
388 std::string to;
389
390 while (std::getline(ss, to, '\n')) {
391 ALOGE("%s", to.c_str());
392 }
393 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800394};
395
396
397TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
398 KeyEvent event;
399
400 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800401 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
402 INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600403 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
404 ARBITRARY_TIME);
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700405 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED,
406 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
407 INPUT_EVENT_INJECTION_SYNC_NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800408 << "Should reject key events with undefined action.";
409
410 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800411 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
412 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600413 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700414 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED,
415 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
416 INPUT_EVENT_INJECTION_SYNC_NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800417 << "Should reject key events with ACTION_MULTIPLE.";
418}
419
420TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
421 MotionEvent event;
422 PointerProperties pointerProperties[MAX_POINTERS + 1];
423 PointerCoords pointerCoords[MAX_POINTERS + 1];
424 for (int i = 0; i <= MAX_POINTERS; i++) {
425 pointerProperties[i].clear();
426 pointerProperties[i].id = i;
427 pointerCoords[i].clear();
428 }
429
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800430 // Some constants commonly used below
431 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
432 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
433 constexpr int32_t metaState = AMETA_NONE;
434 constexpr MotionClassification classification = MotionClassification::NONE;
435
Michael Wrightd02c5b62014-02-10 15:10:22 -0800436 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800437 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600438 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification, 1 /* xScale */,
439 1 /* yScale */, 0, 0, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
440 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700441 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700442 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED,
443 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
444 INPUT_EVENT_INJECTION_SYNC_NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800445 << "Should reject motion events with undefined action.";
446
447 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800448 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700449 AMOTION_EVENT_ACTION_POINTER_DOWN |
450 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600451 0, 0, edgeFlags, metaState, 0, classification, 1 /* xScale */, 1 /* yScale */,
452 0, 0, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
453 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700454 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700455 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED,
456 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
457 INPUT_EVENT_INJECTION_SYNC_NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800458 << "Should reject motion events with pointer down index too large.";
459
Garfield Tanfbe732e2020-01-24 11:26:14 -0800460 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700461 AMOTION_EVENT_ACTION_POINTER_DOWN |
462 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600463 0, 0, edgeFlags, metaState, 0, classification, 1 /* xScale */, 1 /* yScale */,
464 0, 0, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
465 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700466 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700467 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED,
468 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
469 INPUT_EVENT_INJECTION_SYNC_NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800470 << "Should reject motion events with pointer down index too small.";
471
472 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800473 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700474 AMOTION_EVENT_ACTION_POINTER_UP |
475 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600476 0, 0, edgeFlags, metaState, 0, classification, 1 /* xScale */, 1 /* yScale */,
477 0, 0, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
478 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700479 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700480 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED,
481 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
482 INPUT_EVENT_INJECTION_SYNC_NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800483 << "Should reject motion events with pointer up index too large.";
484
Garfield Tanfbe732e2020-01-24 11:26:14 -0800485 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700486 AMOTION_EVENT_ACTION_POINTER_UP |
487 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600488 0, 0, edgeFlags, metaState, 0, classification, 1 /* xScale */, 1 /* yScale */,
489 0, 0, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
490 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700491 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700492 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED,
493 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
494 INPUT_EVENT_INJECTION_SYNC_NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800495 << "Should reject motion events with pointer up index too small.";
496
497 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800498 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
499 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
500 1 /* xScale */, 1 /* yScale */, 0, 0, 0, 0,
501 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
502 ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700503 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700504 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED,
505 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
506 INPUT_EVENT_INJECTION_SYNC_NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800507 << "Should reject motion events with 0 pointers.";
508
Garfield Tanfbe732e2020-01-24 11:26:14 -0800509 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
510 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
511 1 /* xScale */, 1 /* yScale */, 0, 0, 0, 0,
512 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
513 ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700514 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700515 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED,
516 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
517 INPUT_EVENT_INJECTION_SYNC_NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800518 << "Should reject motion events with more than MAX_POINTERS pointers.";
519
520 // Rejects motion events with invalid pointer ids.
521 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800522 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
523 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
524 1 /* xScale */, 1 /* yScale */, 0, 0, 0, 0,
525 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
526 ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700527 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700528 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED,
529 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
530 INPUT_EVENT_INJECTION_SYNC_NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800531 << "Should reject motion events with pointer ids less than 0.";
532
533 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800534 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
535 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
536 1 /* xScale */, 1 /* yScale */, 0, 0, 0, 0,
537 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
538 ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700539 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700540 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED,
541 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
542 INPUT_EVENT_INJECTION_SYNC_NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800543 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
544
545 // Rejects motion events with duplicate pointer ids.
546 pointerProperties[0].id = 1;
547 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800548 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
549 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
550 1 /* xScale */, 1 /* yScale */, 0, 0, 0, 0,
551 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
552 ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700553 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700554 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED,
555 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
556 INPUT_EVENT_INJECTION_SYNC_NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800557 << "Should reject motion events with duplicate pointer ids.";
558}
559
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800560/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
561
562TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
563 constexpr nsecs_t eventTime = 20;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800564 NotifyConfigurationChangedArgs args(10 /*id*/, eventTime);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800565 mDispatcher->notifyConfigurationChanged(&args);
566 ASSERT_TRUE(mDispatcher->waitForIdle());
567
568 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
569}
570
571TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800572 NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/,
573 2 /*switchMask*/);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800574 mDispatcher->notifySwitch(&args);
575
576 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
577 args.policyFlags |= POLICY_FLAG_TRUSTED;
578 mFakePolicy->assertNotifySwitchWasCalled(args);
579}
580
Arthur Hungb92218b2018-08-14 12:00:21 +0800581// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700582static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700583static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s;
Arthur Hungb92218b2018-08-14 12:00:21 +0800584
585class FakeApplicationHandle : public InputApplicationHandle {
586public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700587 FakeApplicationHandle() {
588 mInfo.name = "Fake Application";
589 mInfo.token = new BBinder();
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500590 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700591 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800592 virtual ~FakeApplicationHandle() {}
593
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700594 virtual bool updateInfo() override {
Arthur Hungb92218b2018-08-14 12:00:21 +0800595 return true;
596 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700597
598 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500599 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700600 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800601};
602
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800603class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800604public:
chaviwd1c23182019-12-20 18:44:56 -0800605 explicit FakeInputReceiver(const sp<InputChannel>& clientChannel, const std::string name)
606 : mName(name) {
607 mConsumer = std::make_unique<InputConsumer>(clientChannel);
608 }
609
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800610 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700611 InputEvent* event;
612 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
613 if (!consumeSeq) {
614 return nullptr;
615 }
616 finishEvent(*consumeSeq);
617 return event;
618 }
619
620 /**
621 * Receive an event without acknowledging it.
622 * Return the sequence number that could later be used to send finished signal.
623 */
624 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800625 uint32_t consumeSeq;
626 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800627
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800628 std::chrono::time_point start = std::chrono::steady_clock::now();
629 status_t status = WOULD_BLOCK;
630 while (status == WOULD_BLOCK) {
chaviw81e2bb92019-12-18 15:03:51 -0800631 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800632 &event);
633 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
634 if (elapsed > 100ms) {
635 break;
636 }
637 }
638
639 if (status == WOULD_BLOCK) {
640 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700641 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800642 }
643
644 if (status != OK) {
645 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700646 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800647 }
648 if (event == nullptr) {
649 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700650 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800651 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700652 if (outEvent != nullptr) {
653 *outEvent = event;
654 }
655 return consumeSeq;
656 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800657
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700658 /**
659 * To be used together with "receiveEvent" to complete the consumption of an event.
660 */
661 void finishEvent(uint32_t consumeSeq) {
662 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
663 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800664 }
665
666 void consumeEvent(int32_t expectedEventType, int32_t expectedAction, int32_t expectedDisplayId,
667 int32_t expectedFlags) {
668 InputEvent* event = consume();
669
670 ASSERT_NE(nullptr, event) << mName.c_str()
671 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800672 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700673 << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType)
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800674 << " event, got " << inputEventTypeToString(event->getType()) << " event";
Arthur Hungb92218b2018-08-14 12:00:21 +0800675
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800676 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
Tiger Huang8664f8c2018-10-11 19:14:35 +0800677
Tiger Huang8664f8c2018-10-11 19:14:35 +0800678 switch (expectedEventType) {
679 case AINPUT_EVENT_TYPE_KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800680 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
681 EXPECT_EQ(expectedAction, keyEvent.getAction());
682 EXPECT_EQ(expectedFlags, keyEvent.getFlags());
Tiger Huang8664f8c2018-10-11 19:14:35 +0800683 break;
684 }
685 case AINPUT_EVENT_TYPE_MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800686 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
687 EXPECT_EQ(expectedAction, motionEvent.getAction());
688 EXPECT_EQ(expectedFlags, motionEvent.getFlags());
Tiger Huang8664f8c2018-10-11 19:14:35 +0800689 break;
690 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100691 case AINPUT_EVENT_TYPE_FOCUS: {
692 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
693 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800694 default: {
695 FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType;
696 }
697 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800698 }
699
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100700 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
701 InputEvent* event = consume();
702 ASSERT_NE(nullptr, event) << mName.c_str()
703 << ": consumer should have returned non-NULL event.";
704 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
705 << "Got " << inputEventTypeToString(event->getType())
706 << " event instead of FOCUS event";
707
708 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
709 << mName.c_str() << ": event displayId should always be NONE.";
710
711 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
712 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
713 EXPECT_EQ(inTouchMode, focusEvent->getInTouchMode());
714 }
715
chaviwd1c23182019-12-20 18:44:56 -0800716 void assertNoEvents() {
717 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700718 if (event == nullptr) {
719 return;
720 }
721 if (event->getType() == AINPUT_EVENT_TYPE_KEY) {
722 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
723 ADD_FAILURE() << "Received key event "
724 << KeyEvent::actionToString(keyEvent.getAction());
725 } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) {
726 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
727 ADD_FAILURE() << "Received motion event "
728 << MotionEvent::actionToString(motionEvent.getAction());
729 } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) {
730 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
731 ADD_FAILURE() << "Received focus event, hasFocus = "
732 << (focusEvent.getHasFocus() ? "true" : "false");
733 }
734 FAIL() << mName.c_str()
735 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -0800736 }
737
738 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
739
740protected:
741 std::unique_ptr<InputConsumer> mConsumer;
742 PreallocatedInputEventFactory mEventFactory;
743
744 std::string mName;
745};
746
747class FakeWindowHandle : public InputWindowHandle {
748public:
749 static const int32_t WIDTH = 600;
750 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -0800751
752 FakeWindowHandle(const sp<InputApplicationHandle>& inputApplicationHandle,
753 const sp<InputDispatcher>& dispatcher, const std::string name,
754 int32_t displayId, sp<IBinder> token = nullptr)
755 : mName(name) {
756 if (token == nullptr) {
757 sp<InputChannel> serverChannel, clientChannel;
758 InputChannel::openInputChannelPair(name, serverChannel, clientChannel);
759 mInputReceiver = std::make_unique<FakeInputReceiver>(clientChannel, name);
760 dispatcher->registerInputChannel(serverChannel);
761 token = serverChannel->getConnectionToken();
762 }
763
764 inputApplicationHandle->updateInfo();
765 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
766
767 mInfo.token = token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700768 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -0800769 mInfo.name = name;
Michael Wright44753b12020-07-08 13:48:11 +0100770 mInfo.type = InputWindowInfo::Type::APPLICATION;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500771 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
chaviwd1c23182019-12-20 18:44:56 -0800772 mInfo.frameLeft = 0;
773 mInfo.frameTop = 0;
774 mInfo.frameRight = WIDTH;
775 mInfo.frameBottom = HEIGHT;
776 mInfo.globalScaleFactor = 1.0;
777 mInfo.touchableRegion.clear();
778 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
779 mInfo.visible = true;
780 mInfo.canReceiveKeys = true;
781 mInfo.hasFocus = false;
782 mInfo.hasWallpaper = false;
783 mInfo.paused = false;
chaviwd1c23182019-12-20 18:44:56 -0800784 mInfo.ownerPid = INJECTOR_PID;
785 mInfo.ownerUid = INJECTOR_UID;
chaviwd1c23182019-12-20 18:44:56 -0800786 mInfo.displayId = displayId;
787 }
788
789 virtual bool updateInfo() { return true; }
790
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100791 void setFocus(bool hasFocus) { mInfo.hasFocus = hasFocus; }
chaviwd1c23182019-12-20 18:44:56 -0800792
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700793 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500794 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700795 }
796
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700797 void setPaused(bool paused) { mInfo.paused = paused; }
798
chaviwd1c23182019-12-20 18:44:56 -0800799 void setFrame(const Rect& frame) {
800 mInfo.frameLeft = frame.left;
801 mInfo.frameTop = frame.top;
802 mInfo.frameRight = frame.right;
803 mInfo.frameBottom = frame.bottom;
804 mInfo.touchableRegion.clear();
805 mInfo.addTouchableRegion(frame);
806 }
807
Michael Wright44753b12020-07-08 13:48:11 +0100808 void setFlags(Flags<InputWindowInfo::Flag> flags) { mInfo.flags = flags; }
chaviwd1c23182019-12-20 18:44:56 -0800809
chaviwaf87b3e2019-10-01 16:59:28 -0700810 void setWindowScale(float xScale, float yScale) {
811 mInfo.windowXScale = xScale;
812 mInfo.windowYScale = yScale;
813 }
814
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800815 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
816 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
817 expectedFlags);
818 }
819
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700820 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
821 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
822 }
823
Svet Ganov5d3bc372020-01-26 23:11:07 -0800824 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
825 int32_t expectedFlags = 0) {
826 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId,
827 expectedFlags);
828 }
829
830 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
831 int32_t expectedFlags = 0) {
832 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId,
833 expectedFlags);
834 }
835
836 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
837 int32_t expectedFlags = 0) {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800838 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
839 expectedFlags);
840 }
841
Svet Ganov5d3bc372020-01-26 23:11:07 -0800842 void consumeMotionPointerDown(int32_t pointerIdx,
843 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, int32_t expectedFlags = 0) {
844 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN
845 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
846 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
847 }
848
849 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
850 int32_t expectedFlags = 0) {
851 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP
852 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
853 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
854 }
855
856 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
857 int32_t expectedFlags = 0) {
Michael Wright3a240c42019-12-10 20:53:41 +0000858 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
859 expectedFlags);
860 }
861
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100862 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
863 ASSERT_NE(mInputReceiver, nullptr)
864 << "Cannot consume events from a window with no receiver";
865 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
866 }
867
chaviwd1c23182019-12-20 18:44:56 -0800868 void consumeEvent(int32_t expectedEventType, int32_t expectedAction, int32_t expectedDisplayId,
869 int32_t expectedFlags) {
870 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
871 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
872 expectedFlags);
873 }
874
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700875 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700876 if (mInputReceiver == nullptr) {
877 ADD_FAILURE() << "Invalid receive event on window with no receiver";
878 return std::nullopt;
879 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700880 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700881 }
882
883 void finishEvent(uint32_t sequenceNum) {
884 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
885 mInputReceiver->finishEvent(sequenceNum);
886 }
887
chaviwaf87b3e2019-10-01 16:59:28 -0700888 InputEvent* consume() {
889 if (mInputReceiver == nullptr) {
890 return nullptr;
891 }
892 return mInputReceiver->consume();
893 }
894
Arthur Hungb92218b2018-08-14 12:00:21 +0800895 void assertNoEvents() {
chaviwd1c23182019-12-20 18:44:56 -0800896 ASSERT_NE(mInputReceiver, nullptr)
897 << "Call 'assertNoEvents' on a window with an InputReceiver";
898 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +0800899 }
900
chaviwaf87b3e2019-10-01 16:59:28 -0700901 sp<IBinder> getToken() { return mInfo.token; }
902
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100903 const std::string& getName() { return mName; }
904
chaviwd1c23182019-12-20 18:44:56 -0800905private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100906 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -0800907 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700908 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800909};
910
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700911std::atomic<int32_t> FakeWindowHandle::sId{1};
912
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700913static int32_t injectKey(const sp<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700914 int32_t displayId = ADISPLAY_ID_NONE,
915 int32_t syncMode = INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT,
916 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800917 KeyEvent event;
918 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
919
920 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800921 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700922 INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE,
923 repeatCount, currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +0800924
925 // Inject event until dispatch out.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700926 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, syncMode,
927 injectionTimeout,
928 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
Arthur Hungb92218b2018-08-14 12:00:21 +0800929}
930
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700931static int32_t injectKeyDown(const sp<InputDispatcher>& dispatcher,
932 int32_t displayId = ADISPLAY_ID_NONE) {
933 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId);
934}
935
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700936static int32_t injectKeyUp(const sp<InputDispatcher>& dispatcher,
937 int32_t displayId = ADISPLAY_ID_NONE) {
938 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId);
939}
940
Garfield Tandf26e862020-07-01 20:18:19 -0700941class PointerBuilder {
942public:
943 PointerBuilder(int32_t id, int32_t toolType) {
944 mProperties.clear();
945 mProperties.id = id;
946 mProperties.toolType = toolType;
947 mCoords.clear();
948 }
949
950 PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); }
951
952 PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); }
953
954 PointerBuilder& axis(int32_t axis, float value) {
955 mCoords.setAxisValue(axis, value);
956 return *this;
957 }
958
959 PointerProperties buildProperties() const { return mProperties; }
960
961 PointerCoords buildCoords() const { return mCoords; }
962
963private:
964 PointerProperties mProperties;
965 PointerCoords mCoords;
966};
967
968class MotionEventBuilder {
969public:
970 MotionEventBuilder(int32_t action, int32_t source) {
971 mAction = action;
972 mSource = source;
973 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
974 }
975
976 MotionEventBuilder& eventTime(nsecs_t eventTime) {
977 mEventTime = eventTime;
978 return *this;
979 }
980
981 MotionEventBuilder& displayId(int32_t displayId) {
982 mDisplayId = displayId;
983 return *this;
984 }
985
986 MotionEventBuilder& actionButton(int32_t actionButton) {
987 mActionButton = actionButton;
988 return *this;
989 }
990
991 MotionEventBuilder& buttonState(int32_t actionButton) {
992 mActionButton = actionButton;
993 return *this;
994 }
995
996 MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) {
997 mRawXCursorPosition = rawXCursorPosition;
998 return *this;
999 }
1000
1001 MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) {
1002 mRawYCursorPosition = rawYCursorPosition;
1003 return *this;
1004 }
1005
1006 MotionEventBuilder& pointer(PointerBuilder pointer) {
1007 mPointers.push_back(pointer);
1008 return *this;
1009 }
1010
1011 MotionEvent build() {
1012 std::vector<PointerProperties> pointerProperties;
1013 std::vector<PointerCoords> pointerCoords;
1014 for (const PointerBuilder& pointer : mPointers) {
1015 pointerProperties.push_back(pointer.buildProperties());
1016 pointerCoords.push_back(pointer.buildCoords());
1017 }
1018
1019 // Set mouse cursor position for the most common cases to avoid boilerplate.
1020 if (mSource == AINPUT_SOURCE_MOUSE &&
1021 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) &&
1022 mPointers.size() == 1) {
1023 mRawXCursorPosition = pointerCoords[0].getX();
1024 mRawYCursorPosition = pointerCoords[0].getY();
1025 }
1026
1027 MotionEvent event;
1028 event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC,
1029 mAction, mActionButton, /* flags */ 0, /* edgeFlags */ 0, AMETA_NONE,
1030 mButtonState, MotionClassification::NONE, /* xScale */ 1, /* yScale */ 1,
1031 /* xOffset */ 0,
1032 /* yOffset */ 0, /* xPrecision */ 0, /* yPrecision */ 0,
1033 mRawXCursorPosition, mRawYCursorPosition, mEventTime, mEventTime,
1034 mPointers.size(), pointerProperties.data(), pointerCoords.data());
1035
1036 return event;
1037 }
1038
1039private:
1040 int32_t mAction;
1041 int32_t mSource;
1042 nsecs_t mEventTime;
1043 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1044 int32_t mActionButton{0};
1045 int32_t mButtonState{0};
1046 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1047 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1048
1049 std::vector<PointerBuilder> mPointers;
1050};
1051
1052static int32_t injectMotionEvent(
1053 const sp<InputDispatcher>& dispatcher, const MotionEvent& event,
1054 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
1055 int32_t injectionMode = INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT) {
1056 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, injectionMode,
1057 injectionTimeout,
1058 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
1059}
1060
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001061static int32_t injectMotionEvent(
1062 const sp<InputDispatcher>& dispatcher, int32_t action, int32_t source, int32_t displayId,
1063 const PointF& position,
1064 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001065 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1066 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
1067 int32_t injectionMode = INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT,
1068 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC)) {
Garfield Tandf26e862020-07-01 20:18:19 -07001069 MotionEvent event = MotionEventBuilder(action, source)
1070 .displayId(displayId)
1071 .eventTime(eventTime)
1072 .rawXCursorPosition(cursorPosition.x)
1073 .rawYCursorPosition(cursorPosition.y)
1074 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1075 .x(position.x)
1076 .y(position.y))
1077 .build();
Arthur Hungb92218b2018-08-14 12:00:21 +08001078
1079 // Inject event until dispatch out.
Garfield Tandf26e862020-07-01 20:18:19 -07001080 return injectMotionEvent(dispatcher, event);
Arthur Hungb92218b2018-08-14 12:00:21 +08001081}
1082
Garfield Tan00f511d2019-06-12 16:55:40 -07001083static int32_t injectMotionDown(const sp<InputDispatcher>& dispatcher, int32_t source,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001084 int32_t displayId, const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001085 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001086}
1087
Michael Wright3a240c42019-12-10 20:53:41 +00001088static int32_t injectMotionUp(const sp<InputDispatcher>& dispatcher, int32_t source,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001089 int32_t displayId, const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001090 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001091}
1092
Jackal Guof9696682018-10-05 12:23:23 +08001093static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1094 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1095 // Define a valid key event.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001096 NotifyKeyArgs args(/* id */ 0, currentTime, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
1097 POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, KEY_A,
1098 AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001099
1100 return args;
1101}
1102
chaviwd1c23182019-12-20 18:44:56 -08001103static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId,
1104 const std::vector<PointF>& points) {
1105 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001106 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1107 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1108 }
1109
chaviwd1c23182019-12-20 18:44:56 -08001110 PointerProperties pointerProperties[pointerCount];
1111 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001112
chaviwd1c23182019-12-20 18:44:56 -08001113 for (size_t i = 0; i < pointerCount; i++) {
1114 pointerProperties[i].clear();
1115 pointerProperties[i].id = i;
1116 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001117
chaviwd1c23182019-12-20 18:44:56 -08001118 pointerCoords[i].clear();
1119 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1120 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1121 }
Jackal Guof9696682018-10-05 12:23:23 +08001122
1123 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1124 // Define a valid motion event.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001125 NotifyMotionArgs args(/* id */ 0, currentTime, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001126 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1127 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001128 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1129 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001130 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1131 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001132
1133 return args;
1134}
1135
chaviwd1c23182019-12-20 18:44:56 -08001136static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1137 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1138}
1139
Arthur Hungb92218b2018-08-14 12:00:21 +08001140TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
1141 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001142 sp<FakeWindowHandle> window = new FakeWindowHandle(application, mDispatcher, "Fake Window",
1143 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001144
Arthur Hung72d8dc32020-03-28 00:48:39 +00001145 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001146 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1147 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +08001148 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1149
1150 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001151 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001152}
1153
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001154/**
1155 * Calling setInputWindows once with FLAG_NOT_TOUCH_MODAL should not cause any issues.
1156 * To ensure that window receives only events that were directly inside of it, add
1157 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1158 * when finding touched windows.
1159 * This test serves as a sanity check for the next test, where setInputWindows is
1160 * called twice.
1161 */
1162TEST_F(InputDispatcherTest, SetInputWindowOnce_SingleWindowTouch) {
1163 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1164 sp<FakeWindowHandle> window =
1165 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1166 window->setFrame(Rect(0, 0, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01001167 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001168
1169 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1170 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1171 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1172 {50, 50}))
1173 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1174
1175 // Window should receive motion event.
1176 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1177}
1178
1179/**
1180 * Calling setInputWindows twice, with the same info, should not cause any issues.
1181 * To ensure that window receives only events that were directly inside of it, add
1182 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1183 * when finding touched windows.
1184 */
1185TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
1186 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1187 sp<FakeWindowHandle> window =
1188 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1189 window->setFrame(Rect(0, 0, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01001190 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001191
1192 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1193 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1194 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1195 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1196 {50, 50}))
1197 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1198
1199 // Window should receive motion event.
1200 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1201}
1202
Arthur Hungb92218b2018-08-14 12:00:21 +08001203// The foreground window should receive the first touch down event.
1204TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
1205 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001206 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
1207 ADISPLAY_ID_DEFAULT);
1208 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
1209 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001210
Arthur Hung72d8dc32020-03-28 00:48:39 +00001211 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001212 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1213 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +08001214 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1215
1216 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001217 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001218 windowSecond->assertNoEvents();
1219}
1220
1221TEST_F(InputDispatcherTest, SetInputWindow_FocusedWindow) {
1222 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001223 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
1224 ADISPLAY_ID_DEFAULT);
1225 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
1226 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001227
Arthur Hung7ab76b12019-01-09 19:17:20 +08001228 // Set focused application.
Tiger Huang721e26f2018-07-24 22:26:19 +08001229 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Arthur Hungb92218b2018-08-14 12:00:21 +08001230
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001231 // Display should have only one focused window
1232 windowSecond->setFocus(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001233 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001234
1235 windowSecond->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08001236 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
1237 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1238
1239 // Focused window should receive event.
1240 windowTop->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001241 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08001242}
1243
Arthur Hung7ab76b12019-01-09 19:17:20 +08001244TEST_F(InputDispatcherTest, SetInputWindow_FocusPriority) {
1245 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1246 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
1247 ADISPLAY_ID_DEFAULT);
1248 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
1249 ADISPLAY_ID_DEFAULT);
1250
1251 // Set focused application.
1252 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1253
1254 // Display has two focused windows. Add them to inputWindowsHandles in z-order (top most first)
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001255 windowTop->setFocus(true);
1256 windowSecond->setFocus(true);
Arthur Hung7ab76b12019-01-09 19:17:20 +08001257
Arthur Hung72d8dc32020-03-28 00:48:39 +00001258 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001259 windowTop->consumeFocusEvent(true);
Arthur Hung7ab76b12019-01-09 19:17:20 +08001260 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
1261 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1262
1263 // Top focused window should receive event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001264 windowTop->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung7ab76b12019-01-09 19:17:20 +08001265 windowSecond->assertNoEvents();
1266}
1267
Arthur Hung3b413f22018-10-26 18:05:34 +08001268TEST_F(InputDispatcherTest, SetInputWindow_InputWindowInfo) {
1269 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1270
1271 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
1272 ADISPLAY_ID_DEFAULT);
1273 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
1274 ADISPLAY_ID_DEFAULT);
1275
Arthur Hung832bc4a2019-01-28 11:43:17 +08001276 // Set focused application.
1277 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Arthur Hung3b413f22018-10-26 18:05:34 +08001278
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001279 windowTop->setFocus(true);
1280 windowSecond->setFocus(true);
Arthur Hung3b413f22018-10-26 18:05:34 +08001281 // Release channel for window is no longer valid.
1282 windowTop->releaseChannel();
Arthur Hung72d8dc32020-03-28 00:48:39 +00001283 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001284 windowSecond->consumeFocusEvent(true);
Arthur Hung3b413f22018-10-26 18:05:34 +08001285
Arthur Hung832bc4a2019-01-28 11:43:17 +08001286 // Test inject a key down, should dispatch to a valid window.
1287 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
1288 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Arthur Hung3b413f22018-10-26 18:05:34 +08001289
1290 // Top window is invalid, so it should not receive any input event.
1291 windowTop->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001292 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung3b413f22018-10-26 18:05:34 +08001293}
1294
Garfield Tandf26e862020-07-01 20:18:19 -07001295TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
1296 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1297 sp<FakeWindowHandle> windowLeft =
1298 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1299 windowLeft->setFrame(Rect(0, 0, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001300 windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001301 sp<FakeWindowHandle> windowRight =
1302 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1303 windowRight->setFrame(Rect(600, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001304 windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001305
1306 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1307
1308 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
1309
1310 // Start cursor position in right window so that we can move the cursor to left window.
1311 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1312 injectMotionEvent(mDispatcher,
1313 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1314 AINPUT_SOURCE_MOUSE)
1315 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1316 .x(900)
1317 .y(400))
1318 .build()));
1319 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1320 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1321 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1322 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1323
1324 // Move cursor into left window
1325 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1326 injectMotionEvent(mDispatcher,
1327 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1328 AINPUT_SOURCE_MOUSE)
1329 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1330 .x(300)
1331 .y(400))
1332 .build()));
1333 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1334 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1335 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1336 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1337 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1338 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1339
1340 // Inject a series of mouse events for a mouse click
1341 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1342 injectMotionEvent(mDispatcher,
1343 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1344 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1345 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1346 .x(300)
1347 .y(400))
1348 .build()));
1349 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1350
1351 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1352 injectMotionEvent(mDispatcher,
1353 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
1354 AINPUT_SOURCE_MOUSE)
1355 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1356 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1357 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1358 .x(300)
1359 .y(400))
1360 .build()));
1361 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1362 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1363
1364 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1365 injectMotionEvent(mDispatcher,
1366 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1367 AINPUT_SOURCE_MOUSE)
1368 .buttonState(0)
1369 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1370 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1371 .x(300)
1372 .y(400))
1373 .build()));
1374 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1375 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1376
1377 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1378 injectMotionEvent(mDispatcher,
1379 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
1380 .buttonState(0)
1381 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1382 .x(300)
1383 .y(400))
1384 .build()));
1385 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1386
1387 // Move mouse cursor back to right window
1388 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1389 injectMotionEvent(mDispatcher,
1390 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1391 AINPUT_SOURCE_MOUSE)
1392 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1393 .x(900)
1394 .y(400))
1395 .build()));
1396 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1397 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1398 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1399 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1400 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1401 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1402}
1403
1404// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
1405// directly in this test.
1406TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
1407 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1408 sp<FakeWindowHandle> window =
1409 new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1410 window->setFrame(Rect(0, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001411 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001412
1413 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1414
1415 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1416
1417 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1418 injectMotionEvent(mDispatcher,
1419 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
1420 AINPUT_SOURCE_MOUSE)
1421 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1422 .x(300)
1423 .y(400))
1424 .build()));
1425 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1426 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1427
1428 // Inject a series of mouse events for a mouse click
1429 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1430 injectMotionEvent(mDispatcher,
1431 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1432 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1433 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1434 .x(300)
1435 .y(400))
1436 .build()));
1437 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1438
1439 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1440 injectMotionEvent(mDispatcher,
1441 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
1442 AINPUT_SOURCE_MOUSE)
1443 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1444 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1445 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1446 .x(300)
1447 .y(400))
1448 .build()));
1449 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1450 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1451
1452 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1453 injectMotionEvent(mDispatcher,
1454 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1455 AINPUT_SOURCE_MOUSE)
1456 .buttonState(0)
1457 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1458 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1459 .x(300)
1460 .y(400))
1461 .build()));
1462 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1463 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1464
1465 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1466 injectMotionEvent(mDispatcher,
1467 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
1468 .buttonState(0)
1469 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1470 .x(300)
1471 .y(400))
1472 .build()));
1473 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1474
1475 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1476 injectMotionEvent(mDispatcher,
1477 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
1478 AINPUT_SOURCE_MOUSE)
1479 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1480 .x(300)
1481 .y(400))
1482 .build()));
1483 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1484 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1485}
1486
Garfield Tan00f511d2019-06-12 16:55:40 -07001487TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
1488 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1489
1490 sp<FakeWindowHandle> windowLeft =
1491 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1492 windowLeft->setFrame(Rect(0, 0, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001493 windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07001494 sp<FakeWindowHandle> windowRight =
1495 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1496 windowRight->setFrame(Rect(600, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001497 windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07001498
1499 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1500
Arthur Hung72d8dc32020-03-28 00:48:39 +00001501 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07001502
1503 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
1504 // left window. This event should be dispatched to the left window.
1505 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1506 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001507 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001508 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07001509 windowRight->assertNoEvents();
1510}
1511
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001512TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
1513 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1514 sp<FakeWindowHandle> window =
1515 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001516 window->setFocus(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001517
Arthur Hung72d8dc32020-03-28 00:48:39 +00001518 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001519 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001520
1521 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1522 mDispatcher->notifyKey(&keyArgs);
1523
1524 // Window should receive key down event.
1525 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1526
1527 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
1528 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001529 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001530 mDispatcher->notifyDeviceReset(&args);
1531 window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
1532 AKEY_EVENT_FLAG_CANCELED);
1533}
1534
1535TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
1536 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1537 sp<FakeWindowHandle> window =
1538 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1539
Arthur Hung72d8dc32020-03-28 00:48:39 +00001540 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001541
1542 NotifyMotionArgs motionArgs =
1543 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1544 ADISPLAY_ID_DEFAULT);
1545 mDispatcher->notifyMotion(&motionArgs);
1546
1547 // Window should receive motion down event.
1548 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1549
1550 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
1551 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001552 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001553 mDispatcher->notifyDeviceReset(&args);
1554 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT,
1555 0 /*expectedFlags*/);
1556}
1557
Svet Ganov5d3bc372020-01-26 23:11:07 -08001558TEST_F(InputDispatcherTest, TransferTouchFocus_OnePointer) {
1559 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1560
1561 // Create a couple of windows
1562 sp<FakeWindowHandle> firstWindow = new FakeWindowHandle(application, mDispatcher,
1563 "First Window", ADISPLAY_ID_DEFAULT);
1564 sp<FakeWindowHandle> secondWindow = new FakeWindowHandle(application, mDispatcher,
1565 "Second Window", ADISPLAY_ID_DEFAULT);
1566
1567 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001568 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001569
1570 // Send down to the first window
1571 NotifyMotionArgs downMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
1572 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT);
1573 mDispatcher->notifyMotion(&downMotionArgs);
1574 // Only the first window should get the down event
1575 firstWindow->consumeMotionDown();
1576 secondWindow->assertNoEvents();
1577
1578 // Transfer touch focus to the second window
1579 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1580 // The first window gets cancel and the second gets down
1581 firstWindow->consumeMotionCancel();
1582 secondWindow->consumeMotionDown();
1583
1584 // Send up event to the second window
1585 NotifyMotionArgs upMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP,
1586 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT);
1587 mDispatcher->notifyMotion(&upMotionArgs);
1588 // The first window gets no events and the second gets up
1589 firstWindow->assertNoEvents();
1590 secondWindow->consumeMotionUp();
1591}
1592
1593TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointerNoSplitTouch) {
1594 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1595
1596 PointF touchPoint = {10, 10};
1597
1598 // Create a couple of windows
1599 sp<FakeWindowHandle> firstWindow = new FakeWindowHandle(application, mDispatcher,
1600 "First Window", ADISPLAY_ID_DEFAULT);
1601 sp<FakeWindowHandle> secondWindow = new FakeWindowHandle(application, mDispatcher,
1602 "Second Window", ADISPLAY_ID_DEFAULT);
1603
1604 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001605 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001606
1607 // Send down to the first window
1608 NotifyMotionArgs downMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
1609 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {touchPoint});
1610 mDispatcher->notifyMotion(&downMotionArgs);
1611 // Only the first window should get the down event
1612 firstWindow->consumeMotionDown();
1613 secondWindow->assertNoEvents();
1614
1615 // Send pointer down to the first window
1616 NotifyMotionArgs pointerDownMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN
1617 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1618 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint});
1619 mDispatcher->notifyMotion(&pointerDownMotionArgs);
1620 // Only the first window should get the pointer down event
1621 firstWindow->consumeMotionPointerDown(1);
1622 secondWindow->assertNoEvents();
1623
1624 // Transfer touch focus to the second window
1625 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1626 // The first window gets cancel and the second gets down and pointer down
1627 firstWindow->consumeMotionCancel();
1628 secondWindow->consumeMotionDown();
1629 secondWindow->consumeMotionPointerDown(1);
1630
1631 // Send pointer up to the second window
1632 NotifyMotionArgs pointerUpMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP
1633 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1634 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint});
1635 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1636 // The first window gets nothing and the second gets pointer up
1637 firstWindow->assertNoEvents();
1638 secondWindow->consumeMotionPointerUp(1);
1639
1640 // Send up event to the second window
1641 NotifyMotionArgs upMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP,
1642 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT);
1643 mDispatcher->notifyMotion(&upMotionArgs);
1644 // The first window gets nothing and the second gets up
1645 firstWindow->assertNoEvents();
1646 secondWindow->consumeMotionUp();
1647}
1648
1649TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
1650 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1651
1652 // Create a non touch modal window that supports split touch
1653 sp<FakeWindowHandle> firstWindow = new FakeWindowHandle(application, mDispatcher,
1654 "First Window", ADISPLAY_ID_DEFAULT);
1655 firstWindow->setFrame(Rect(0, 0, 600, 400));
Michael Wright44753b12020-07-08 13:48:11 +01001656 firstWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1657 InputWindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001658
1659 // Create a non touch modal window that supports split touch
1660 sp<FakeWindowHandle> secondWindow = new FakeWindowHandle(application, mDispatcher,
1661 "Second Window", ADISPLAY_ID_DEFAULT);
1662 secondWindow->setFrame(Rect(0, 400, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001663 secondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1664 InputWindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001665
1666 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001667 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001668
1669 PointF pointInFirst = {300, 200};
1670 PointF pointInSecond = {300, 600};
1671
1672 // Send down to the first window
1673 NotifyMotionArgs firstDownMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
1674 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {pointInFirst});
1675 mDispatcher->notifyMotion(&firstDownMotionArgs);
1676 // Only the first window should get the down event
1677 firstWindow->consumeMotionDown();
1678 secondWindow->assertNoEvents();
1679
1680 // Send down to the second window
1681 NotifyMotionArgs secondDownMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN
1682 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1683 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {pointInFirst, pointInSecond});
1684 mDispatcher->notifyMotion(&secondDownMotionArgs);
1685 // The first window gets a move and the second a down
1686 firstWindow->consumeMotionMove();
1687 secondWindow->consumeMotionDown();
1688
1689 // Transfer touch focus to the second window
1690 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1691 // The first window gets cancel and the new gets pointer down (it already saw down)
1692 firstWindow->consumeMotionCancel();
1693 secondWindow->consumeMotionPointerDown(1);
1694
1695 // Send pointer up to the second window
1696 NotifyMotionArgs pointerUpMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP
1697 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1698 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {pointInFirst, pointInSecond});
1699 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1700 // The first window gets nothing and the second gets pointer up
1701 firstWindow->assertNoEvents();
1702 secondWindow->consumeMotionPointerUp(1);
1703
1704 // Send up event to the second window
1705 NotifyMotionArgs upMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP,
1706 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT);
1707 mDispatcher->notifyMotion(&upMotionArgs);
1708 // The first window gets nothing and the second gets up
1709 firstWindow->assertNoEvents();
1710 secondWindow->consumeMotionUp();
1711}
1712
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001713TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
1714 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1715 sp<FakeWindowHandle> window =
1716 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1717
1718 window->setFocus(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001719 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001720
1721 window->consumeFocusEvent(true);
1722
1723 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1724 mDispatcher->notifyKey(&keyArgs);
1725
1726 // Window should receive key down event.
1727 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1728}
1729
1730TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
1731 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1732 sp<FakeWindowHandle> window =
1733 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1734
Arthur Hung72d8dc32020-03-28 00:48:39 +00001735 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001736
1737 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1738 mDispatcher->notifyKey(&keyArgs);
1739 mDispatcher->waitForIdle();
1740
1741 window->assertNoEvents();
1742}
1743
1744// If a window is touchable, but does not have focus, it should receive motion events, but not keys
1745TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
1746 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1747 sp<FakeWindowHandle> window =
1748 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1749
Arthur Hung72d8dc32020-03-28 00:48:39 +00001750 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001751
1752 // Send key
1753 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1754 mDispatcher->notifyKey(&keyArgs);
1755 // Send motion
1756 NotifyMotionArgs motionArgs =
1757 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1758 ADISPLAY_ID_DEFAULT);
1759 mDispatcher->notifyMotion(&motionArgs);
1760
1761 // Window should receive only the motion event
1762 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1763 window->assertNoEvents(); // Key event or focus event will not be received
1764}
1765
chaviwd1c23182019-12-20 18:44:56 -08001766class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00001767public:
1768 FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name,
chaviwd1c23182019-12-20 18:44:56 -08001769 int32_t displayId, bool isGestureMonitor = false) {
1770 sp<InputChannel> serverChannel, clientChannel;
1771 InputChannel::openInputChannelPair(name, serverChannel, clientChannel);
1772 mInputReceiver = std::make_unique<FakeInputReceiver>(clientChannel, name);
1773 dispatcher->registerInputMonitor(serverChannel, displayId, isGestureMonitor);
Michael Wright3a240c42019-12-10 20:53:41 +00001774 }
1775
chaviwd1c23182019-12-20 18:44:56 -08001776 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
1777
1778 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1779 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN,
1780 expectedDisplayId, expectedFlags);
1781 }
1782
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001783 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
1784
1785 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
1786
chaviwd1c23182019-12-20 18:44:56 -08001787 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1788 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
1789 expectedDisplayId, expectedFlags);
1790 }
1791
1792 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1793 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP,
1794 expectedDisplayId, expectedFlags);
1795 }
1796
1797 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
1798
1799private:
1800 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00001801};
1802
1803// Tests for gesture monitors
1804TEST_F(InputDispatcherTest, GestureMonitor_ReceivesMotionEvents) {
1805 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1806 sp<FakeWindowHandle> window =
1807 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001808 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00001809
chaviwd1c23182019-12-20 18:44:56 -08001810 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
1811 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00001812
1813 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1814 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1815 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1816 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001817 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00001818}
1819
1820TEST_F(InputDispatcherTest, GestureMonitor_DoesNotReceiveKeyEvents) {
1821 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1822 sp<FakeWindowHandle> window =
1823 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1824
1825 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001826 window->setFocus(true);
Michael Wright3a240c42019-12-10 20:53:41 +00001827
Arthur Hung72d8dc32020-03-28 00:48:39 +00001828 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001829 window->consumeFocusEvent(true);
Michael Wright3a240c42019-12-10 20:53:41 +00001830
chaviwd1c23182019-12-20 18:44:56 -08001831 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
1832 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00001833
1834 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
1835 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1836 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001837 monitor.assertNoEvents();
Michael Wright3a240c42019-12-10 20:53:41 +00001838}
1839
1840TEST_F(InputDispatcherTest, GestureMonitor_CanPilferAfterWindowIsRemovedMidStream) {
1841 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1842 sp<FakeWindowHandle> window =
1843 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001844 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00001845
chaviwd1c23182019-12-20 18:44:56 -08001846 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
1847 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00001848
1849 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1850 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1851 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1852 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001853 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00001854
1855 window->releaseChannel();
1856
chaviwd1c23182019-12-20 18:44:56 -08001857 mDispatcher->pilferPointers(monitor.getToken());
Michael Wright3a240c42019-12-10 20:53:41 +00001858
1859 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1860 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1861 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08001862 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00001863}
1864
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001865TEST_F(InputDispatcherTest, UnresponsiveGestureMonitor_GetsAnr) {
1866 FakeMonitorReceiver monitor =
1867 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
1868 true /*isGestureMonitor*/);
1869
1870 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1871 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
1872 std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
1873 ASSERT_TRUE(consumeSeq);
1874
1875 mFakePolicy->assertNotifyAnrWasCalled(DISPATCHING_TIMEOUT, nullptr, monitor.getToken());
1876 monitor.finishEvent(*consumeSeq);
1877 ASSERT_TRUE(mDispatcher->waitForIdle());
1878}
1879
chaviw81e2bb92019-12-18 15:03:51 -08001880TEST_F(InputDispatcherTest, TestMoveEvent) {
1881 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1882 sp<FakeWindowHandle> window =
1883 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1884
Arthur Hung72d8dc32020-03-28 00:48:39 +00001885 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08001886
1887 NotifyMotionArgs motionArgs =
1888 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1889 ADISPLAY_ID_DEFAULT);
1890
1891 mDispatcher->notifyMotion(&motionArgs);
1892 // Window should receive motion down event.
1893 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1894
1895 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001896 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08001897 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1898 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
1899 motionArgs.pointerCoords[0].getX() - 10);
1900
1901 mDispatcher->notifyMotion(&motionArgs);
1902 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
1903 0 /*expectedFlags*/);
1904}
1905
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001906/**
1907 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
1908 * the device default right away. In the test scenario, we check both the default value,
1909 * and the action of enabling / disabling.
1910 */
1911TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
1912 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1913 sp<FakeWindowHandle> window =
1914 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
1915
1916 // Set focused application.
1917 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1918 window->setFocus(true);
1919
1920 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00001921 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001922 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
1923
1924 SCOPED_TRACE("Remove the window to trigger focus loss");
1925 window->setFocus(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001926 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001927 window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/);
1928
1929 SCOPED_TRACE("Disable touch mode");
1930 mDispatcher->setInTouchMode(false);
1931 window->setFocus(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001932 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001933 window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/);
1934
1935 SCOPED_TRACE("Remove the window to trigger focus loss");
1936 window->setFocus(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001937 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001938 window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/);
1939
1940 SCOPED_TRACE("Enable touch mode again");
1941 mDispatcher->setInTouchMode(true);
1942 window->setFocus(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001943 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001944 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
1945
1946 window->assertNoEvents();
1947}
1948
Gang Wange9087892020-01-07 12:17:14 -05001949TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
1950 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1951 sp<FakeWindowHandle> window =
1952 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
1953
1954 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1955 window->setFocus(true);
1956
Arthur Hung72d8dc32020-03-28 00:48:39 +00001957 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Gang Wange9087892020-01-07 12:17:14 -05001958 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
1959
1960 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
1961 mDispatcher->notifyKey(&keyArgs);
1962
1963 InputEvent* event = window->consume();
1964 ASSERT_NE(event, nullptr);
1965
1966 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
1967 ASSERT_NE(verified, nullptr);
1968 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
1969
1970 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
1971 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
1972 ASSERT_EQ(keyArgs.source, verified->source);
1973 ASSERT_EQ(keyArgs.displayId, verified->displayId);
1974
1975 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
1976
1977 ASSERT_EQ(keyArgs.action, verifiedKey.action);
1978 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05001979 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
1980 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
1981 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
1982 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
1983 ASSERT_EQ(0, verifiedKey.repeatCount);
1984}
1985
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08001986TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
1987 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1988 sp<FakeWindowHandle> window =
1989 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
1990
1991 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1992
Arthur Hung72d8dc32020-03-28 00:48:39 +00001993 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08001994
1995 NotifyMotionArgs motionArgs =
1996 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1997 ADISPLAY_ID_DEFAULT);
1998 mDispatcher->notifyMotion(&motionArgs);
1999
2000 InputEvent* event = window->consume();
2001 ASSERT_NE(event, nullptr);
2002
2003 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
2004 ASSERT_NE(verified, nullptr);
2005 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
2006
2007 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
2008 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
2009 EXPECT_EQ(motionArgs.source, verified->source);
2010 EXPECT_EQ(motionArgs.displayId, verified->displayId);
2011
2012 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
2013
2014 EXPECT_EQ(motionArgs.pointerCoords[0].getX(), verifiedMotion.rawX);
2015 EXPECT_EQ(motionArgs.pointerCoords[0].getY(), verifiedMotion.rawY);
2016 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
2017 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
2018 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
2019 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
2020 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
2021}
2022
Garfield Tan1c7bc862020-01-28 13:24:04 -08002023class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
2024protected:
2025 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
2026 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
2027
2028 sp<FakeApplicationHandle> mApp;
2029 sp<FakeWindowHandle> mWindow;
2030
2031 virtual void SetUp() override {
2032 mFakePolicy = new FakeInputDispatcherPolicy();
2033 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
2034 mDispatcher = new InputDispatcher(mFakePolicy);
2035 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
2036 ASSERT_EQ(OK, mDispatcher->start());
2037
2038 setUpWindow();
2039 }
2040
2041 void setUpWindow() {
2042 mApp = new FakeApplicationHandle();
2043 mWindow = new FakeWindowHandle(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2044
2045 mWindow->setFocus(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002046 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Garfield Tan1c7bc862020-01-28 13:24:04 -08002047
2048 mWindow->consumeFocusEvent(true);
2049 }
2050
2051 void sendAndConsumeKeyDown() {
2052 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2053 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
2054 mDispatcher->notifyKey(&keyArgs);
2055
2056 // Window should receive key down event.
2057 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2058 }
2059
2060 void expectKeyRepeatOnce(int32_t repeatCount) {
2061 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
2062 InputEvent* repeatEvent = mWindow->consume();
2063 ASSERT_NE(nullptr, repeatEvent);
2064
2065 uint32_t eventType = repeatEvent->getType();
2066 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType);
2067
2068 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
2069 uint32_t eventAction = repeatKeyEvent->getAction();
2070 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
2071 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
2072 }
2073
2074 void sendAndConsumeKeyUp() {
2075 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
2076 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
2077 mDispatcher->notifyKey(&keyArgs);
2078
2079 // Window should receive key down event.
2080 mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
2081 0 /*expectedFlags*/);
2082 }
2083};
2084
2085TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
2086 sendAndConsumeKeyDown();
2087 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2088 expectKeyRepeatOnce(repeatCount);
2089 }
2090}
2091
2092TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
2093 sendAndConsumeKeyDown();
2094 expectKeyRepeatOnce(1 /*repeatCount*/);
2095 sendAndConsumeKeyUp();
2096 mWindow->assertNoEvents();
2097}
2098
2099TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
2100 sendAndConsumeKeyDown();
2101 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2102 InputEvent* repeatEvent = mWindow->consume();
2103 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2104 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
2105 IdGenerator::getSource(repeatEvent->getId()));
2106 }
2107}
2108
2109TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
2110 sendAndConsumeKeyDown();
2111
2112 std::unordered_set<int32_t> idSet;
2113 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2114 InputEvent* repeatEvent = mWindow->consume();
2115 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2116 int32_t id = repeatEvent->getId();
2117 EXPECT_EQ(idSet.end(), idSet.find(id));
2118 idSet.insert(id);
2119 }
2120}
2121
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002122/* Test InputDispatcher for MultiDisplay */
2123class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
2124public:
2125 static constexpr int32_t SECOND_DISPLAY_ID = 1;
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002126 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002127 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08002128
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002129 application1 = new FakeApplicationHandle();
2130 windowInPrimary = new FakeWindowHandle(application1, mDispatcher, "D_1",
2131 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002132
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002133 // Set focus window for primary display, but focused display would be second one.
2134 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002135 windowInPrimary->setFocus(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002136 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002137 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08002138
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002139 application2 = new FakeApplicationHandle();
2140 windowInSecondary = new FakeWindowHandle(application2, mDispatcher, "D_2",
2141 SECOND_DISPLAY_ID);
2142 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002143 // Set focus display to second one.
2144 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
2145 // Set focus window for second display.
2146 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002147 windowInSecondary->setFocus(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002148 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002149 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002150 }
2151
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002152 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002153 InputDispatcherTest::TearDown();
2154
2155 application1.clear();
2156 windowInPrimary.clear();
2157 application2.clear();
2158 windowInSecondary.clear();
2159 }
2160
2161protected:
2162 sp<FakeApplicationHandle> application1;
2163 sp<FakeWindowHandle> windowInPrimary;
2164 sp<FakeApplicationHandle> application2;
2165 sp<FakeWindowHandle> windowInSecondary;
2166};
2167
2168TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
2169 // Test touch down on primary display.
2170 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
2171 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +08002172 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002173 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08002174 windowInSecondary->assertNoEvents();
2175
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002176 // Test touch down on second display.
2177 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
2178 AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Arthur Hungb92218b2018-08-14 12:00:21 +08002179 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
2180 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002181 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08002182}
2183
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002184TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08002185 // Test inject a key down with display id specified.
2186 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2187 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002188 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08002189 windowInSecondary->assertNoEvents();
2190
2191 // Test inject a key down without display id specified.
Arthur Hungb92218b2018-08-14 12:00:21 +08002192 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
2193 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
2194 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002195 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08002196
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002197 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00002198 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08002199
2200 // Expect old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002201 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
2202 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08002203
2204 // Test inject a key down, should timeout because of no target window.
2205 ASSERT_EQ(INPUT_EVENT_INJECTION_TIMED_OUT, injectKeyDown(mDispatcher))
2206 << "Inject key event should return INPUT_EVENT_INJECTION_TIMED_OUT";
2207 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002208 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08002209 windowInSecondary->assertNoEvents();
2210}
2211
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002212// Test per-display input monitors for motion event.
2213TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08002214 FakeMonitorReceiver monitorInPrimary =
2215 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
2216 FakeMonitorReceiver monitorInSecondary =
2217 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002218
2219 // Test touch down on primary display.
2220 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
2221 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2222 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002223 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002224 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002225 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002226 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002227
2228 // Test touch down on second display.
2229 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
2230 AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
2231 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
2232 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002233 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002234 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08002235 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002236
2237 // Test inject a non-pointer motion event.
2238 // If specific a display, it will dispatch to the focused window of particular display,
2239 // or it will dispatch to the focused window of focused display.
2240 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
2241 AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
2242 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
2243 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002244 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002245 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08002246 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002247}
2248
2249// Test per-display input monitors for key event.
2250TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
2251 //Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08002252 FakeMonitorReceiver monitorInPrimary =
2253 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
2254 FakeMonitorReceiver monitorInSecondary =
2255 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002256
2257 // Test inject a key down.
2258 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
2259 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
2260 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002261 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002262 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08002263 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002264}
2265
Jackal Guof9696682018-10-05 12:23:23 +08002266class InputFilterTest : public InputDispatcherTest {
2267protected:
2268 static constexpr int32_t SECOND_DISPLAY_ID = 1;
2269
2270 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) {
2271 NotifyMotionArgs motionArgs;
2272
2273 motionArgs = generateMotionArgs(
2274 AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
2275 mDispatcher->notifyMotion(&motionArgs);
2276 motionArgs = generateMotionArgs(
2277 AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
2278 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002279 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08002280 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08002281 mFakePolicy->assertFilterInputEventWasCalled(motionArgs);
Jackal Guof9696682018-10-05 12:23:23 +08002282 } else {
2283 mFakePolicy->assertFilterInputEventWasNotCalled();
2284 }
2285 }
2286
2287 void testNotifyKey(bool expectToBeFiltered) {
2288 NotifyKeyArgs keyArgs;
2289
2290 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
2291 mDispatcher->notifyKey(&keyArgs);
2292 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
2293 mDispatcher->notifyKey(&keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002294 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08002295
2296 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08002297 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08002298 } else {
2299 mFakePolicy->assertFilterInputEventWasNotCalled();
2300 }
2301 }
2302};
2303
2304// Test InputFilter for MotionEvent
2305TEST_F(InputFilterTest, MotionEvent_InputFilter) {
2306 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
2307 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
2308 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
2309
2310 // Enable InputFilter
2311 mDispatcher->setInputFilterEnabled(true);
2312 // Test touch on both primary and second display, and check if both events are filtered.
2313 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
2314 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
2315
2316 // Disable InputFilter
2317 mDispatcher->setInputFilterEnabled(false);
2318 // Test touch on both primary and second display, and check if both events aren't filtered.
2319 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
2320 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
2321}
2322
2323// Test InputFilter for KeyEvent
2324TEST_F(InputFilterTest, KeyEvent_InputFilter) {
2325 // Since the InputFilter is disabled by default, check if key event aren't filtered.
2326 testNotifyKey(/*expectToBeFiltered*/ false);
2327
2328 // Enable InputFilter
2329 mDispatcher->setInputFilterEnabled(true);
2330 // Send a key event, and check if it is filtered.
2331 testNotifyKey(/*expectToBeFiltered*/ true);
2332
2333 // Disable InputFilter
2334 mDispatcher->setInputFilterEnabled(false);
2335 // Send a key event, and check if it isn't filtered.
2336 testNotifyKey(/*expectToBeFiltered*/ false);
2337}
2338
chaviwfd6d3512019-03-25 13:23:49 -07002339class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002340 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07002341 InputDispatcherTest::SetUp();
2342
2343 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
2344 mUnfocusedWindow = new FakeWindowHandle(application, mDispatcher, "Top",
2345 ADISPLAY_ID_DEFAULT);
2346 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
2347 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
2348 // window.
Michael Wright44753b12020-07-08 13:48:11 +01002349 mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07002350
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002351 mFocusedWindow =
2352 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2353 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01002354 mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07002355
2356 // Set focused application.
2357 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002358 mFocusedWindow->setFocus(true);
chaviwfd6d3512019-03-25 13:23:49 -07002359
2360 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00002361 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002362 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07002363 }
2364
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002365 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07002366 InputDispatcherTest::TearDown();
2367
2368 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002369 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07002370 }
2371
2372protected:
2373 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002374 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002375 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07002376};
2377
2378// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
2379// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
2380// the onPointerDownOutsideFocus callback.
2381TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002382 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
2383 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2384 {20, 20}))
chaviwfd6d3512019-03-25 13:23:49 -07002385 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002386 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07002387
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002388 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07002389 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
2390}
2391
2392// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
2393// DOWN on the window that doesn't have focus. Ensure no window received the
2394// onPointerDownOutsideFocus callback.
2395TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002396 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
2397 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
chaviwfd6d3512019-03-25 13:23:49 -07002398 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002399 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07002400
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002401 ASSERT_TRUE(mDispatcher->waitForIdle());
2402 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07002403}
2404
2405// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
2406// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
2407TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
2408 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2409 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002410 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07002411
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002412 ASSERT_TRUE(mDispatcher->waitForIdle());
2413 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07002414}
2415
2416// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
2417// DOWN on the window that already has focus. Ensure no window received the
2418// onPointerDownOutsideFocus callback.
2419TEST_F(InputDispatcherOnPointerDownOutsideFocus,
2420 OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002421 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
2422 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002423 FOCUSED_WINDOW_TOUCH_POINT))
chaviwfd6d3512019-03-25 13:23:49 -07002424 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002425 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07002426
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002427 ASSERT_TRUE(mDispatcher->waitForIdle());
2428 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07002429}
2430
chaviwaf87b3e2019-10-01 16:59:28 -07002431// These tests ensures we can send touch events to a single client when there are multiple input
2432// windows that point to the same client token.
2433class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
2434 virtual void SetUp() override {
2435 InputDispatcherTest::SetUp();
2436
2437 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
2438 mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1",
2439 ADISPLAY_ID_DEFAULT);
2440 // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window.
2441 // 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 +01002442 mWindow1->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
2443 InputWindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07002444 mWindow1->setFrame(Rect(0, 0, 100, 100));
2445
2446 mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2",
2447 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
Michael Wright44753b12020-07-08 13:48:11 +01002448 mWindow2->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
2449 InputWindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07002450 mWindow2->setFrame(Rect(100, 100, 200, 200));
2451
Arthur Hung72d8dc32020-03-28 00:48:39 +00002452 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07002453 }
2454
2455protected:
2456 sp<FakeWindowHandle> mWindow1;
2457 sp<FakeWindowHandle> mWindow2;
2458
2459 // Helper function to convert the point from screen coordinates into the window's space
2460 static PointF getPointInWindow(const InputWindowInfo* windowInfo, const PointF& point) {
2461 float x = windowInfo->windowXScale * (point.x - windowInfo->frameLeft);
2462 float y = windowInfo->windowYScale * (point.y - windowInfo->frameTop);
2463 return {x, y};
2464 }
2465
2466 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
2467 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002468 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07002469 InputEvent* event = window->consume();
2470
2471 ASSERT_NE(nullptr, event) << name.c_str()
2472 << ": consumer should have returned non-NULL event.";
2473
2474 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
2475 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
2476 << " event, got " << inputEventTypeToString(event->getType()) << " event";
2477
2478 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
2479 EXPECT_EQ(expectedAction, motionEvent.getAction());
2480
2481 for (size_t i = 0; i < points.size(); i++) {
2482 float expectedX = points[i].x;
2483 float expectedY = points[i].y;
2484
2485 EXPECT_EQ(expectedX, motionEvent.getX(i))
2486 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
2487 << ", got " << motionEvent.getX(i);
2488 EXPECT_EQ(expectedY, motionEvent.getY(i))
2489 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
2490 << ", got " << motionEvent.getY(i);
2491 }
2492 }
2493};
2494
2495TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
2496 // Touch Window 1
2497 PointF touchedPoint = {10, 10};
2498 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
2499
2500 NotifyMotionArgs motionArgs =
2501 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2502 ADISPLAY_ID_DEFAULT, {touchedPoint});
2503 mDispatcher->notifyMotion(&motionArgs);
2504 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, {expectedPoint});
2505
2506 // Release touch on Window 1
2507 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2508 ADISPLAY_ID_DEFAULT, {touchedPoint});
2509 mDispatcher->notifyMotion(&motionArgs);
2510 // consume the UP event
2511 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_UP, {expectedPoint});
2512
2513 // Touch Window 2
2514 touchedPoint = {150, 150};
2515 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
2516
2517 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2518 ADISPLAY_ID_DEFAULT, {touchedPoint});
2519 mDispatcher->notifyMotion(&motionArgs);
2520
2521 // Consuming from window1 since it's the window that has the InputReceiver
2522 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, {expectedPoint});
2523}
2524
2525TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentScale) {
2526 mWindow2->setWindowScale(0.5f, 0.5f);
2527
2528 // Touch Window 1
2529 PointF touchedPoint = {10, 10};
2530 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
2531
2532 NotifyMotionArgs motionArgs =
2533 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2534 ADISPLAY_ID_DEFAULT, {touchedPoint});
2535 mDispatcher->notifyMotion(&motionArgs);
2536 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, {expectedPoint});
2537
2538 // Release touch on Window 1
2539 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2540 ADISPLAY_ID_DEFAULT, {touchedPoint});
2541 mDispatcher->notifyMotion(&motionArgs);
2542 // consume the UP event
2543 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_UP, {expectedPoint});
2544
2545 // Touch Window 2
2546 touchedPoint = {150, 150};
2547 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
2548
2549 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2550 ADISPLAY_ID_DEFAULT, {touchedPoint});
2551 mDispatcher->notifyMotion(&motionArgs);
2552
2553 // Consuming from window1 since it's the window that has the InputReceiver
2554 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, {expectedPoint});
2555}
2556
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002557TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentScale) {
2558 mWindow2->setWindowScale(0.5f, 0.5f);
2559
2560 // Touch Window 1
2561 std::vector<PointF> touchedPoints = {PointF{10, 10}};
2562 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
2563
2564 NotifyMotionArgs motionArgs =
2565 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2566 ADISPLAY_ID_DEFAULT, touchedPoints);
2567 mDispatcher->notifyMotion(&motionArgs);
2568 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, expectedPoints);
2569
2570 // Touch Window 2
2571 int32_t actionPointerDown =
2572 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
2573 touchedPoints.emplace_back(PointF{150, 150});
2574 expectedPoints.emplace_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
2575
2576 motionArgs = generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN,
2577 ADISPLAY_ID_DEFAULT, touchedPoints);
2578 mDispatcher->notifyMotion(&motionArgs);
2579
2580 // Consuming from window1 since it's the window that has the InputReceiver
2581 consumeMotionEvent(mWindow1, actionPointerDown, expectedPoints);
2582}
2583
2584TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentScale) {
2585 mWindow2->setWindowScale(0.5f, 0.5f);
2586
2587 // Touch Window 1
2588 std::vector<PointF> touchedPoints = {PointF{10, 10}};
2589 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
2590
2591 NotifyMotionArgs motionArgs =
2592 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2593 ADISPLAY_ID_DEFAULT, touchedPoints);
2594 mDispatcher->notifyMotion(&motionArgs);
2595 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, expectedPoints);
2596
2597 // Touch Window 2
2598 int32_t actionPointerDown =
2599 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
2600 touchedPoints.emplace_back(PointF{150, 150});
2601 expectedPoints.emplace_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
2602
2603 motionArgs = generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN,
2604 ADISPLAY_ID_DEFAULT, touchedPoints);
2605 mDispatcher->notifyMotion(&motionArgs);
2606
2607 // Consuming from window1 since it's the window that has the InputReceiver
2608 consumeMotionEvent(mWindow1, actionPointerDown, expectedPoints);
2609
2610 // Move both windows
2611 touchedPoints = {{20, 20}, {175, 175}};
2612 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
2613 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
2614
2615 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2616 ADISPLAY_ID_DEFAULT, touchedPoints);
2617 mDispatcher->notifyMotion(&motionArgs);
2618
2619 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_MOVE, expectedPoints);
2620}
2621
2622TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
2623 mWindow1->setWindowScale(0.5f, 0.5f);
2624
2625 // Touch Window 1
2626 std::vector<PointF> touchedPoints = {PointF{10, 10}};
2627 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
2628
2629 NotifyMotionArgs motionArgs =
2630 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2631 ADISPLAY_ID_DEFAULT, touchedPoints);
2632 mDispatcher->notifyMotion(&motionArgs);
2633 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, expectedPoints);
2634
2635 // Touch Window 2
2636 int32_t actionPointerDown =
2637 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
2638 touchedPoints.emplace_back(PointF{150, 150});
2639 expectedPoints.emplace_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
2640
2641 motionArgs = generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN,
2642 ADISPLAY_ID_DEFAULT, touchedPoints);
2643 mDispatcher->notifyMotion(&motionArgs);
2644
2645 // Consuming from window1 since it's the window that has the InputReceiver
2646 consumeMotionEvent(mWindow1, actionPointerDown, expectedPoints);
2647
2648 // Move both windows
2649 touchedPoints = {{20, 20}, {175, 175}};
2650 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
2651 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
2652
2653 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2654 ADISPLAY_ID_DEFAULT, touchedPoints);
2655 mDispatcher->notifyMotion(&motionArgs);
2656
2657 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_MOVE, expectedPoints);
2658}
2659
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002660class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
2661 virtual void SetUp() override {
2662 InputDispatcherTest::SetUp();
2663
2664 mApplication = new FakeApplicationHandle();
2665 mApplication->setDispatchingTimeout(20ms);
2666 mWindow =
2667 new FakeWindowHandle(mApplication, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2668 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05002669 mWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002670 mWindow->setFocus(true);
2671 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
2672 // window.
Michael Wright44753b12020-07-08 13:48:11 +01002673 mWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002674
2675 // Set focused application.
2676 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
2677
2678 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
2679 mWindow->consumeFocusEvent(true);
2680 }
2681
2682 virtual void TearDown() override {
2683 InputDispatcherTest::TearDown();
2684 mWindow.clear();
2685 }
2686
2687protected:
2688 sp<FakeApplicationHandle> mApplication;
2689 sp<FakeWindowHandle> mWindow;
2690 static constexpr PointF WINDOW_LOCATION = {20, 20};
2691
2692 void tapOnWindow() {
2693 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
2694 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2695 WINDOW_LOCATION));
2696 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
2697 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2698 WINDOW_LOCATION));
2699 }
2700};
2701
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002702// Send a tap and respond, which should not cause an ANR.
2703TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
2704 tapOnWindow();
2705 mWindow->consumeMotionDown();
2706 mWindow->consumeMotionUp();
2707 ASSERT_TRUE(mDispatcher->waitForIdle());
2708 mFakePolicy->assertNotifyAnrWasNotCalled();
2709}
2710
2711// Send a regular key and respond, which should not cause an ANR.
2712TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
2713 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher));
2714 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
2715 ASSERT_TRUE(mDispatcher->waitForIdle());
2716 mFakePolicy->assertNotifyAnrWasNotCalled();
2717}
2718
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002719// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002720// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
2721// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002722TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
2723 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
2724 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2725 WINDOW_LOCATION));
2726
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002727 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
2728 ASSERT_TRUE(sequenceNum);
2729 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
2730 mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002731
2732 // The remaining lines are not really needed for the test, but kept as a sanity check
2733 mWindow->finishEvent(*sequenceNum);
2734 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
2735 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002736 ASSERT_TRUE(mDispatcher->waitForIdle());
2737}
2738
2739// Send a key to the app and have the app not respond right away.
2740TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
2741 // Inject a key, and don't respond - expect that ANR is called.
2742 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher));
2743 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
2744 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002745 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002746 mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/, mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002747 ASSERT_TRUE(mDispatcher->waitForIdle());
2748}
2749
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002750// We have a focused application, but no focused window
2751TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
2752 mWindow->setFocus(false);
2753 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
2754 mWindow->consumeFocusEvent(false);
2755
2756 // taps on the window work as normal
2757 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
2758 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2759 WINDOW_LOCATION));
2760 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
2761 mDispatcher->waitForIdle();
2762 mFakePolicy->assertNotifyAnrWasNotCalled();
2763
2764 // Once a focused event arrives, we get an ANR for this application
2765 // We specify the injection timeout to be smaller than the application timeout, to ensure that
2766 // injection times out (instead of failing).
2767 const int32_t result =
2768 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
2769 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT, 10ms);
2770 ASSERT_EQ(INPUT_EVENT_INJECTION_TIMED_OUT, result);
2771 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
2772 mFakePolicy->assertNotifyAnrWasCalled(timeout, mApplication, nullptr /*windowToken*/);
2773 ASSERT_TRUE(mDispatcher->waitForIdle());
2774}
2775
2776// We have a focused application, but no focused window
2777// If the policy wants to keep waiting on the focused window to be added, make sure
2778// that this timeout extension is honored and ANR is raised again.
2779TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_ExtendsAnr) {
2780 mWindow->setFocus(false);
2781 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
2782 mWindow->consumeFocusEvent(false);
2783 const std::chrono::duration timeout = 5ms;
2784 mFakePolicy->setAnrTimeout(timeout);
2785
2786 // Once a focused event arrives, we get an ANR for this application
2787 // We specify the injection timeout to be smaller than the application timeout, to ensure that
2788 // injection times out (instead of failing).
2789 const int32_t result =
2790 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
2791 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT, 10ms);
2792 ASSERT_EQ(INPUT_EVENT_INJECTION_TIMED_OUT, result);
2793 const std::chrono::duration appTimeout =
2794 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
2795 mFakePolicy->assertNotifyAnrWasCalled(appTimeout, mApplication, nullptr /*windowToken*/);
2796
2797 // After the extended time has passed, ANR should be raised again
2798 mFakePolicy->assertNotifyAnrWasCalled(timeout, mApplication, nullptr /*windowToken*/);
2799
2800 // If we stop extending the timeout, dispatcher should go to idle.
2801 // Another ANR may be raised during this time
2802 mFakePolicy->setAnrTimeout(0ms);
2803 ASSERT_TRUE(mDispatcher->waitForIdle());
2804}
2805
2806// We have a focused application, but no focused window
2807TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
2808 mWindow->setFocus(false);
2809 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
2810 mWindow->consumeFocusEvent(false);
2811
2812 // Once a focused event arrives, we get an ANR for this application
2813 const int32_t result =
2814 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
2815 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT, 10ms);
2816 ASSERT_EQ(INPUT_EVENT_INJECTION_TIMED_OUT, result);
2817
2818 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
2819 mFakePolicy->assertNotifyAnrWasCalled(timeout, mApplication, nullptr /*windowToken*/);
2820
2821 // Future focused events get dropped right away
2822 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, injectKeyDown(mDispatcher));
2823 ASSERT_TRUE(mDispatcher->waitForIdle());
2824 mWindow->assertNoEvents();
2825}
2826
2827/**
2828 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
2829 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
2830 * If we process 1 of the events, but ANR on the second event with the same timestamp,
2831 * the ANR mechanism should still work.
2832 *
2833 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
2834 * DOWN event, while not responding on the second one.
2835 */
2836TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
2837 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
2838 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2839 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
2840 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
2841 AMOTION_EVENT_INVALID_CURSOR_POSITION},
2842 500ms, INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT, currentTime);
2843
2844 // Now send ACTION_UP, with identical timestamp
2845 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2846 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
2847 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
2848 AMOTION_EVENT_INVALID_CURSOR_POSITION},
2849 500ms, INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT, currentTime);
2850
2851 // We have now sent down and up. Let's consume first event and then ANR on the second.
2852 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2853 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
2854 mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/, mWindow->getToken());
2855}
2856
2857// If an app is not responding to a key event, gesture monitors should continue to receive
2858// new motion events
2859TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnKey) {
2860 FakeMonitorReceiver monitor =
2861 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
2862 true /*isGestureMonitor*/);
2863
2864 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
2865 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2866 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
2867
2868 // Stuck on the ACTION_UP
2869 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
2870 mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr, mWindow->getToken());
2871
2872 // New tap will go to the gesture monitor, but not to the window
2873 tapOnWindow();
2874 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
2875 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
2876
2877 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
2878 mDispatcher->waitForIdle();
2879 mWindow->assertNoEvents();
2880 monitor.assertNoEvents();
2881}
2882
2883// If an app is not responding to a motion event, gesture monitors should continue to receive
2884// new motion events
2885TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnMotion) {
2886 FakeMonitorReceiver monitor =
2887 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
2888 true /*isGestureMonitor*/);
2889
2890 tapOnWindow();
2891 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
2892 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
2893
2894 mWindow->consumeMotionDown();
2895 // Stuck on the ACTION_UP
2896 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
2897 mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr, mWindow->getToken());
2898
2899 // New tap will go to the gesture monitor, but not to the window
2900 tapOnWindow();
2901 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
2902 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
2903
2904 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
2905 mDispatcher->waitForIdle();
2906 mWindow->assertNoEvents();
2907 monitor.assertNoEvents();
2908}
2909
2910// If a window is unresponsive, then you get anr. if the window later catches up and starts to
2911// process events, you don't get an anr. When the window later becomes unresponsive again, you
2912// get an ANR again.
2913// 1. tap -> block on ACTION_UP -> receive ANR
2914// 2. consume all pending events (= queue becomes healthy again)
2915// 3. tap again -> block on ACTION_UP again -> receive ANR second time
2916TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
2917 tapOnWindow();
2918
2919 mWindow->consumeMotionDown();
2920 // Block on ACTION_UP
2921 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
2922 mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/, mWindow->getToken());
2923 mWindow->consumeMotionUp(); // Now the connection should be healthy again
2924 mDispatcher->waitForIdle();
2925 mWindow->assertNoEvents();
2926
2927 tapOnWindow();
2928 mWindow->consumeMotionDown();
2929 mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/, mWindow->getToken());
2930 mWindow->consumeMotionUp();
2931
2932 mDispatcher->waitForIdle();
2933 mWindow->assertNoEvents();
2934}
2935
2936// If the policy tells us to raise ANR again after some time, ensure that the timeout extension
2937// is honored
2938TEST_F(InputDispatcherSingleWindowAnr, Policy_CanExtendTimeout) {
2939 const std::chrono::duration timeout = 5ms;
2940 mFakePolicy->setAnrTimeout(timeout);
2941
2942 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
2943 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2944 WINDOW_LOCATION));
2945
2946 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
2947 mFakePolicy->assertNotifyAnrWasCalled(windowTimeout, nullptr /*application*/,
2948 mWindow->getToken());
2949
2950 // Since the policy wanted to extend ANR, make sure it is called again after the extension
2951 mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/, mWindow->getToken());
2952 mFakePolicy->setAnrTimeout(0ms);
2953 std::this_thread::sleep_for(windowTimeout);
2954 // We are not checking if ANR has been called, because it may have been called again by the
2955 // time we set the timeout to 0
2956
2957 // When the policy finally says stop, we should get ACTION_CANCEL
2958 mWindow->consumeMotionDown();
2959 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
2960 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
2961 mWindow->assertNoEvents();
2962}
2963
2964/**
2965 * If a window is processing a motion event, and then a key event comes in, the key event should
2966 * not to to the focused window until the motion is processed.
2967 *
2968 * Warning!!!
2969 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
2970 * and the injection timeout that we specify when injecting the key.
2971 * We must have the injection timeout (10ms) be smaller than
2972 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
2973 *
2974 * If that value changes, this test should also change.
2975 */
2976TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
2977 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
2978 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
2979
2980 tapOnWindow();
2981 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
2982 ASSERT_TRUE(downSequenceNum);
2983 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
2984 ASSERT_TRUE(upSequenceNum);
2985 // Don't finish the events yet, and send a key
2986 // Injection will "succeed" because we will eventually give up and send the key to the focused
2987 // window even if motions are still being processed. But because the injection timeout is short,
2988 // we will receive INJECTION_TIMED_OUT as the result.
2989
2990 int32_t result =
2991 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
2992 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT, 10ms);
2993 ASSERT_EQ(INPUT_EVENT_INJECTION_TIMED_OUT, result);
2994 // Key will not be sent to the window, yet, because the window is still processing events
2995 // and the key remains pending, waiting for the touch events to be processed
2996 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
2997 ASSERT_FALSE(keySequenceNum);
2998
2999 std::this_thread::sleep_for(500ms);
3000 // if we wait long enough though, dispatcher will give up, and still send the key
3001 // to the focused window, even though we have not yet finished the motion event
3002 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3003 mWindow->finishEvent(*downSequenceNum);
3004 mWindow->finishEvent(*upSequenceNum);
3005}
3006
3007/**
3008 * If a window is processing a motion event, and then a key event comes in, the key event should
3009 * not go to the focused window until the motion is processed.
3010 * If then a new motion comes in, then the pending key event should be going to the currently
3011 * focused window right away.
3012 */
3013TEST_F(InputDispatcherSingleWindowAnr,
3014 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
3015 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3016 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3017
3018 tapOnWindow();
3019 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3020 ASSERT_TRUE(downSequenceNum);
3021 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3022 ASSERT_TRUE(upSequenceNum);
3023 // Don't finish the events yet, and send a key
3024 // Injection is async, so it will succeed
3025 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
3026 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
3027 ADISPLAY_ID_DEFAULT, INPUT_EVENT_INJECTION_SYNC_NONE));
3028 // At this point, key is still pending, and should not be sent to the application yet.
3029 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3030 ASSERT_FALSE(keySequenceNum);
3031
3032 // Now tap down again. It should cause the pending key to go to the focused window right away.
3033 tapOnWindow();
3034 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
3035 // the other events yet. We can finish events in any order.
3036 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
3037 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
3038 mWindow->consumeMotionDown();
3039 mWindow->consumeMotionUp();
3040 mWindow->assertNoEvents();
3041}
3042
3043class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
3044 virtual void SetUp() override {
3045 InputDispatcherTest::SetUp();
3046
3047 mApplication = new FakeApplicationHandle();
3048 mApplication->setDispatchingTimeout(10ms);
3049 mUnfocusedWindow =
3050 new FakeWindowHandle(mApplication, mDispatcher, "Unfocused", ADISPLAY_ID_DEFAULT);
3051 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
3052 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3053 // window.
3054 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Michael Wright44753b12020-07-08 13:48:11 +01003055 mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3056 InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH |
3057 InputWindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003058
3059 mFocusedWindow =
3060 new FakeWindowHandle(mApplication, mDispatcher, "Focused", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05003061 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003062 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01003063 mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3064 InputWindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003065
3066 // Set focused application.
3067 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
3068 mFocusedWindow->setFocus(true);
3069
3070 // Expect one focus window exist in display.
3071 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
3072 mFocusedWindow->consumeFocusEvent(true);
3073 }
3074
3075 virtual void TearDown() override {
3076 InputDispatcherTest::TearDown();
3077
3078 mUnfocusedWindow.clear();
3079 mFocusedWindow.clear();
3080 }
3081
3082protected:
3083 sp<FakeApplicationHandle> mApplication;
3084 sp<FakeWindowHandle> mUnfocusedWindow;
3085 sp<FakeWindowHandle> mFocusedWindow;
3086 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
3087 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
3088 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
3089
3090 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
3091
3092 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
3093
3094private:
3095 void tap(const PointF& location) {
3096 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
3097 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3098 location));
3099 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
3100 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3101 location));
3102 }
3103};
3104
3105// If we have 2 windows that are both unresponsive, the one with the shortest timeout
3106// should be ANR'd first.
3107TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
3108 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
3109 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3110 FOCUSED_WINDOW_LOCATION))
3111 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
3112 mFocusedWindow->consumeMotionDown();
3113 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3114 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3115 // We consumed all events, so no ANR
3116 ASSERT_TRUE(mDispatcher->waitForIdle());
3117 mFakePolicy->assertNotifyAnrWasNotCalled();
3118
3119 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
3120 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3121 FOCUSED_WINDOW_LOCATION));
3122 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
3123 ASSERT_TRUE(unfocusedSequenceNum);
3124 std::optional<uint32_t> focusedSequenceNum = mFocusedWindow->receiveEvent();
3125 ASSERT_TRUE(focusedSequenceNum);
3126
3127 const std::chrono::duration timeout =
3128 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
3129 mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/,
3130 mFocusedWindow->getToken());
3131
3132 mFocusedWindow->finishEvent(*focusedSequenceNum);
3133 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
3134 ASSERT_TRUE(mDispatcher->waitForIdle());
3135}
3136
3137// If we have 2 windows with identical timeouts that are both unresponsive,
3138// it doesn't matter which order they should have ANR.
3139// But we should receive ANR for both.
3140TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
3141 // Set the timeout for unfocused window to match the focused window
3142 mUnfocusedWindow->setDispatchingTimeout(10ms);
3143 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
3144
3145 tapOnFocusedWindow();
3146 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
3147 std::pair<sp<InputApplicationHandle>, sp<IBinder>> anrData1 =
3148 mFakePolicy->getNotifyAnrData(10ms);
3149 std::pair<sp<InputApplicationHandle>, sp<IBinder>> anrData2 =
3150 mFakePolicy->getNotifyAnrData(0ms);
3151
3152 // We don't know which window will ANR first. But both of them should happen eventually.
3153 ASSERT_TRUE(mFocusedWindow->getToken() == anrData1.second ||
3154 mFocusedWindow->getToken() == anrData2.second);
3155 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrData1.second ||
3156 mUnfocusedWindow->getToken() == anrData2.second);
3157
3158 ASSERT_TRUE(mDispatcher->waitForIdle());
3159 mFakePolicy->assertNotifyAnrWasNotCalled();
3160}
3161
3162// If a window is already not responding, the second tap on the same window should be ignored.
3163// We should also log an error to account for the dropped event (not tested here).
3164// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
3165TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
3166 tapOnFocusedWindow();
3167 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3168 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3169 // Receive the events, but don't respond
3170 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
3171 ASSERT_TRUE(downEventSequenceNum);
3172 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
3173 ASSERT_TRUE(upEventSequenceNum);
3174 const std::chrono::duration timeout =
3175 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
3176 mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/,
3177 mFocusedWindow->getToken());
3178
3179 // Tap once again
3180 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
3181 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED,
3182 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3183 FOCUSED_WINDOW_LOCATION));
3184 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED,
3185 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3186 FOCUSED_WINDOW_LOCATION));
3187 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
3188 // valid touch target
3189 mUnfocusedWindow->assertNoEvents();
3190
3191 // Consume the first tap
3192 mFocusedWindow->finishEvent(*downEventSequenceNum);
3193 mFocusedWindow->finishEvent(*upEventSequenceNum);
3194 ASSERT_TRUE(mDispatcher->waitForIdle());
3195 // The second tap did not go to the focused window
3196 mFocusedWindow->assertNoEvents();
3197 // should not have another ANR after the window just became healthy again
3198 mFakePolicy->assertNotifyAnrWasNotCalled();
3199}
3200
3201// If you tap outside of all windows, there will not be ANR
3202TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
3203 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED,
3204 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3205 LOCATION_OUTSIDE_ALL_WINDOWS));
3206 ASSERT_TRUE(mDispatcher->waitForIdle());
3207 mFakePolicy->assertNotifyAnrWasNotCalled();
3208}
3209
3210// Since the focused window is paused, tapping on it should not produce any events
3211TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
3212 mFocusedWindow->setPaused(true);
3213 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
3214
3215 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED,
3216 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3217 FOCUSED_WINDOW_LOCATION));
3218
3219 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
3220 ASSERT_TRUE(mDispatcher->waitForIdle());
3221 // Should not ANR because the window is paused, and touches shouldn't go to it
3222 mFakePolicy->assertNotifyAnrWasNotCalled();
3223
3224 mFocusedWindow->assertNoEvents();
3225 mUnfocusedWindow->assertNoEvents();
3226}
3227
3228/**
3229 * If a window is processing a motion event, and then a key event comes in, the key event should
3230 * not to to the focused window until the motion is processed.
3231 * If a different window becomes focused at this time, the key should go to that window instead.
3232 *
3233 * Warning!!!
3234 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
3235 * and the injection timeout that we specify when injecting the key.
3236 * We must have the injection timeout (10ms) be smaller than
3237 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
3238 *
3239 * If that value changes, this test should also change.
3240 */
3241TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
3242 // Set a long ANR timeout to prevent it from triggering
3243 mFocusedWindow->setDispatchingTimeout(2s);
3244 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
3245
3246 tapOnUnfocusedWindow();
3247 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
3248 ASSERT_TRUE(downSequenceNum);
3249 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
3250 ASSERT_TRUE(upSequenceNum);
3251 // Don't finish the events yet, and send a key
3252 // Injection will succeed because we will eventually give up and send the key to the focused
3253 // window even if motions are still being processed.
3254
3255 int32_t result =
3256 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
3257 INPUT_EVENT_INJECTION_SYNC_NONE, 10ms /*injectionTimeout*/);
3258 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, result);
3259 // Key will not be sent to the window, yet, because the window is still processing events
3260 // and the key remains pending, waiting for the touch events to be processed
3261 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
3262 ASSERT_FALSE(keySequenceNum);
3263
3264 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
3265 mFocusedWindow->setFocus(false);
3266 mUnfocusedWindow->setFocus(true);
3267 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
3268
3269 // Focus events should precede the key events
3270 mUnfocusedWindow->consumeFocusEvent(true);
3271 mFocusedWindow->consumeFocusEvent(false);
3272
3273 // Finish the tap events, which should unblock dispatcher
3274 mUnfocusedWindow->finishEvent(*downSequenceNum);
3275 mUnfocusedWindow->finishEvent(*upSequenceNum);
3276
3277 // Now that all queues are cleared and no backlog in the connections, the key event
3278 // can finally go to the newly focused "mUnfocusedWindow".
3279 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3280 mFocusedWindow->assertNoEvents();
3281 mUnfocusedWindow->assertNoEvents();
3282}
3283
3284// When the touch stream is split across 2 windows, and one of them does not respond,
3285// then ANR should be raised and the touch should be canceled for the unresponsive window.
3286// The other window should not be affected by that.
3287TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
3288 // Touch Window 1
3289 NotifyMotionArgs motionArgs =
3290 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3291 ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION});
3292 mDispatcher->notifyMotion(&motionArgs);
3293 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3294 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3295
3296 // Touch Window 2
3297 int32_t actionPointerDown =
3298 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3299
3300 motionArgs =
3301 generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3302 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION});
3303 mDispatcher->notifyMotion(&motionArgs);
3304
3305 const std::chrono::duration timeout =
3306 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
3307 mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/,
3308 mFocusedWindow->getToken());
3309
3310 mUnfocusedWindow->consumeMotionDown();
3311 mFocusedWindow->consumeMotionDown();
3312 // Focused window may or may not receive ACTION_MOVE
3313 // But it should definitely receive ACTION_CANCEL due to the ANR
3314 InputEvent* event;
3315 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
3316 ASSERT_TRUE(moveOrCancelSequenceNum);
3317 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
3318 ASSERT_NE(nullptr, event);
3319 ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION);
3320 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
3321 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
3322 mFocusedWindow->consumeMotionCancel();
3323 } else {
3324 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
3325 }
3326
3327 ASSERT_TRUE(mDispatcher->waitForIdle());
3328 mUnfocusedWindow->assertNoEvents();
3329 mFocusedWindow->assertNoEvents();
3330}
3331
Garfield Tane84e6f92019-08-29 17:28:41 -07003332} // namespace android::inputdispatcher