blob: 40471b2bb1fd3a7ffbd7330f5c1600f6d4e48a55 [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;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080032using android::os::InputEventInjectionResult;
33using android::os::InputEventInjectionSync;
Michael Wright44753b12020-07-08 13:48:11 +010034using namespace android::flag_operators;
Garfield Tan1c7bc862020-01-28 13:24:04 -080035
Garfield Tane84e6f92019-08-29 17:28:41 -070036namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080037
38// An arbitrary time value.
39static const nsecs_t ARBITRARY_TIME = 1234;
40
41// An arbitrary device id.
42static const int32_t DEVICE_ID = 1;
43
Jeff Brownf086ddb2014-02-11 14:28:48 -080044// An arbitrary display id.
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -080045static const int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
Jeff Brownf086ddb2014-02-11 14:28:48 -080046
Michael Wrightd02c5b62014-02-10 15:10:22 -080047// An arbitrary injector pid / uid pair that has permission to inject events.
48static const int32_t INJECTOR_PID = 999;
49static const int32_t INJECTOR_UID = 1001;
50
chaviwd1c23182019-12-20 18:44:56 -080051struct PointF {
52 float x;
53 float y;
54};
Michael Wrightd02c5b62014-02-10 15:10:22 -080055
Gang Wang342c9272020-01-13 13:15:04 -050056/**
57 * Return a DOWN key event with KEYCODE_A.
58 */
59static KeyEvent getTestKeyEvent() {
60 KeyEvent event;
61
Garfield Tanfbe732e2020-01-24 11:26:14 -080062 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
63 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
64 ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -050065 return event;
66}
67
Michael Wrightd02c5b62014-02-10 15:10:22 -080068// --- FakeInputDispatcherPolicy ---
69
70class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
71 InputDispatcherConfiguration mConfig;
72
73protected:
74 virtual ~FakeInputDispatcherPolicy() {
75 }
76
77public:
78 FakeInputDispatcherPolicy() {
Jackal Guof9696682018-10-05 12:23:23 +080079 }
80
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080081 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080082 assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_KEY, args.eventTime, args.action,
83 args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080084 }
85
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080086 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args) {
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080087 assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_MOTION, args.eventTime, args.action,
88 args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080089 }
90
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -070091 void assertFilterInputEventWasNotCalled() {
92 std::scoped_lock lock(mLock);
93 ASSERT_EQ(nullptr, mFilteredEvent);
94 }
Michael Wrightd02c5b62014-02-10 15:10:22 -080095
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -080096 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -070097 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -080098 ASSERT_TRUE(mConfigurationChangedTime)
99 << "Timed out waiting for configuration changed call";
100 ASSERT_EQ(*mConfigurationChangedTime, when);
101 mConfigurationChangedTime = std::nullopt;
102 }
103
104 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700105 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800106 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800107 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800108 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
109 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
110 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
111 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
112 mLastNotifySwitch = std::nullopt;
113 }
114
chaviwfd6d3512019-03-25 13:23:49 -0700115 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700116 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800117 ASSERT_EQ(touchedToken, mOnPointerDownToken);
118 mOnPointerDownToken.clear();
119 }
120
121 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700122 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800123 ASSERT_TRUE(mOnPointerDownToken == nullptr)
124 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700125 }
126
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700127 // This function must be called soon after the expected ANR timer starts,
128 // because we are also checking how much time has passed.
Chris Yea209fde2020-07-22 13:54:51 -0700129 void assertNotifyAnrWasCalled(
130 std::chrono::nanoseconds timeout,
131 const std::shared_ptr<InputApplicationHandle>& expectedApplication,
132 const sp<IBinder>& expectedToken) {
133 std::pair<std::shared_ptr<InputApplicationHandle>, sp<IBinder>> anrData;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700134 ASSERT_NO_FATAL_FAILURE(anrData = getNotifyAnrData(timeout));
135 ASSERT_EQ(expectedApplication, anrData.first);
136 ASSERT_EQ(expectedToken, anrData.second);
137 }
138
Chris Yea209fde2020-07-22 13:54:51 -0700139 std::pair<std::shared_ptr<InputApplicationHandle>, sp<IBinder>> getNotifyAnrData(
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700140 std::chrono::nanoseconds timeout) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700141 const std::chrono::time_point start = std::chrono::steady_clock::now();
142 std::unique_lock lock(mLock);
143 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
144 android::base::ScopedLockAssertion assumeLocked(mLock);
145
146 // If there is an ANR, Dispatcher won't be idle because there are still events
147 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
148 // before checking if ANR was called.
149 // Since dispatcher is not guaranteed to call notifyAnr right away, we need to provide
150 // it some time to act. 100ms seems reasonable.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700151 mNotifyAnr.wait_for(lock, timeToWait, [this]() REQUIRES(mLock) {
152 return !mAnrApplications.empty() && !mAnrWindowTokens.empty();
153 });
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700154 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700155 if (mAnrApplications.empty() || mAnrWindowTokens.empty()) {
156 ADD_FAILURE() << "Did not receive ANR callback";
Siarhei Vishniakoua72accd2020-09-22 21:43:09 -0500157 return {};
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700158 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700159 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
160 // the dispatcher started counting before this function was called
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700161 if (std::chrono::abs(timeout - waited) > 100ms) {
162 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
163 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
164 << "ms, but waited "
165 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
166 << "ms instead";
167 }
Chris Yea209fde2020-07-22 13:54:51 -0700168 std::pair<std::shared_ptr<InputApplicationHandle>, sp<IBinder>> result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700169 std::make_pair(mAnrApplications.front(), mAnrWindowTokens.front());
170 mAnrApplications.pop();
171 mAnrWindowTokens.pop();
172 return result;
173 }
174
175 void assertNotifyAnrWasNotCalled() {
176 std::scoped_lock lock(mLock);
177 ASSERT_TRUE(mAnrApplications.empty());
178 ASSERT_TRUE(mAnrWindowTokens.empty());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700179 }
180
Garfield Tan1c7bc862020-01-28 13:24:04 -0800181 void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) {
182 mConfig.keyRepeatTimeout = timeout;
183 mConfig.keyRepeatDelay = delay;
184 }
185
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700186 void setAnrTimeout(std::chrono::nanoseconds timeout) { mAnrTimeout = timeout; }
187
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700189 std::mutex mLock;
190 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
191 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
192 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
193 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800194
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700195 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700196 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700197 std::queue<sp<IBinder>> mAnrWindowTokens GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700198 std::condition_variable mNotifyAnr;
199 std::chrono::nanoseconds mAnrTimeout = 0ms;
200
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600201 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700202 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800203 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800204 }
205
Chris Yea209fde2020-07-22 13:54:51 -0700206 std::chrono::nanoseconds notifyAnr(const std::shared_ptr<InputApplicationHandle>& application,
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500207 const sp<IBinder>& windowToken,
208 const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700209 std::scoped_lock lock(mLock);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700210 mAnrApplications.push(application);
211 mAnrWindowTokens.push(windowToken);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700212 mNotifyAnr.notify_all();
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500213 return mAnrTimeout;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800214 }
215
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600216 void notifyInputChannelBroken(const sp<IBinder>&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800217
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600218 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700219
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600220 void notifyUntrustedTouch(const std::string& obscuringPackage) override {}
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000221
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600222 void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800223 *outConfig = mConfig;
224 }
225
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600226 bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700227 std::scoped_lock lock(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800228 switch (inputEvent->getType()) {
229 case AINPUT_EVENT_TYPE_KEY: {
230 const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800231 mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800232 break;
233 }
234
235 case AINPUT_EVENT_TYPE_MOTION: {
236 const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800237 mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800238 break;
239 }
240 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800241 return true;
242 }
243
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600244 void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800245
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600246 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800247
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600248 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800249 return 0;
250 }
251
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600252 bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800253 return false;
254 }
255
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600256 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
257 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700258 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800259 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
260 * essentially a passthrough for notifySwitch.
261 */
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800262 mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800263 }
264
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600265 void pokeUserActivity(nsecs_t, int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800266
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600267 bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) override { return false; }
Jackal Guof9696682018-10-05 12:23:23 +0800268
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600269 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700270 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700271 mOnPointerDownToken = newToken;
272 }
273
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800274 void assertFilterInputEventWasCalled(int type, nsecs_t eventTime, int32_t action,
275 int32_t displayId) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700276 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800277 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
278 ASSERT_EQ(mFilteredEvent->getType(), type);
279
280 if (type == AINPUT_EVENT_TYPE_KEY) {
281 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*mFilteredEvent);
282 EXPECT_EQ(keyEvent.getEventTime(), eventTime);
283 EXPECT_EQ(keyEvent.getAction(), action);
284 EXPECT_EQ(keyEvent.getDisplayId(), displayId);
285 } else if (type == AINPUT_EVENT_TYPE_MOTION) {
286 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*mFilteredEvent);
287 EXPECT_EQ(motionEvent.getEventTime(), eventTime);
288 EXPECT_EQ(motionEvent.getAction(), action);
289 EXPECT_EQ(motionEvent.getDisplayId(), displayId);
290 } else {
291 FAIL() << "Unknown type: " << type;
292 }
293
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800294 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800295 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800296};
297
Michael Wrightd02c5b62014-02-10 15:10:22 -0800298// --- InputDispatcherTest ---
299
300class InputDispatcherTest : public testing::Test {
301protected:
302 sp<FakeInputDispatcherPolicy> mFakePolicy;
303 sp<InputDispatcher> mDispatcher;
304
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700305 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800306 mFakePolicy = new FakeInputDispatcherPolicy();
307 mDispatcher = new InputDispatcher(mFakePolicy);
Arthur Hungb92218b2018-08-14 12:00:21 +0800308 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
309 //Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700310 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800311 }
312
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700313 virtual void TearDown() override {
314 ASSERT_EQ(OK, mDispatcher->stop());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800315 mFakePolicy.clear();
316 mDispatcher.clear();
317 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700318
319 /**
320 * Used for debugging when writing the test
321 */
322 void dumpDispatcherState() {
323 std::string dump;
324 mDispatcher->dump(dump);
325 std::stringstream ss(dump);
326 std::string to;
327
328 while (std::getline(ss, to, '\n')) {
329 ALOGE("%s", to.c_str());
330 }
331 }
Vishnu Nair958da932020-08-21 17:12:37 -0700332
333 void setFocusedWindow(const sp<InputWindowHandle>& window,
334 const sp<InputWindowHandle>& focusedWindow = nullptr) {
335 FocusRequest request;
336 request.token = window->getToken();
337 if (focusedWindow) {
338 request.focusedToken = focusedWindow->getToken();
339 }
340 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
341 request.displayId = window->getInfo()->displayId;
342 mDispatcher->setFocusedWindow(request);
343 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344};
345
346
347TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
348 KeyEvent event;
349
350 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800351 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
352 INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600353 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
354 ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800355 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700356 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800357 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800358 << "Should reject key events with undefined action.";
359
360 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800361 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
362 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600363 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800364 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700365 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800366 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800367 << "Should reject key events with ACTION_MULTIPLE.";
368}
369
370TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
371 MotionEvent event;
372 PointerProperties pointerProperties[MAX_POINTERS + 1];
373 PointerCoords pointerCoords[MAX_POINTERS + 1];
374 for (int i = 0; i <= MAX_POINTERS; i++) {
375 pointerProperties[i].clear();
376 pointerProperties[i].id = i;
377 pointerCoords[i].clear();
378 }
379
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800380 // Some constants commonly used below
381 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
382 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
383 constexpr int32_t metaState = AMETA_NONE;
384 constexpr MotionClassification classification = MotionClassification::NONE;
385
chaviw9eaa22c2020-07-01 16:21:27 -0700386 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800387 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800388 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
chaviw9eaa22c2020-07-01 16:21:27 -0700389 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification,
390 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600391 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700392 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800393 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700394 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800395 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800396 << "Should reject motion events with undefined action.";
397
398 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800399 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700400 AMOTION_EVENT_ACTION_POINTER_DOWN |
401 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700402 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
403 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
404 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
405 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800406 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700407 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800408 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800409 << "Should reject motion events with pointer down index too large.";
410
Garfield Tanfbe732e2020-01-24 11:26:14 -0800411 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700412 AMOTION_EVENT_ACTION_POINTER_DOWN |
413 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700414 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
415 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
416 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
417 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800418 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700419 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800420 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800421 << "Should reject motion events with pointer down index too small.";
422
423 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800424 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700425 AMOTION_EVENT_ACTION_POINTER_UP |
426 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700427 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
428 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
429 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
430 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800431 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700432 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800433 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800434 << "Should reject motion events with pointer up index too large.";
435
Garfield Tanfbe732e2020-01-24 11:26:14 -0800436 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700437 AMOTION_EVENT_ACTION_POINTER_UP |
438 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700439 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
440 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
441 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
442 pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800443 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700444 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800445 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800446 << "Should reject motion events with pointer up index too small.";
447
448 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800449 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
450 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700451 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
452 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700453 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800454 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700455 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800456 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800457 << "Should reject motion events with 0 pointers.";
458
Garfield Tanfbe732e2020-01-24 11:26:14 -0800459 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
460 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700461 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
462 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700463 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800464 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700465 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800466 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800467 << "Should reject motion events with more than MAX_POINTERS pointers.";
468
469 // Rejects motion events with invalid pointer ids.
470 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800471 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
472 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700473 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
474 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700475 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800476 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700477 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800478 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800479 << "Should reject motion events with pointer ids less than 0.";
480
481 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800482 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
483 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700484 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
485 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700486 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800487 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700488 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800489 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800490 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
491
492 // Rejects motion events with duplicate pointer ids.
493 pointerProperties[0].id = 1;
494 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800495 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
496 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700497 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
498 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700499 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800500 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700501 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800502 InputEventInjectionSync::NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800503 << "Should reject motion events with duplicate pointer ids.";
504}
505
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800506/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
507
508TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
509 constexpr nsecs_t eventTime = 20;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800510 NotifyConfigurationChangedArgs args(10 /*id*/, eventTime);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800511 mDispatcher->notifyConfigurationChanged(&args);
512 ASSERT_TRUE(mDispatcher->waitForIdle());
513
514 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
515}
516
517TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800518 NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/,
519 2 /*switchMask*/);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800520 mDispatcher->notifySwitch(&args);
521
522 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
523 args.policyFlags |= POLICY_FLAG_TRUSTED;
524 mFakePolicy->assertNotifySwitchWasCalled(args);
525}
526
Arthur Hungb92218b2018-08-14 12:00:21 +0800527// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700528static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700529static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s;
Arthur Hungb92218b2018-08-14 12:00:21 +0800530
531class FakeApplicationHandle : public InputApplicationHandle {
532public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700533 FakeApplicationHandle() {
534 mInfo.name = "Fake Application";
535 mInfo.token = new BBinder();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500536 mInfo.dispatchingTimeoutMillis =
537 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700538 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800539 virtual ~FakeApplicationHandle() {}
540
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700541 virtual bool updateInfo() override {
Arthur Hungb92218b2018-08-14 12:00:21 +0800542 return true;
543 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700544
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500545 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
546 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700547 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800548};
549
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800550class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800551public:
Garfield Tan15601662020-09-22 15:32:38 -0700552 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800553 : mName(name) {
Garfield Tan15601662020-09-22 15:32:38 -0700554 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800555 }
556
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800557 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700558 InputEvent* event;
559 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
560 if (!consumeSeq) {
561 return nullptr;
562 }
563 finishEvent(*consumeSeq);
564 return event;
565 }
566
567 /**
568 * Receive an event without acknowledging it.
569 * Return the sequence number that could later be used to send finished signal.
570 */
571 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800572 uint32_t consumeSeq;
573 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800574
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800575 std::chrono::time_point start = std::chrono::steady_clock::now();
576 status_t status = WOULD_BLOCK;
577 while (status == WOULD_BLOCK) {
chaviw81e2bb92019-12-18 15:03:51 -0800578 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800579 &event);
580 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
581 if (elapsed > 100ms) {
582 break;
583 }
584 }
585
586 if (status == WOULD_BLOCK) {
587 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700588 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800589 }
590
591 if (status != OK) {
592 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700593 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800594 }
595 if (event == nullptr) {
596 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700597 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800598 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700599 if (outEvent != nullptr) {
600 *outEvent = event;
601 }
602 return consumeSeq;
603 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800604
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700605 /**
606 * To be used together with "receiveEvent" to complete the consumption of an event.
607 */
608 void finishEvent(uint32_t consumeSeq) {
609 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
610 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800611 }
612
613 void consumeEvent(int32_t expectedEventType, int32_t expectedAction, int32_t expectedDisplayId,
614 int32_t expectedFlags) {
615 InputEvent* event = consume();
616
617 ASSERT_NE(nullptr, event) << mName.c_str()
618 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800619 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700620 << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType)
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800621 << " event, got " << inputEventTypeToString(event->getType()) << " event";
Arthur Hungb92218b2018-08-14 12:00:21 +0800622
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800623 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
Tiger Huang8664f8c2018-10-11 19:14:35 +0800624
Tiger Huang8664f8c2018-10-11 19:14:35 +0800625 switch (expectedEventType) {
626 case AINPUT_EVENT_TYPE_KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800627 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
628 EXPECT_EQ(expectedAction, keyEvent.getAction());
629 EXPECT_EQ(expectedFlags, keyEvent.getFlags());
Tiger Huang8664f8c2018-10-11 19:14:35 +0800630 break;
631 }
632 case AINPUT_EVENT_TYPE_MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800633 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
634 EXPECT_EQ(expectedAction, motionEvent.getAction());
635 EXPECT_EQ(expectedFlags, motionEvent.getFlags());
Tiger Huang8664f8c2018-10-11 19:14:35 +0800636 break;
637 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100638 case AINPUT_EVENT_TYPE_FOCUS: {
639 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
640 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800641 default: {
642 FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType;
643 }
644 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800645 }
646
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100647 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
648 InputEvent* event = consume();
649 ASSERT_NE(nullptr, event) << mName.c_str()
650 << ": consumer should have returned non-NULL event.";
651 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
652 << "Got " << inputEventTypeToString(event->getType())
653 << " event instead of FOCUS event";
654
655 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
656 << mName.c_str() << ": event displayId should always be NONE.";
657
658 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
659 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
660 EXPECT_EQ(inTouchMode, focusEvent->getInTouchMode());
661 }
662
chaviwd1c23182019-12-20 18:44:56 -0800663 void assertNoEvents() {
664 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700665 if (event == nullptr) {
666 return;
667 }
668 if (event->getType() == AINPUT_EVENT_TYPE_KEY) {
669 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
670 ADD_FAILURE() << "Received key event "
671 << KeyEvent::actionToString(keyEvent.getAction());
672 } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) {
673 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
674 ADD_FAILURE() << "Received motion event "
675 << MotionEvent::actionToString(motionEvent.getAction());
676 } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) {
677 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
678 ADD_FAILURE() << "Received focus event, hasFocus = "
679 << (focusEvent.getHasFocus() ? "true" : "false");
680 }
681 FAIL() << mName.c_str()
682 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -0800683 }
684
685 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
686
687protected:
688 std::unique_ptr<InputConsumer> mConsumer;
689 PreallocatedInputEventFactory mEventFactory;
690
691 std::string mName;
692};
693
694class FakeWindowHandle : public InputWindowHandle {
695public:
696 static const int32_t WIDTH = 600;
697 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -0800698
Chris Yea209fde2020-07-22 13:54:51 -0700699 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
chaviwd1c23182019-12-20 18:44:56 -0800700 const sp<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500701 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -0800702 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500703 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -0700704 base::Result<std::unique_ptr<InputChannel>> channel =
705 dispatcher->createInputChannel(name);
706 token = (*channel)->getConnectionToken();
707 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -0800708 }
709
710 inputApplicationHandle->updateInfo();
711 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
712
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500713 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700714 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -0800715 mInfo.name = name;
Michael Wright44753b12020-07-08 13:48:11 +0100716 mInfo.type = InputWindowInfo::Type::APPLICATION;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500717 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
chaviwd1c23182019-12-20 18:44:56 -0800718 mInfo.frameLeft = 0;
719 mInfo.frameTop = 0;
720 mInfo.frameRight = WIDTH;
721 mInfo.frameBottom = HEIGHT;
chaviw1ff3d1e2020-07-01 15:53:47 -0700722 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -0800723 mInfo.globalScaleFactor = 1.0;
724 mInfo.touchableRegion.clear();
725 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
726 mInfo.visible = true;
Vishnu Nair47074b82020-08-14 11:54:47 -0700727 mInfo.focusable = false;
chaviwd1c23182019-12-20 18:44:56 -0800728 mInfo.hasWallpaper = false;
729 mInfo.paused = false;
chaviwd1c23182019-12-20 18:44:56 -0800730 mInfo.ownerPid = INJECTOR_PID;
731 mInfo.ownerUid = INJECTOR_UID;
chaviwd1c23182019-12-20 18:44:56 -0800732 mInfo.displayId = displayId;
733 }
734
735 virtual bool updateInfo() { return true; }
736
Vishnu Nair47074b82020-08-14 11:54:47 -0700737 void setFocusable(bool focusable) { mInfo.focusable = focusable; }
chaviwd1c23182019-12-20 18:44:56 -0800738
Vishnu Nair958da932020-08-21 17:12:37 -0700739 void setVisible(bool visible) { mInfo.visible = visible; }
740
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700741 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500742 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700743 }
744
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700745 void setPaused(bool paused) { mInfo.paused = paused; }
746
chaviwd1c23182019-12-20 18:44:56 -0800747 void setFrame(const Rect& frame) {
748 mInfo.frameLeft = frame.left;
749 mInfo.frameTop = frame.top;
750 mInfo.frameRight = frame.right;
751 mInfo.frameBottom = frame.bottom;
chaviw1ff3d1e2020-07-01 15:53:47 -0700752 mInfo.transform.set(frame.left, frame.top);
chaviwd1c23182019-12-20 18:44:56 -0800753 mInfo.touchableRegion.clear();
754 mInfo.addTouchableRegion(frame);
755 }
756
Michael Wright44753b12020-07-08 13:48:11 +0100757 void setFlags(Flags<InputWindowInfo::Flag> flags) { mInfo.flags = flags; }
chaviwd1c23182019-12-20 18:44:56 -0800758
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500759 void setInputFeatures(InputWindowInfo::Feature features) { mInfo.inputFeatures = features; }
760
chaviw9eaa22c2020-07-01 16:21:27 -0700761 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
762 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
763 }
764
765 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -0700766
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800767 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
768 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
769 expectedFlags);
770 }
771
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700772 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
773 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
774 }
775
Svet Ganov5d3bc372020-01-26 23:11:07 -0800776 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
777 int32_t expectedFlags = 0) {
778 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId,
779 expectedFlags);
780 }
781
782 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
783 int32_t expectedFlags = 0) {
784 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId,
785 expectedFlags);
786 }
787
788 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
789 int32_t expectedFlags = 0) {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800790 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
791 expectedFlags);
792 }
793
Svet Ganov5d3bc372020-01-26 23:11:07 -0800794 void consumeMotionPointerDown(int32_t pointerIdx,
795 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, int32_t expectedFlags = 0) {
796 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN
797 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
798 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
799 }
800
801 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
802 int32_t expectedFlags = 0) {
803 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP
804 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
805 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
806 }
807
808 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
809 int32_t expectedFlags = 0) {
Michael Wright3a240c42019-12-10 20:53:41 +0000810 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
811 expectedFlags);
812 }
813
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100814 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
815 ASSERT_NE(mInputReceiver, nullptr)
816 << "Cannot consume events from a window with no receiver";
817 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
818 }
819
chaviwd1c23182019-12-20 18:44:56 -0800820 void consumeEvent(int32_t expectedEventType, int32_t expectedAction, int32_t expectedDisplayId,
821 int32_t expectedFlags) {
822 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
823 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
824 expectedFlags);
825 }
826
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700827 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700828 if (mInputReceiver == nullptr) {
829 ADD_FAILURE() << "Invalid receive event on window with no receiver";
830 return std::nullopt;
831 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700832 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700833 }
834
835 void finishEvent(uint32_t sequenceNum) {
836 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
837 mInputReceiver->finishEvent(sequenceNum);
838 }
839
chaviwaf87b3e2019-10-01 16:59:28 -0700840 InputEvent* consume() {
841 if (mInputReceiver == nullptr) {
842 return nullptr;
843 }
844 return mInputReceiver->consume();
845 }
846
Arthur Hungb92218b2018-08-14 12:00:21 +0800847 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500848 if (mInputReceiver == nullptr &&
849 mInfo.inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL)) {
850 return; // Can't receive events if the window does not have input channel
851 }
852 ASSERT_NE(nullptr, mInputReceiver)
853 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -0800854 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +0800855 }
856
chaviwaf87b3e2019-10-01 16:59:28 -0700857 sp<IBinder> getToken() { return mInfo.token; }
858
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100859 const std::string& getName() { return mName; }
860
chaviwd1c23182019-12-20 18:44:56 -0800861private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100862 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -0800863 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700864 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800865};
866
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700867std::atomic<int32_t> FakeWindowHandle::sId{1};
868
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800869static InputEventInjectionResult injectKey(
870 const sp<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
871 int32_t displayId = ADISPLAY_ID_NONE,
872 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
873 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800874 KeyEvent event;
875 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
876
877 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800878 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700879 INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE,
880 repeatCount, currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +0800881
882 // Inject event until dispatch out.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700883 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, syncMode,
884 injectionTimeout,
885 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
Arthur Hungb92218b2018-08-14 12:00:21 +0800886}
887
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800888static InputEventInjectionResult injectKeyDown(const sp<InputDispatcher>& dispatcher,
889 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700890 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId);
891}
892
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800893static InputEventInjectionResult injectKeyUp(const sp<InputDispatcher>& dispatcher,
894 int32_t displayId = ADISPLAY_ID_NONE) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700895 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId);
896}
897
Garfield Tandf26e862020-07-01 20:18:19 -0700898class PointerBuilder {
899public:
900 PointerBuilder(int32_t id, int32_t toolType) {
901 mProperties.clear();
902 mProperties.id = id;
903 mProperties.toolType = toolType;
904 mCoords.clear();
905 }
906
907 PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); }
908
909 PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); }
910
911 PointerBuilder& axis(int32_t axis, float value) {
912 mCoords.setAxisValue(axis, value);
913 return *this;
914 }
915
916 PointerProperties buildProperties() const { return mProperties; }
917
918 PointerCoords buildCoords() const { return mCoords; }
919
920private:
921 PointerProperties mProperties;
922 PointerCoords mCoords;
923};
924
925class MotionEventBuilder {
926public:
927 MotionEventBuilder(int32_t action, int32_t source) {
928 mAction = action;
929 mSource = source;
930 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
931 }
932
933 MotionEventBuilder& eventTime(nsecs_t eventTime) {
934 mEventTime = eventTime;
935 return *this;
936 }
937
938 MotionEventBuilder& displayId(int32_t displayId) {
939 mDisplayId = displayId;
940 return *this;
941 }
942
943 MotionEventBuilder& actionButton(int32_t actionButton) {
944 mActionButton = actionButton;
945 return *this;
946 }
947
948 MotionEventBuilder& buttonState(int32_t actionButton) {
949 mActionButton = actionButton;
950 return *this;
951 }
952
953 MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) {
954 mRawXCursorPosition = rawXCursorPosition;
955 return *this;
956 }
957
958 MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) {
959 mRawYCursorPosition = rawYCursorPosition;
960 return *this;
961 }
962
963 MotionEventBuilder& pointer(PointerBuilder pointer) {
964 mPointers.push_back(pointer);
965 return *this;
966 }
967
968 MotionEvent build() {
969 std::vector<PointerProperties> pointerProperties;
970 std::vector<PointerCoords> pointerCoords;
971 for (const PointerBuilder& pointer : mPointers) {
972 pointerProperties.push_back(pointer.buildProperties());
973 pointerCoords.push_back(pointer.buildCoords());
974 }
975
976 // Set mouse cursor position for the most common cases to avoid boilerplate.
977 if (mSource == AINPUT_SOURCE_MOUSE &&
978 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) &&
979 mPointers.size() == 1) {
980 mRawXCursorPosition = pointerCoords[0].getX();
981 mRawYCursorPosition = pointerCoords[0].getY();
982 }
983
984 MotionEvent event;
chaviw9eaa22c2020-07-01 16:21:27 -0700985 ui::Transform identityTransform;
Garfield Tandf26e862020-07-01 20:18:19 -0700986 event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC,
987 mAction, mActionButton, /* flags */ 0, /* edgeFlags */ 0, AMETA_NONE,
chaviw9eaa22c2020-07-01 16:21:27 -0700988 mButtonState, MotionClassification::NONE, identityTransform,
989 /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition,
990 mRawYCursorPosition, mEventTime, mEventTime, mPointers.size(),
991 pointerProperties.data(), pointerCoords.data());
Garfield Tandf26e862020-07-01 20:18:19 -0700992
993 return event;
994 }
995
996private:
997 int32_t mAction;
998 int32_t mSource;
999 nsecs_t mEventTime;
1000 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1001 int32_t mActionButton{0};
1002 int32_t mButtonState{0};
1003 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1004 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1005
1006 std::vector<PointerBuilder> mPointers;
1007};
1008
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001009static InputEventInjectionResult injectMotionEvent(
Garfield Tandf26e862020-07-01 20:18:19 -07001010 const sp<InputDispatcher>& dispatcher, const MotionEvent& event,
1011 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001012 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT) {
Garfield Tandf26e862020-07-01 20:18:19 -07001013 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, injectionMode,
1014 injectionTimeout,
1015 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
1016}
1017
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001018static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001019 const sp<InputDispatcher>& dispatcher, int32_t action, int32_t source, int32_t displayId,
1020 const PointF& position,
1021 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001022 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1023 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001024 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001025 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC)) {
Garfield Tandf26e862020-07-01 20:18:19 -07001026 MotionEvent event = MotionEventBuilder(action, source)
1027 .displayId(displayId)
1028 .eventTime(eventTime)
1029 .rawXCursorPosition(cursorPosition.x)
1030 .rawYCursorPosition(cursorPosition.y)
1031 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1032 .x(position.x)
1033 .y(position.y))
1034 .build();
Arthur Hungb92218b2018-08-14 12:00:21 +08001035
1036 // Inject event until dispatch out.
Garfield Tandf26e862020-07-01 20:18:19 -07001037 return injectMotionEvent(dispatcher, event);
Arthur Hungb92218b2018-08-14 12:00:21 +08001038}
1039
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001040static InputEventInjectionResult injectMotionDown(const sp<InputDispatcher>& dispatcher,
1041 int32_t source, int32_t displayId,
1042 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001043 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001044}
1045
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001046static InputEventInjectionResult injectMotionUp(const sp<InputDispatcher>& dispatcher,
1047 int32_t source, int32_t displayId,
1048 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001049 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001050}
1051
Jackal Guof9696682018-10-05 12:23:23 +08001052static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1053 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1054 // Define a valid key event.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001055 NotifyKeyArgs args(/* id */ 0, currentTime, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
1056 POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, KEY_A,
1057 AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001058
1059 return args;
1060}
1061
chaviwd1c23182019-12-20 18:44:56 -08001062static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId,
1063 const std::vector<PointF>& points) {
1064 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001065 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1066 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1067 }
1068
chaviwd1c23182019-12-20 18:44:56 -08001069 PointerProperties pointerProperties[pointerCount];
1070 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001071
chaviwd1c23182019-12-20 18:44:56 -08001072 for (size_t i = 0; i < pointerCount; i++) {
1073 pointerProperties[i].clear();
1074 pointerProperties[i].id = i;
1075 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001076
chaviwd1c23182019-12-20 18:44:56 -08001077 pointerCoords[i].clear();
1078 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1079 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1080 }
Jackal Guof9696682018-10-05 12:23:23 +08001081
1082 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1083 // Define a valid motion event.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001084 NotifyMotionArgs args(/* id */ 0, currentTime, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001085 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1086 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001087 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1088 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001089 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1090 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001091
1092 return args;
1093}
1094
chaviwd1c23182019-12-20 18:44:56 -08001095static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1096 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1097}
1098
Arthur Hungb92218b2018-08-14 12:00:21 +08001099TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001100 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001101 sp<FakeWindowHandle> window = new FakeWindowHandle(application, mDispatcher, "Fake Window",
1102 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001103
Arthur Hung72d8dc32020-03-28 00:48:39 +00001104 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001105 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1106 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1107 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001108
1109 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001110 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001111}
1112
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001113/**
1114 * Calling setInputWindows once with FLAG_NOT_TOUCH_MODAL should not cause any issues.
1115 * To ensure that window receives only events that were directly inside of it, add
1116 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1117 * when finding touched windows.
1118 * This test serves as a sanity check for the next test, where setInputWindows is
1119 * called twice.
1120 */
1121TEST_F(InputDispatcherTest, SetInputWindowOnce_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001122 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001123 sp<FakeWindowHandle> window =
1124 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1125 window->setFrame(Rect(0, 0, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01001126 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001127
1128 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001129 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001130 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1131 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001132 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001133
1134 // Window should receive motion event.
1135 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1136}
1137
1138/**
1139 * Calling setInputWindows twice, with the same info, should not cause any issues.
1140 * To ensure that window receives only events that were directly inside of it, add
1141 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1142 * when finding touched windows.
1143 */
1144TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001145 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001146 sp<FakeWindowHandle> window =
1147 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1148 window->setFrame(Rect(0, 0, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01001149 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001150
1151 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1152 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001153 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001154 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1155 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001156 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001157
1158 // Window should receive motion event.
1159 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1160}
1161
Arthur Hungb92218b2018-08-14 12:00:21 +08001162// The foreground window should receive the first touch down event.
1163TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001164 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001165 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
1166 ADISPLAY_ID_DEFAULT);
1167 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
1168 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001169
Arthur Hung72d8dc32020-03-28 00:48:39 +00001170 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001171 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1172 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1173 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001174
1175 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001176 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001177 windowSecond->assertNoEvents();
1178}
1179
Garfield Tandf26e862020-07-01 20:18:19 -07001180TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07001181 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07001182 sp<FakeWindowHandle> windowLeft =
1183 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1184 windowLeft->setFrame(Rect(0, 0, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001185 windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001186 sp<FakeWindowHandle> windowRight =
1187 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1188 windowRight->setFrame(Rect(600, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001189 windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001190
1191 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1192
1193 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
1194
1195 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001196 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001197 injectMotionEvent(mDispatcher,
1198 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1199 AINPUT_SOURCE_MOUSE)
1200 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1201 .x(900)
1202 .y(400))
1203 .build()));
1204 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1205 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1206 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1207 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1208
1209 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001210 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001211 injectMotionEvent(mDispatcher,
1212 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1213 AINPUT_SOURCE_MOUSE)
1214 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1215 .x(300)
1216 .y(400))
1217 .build()));
1218 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1219 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1220 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1221 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1222 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1223 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1224
1225 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001226 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001227 injectMotionEvent(mDispatcher,
1228 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1229 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1230 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1231 .x(300)
1232 .y(400))
1233 .build()));
1234 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1235
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001236 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001237 injectMotionEvent(mDispatcher,
1238 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
1239 AINPUT_SOURCE_MOUSE)
1240 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1241 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1242 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1243 .x(300)
1244 .y(400))
1245 .build()));
1246 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1247 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1248
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001249 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001250 injectMotionEvent(mDispatcher,
1251 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1252 AINPUT_SOURCE_MOUSE)
1253 .buttonState(0)
1254 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1255 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1256 .x(300)
1257 .y(400))
1258 .build()));
1259 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1260 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1261
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001262 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001263 injectMotionEvent(mDispatcher,
1264 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
1265 .buttonState(0)
1266 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1267 .x(300)
1268 .y(400))
1269 .build()));
1270 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1271
1272 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001273 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001274 injectMotionEvent(mDispatcher,
1275 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1276 AINPUT_SOURCE_MOUSE)
1277 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1278 .x(900)
1279 .y(400))
1280 .build()));
1281 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1282 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1283 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1284 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1285 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1286 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1287}
1288
1289// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
1290// directly in this test.
1291TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07001292 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07001293 sp<FakeWindowHandle> window =
1294 new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1295 window->setFrame(Rect(0, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001296 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001297
1298 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1299
1300 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1301
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001302 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001303 injectMotionEvent(mDispatcher,
1304 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
1305 AINPUT_SOURCE_MOUSE)
1306 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1307 .x(300)
1308 .y(400))
1309 .build()));
1310 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1311 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1312
1313 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001314 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001315 injectMotionEvent(mDispatcher,
1316 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1317 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1318 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1319 .x(300)
1320 .y(400))
1321 .build()));
1322 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1323
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001324 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001325 injectMotionEvent(mDispatcher,
1326 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
1327 AINPUT_SOURCE_MOUSE)
1328 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1329 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1330 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1331 .x(300)
1332 .y(400))
1333 .build()));
1334 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1335 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1336
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001337 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001338 injectMotionEvent(mDispatcher,
1339 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1340 AINPUT_SOURCE_MOUSE)
1341 .buttonState(0)
1342 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1343 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1344 .x(300)
1345 .y(400))
1346 .build()));
1347 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1348 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1349
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001350 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001351 injectMotionEvent(mDispatcher,
1352 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
1353 .buttonState(0)
1354 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1355 .x(300)
1356 .y(400))
1357 .build()));
1358 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1359
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001360 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07001361 injectMotionEvent(mDispatcher,
1362 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
1363 AINPUT_SOURCE_MOUSE)
1364 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1365 .x(300)
1366 .y(400))
1367 .build()));
1368 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1369 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1370}
1371
Garfield Tan00f511d2019-06-12 16:55:40 -07001372TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07001373 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07001374
1375 sp<FakeWindowHandle> windowLeft =
1376 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1377 windowLeft->setFrame(Rect(0, 0, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001378 windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07001379 sp<FakeWindowHandle> windowRight =
1380 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1381 windowRight->setFrame(Rect(600, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001382 windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07001383
1384 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1385
Arthur Hung72d8dc32020-03-28 00:48:39 +00001386 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07001387
1388 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
1389 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001390 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07001391 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001392 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001393 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07001394 windowRight->assertNoEvents();
1395}
1396
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001397TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001398 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001399 sp<FakeWindowHandle> window =
1400 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07001401 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001402
Arthur Hung72d8dc32020-03-28 00:48:39 +00001403 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001404 setFocusedWindow(window);
1405
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001406 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001407
1408 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1409 mDispatcher->notifyKey(&keyArgs);
1410
1411 // Window should receive key down event.
1412 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1413
1414 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
1415 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001416 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001417 mDispatcher->notifyDeviceReset(&args);
1418 window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
1419 AKEY_EVENT_FLAG_CANCELED);
1420}
1421
1422TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001423 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001424 sp<FakeWindowHandle> window =
1425 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1426
Arthur Hung72d8dc32020-03-28 00:48:39 +00001427 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001428
1429 NotifyMotionArgs motionArgs =
1430 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1431 ADISPLAY_ID_DEFAULT);
1432 mDispatcher->notifyMotion(&motionArgs);
1433
1434 // Window should receive motion down event.
1435 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1436
1437 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
1438 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001439 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001440 mDispatcher->notifyDeviceReset(&args);
1441 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT,
1442 0 /*expectedFlags*/);
1443}
1444
Svet Ganov5d3bc372020-01-26 23:11:07 -08001445TEST_F(InputDispatcherTest, TransferTouchFocus_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07001446 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001447
1448 // Create a couple of windows
1449 sp<FakeWindowHandle> firstWindow = new FakeWindowHandle(application, mDispatcher,
1450 "First Window", ADISPLAY_ID_DEFAULT);
1451 sp<FakeWindowHandle> secondWindow = new FakeWindowHandle(application, mDispatcher,
1452 "Second Window", ADISPLAY_ID_DEFAULT);
1453
1454 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001455 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001456
1457 // Send down to the first window
1458 NotifyMotionArgs downMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
1459 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT);
1460 mDispatcher->notifyMotion(&downMotionArgs);
1461 // Only the first window should get the down event
1462 firstWindow->consumeMotionDown();
1463 secondWindow->assertNoEvents();
1464
1465 // Transfer touch focus to the second window
1466 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1467 // The first window gets cancel and the second gets down
1468 firstWindow->consumeMotionCancel();
1469 secondWindow->consumeMotionDown();
1470
1471 // Send up event to the second window
1472 NotifyMotionArgs upMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP,
1473 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT);
1474 mDispatcher->notifyMotion(&upMotionArgs);
1475 // The first window gets no events and the second gets up
1476 firstWindow->assertNoEvents();
1477 secondWindow->consumeMotionUp();
1478}
1479
1480TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointerNoSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001481 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001482
1483 PointF touchPoint = {10, 10};
1484
1485 // Create a couple of windows
1486 sp<FakeWindowHandle> firstWindow = new FakeWindowHandle(application, mDispatcher,
1487 "First Window", ADISPLAY_ID_DEFAULT);
1488 sp<FakeWindowHandle> secondWindow = new FakeWindowHandle(application, mDispatcher,
1489 "Second Window", ADISPLAY_ID_DEFAULT);
1490
1491 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001492 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001493
1494 // Send down to the first window
1495 NotifyMotionArgs downMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
1496 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {touchPoint});
1497 mDispatcher->notifyMotion(&downMotionArgs);
1498 // Only the first window should get the down event
1499 firstWindow->consumeMotionDown();
1500 secondWindow->assertNoEvents();
1501
1502 // Send pointer down to the first window
1503 NotifyMotionArgs pointerDownMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN
1504 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1505 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint});
1506 mDispatcher->notifyMotion(&pointerDownMotionArgs);
1507 // Only the first window should get the pointer down event
1508 firstWindow->consumeMotionPointerDown(1);
1509 secondWindow->assertNoEvents();
1510
1511 // Transfer touch focus to the second window
1512 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1513 // The first window gets cancel and the second gets down and pointer down
1514 firstWindow->consumeMotionCancel();
1515 secondWindow->consumeMotionDown();
1516 secondWindow->consumeMotionPointerDown(1);
1517
1518 // Send pointer up to the second window
1519 NotifyMotionArgs pointerUpMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP
1520 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1521 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint});
1522 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1523 // The first window gets nothing and the second gets pointer up
1524 firstWindow->assertNoEvents();
1525 secondWindow->consumeMotionPointerUp(1);
1526
1527 // Send up event to the second window
1528 NotifyMotionArgs upMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP,
1529 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT);
1530 mDispatcher->notifyMotion(&upMotionArgs);
1531 // The first window gets nothing and the second gets up
1532 firstWindow->assertNoEvents();
1533 secondWindow->consumeMotionUp();
1534}
1535
1536TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001537 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001538
1539 // Create a non touch modal window that supports split touch
1540 sp<FakeWindowHandle> firstWindow = new FakeWindowHandle(application, mDispatcher,
1541 "First Window", ADISPLAY_ID_DEFAULT);
1542 firstWindow->setFrame(Rect(0, 0, 600, 400));
Michael Wright44753b12020-07-08 13:48:11 +01001543 firstWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1544 InputWindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001545
1546 // Create a non touch modal window that supports split touch
1547 sp<FakeWindowHandle> secondWindow = new FakeWindowHandle(application, mDispatcher,
1548 "Second Window", ADISPLAY_ID_DEFAULT);
1549 secondWindow->setFrame(Rect(0, 400, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001550 secondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1551 InputWindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001552
1553 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001554 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001555
1556 PointF pointInFirst = {300, 200};
1557 PointF pointInSecond = {300, 600};
1558
1559 // Send down to the first window
1560 NotifyMotionArgs firstDownMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
1561 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {pointInFirst});
1562 mDispatcher->notifyMotion(&firstDownMotionArgs);
1563 // Only the first window should get the down event
1564 firstWindow->consumeMotionDown();
1565 secondWindow->assertNoEvents();
1566
1567 // Send down to the second window
1568 NotifyMotionArgs secondDownMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN
1569 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1570 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {pointInFirst, pointInSecond});
1571 mDispatcher->notifyMotion(&secondDownMotionArgs);
1572 // The first window gets a move and the second a down
1573 firstWindow->consumeMotionMove();
1574 secondWindow->consumeMotionDown();
1575
1576 // Transfer touch focus to the second window
1577 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1578 // The first window gets cancel and the new gets pointer down (it already saw down)
1579 firstWindow->consumeMotionCancel();
1580 secondWindow->consumeMotionPointerDown(1);
1581
1582 // Send pointer up to the second window
1583 NotifyMotionArgs pointerUpMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP
1584 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1585 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {pointInFirst, pointInSecond});
1586 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1587 // The first window gets nothing and the second gets pointer up
1588 firstWindow->assertNoEvents();
1589 secondWindow->consumeMotionPointerUp(1);
1590
1591 // Send up event to the second window
1592 NotifyMotionArgs upMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP,
1593 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT);
1594 mDispatcher->notifyMotion(&upMotionArgs);
1595 // The first window gets nothing and the second gets up
1596 firstWindow->assertNoEvents();
1597 secondWindow->consumeMotionUp();
1598}
1599
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001600TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07001601 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001602 sp<FakeWindowHandle> window =
1603 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1604
Vishnu Nair47074b82020-08-14 11:54:47 -07001605 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001606 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001607 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001608
1609 window->consumeFocusEvent(true);
1610
1611 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1612 mDispatcher->notifyKey(&keyArgs);
1613
1614 // Window should receive key down event.
1615 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1616}
1617
1618TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07001619 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001620 sp<FakeWindowHandle> window =
1621 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1622
Arthur Hung72d8dc32020-03-28 00:48:39 +00001623 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001624
1625 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1626 mDispatcher->notifyKey(&keyArgs);
1627 mDispatcher->waitForIdle();
1628
1629 window->assertNoEvents();
1630}
1631
1632// If a window is touchable, but does not have focus, it should receive motion events, but not keys
1633TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07001634 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001635 sp<FakeWindowHandle> window =
1636 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1637
Arthur Hung72d8dc32020-03-28 00:48:39 +00001638 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001639
1640 // Send key
1641 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1642 mDispatcher->notifyKey(&keyArgs);
1643 // Send motion
1644 NotifyMotionArgs motionArgs =
1645 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1646 ADISPLAY_ID_DEFAULT);
1647 mDispatcher->notifyMotion(&motionArgs);
1648
1649 // Window should receive only the motion event
1650 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1651 window->assertNoEvents(); // Key event or focus event will not be received
1652}
1653
chaviwd1c23182019-12-20 18:44:56 -08001654class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00001655public:
1656 FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name,
chaviwd1c23182019-12-20 18:44:56 -08001657 int32_t displayId, bool isGestureMonitor = false) {
Garfield Tan15601662020-09-22 15:32:38 -07001658 base::Result<std::unique_ptr<InputChannel>> channel =
1659 dispatcher->createInputMonitor(displayId, isGestureMonitor, name);
1660 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00001661 }
1662
chaviwd1c23182019-12-20 18:44:56 -08001663 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
1664
1665 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1666 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN,
1667 expectedDisplayId, expectedFlags);
1668 }
1669
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001670 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
1671
1672 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
1673
chaviwd1c23182019-12-20 18:44:56 -08001674 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1675 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
1676 expectedDisplayId, expectedFlags);
1677 }
1678
1679 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1680 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP,
1681 expectedDisplayId, expectedFlags);
1682 }
1683
1684 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
1685
1686private:
1687 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00001688};
1689
1690// Tests for gesture monitors
1691TEST_F(InputDispatcherTest, GestureMonitor_ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07001692 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00001693 sp<FakeWindowHandle> window =
1694 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001695 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00001696
chaviwd1c23182019-12-20 18:44:56 -08001697 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
1698 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00001699
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001700 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00001701 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001702 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00001703 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001704 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00001705}
1706
1707TEST_F(InputDispatcherTest, GestureMonitor_DoesNotReceiveKeyEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07001708 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00001709 sp<FakeWindowHandle> window =
1710 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1711
1712 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07001713 window->setFocusable(true);
Michael Wright3a240c42019-12-10 20:53:41 +00001714
Arthur Hung72d8dc32020-03-28 00:48:39 +00001715 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001716 setFocusedWindow(window);
1717
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001718 window->consumeFocusEvent(true);
Michael Wright3a240c42019-12-10 20:53:41 +00001719
chaviwd1c23182019-12-20 18:44:56 -08001720 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
1721 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00001722
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001723 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
1724 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00001725 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001726 monitor.assertNoEvents();
Michael Wright3a240c42019-12-10 20:53:41 +00001727}
1728
1729TEST_F(InputDispatcherTest, GestureMonitor_CanPilferAfterWindowIsRemovedMidStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001730 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00001731 sp<FakeWindowHandle> window =
1732 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001733 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00001734
chaviwd1c23182019-12-20 18:44:56 -08001735 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
1736 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00001737
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001738 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00001739 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001740 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00001741 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001742 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00001743
1744 window->releaseChannel();
1745
chaviwd1c23182019-12-20 18:44:56 -08001746 mDispatcher->pilferPointers(monitor.getToken());
Michael Wright3a240c42019-12-10 20:53:41 +00001747
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001748 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00001749 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001750 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08001751 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00001752}
1753
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001754TEST_F(InputDispatcherTest, UnresponsiveGestureMonitor_GetsAnr) {
1755 FakeMonitorReceiver monitor =
1756 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
1757 true /*isGestureMonitor*/);
1758
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001759 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001760 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
1761 std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
1762 ASSERT_TRUE(consumeSeq);
1763
1764 mFakePolicy->assertNotifyAnrWasCalled(DISPATCHING_TIMEOUT, nullptr, monitor.getToken());
1765 monitor.finishEvent(*consumeSeq);
1766 ASSERT_TRUE(mDispatcher->waitForIdle());
1767}
1768
chaviw81e2bb92019-12-18 15:03:51 -08001769TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07001770 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
chaviw81e2bb92019-12-18 15:03:51 -08001771 sp<FakeWindowHandle> window =
1772 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1773
Arthur Hung72d8dc32020-03-28 00:48:39 +00001774 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08001775
1776 NotifyMotionArgs motionArgs =
1777 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1778 ADISPLAY_ID_DEFAULT);
1779
1780 mDispatcher->notifyMotion(&motionArgs);
1781 // Window should receive motion down event.
1782 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1783
1784 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001785 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08001786 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1787 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
1788 motionArgs.pointerCoords[0].getX() - 10);
1789
1790 mDispatcher->notifyMotion(&motionArgs);
1791 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
1792 0 /*expectedFlags*/);
1793}
1794
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001795/**
1796 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
1797 * the device default right away. In the test scenario, we check both the default value,
1798 * and the action of enabling / disabling.
1799 */
1800TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07001801 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001802 sp<FakeWindowHandle> window =
1803 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
1804
1805 // Set focused application.
1806 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07001807 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001808
1809 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00001810 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001811 setFocusedWindow(window);
1812
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001813 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
1814
1815 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07001816 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001817 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001818 window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/);
1819
1820 SCOPED_TRACE("Disable touch mode");
1821 mDispatcher->setInTouchMode(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07001822 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001823 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001824 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001825 window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/);
1826
1827 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07001828 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001829 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001830 window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/);
1831
1832 SCOPED_TRACE("Enable touch mode again");
1833 mDispatcher->setInTouchMode(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07001834 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001835 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001836 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001837 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
1838
1839 window->assertNoEvents();
1840}
1841
Gang Wange9087892020-01-07 12:17:14 -05001842TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07001843 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Gang Wange9087892020-01-07 12:17:14 -05001844 sp<FakeWindowHandle> window =
1845 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
1846
1847 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07001848 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05001849
Arthur Hung72d8dc32020-03-28 00:48:39 +00001850 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001851 setFocusedWindow(window);
1852
Gang Wange9087892020-01-07 12:17:14 -05001853 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
1854
1855 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
1856 mDispatcher->notifyKey(&keyArgs);
1857
1858 InputEvent* event = window->consume();
1859 ASSERT_NE(event, nullptr);
1860
1861 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
1862 ASSERT_NE(verified, nullptr);
1863 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
1864
1865 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
1866 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
1867 ASSERT_EQ(keyArgs.source, verified->source);
1868 ASSERT_EQ(keyArgs.displayId, verified->displayId);
1869
1870 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
1871
1872 ASSERT_EQ(keyArgs.action, verifiedKey.action);
1873 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05001874 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
1875 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
1876 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
1877 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
1878 ASSERT_EQ(0, verifiedKey.repeatCount);
1879}
1880
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08001881TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07001882 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08001883 sp<FakeWindowHandle> window =
1884 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
1885
1886 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1887
Arthur Hung72d8dc32020-03-28 00:48:39 +00001888 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08001889
1890 NotifyMotionArgs motionArgs =
1891 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1892 ADISPLAY_ID_DEFAULT);
1893 mDispatcher->notifyMotion(&motionArgs);
1894
1895 InputEvent* event = window->consume();
1896 ASSERT_NE(event, nullptr);
1897
1898 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
1899 ASSERT_NE(verified, nullptr);
1900 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
1901
1902 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
1903 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
1904 EXPECT_EQ(motionArgs.source, verified->source);
1905 EXPECT_EQ(motionArgs.displayId, verified->displayId);
1906
1907 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
1908
1909 EXPECT_EQ(motionArgs.pointerCoords[0].getX(), verifiedMotion.rawX);
1910 EXPECT_EQ(motionArgs.pointerCoords[0].getY(), verifiedMotion.rawY);
1911 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
1912 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
1913 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
1914 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
1915 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
1916}
1917
chaviw09c8d2d2020-08-24 15:48:26 -07001918/**
1919 * Ensure that separate calls to sign the same data are generating the same key.
1920 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
1921 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
1922 * tests.
1923 */
1924TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
1925 KeyEvent event = getTestKeyEvent();
1926 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
1927
1928 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
1929 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
1930 ASSERT_EQ(hmac1, hmac2);
1931}
1932
1933/**
1934 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
1935 */
1936TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
1937 KeyEvent event = getTestKeyEvent();
1938 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
1939 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
1940
1941 verifiedEvent.deviceId += 1;
1942 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
1943
1944 verifiedEvent.source += 1;
1945 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
1946
1947 verifiedEvent.eventTimeNanos += 1;
1948 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
1949
1950 verifiedEvent.displayId += 1;
1951 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
1952
1953 verifiedEvent.action += 1;
1954 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
1955
1956 verifiedEvent.downTimeNanos += 1;
1957 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
1958
1959 verifiedEvent.flags += 1;
1960 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
1961
1962 verifiedEvent.keyCode += 1;
1963 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
1964
1965 verifiedEvent.scanCode += 1;
1966 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
1967
1968 verifiedEvent.metaState += 1;
1969 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
1970
1971 verifiedEvent.repeatCount += 1;
1972 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
1973}
1974
Vishnu Nair958da932020-08-21 17:12:37 -07001975TEST_F(InputDispatcherTest, SetFocusedWindow) {
1976 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1977 sp<FakeWindowHandle> windowTop =
1978 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
1979 sp<FakeWindowHandle> windowSecond =
1980 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
1981 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1982
1983 // Top window is also focusable but is not granted focus.
1984 windowTop->setFocusable(true);
1985 windowSecond->setFocusable(true);
1986 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
1987 setFocusedWindow(windowSecond);
1988
1989 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001990 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
1991 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07001992
1993 // Focused window should receive event.
1994 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
1995 windowTop->assertNoEvents();
1996}
1997
1998TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
1999 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2000 sp<FakeWindowHandle> window =
2001 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2002 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2003
2004 window->setFocusable(true);
2005 // Release channel for window is no longer valid.
2006 window->releaseChannel();
2007 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2008 setFocusedWindow(window);
2009
2010 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002011 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2012 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002013
2014 // window channel is invalid, so it should not receive any input event.
2015 window->assertNoEvents();
2016}
2017
2018TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
2019 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2020 sp<FakeWindowHandle> window =
2021 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2022 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2023
2024 // Window is not focusable.
2025 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2026 setFocusedWindow(window);
2027
2028 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002029 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2030 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002031
2032 // window is invalid, so it should not receive any input event.
2033 window->assertNoEvents();
2034}
2035
2036TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
2037 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2038 sp<FakeWindowHandle> windowTop =
2039 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2040 sp<FakeWindowHandle> windowSecond =
2041 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2042 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2043
2044 windowTop->setFocusable(true);
2045 windowSecond->setFocusable(true);
2046 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2047 setFocusedWindow(windowTop);
2048 windowTop->consumeFocusEvent(true);
2049
2050 setFocusedWindow(windowSecond, windowTop);
2051 windowSecond->consumeFocusEvent(true);
2052 windowTop->consumeFocusEvent(false);
2053
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002054 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2055 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002056
2057 // Focused window should receive event.
2058 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
2059}
2060
2061TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) {
2062 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2063 sp<FakeWindowHandle> windowTop =
2064 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2065 sp<FakeWindowHandle> windowSecond =
2066 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2067 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2068
2069 windowTop->setFocusable(true);
2070 windowSecond->setFocusable(true);
2071 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2072 setFocusedWindow(windowSecond, windowTop);
2073
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002074 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2075 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07002076
2077 // Event should be dropped.
2078 windowTop->assertNoEvents();
2079 windowSecond->assertNoEvents();
2080}
2081
2082TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
2083 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2084 sp<FakeWindowHandle> window =
2085 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2086 sp<FakeWindowHandle> previousFocusedWindow =
2087 new FakeWindowHandle(application, mDispatcher, "previousFocusedWindow",
2088 ADISPLAY_ID_DEFAULT);
2089 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2090
2091 window->setFocusable(true);
2092 previousFocusedWindow->setFocusable(true);
2093 window->setVisible(false);
2094 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
2095 setFocusedWindow(previousFocusedWindow);
2096 previousFocusedWindow->consumeFocusEvent(true);
2097
2098 // Requesting focus on invisible window takes focus from currently focused window.
2099 setFocusedWindow(window);
2100 previousFocusedWindow->consumeFocusEvent(false);
2101
2102 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002103 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07002104 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002105 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07002106
2107 // Window does not get focus event or key down.
2108 window->assertNoEvents();
2109
2110 // Window becomes visible.
2111 window->setVisible(true);
2112 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2113
2114 // Window receives focus event.
2115 window->consumeFocusEvent(true);
2116 // Focused window receives key down.
2117 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2118}
2119
Garfield Tan1c7bc862020-01-28 13:24:04 -08002120class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
2121protected:
2122 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
2123 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
2124
Chris Yea209fde2020-07-22 13:54:51 -07002125 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002126 sp<FakeWindowHandle> mWindow;
2127
2128 virtual void SetUp() override {
2129 mFakePolicy = new FakeInputDispatcherPolicy();
2130 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
2131 mDispatcher = new InputDispatcher(mFakePolicy);
2132 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
2133 ASSERT_EQ(OK, mDispatcher->start());
2134
2135 setUpWindow();
2136 }
2137
2138 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07002139 mApp = std::make_shared<FakeApplicationHandle>();
Garfield Tan1c7bc862020-01-28 13:24:04 -08002140 mWindow = new FakeWindowHandle(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2141
Vishnu Nair47074b82020-08-14 11:54:47 -07002142 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002143 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002144 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002145 mWindow->consumeFocusEvent(true);
2146 }
2147
Chris Ye2ad95392020-09-01 13:44:44 -07002148 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08002149 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07002150 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002151 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
2152 mDispatcher->notifyKey(&keyArgs);
2153
2154 // Window should receive key down event.
2155 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2156 }
2157
2158 void expectKeyRepeatOnce(int32_t repeatCount) {
2159 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
2160 InputEvent* repeatEvent = mWindow->consume();
2161 ASSERT_NE(nullptr, repeatEvent);
2162
2163 uint32_t eventType = repeatEvent->getType();
2164 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType);
2165
2166 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
2167 uint32_t eventAction = repeatKeyEvent->getAction();
2168 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
2169 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
2170 }
2171
Chris Ye2ad95392020-09-01 13:44:44 -07002172 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08002173 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07002174 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002175 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
2176 mDispatcher->notifyKey(&keyArgs);
2177
2178 // Window should receive key down event.
2179 mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
2180 0 /*expectedFlags*/);
2181 }
2182};
2183
2184TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Chris Ye2ad95392020-09-01 13:44:44 -07002185 sendAndConsumeKeyDown(1 /* deviceId */);
2186 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2187 expectKeyRepeatOnce(repeatCount);
2188 }
2189}
2190
2191TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
2192 sendAndConsumeKeyDown(1 /* deviceId */);
2193 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2194 expectKeyRepeatOnce(repeatCount);
2195 }
2196 sendAndConsumeKeyDown(2 /* deviceId */);
2197 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08002198 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2199 expectKeyRepeatOnce(repeatCount);
2200 }
2201}
2202
2203TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Chris Ye2ad95392020-09-01 13:44:44 -07002204 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002205 expectKeyRepeatOnce(1 /*repeatCount*/);
Chris Ye2ad95392020-09-01 13:44:44 -07002206 sendAndConsumeKeyUp(1 /* deviceId */);
2207 mWindow->assertNoEvents();
2208}
2209
2210TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
2211 sendAndConsumeKeyDown(1 /* deviceId */);
2212 expectKeyRepeatOnce(1 /*repeatCount*/);
2213 sendAndConsumeKeyDown(2 /* deviceId */);
2214 expectKeyRepeatOnce(1 /*repeatCount*/);
2215 // Stale key up from device 1.
2216 sendAndConsumeKeyUp(1 /* deviceId */);
2217 // Device 2 is still down, keep repeating
2218 expectKeyRepeatOnce(2 /*repeatCount*/);
2219 expectKeyRepeatOnce(3 /*repeatCount*/);
2220 // Device 2 key up
2221 sendAndConsumeKeyUp(2 /* deviceId */);
2222 mWindow->assertNoEvents();
2223}
2224
2225TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
2226 sendAndConsumeKeyDown(1 /* deviceId */);
2227 expectKeyRepeatOnce(1 /*repeatCount*/);
2228 sendAndConsumeKeyDown(2 /* deviceId */);
2229 expectKeyRepeatOnce(1 /*repeatCount*/);
2230 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
2231 sendAndConsumeKeyUp(2 /* deviceId */);
2232 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08002233 mWindow->assertNoEvents();
2234}
2235
2236TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Chris Ye2ad95392020-09-01 13:44:44 -07002237 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002238 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2239 InputEvent* repeatEvent = mWindow->consume();
2240 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2241 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
2242 IdGenerator::getSource(repeatEvent->getId()));
2243 }
2244}
2245
2246TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Chris Ye2ad95392020-09-01 13:44:44 -07002247 sendAndConsumeKeyDown(1 /* deviceId */);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002248
2249 std::unordered_set<int32_t> idSet;
2250 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2251 InputEvent* repeatEvent = mWindow->consume();
2252 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2253 int32_t id = repeatEvent->getId();
2254 EXPECT_EQ(idSet.end(), idSet.find(id));
2255 idSet.insert(id);
2256 }
2257}
2258
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002259/* Test InputDispatcher for MultiDisplay */
2260class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
2261public:
2262 static constexpr int32_t SECOND_DISPLAY_ID = 1;
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002263 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002264 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08002265
Chris Yea209fde2020-07-22 13:54:51 -07002266 application1 = std::make_shared<FakeApplicationHandle>();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002267 windowInPrimary = new FakeWindowHandle(application1, mDispatcher, "D_1",
2268 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002269
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002270 // Set focus window for primary display, but focused display would be second one.
2271 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07002272 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002273 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002274 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002275 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08002276
Chris Yea209fde2020-07-22 13:54:51 -07002277 application2 = std::make_shared<FakeApplicationHandle>();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002278 windowInSecondary = new FakeWindowHandle(application2, mDispatcher, "D_2",
2279 SECOND_DISPLAY_ID);
2280 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002281 // Set focus display to second one.
2282 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
2283 // Set focus window for second display.
2284 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07002285 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002286 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002287 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002288 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002289 }
2290
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002291 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002292 InputDispatcherTest::TearDown();
2293
Chris Yea209fde2020-07-22 13:54:51 -07002294 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002295 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07002296 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002297 windowInSecondary.clear();
2298 }
2299
2300protected:
Chris Yea209fde2020-07-22 13:54:51 -07002301 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002302 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07002303 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002304 sp<FakeWindowHandle> windowInSecondary;
2305};
2306
2307TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
2308 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002309 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2310 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2311 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002312 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08002313 windowInSecondary->assertNoEvents();
2314
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002315 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002316 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2317 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
2318 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08002319 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002320 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08002321}
2322
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002323TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08002324 // Test inject a key down with display id specified.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002325 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2326 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002327 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08002328 windowInSecondary->assertNoEvents();
2329
2330 // Test inject a key down without display id specified.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002331 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2332 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08002333 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002334 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08002335
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002336 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00002337 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08002338
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002339 // Old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002340 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
2341 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08002342
2343 // Test inject a key down, should timeout because of no target window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002344 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
2345 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08002346 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002347 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08002348 windowInSecondary->assertNoEvents();
2349}
2350
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002351// Test per-display input monitors for motion event.
2352TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08002353 FakeMonitorReceiver monitorInPrimary =
2354 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
2355 FakeMonitorReceiver monitorInSecondary =
2356 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002357
2358 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002359 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2360 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2361 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002362 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002363 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002364 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002365 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002366
2367 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002368 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2369 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
2370 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002371 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002372 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002373 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08002374 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002375
2376 // Test inject a non-pointer motion event.
2377 // If specific a display, it will dispatch to the focused window of particular display,
2378 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002379 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2380 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
2381 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002382 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002383 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002384 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08002385 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002386}
2387
2388// Test per-display input monitors for key event.
2389TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
2390 //Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08002391 FakeMonitorReceiver monitorInPrimary =
2392 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
2393 FakeMonitorReceiver monitorInSecondary =
2394 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002395
2396 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002397 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
2398 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002399 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002400 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002401 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08002402 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002403}
2404
Vishnu Nair958da932020-08-21 17:12:37 -07002405TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
2406 sp<FakeWindowHandle> secondWindowInPrimary =
2407 new FakeWindowHandle(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
2408 secondWindowInPrimary->setFocusable(true);
2409 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
2410 setFocusedWindow(secondWindowInPrimary);
2411 windowInPrimary->consumeFocusEvent(false);
2412 secondWindowInPrimary->consumeFocusEvent(true);
2413
2414 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002415 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2416 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07002417 windowInPrimary->assertNoEvents();
2418 windowInSecondary->assertNoEvents();
2419 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2420}
2421
Jackal Guof9696682018-10-05 12:23:23 +08002422class InputFilterTest : public InputDispatcherTest {
2423protected:
2424 static constexpr int32_t SECOND_DISPLAY_ID = 1;
2425
2426 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) {
2427 NotifyMotionArgs motionArgs;
2428
2429 motionArgs = generateMotionArgs(
2430 AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
2431 mDispatcher->notifyMotion(&motionArgs);
2432 motionArgs = generateMotionArgs(
2433 AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
2434 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002435 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08002436 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08002437 mFakePolicy->assertFilterInputEventWasCalled(motionArgs);
Jackal Guof9696682018-10-05 12:23:23 +08002438 } else {
2439 mFakePolicy->assertFilterInputEventWasNotCalled();
2440 }
2441 }
2442
2443 void testNotifyKey(bool expectToBeFiltered) {
2444 NotifyKeyArgs keyArgs;
2445
2446 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
2447 mDispatcher->notifyKey(&keyArgs);
2448 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
2449 mDispatcher->notifyKey(&keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002450 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08002451
2452 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08002453 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08002454 } else {
2455 mFakePolicy->assertFilterInputEventWasNotCalled();
2456 }
2457 }
2458};
2459
2460// Test InputFilter for MotionEvent
2461TEST_F(InputFilterTest, MotionEvent_InputFilter) {
2462 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
2463 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
2464 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
2465
2466 // Enable InputFilter
2467 mDispatcher->setInputFilterEnabled(true);
2468 // Test touch on both primary and second display, and check if both events are filtered.
2469 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
2470 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
2471
2472 // Disable InputFilter
2473 mDispatcher->setInputFilterEnabled(false);
2474 // Test touch on both primary and second display, and check if both events aren't filtered.
2475 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
2476 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
2477}
2478
2479// Test InputFilter for KeyEvent
2480TEST_F(InputFilterTest, KeyEvent_InputFilter) {
2481 // Since the InputFilter is disabled by default, check if key event aren't filtered.
2482 testNotifyKey(/*expectToBeFiltered*/ false);
2483
2484 // Enable InputFilter
2485 mDispatcher->setInputFilterEnabled(true);
2486 // Send a key event, and check if it is filtered.
2487 testNotifyKey(/*expectToBeFiltered*/ true);
2488
2489 // Disable InputFilter
2490 mDispatcher->setInputFilterEnabled(false);
2491 // Send a key event, and check if it isn't filtered.
2492 testNotifyKey(/*expectToBeFiltered*/ false);
2493}
2494
chaviwfd6d3512019-03-25 13:23:49 -07002495class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002496 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07002497 InputDispatcherTest::SetUp();
2498
Chris Yea209fde2020-07-22 13:54:51 -07002499 std::shared_ptr<FakeApplicationHandle> application =
2500 std::make_shared<FakeApplicationHandle>();
chaviwfd6d3512019-03-25 13:23:49 -07002501 mUnfocusedWindow = new FakeWindowHandle(application, mDispatcher, "Top",
2502 ADISPLAY_ID_DEFAULT);
2503 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
2504 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
2505 // window.
Michael Wright44753b12020-07-08 13:48:11 +01002506 mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07002507
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002508 mFocusedWindow =
2509 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2510 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01002511 mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07002512
2513 // Set focused application.
2514 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002515 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07002516
2517 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00002518 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002519 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002520 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07002521 }
2522
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002523 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07002524 InputDispatcherTest::TearDown();
2525
2526 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002527 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07002528 }
2529
2530protected:
2531 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002532 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002533 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07002534};
2535
2536// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
2537// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
2538// the onPointerDownOutsideFocus callback.
2539TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002540 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002541 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2542 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002543 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002544 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07002545
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002546 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07002547 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
2548}
2549
2550// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
2551// DOWN on the window that doesn't have focus. Ensure no window received the
2552// onPointerDownOutsideFocus callback.
2553TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002554 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002555 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002556 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002557 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07002558
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002559 ASSERT_TRUE(mDispatcher->waitForIdle());
2560 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07002561}
2562
2563// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
2564// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
2565TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002566 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2567 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002568 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07002569
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002570 ASSERT_TRUE(mDispatcher->waitForIdle());
2571 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07002572}
2573
2574// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
2575// DOWN on the window that already has focus. Ensure no window received the
2576// onPointerDownOutsideFocus callback.
2577TEST_F(InputDispatcherOnPointerDownOutsideFocus,
2578 OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002579 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002580 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002581 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002582 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002583 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07002584
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002585 ASSERT_TRUE(mDispatcher->waitForIdle());
2586 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07002587}
2588
chaviwaf87b3e2019-10-01 16:59:28 -07002589// These tests ensures we can send touch events to a single client when there are multiple input
2590// windows that point to the same client token.
2591class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
2592 virtual void SetUp() override {
2593 InputDispatcherTest::SetUp();
2594
Chris Yea209fde2020-07-22 13:54:51 -07002595 std::shared_ptr<FakeApplicationHandle> application =
2596 std::make_shared<FakeApplicationHandle>();
chaviwaf87b3e2019-10-01 16:59:28 -07002597 mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1",
2598 ADISPLAY_ID_DEFAULT);
2599 // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window.
2600 // 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 +01002601 mWindow1->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
2602 InputWindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07002603 mWindow1->setFrame(Rect(0, 0, 100, 100));
2604
2605 mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2",
2606 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
Michael Wright44753b12020-07-08 13:48:11 +01002607 mWindow2->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
2608 InputWindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07002609 mWindow2->setFrame(Rect(100, 100, 200, 200));
2610
Arthur Hung72d8dc32020-03-28 00:48:39 +00002611 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07002612 }
2613
2614protected:
2615 sp<FakeWindowHandle> mWindow1;
2616 sp<FakeWindowHandle> mWindow2;
2617
2618 // Helper function to convert the point from screen coordinates into the window's space
2619 static PointF getPointInWindow(const InputWindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07002620 vec2 vals = windowInfo->transform.transform(point.x, point.y);
2621 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07002622 }
2623
2624 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
2625 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002626 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07002627 InputEvent* event = window->consume();
2628
2629 ASSERT_NE(nullptr, event) << name.c_str()
2630 << ": consumer should have returned non-NULL event.";
2631
2632 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
2633 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
2634 << " event, got " << inputEventTypeToString(event->getType()) << " event";
2635
2636 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
2637 EXPECT_EQ(expectedAction, motionEvent.getAction());
2638
2639 for (size_t i = 0; i < points.size(); i++) {
2640 float expectedX = points[i].x;
2641 float expectedY = points[i].y;
2642
2643 EXPECT_EQ(expectedX, motionEvent.getX(i))
2644 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
2645 << ", got " << motionEvent.getX(i);
2646 EXPECT_EQ(expectedY, motionEvent.getY(i))
2647 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
2648 << ", got " << motionEvent.getY(i);
2649 }
2650 }
chaviw9eaa22c2020-07-01 16:21:27 -07002651
2652 void touchAndAssertPositions(int32_t action, std::vector<PointF> touchedPoints,
2653 std::vector<PointF> expectedPoints) {
2654 NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
2655 ADISPLAY_ID_DEFAULT, touchedPoints);
2656 mDispatcher->notifyMotion(&motionArgs);
2657
2658 // Always consume from window1 since it's the window that has the InputReceiver
2659 consumeMotionEvent(mWindow1, action, expectedPoints);
2660 }
chaviwaf87b3e2019-10-01 16:59:28 -07002661};
2662
2663TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
2664 // Touch Window 1
2665 PointF touchedPoint = {10, 10};
2666 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07002667 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07002668
2669 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07002670 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07002671
2672 // Touch Window 2
2673 touchedPoint = {150, 150};
2674 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07002675 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07002676}
2677
chaviw9eaa22c2020-07-01 16:21:27 -07002678TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
2679 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07002680 mWindow2->setWindowScale(0.5f, 0.5f);
2681
2682 // Touch Window 1
2683 PointF touchedPoint = {10, 10};
2684 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07002685 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07002686 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07002687 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07002688
2689 // Touch Window 2
2690 touchedPoint = {150, 150};
2691 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07002692 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
2693 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07002694
chaviw9eaa22c2020-07-01 16:21:27 -07002695 // Update the transform so rotation is set
2696 mWindow2->setWindowTransform(0, -1, 1, 0);
2697 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
2698 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07002699}
2700
chaviw9eaa22c2020-07-01 16:21:27 -07002701TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002702 mWindow2->setWindowScale(0.5f, 0.5f);
2703
2704 // Touch Window 1
2705 std::vector<PointF> touchedPoints = {PointF{10, 10}};
2706 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07002707 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002708
2709 // Touch Window 2
2710 int32_t actionPointerDown =
2711 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07002712 touchedPoints.push_back(PointF{150, 150});
2713 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
2714 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002715
chaviw9eaa22c2020-07-01 16:21:27 -07002716 // Release Window 2
2717 int32_t actionPointerUp =
2718 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
2719 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
2720 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002721
chaviw9eaa22c2020-07-01 16:21:27 -07002722 // Update the transform so rotation is set for Window 2
2723 mWindow2->setWindowTransform(0, -1, 1, 0);
2724 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
2725 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002726}
2727
chaviw9eaa22c2020-07-01 16:21:27 -07002728TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002729 mWindow2->setWindowScale(0.5f, 0.5f);
2730
2731 // Touch Window 1
2732 std::vector<PointF> touchedPoints = {PointF{10, 10}};
2733 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07002734 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002735
2736 // Touch Window 2
2737 int32_t actionPointerDown =
2738 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07002739 touchedPoints.push_back(PointF{150, 150});
2740 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002741
chaviw9eaa22c2020-07-01 16:21:27 -07002742 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002743
2744 // Move both windows
2745 touchedPoints = {{20, 20}, {175, 175}};
2746 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
2747 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
2748
chaviw9eaa22c2020-07-01 16:21:27 -07002749 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002750
chaviw9eaa22c2020-07-01 16:21:27 -07002751 // Release Window 2
2752 int32_t actionPointerUp =
2753 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
2754 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
2755 expectedPoints.pop_back();
2756
2757 // Touch Window 2
2758 mWindow2->setWindowTransform(0, -1, 1, 0);
2759 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
2760 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
2761
2762 // Move both windows
2763 touchedPoints = {{20, 20}, {175, 175}};
2764 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
2765 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
2766
2767 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002768}
2769
2770TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
2771 mWindow1->setWindowScale(0.5f, 0.5f);
2772
2773 // Touch Window 1
2774 std::vector<PointF> touchedPoints = {PointF{10, 10}};
2775 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07002776 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002777
2778 // Touch Window 2
2779 int32_t actionPointerDown =
2780 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07002781 touchedPoints.push_back(PointF{150, 150});
2782 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002783
chaviw9eaa22c2020-07-01 16:21:27 -07002784 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002785
2786 // Move both windows
2787 touchedPoints = {{20, 20}, {175, 175}};
2788 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
2789 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
2790
chaviw9eaa22c2020-07-01 16:21:27 -07002791 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002792}
2793
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002794class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
2795 virtual void SetUp() override {
2796 InputDispatcherTest::SetUp();
2797
Chris Yea209fde2020-07-22 13:54:51 -07002798 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002799 mApplication->setDispatchingTimeout(20ms);
2800 mWindow =
2801 new FakeWindowHandle(mApplication, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2802 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05002803 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07002804 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002805 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
2806 // window.
Michael Wright44753b12020-07-08 13:48:11 +01002807 mWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002808
2809 // Set focused application.
2810 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
2811
2812 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002813 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002814 mWindow->consumeFocusEvent(true);
2815 }
2816
2817 virtual void TearDown() override {
2818 InputDispatcherTest::TearDown();
2819 mWindow.clear();
2820 }
2821
2822protected:
Chris Yea209fde2020-07-22 13:54:51 -07002823 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002824 sp<FakeWindowHandle> mWindow;
2825 static constexpr PointF WINDOW_LOCATION = {20, 20};
2826
2827 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002828 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002829 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2830 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002831 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002832 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2833 WINDOW_LOCATION));
2834 }
2835};
2836
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002837// Send a tap and respond, which should not cause an ANR.
2838TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
2839 tapOnWindow();
2840 mWindow->consumeMotionDown();
2841 mWindow->consumeMotionUp();
2842 ASSERT_TRUE(mDispatcher->waitForIdle());
2843 mFakePolicy->assertNotifyAnrWasNotCalled();
2844}
2845
2846// Send a regular key and respond, which should not cause an ANR.
2847TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002848 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002849 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
2850 ASSERT_TRUE(mDispatcher->waitForIdle());
2851 mFakePolicy->assertNotifyAnrWasNotCalled();
2852}
2853
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05002854TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
2855 mWindow->setFocusable(false);
2856 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
2857 mWindow->consumeFocusEvent(false);
2858
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002859 InputEventInjectionResult result =
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05002860 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002861 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
2862 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05002863 // Key will not go to window because we have no focused window.
2864 // The 'no focused window' ANR timer should start instead.
2865
2866 // Now, the focused application goes away.
2867 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
2868 // The key should get dropped and there should be no ANR.
2869
2870 ASSERT_TRUE(mDispatcher->waitForIdle());
2871 mFakePolicy->assertNotifyAnrWasNotCalled();
2872}
2873
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002874// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002875// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
2876// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002877TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002878 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002879 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2880 WINDOW_LOCATION));
2881
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002882 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
2883 ASSERT_TRUE(sequenceNum);
2884 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
2885 mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002886
2887 // The remaining lines are not really needed for the test, but kept as a sanity check
2888 mWindow->finishEvent(*sequenceNum);
2889 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
2890 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002891 ASSERT_TRUE(mDispatcher->waitForIdle());
2892}
2893
2894// Send a key to the app and have the app not respond right away.
2895TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
2896 // Inject a key, and don't respond - expect that ANR is called.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002897 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002898 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
2899 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002900 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002901 mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/, mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002902 ASSERT_TRUE(mDispatcher->waitForIdle());
2903}
2904
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002905// We have a focused application, but no focused window
2906TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07002907 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002908 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
2909 mWindow->consumeFocusEvent(false);
2910
2911 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002912 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002913 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2914 WINDOW_LOCATION));
2915 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
2916 mDispatcher->waitForIdle();
2917 mFakePolicy->assertNotifyAnrWasNotCalled();
2918
2919 // Once a focused event arrives, we get an ANR for this application
2920 // We specify the injection timeout to be smaller than the application timeout, to ensure that
2921 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002922 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002923 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002924 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
2925 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002926 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
2927 mFakePolicy->assertNotifyAnrWasCalled(timeout, mApplication, nullptr /*windowToken*/);
2928 ASSERT_TRUE(mDispatcher->waitForIdle());
2929}
2930
2931// We have a focused application, but no focused window
2932// If the policy wants to keep waiting on the focused window to be added, make sure
2933// that this timeout extension is honored and ANR is raised again.
2934TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_ExtendsAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07002935 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002936 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
2937 mWindow->consumeFocusEvent(false);
2938 const std::chrono::duration timeout = 5ms;
2939 mFakePolicy->setAnrTimeout(timeout);
2940
2941 // Once a focused event arrives, we get an ANR for this application
2942 // We specify the injection timeout to be smaller than the application timeout, to ensure that
2943 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002944 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002945 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002946 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
2947 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002948 const std::chrono::duration appTimeout =
2949 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
2950 mFakePolicy->assertNotifyAnrWasCalled(appTimeout, mApplication, nullptr /*windowToken*/);
2951
2952 // After the extended time has passed, ANR should be raised again
2953 mFakePolicy->assertNotifyAnrWasCalled(timeout, mApplication, nullptr /*windowToken*/);
2954
2955 // If we stop extending the timeout, dispatcher should go to idle.
2956 // Another ANR may be raised during this time
2957 mFakePolicy->setAnrTimeout(0ms);
2958 ASSERT_TRUE(mDispatcher->waitForIdle());
2959}
2960
2961// We have a focused application, but no focused window
2962TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07002963 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002964 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
2965 mWindow->consumeFocusEvent(false);
2966
2967 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002968 const InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002969 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002970 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
2971 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002972
2973 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
2974 mFakePolicy->assertNotifyAnrWasCalled(timeout, mApplication, nullptr /*windowToken*/);
2975
2976 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002977 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002978 ASSERT_TRUE(mDispatcher->waitForIdle());
2979 mWindow->assertNoEvents();
2980}
2981
2982/**
2983 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
2984 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
2985 * If we process 1 of the events, but ANR on the second event with the same timestamp,
2986 * the ANR mechanism should still work.
2987 *
2988 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
2989 * DOWN event, while not responding on the second one.
2990 */
2991TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
2992 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
2993 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2994 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
2995 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
2996 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002997 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002998
2999 // Now send ACTION_UP, with identical timestamp
3000 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3001 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
3002 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
3003 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003004 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003005
3006 // We have now sent down and up. Let's consume first event and then ANR on the second.
3007 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3008 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
3009 mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/, mWindow->getToken());
3010}
3011
3012// If an app is not responding to a key event, gesture monitors should continue to receive
3013// new motion events
3014TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnKey) {
3015 FakeMonitorReceiver monitor =
3016 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
3017 true /*isGestureMonitor*/);
3018
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003019 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3020 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003021 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003022 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003023
3024 // Stuck on the ACTION_UP
3025 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
3026 mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr, mWindow->getToken());
3027
3028 // New tap will go to the gesture monitor, but not to the window
3029 tapOnWindow();
3030 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3031 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3032
3033 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
3034 mDispatcher->waitForIdle();
3035 mWindow->assertNoEvents();
3036 monitor.assertNoEvents();
3037}
3038
3039// If an app is not responding to a motion event, gesture monitors should continue to receive
3040// new motion events
3041TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnMotion) {
3042 FakeMonitorReceiver monitor =
3043 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
3044 true /*isGestureMonitor*/);
3045
3046 tapOnWindow();
3047 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3048 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3049
3050 mWindow->consumeMotionDown();
3051 // Stuck on the ACTION_UP
3052 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
3053 mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr, mWindow->getToken());
3054
3055 // New tap will go to the gesture monitor, but not to the window
3056 tapOnWindow();
3057 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3058 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3059
3060 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
3061 mDispatcher->waitForIdle();
3062 mWindow->assertNoEvents();
3063 monitor.assertNoEvents();
3064}
3065
3066// If a window is unresponsive, then you get anr. if the window later catches up and starts to
3067// process events, you don't get an anr. When the window later becomes unresponsive again, you
3068// get an ANR again.
3069// 1. tap -> block on ACTION_UP -> receive ANR
3070// 2. consume all pending events (= queue becomes healthy again)
3071// 3. tap again -> block on ACTION_UP again -> receive ANR second time
3072TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
3073 tapOnWindow();
3074
3075 mWindow->consumeMotionDown();
3076 // Block on ACTION_UP
3077 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
3078 mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/, mWindow->getToken());
3079 mWindow->consumeMotionUp(); // Now the connection should be healthy again
3080 mDispatcher->waitForIdle();
3081 mWindow->assertNoEvents();
3082
3083 tapOnWindow();
3084 mWindow->consumeMotionDown();
3085 mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/, mWindow->getToken());
3086 mWindow->consumeMotionUp();
3087
3088 mDispatcher->waitForIdle();
3089 mWindow->assertNoEvents();
3090}
3091
3092// If the policy tells us to raise ANR again after some time, ensure that the timeout extension
3093// is honored
3094TEST_F(InputDispatcherSingleWindowAnr, Policy_CanExtendTimeout) {
3095 const std::chrono::duration timeout = 5ms;
3096 mFakePolicy->setAnrTimeout(timeout);
3097
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003098 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003099 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3100 WINDOW_LOCATION));
3101
3102 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
3103 mFakePolicy->assertNotifyAnrWasCalled(windowTimeout, nullptr /*application*/,
3104 mWindow->getToken());
3105
3106 // Since the policy wanted to extend ANR, make sure it is called again after the extension
3107 mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/, mWindow->getToken());
3108 mFakePolicy->setAnrTimeout(0ms);
3109 std::this_thread::sleep_for(windowTimeout);
3110 // We are not checking if ANR has been called, because it may have been called again by the
3111 // time we set the timeout to 0
3112
3113 // When the policy finally says stop, we should get ACTION_CANCEL
3114 mWindow->consumeMotionDown();
3115 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3116 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3117 mWindow->assertNoEvents();
3118}
3119
3120/**
3121 * If a window is processing a motion event, and then a key event comes in, the key event should
3122 * not to to the focused window until the motion is processed.
3123 *
3124 * Warning!!!
3125 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
3126 * and the injection timeout that we specify when injecting the key.
3127 * We must have the injection timeout (10ms) be smaller than
3128 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
3129 *
3130 * If that value changes, this test should also change.
3131 */
3132TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
3133 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3134 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3135
3136 tapOnWindow();
3137 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3138 ASSERT_TRUE(downSequenceNum);
3139 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3140 ASSERT_TRUE(upSequenceNum);
3141 // Don't finish the events yet, and send a key
3142 // Injection will "succeed" because we will eventually give up and send the key to the focused
3143 // window even if motions are still being processed. But because the injection timeout is short,
3144 // we will receive INJECTION_TIMED_OUT as the result.
3145
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003146 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003147 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003148 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
3149 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003150 // Key will not be sent to the window, yet, because the window is still processing events
3151 // and the key remains pending, waiting for the touch events to be processed
3152 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3153 ASSERT_FALSE(keySequenceNum);
3154
3155 std::this_thread::sleep_for(500ms);
3156 // if we wait long enough though, dispatcher will give up, and still send the key
3157 // to the focused window, even though we have not yet finished the motion event
3158 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3159 mWindow->finishEvent(*downSequenceNum);
3160 mWindow->finishEvent(*upSequenceNum);
3161}
3162
3163/**
3164 * If a window is processing a motion event, and then a key event comes in, the key event should
3165 * not go to the focused window until the motion is processed.
3166 * If then a new motion comes in, then the pending key event should be going to the currently
3167 * focused window right away.
3168 */
3169TEST_F(InputDispatcherSingleWindowAnr,
3170 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
3171 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3172 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3173
3174 tapOnWindow();
3175 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3176 ASSERT_TRUE(downSequenceNum);
3177 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3178 ASSERT_TRUE(upSequenceNum);
3179 // Don't finish the events yet, and send a key
3180 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003181 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003182 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003183 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003184 // At this point, key is still pending, and should not be sent to the application yet.
3185 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3186 ASSERT_FALSE(keySequenceNum);
3187
3188 // Now tap down again. It should cause the pending key to go to the focused window right away.
3189 tapOnWindow();
3190 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
3191 // the other events yet. We can finish events in any order.
3192 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
3193 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
3194 mWindow->consumeMotionDown();
3195 mWindow->consumeMotionUp();
3196 mWindow->assertNoEvents();
3197}
3198
3199class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
3200 virtual void SetUp() override {
3201 InputDispatcherTest::SetUp();
3202
Chris Yea209fde2020-07-22 13:54:51 -07003203 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003204 mApplication->setDispatchingTimeout(10ms);
3205 mUnfocusedWindow =
3206 new FakeWindowHandle(mApplication, mDispatcher, "Unfocused", ADISPLAY_ID_DEFAULT);
3207 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
3208 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3209 // window.
3210 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Michael Wright44753b12020-07-08 13:48:11 +01003211 mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3212 InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH |
3213 InputWindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003214
3215 mFocusedWindow =
3216 new FakeWindowHandle(mApplication, mDispatcher, "Focused", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05003217 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003218 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01003219 mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3220 InputWindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003221
3222 // Set focused application.
3223 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07003224 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003225
3226 // Expect one focus window exist in display.
3227 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003228 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003229 mFocusedWindow->consumeFocusEvent(true);
3230 }
3231
3232 virtual void TearDown() override {
3233 InputDispatcherTest::TearDown();
3234
3235 mUnfocusedWindow.clear();
3236 mFocusedWindow.clear();
3237 }
3238
3239protected:
Chris Yea209fde2020-07-22 13:54:51 -07003240 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003241 sp<FakeWindowHandle> mUnfocusedWindow;
3242 sp<FakeWindowHandle> mFocusedWindow;
3243 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
3244 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
3245 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
3246
3247 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
3248
3249 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
3250
3251private:
3252 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003253 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003254 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3255 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003256 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003257 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3258 location));
3259 }
3260};
3261
3262// If we have 2 windows that are both unresponsive, the one with the shortest timeout
3263// should be ANR'd first.
3264TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003265 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003266 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3267 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003268 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003269 mFocusedWindow->consumeMotionDown();
3270 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3271 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3272 // We consumed all events, so no ANR
3273 ASSERT_TRUE(mDispatcher->waitForIdle());
3274 mFakePolicy->assertNotifyAnrWasNotCalled();
3275
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003276 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003277 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3278 FOCUSED_WINDOW_LOCATION));
3279 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
3280 ASSERT_TRUE(unfocusedSequenceNum);
3281 std::optional<uint32_t> focusedSequenceNum = mFocusedWindow->receiveEvent();
3282 ASSERT_TRUE(focusedSequenceNum);
3283
3284 const std::chrono::duration timeout =
3285 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
3286 mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/,
3287 mFocusedWindow->getToken());
3288
3289 mFocusedWindow->finishEvent(*focusedSequenceNum);
3290 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
3291 ASSERT_TRUE(mDispatcher->waitForIdle());
3292}
3293
3294// If we have 2 windows with identical timeouts that are both unresponsive,
3295// it doesn't matter which order they should have ANR.
3296// But we should receive ANR for both.
3297TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
3298 // Set the timeout for unfocused window to match the focused window
3299 mUnfocusedWindow->setDispatchingTimeout(10ms);
3300 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
3301
3302 tapOnFocusedWindow();
3303 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Chris Yea209fde2020-07-22 13:54:51 -07003304 std::pair<std::shared_ptr<InputApplicationHandle>, sp<IBinder>> anrData1 =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003305 mFakePolicy->getNotifyAnrData(10ms);
Chris Yea209fde2020-07-22 13:54:51 -07003306 std::pair<std::shared_ptr<InputApplicationHandle>, sp<IBinder>> anrData2 =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003307 mFakePolicy->getNotifyAnrData(0ms);
3308
3309 // We don't know which window will ANR first. But both of them should happen eventually.
3310 ASSERT_TRUE(mFocusedWindow->getToken() == anrData1.second ||
3311 mFocusedWindow->getToken() == anrData2.second);
3312 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrData1.second ||
3313 mUnfocusedWindow->getToken() == anrData2.second);
3314
3315 ASSERT_TRUE(mDispatcher->waitForIdle());
3316 mFakePolicy->assertNotifyAnrWasNotCalled();
3317}
3318
3319// If a window is already not responding, the second tap on the same window should be ignored.
3320// We should also log an error to account for the dropped event (not tested here).
3321// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
3322TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
3323 tapOnFocusedWindow();
3324 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3325 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3326 // Receive the events, but don't respond
3327 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
3328 ASSERT_TRUE(downEventSequenceNum);
3329 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
3330 ASSERT_TRUE(upEventSequenceNum);
3331 const std::chrono::duration timeout =
3332 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
3333 mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/,
3334 mFocusedWindow->getToken());
3335
3336 // Tap once again
3337 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003338 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003339 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3340 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003341 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003342 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3343 FOCUSED_WINDOW_LOCATION));
3344 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
3345 // valid touch target
3346 mUnfocusedWindow->assertNoEvents();
3347
3348 // Consume the first tap
3349 mFocusedWindow->finishEvent(*downEventSequenceNum);
3350 mFocusedWindow->finishEvent(*upEventSequenceNum);
3351 ASSERT_TRUE(mDispatcher->waitForIdle());
3352 // The second tap did not go to the focused window
3353 mFocusedWindow->assertNoEvents();
3354 // should not have another ANR after the window just became healthy again
3355 mFakePolicy->assertNotifyAnrWasNotCalled();
3356}
3357
3358// If you tap outside of all windows, there will not be ANR
3359TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003360 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003361 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3362 LOCATION_OUTSIDE_ALL_WINDOWS));
3363 ASSERT_TRUE(mDispatcher->waitForIdle());
3364 mFakePolicy->assertNotifyAnrWasNotCalled();
3365}
3366
3367// Since the focused window is paused, tapping on it should not produce any events
3368TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
3369 mFocusedWindow->setPaused(true);
3370 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
3371
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003372 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003373 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3374 FOCUSED_WINDOW_LOCATION));
3375
3376 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
3377 ASSERT_TRUE(mDispatcher->waitForIdle());
3378 // Should not ANR because the window is paused, and touches shouldn't go to it
3379 mFakePolicy->assertNotifyAnrWasNotCalled();
3380
3381 mFocusedWindow->assertNoEvents();
3382 mUnfocusedWindow->assertNoEvents();
3383}
3384
3385/**
3386 * If a window is processing a motion event, and then a key event comes in, the key event should
3387 * not to to the focused window until the motion is processed.
3388 * If a different window becomes focused at this time, the key should go to that window instead.
3389 *
3390 * Warning!!!
3391 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
3392 * and the injection timeout that we specify when injecting the key.
3393 * We must have the injection timeout (10ms) be smaller than
3394 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
3395 *
3396 * If that value changes, this test should also change.
3397 */
3398TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
3399 // Set a long ANR timeout to prevent it from triggering
3400 mFocusedWindow->setDispatchingTimeout(2s);
3401 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
3402
3403 tapOnUnfocusedWindow();
3404 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
3405 ASSERT_TRUE(downSequenceNum);
3406 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
3407 ASSERT_TRUE(upSequenceNum);
3408 // Don't finish the events yet, and send a key
3409 // Injection will succeed because we will eventually give up and send the key to the focused
3410 // window even if motions are still being processed.
3411
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003412 InputEventInjectionResult result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003413 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003414 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
3415 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003416 // Key will not be sent to the window, yet, because the window is still processing events
3417 // and the key remains pending, waiting for the touch events to be processed
3418 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
3419 ASSERT_FALSE(keySequenceNum);
3420
3421 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07003422 mFocusedWindow->setFocusable(false);
3423 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003424 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003425 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003426
3427 // Focus events should precede the key events
3428 mUnfocusedWindow->consumeFocusEvent(true);
3429 mFocusedWindow->consumeFocusEvent(false);
3430
3431 // Finish the tap events, which should unblock dispatcher
3432 mUnfocusedWindow->finishEvent(*downSequenceNum);
3433 mUnfocusedWindow->finishEvent(*upSequenceNum);
3434
3435 // Now that all queues are cleared and no backlog in the connections, the key event
3436 // can finally go to the newly focused "mUnfocusedWindow".
3437 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3438 mFocusedWindow->assertNoEvents();
3439 mUnfocusedWindow->assertNoEvents();
3440}
3441
3442// When the touch stream is split across 2 windows, and one of them does not respond,
3443// then ANR should be raised and the touch should be canceled for the unresponsive window.
3444// The other window should not be affected by that.
3445TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
3446 // Touch Window 1
3447 NotifyMotionArgs motionArgs =
3448 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3449 ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION});
3450 mDispatcher->notifyMotion(&motionArgs);
3451 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3452 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3453
3454 // Touch Window 2
3455 int32_t actionPointerDown =
3456 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3457
3458 motionArgs =
3459 generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3460 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION});
3461 mDispatcher->notifyMotion(&motionArgs);
3462
3463 const std::chrono::duration timeout =
3464 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
3465 mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/,
3466 mFocusedWindow->getToken());
3467
3468 mUnfocusedWindow->consumeMotionDown();
3469 mFocusedWindow->consumeMotionDown();
3470 // Focused window may or may not receive ACTION_MOVE
3471 // But it should definitely receive ACTION_CANCEL due to the ANR
3472 InputEvent* event;
3473 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
3474 ASSERT_TRUE(moveOrCancelSequenceNum);
3475 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
3476 ASSERT_NE(nullptr, event);
3477 ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION);
3478 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
3479 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
3480 mFocusedWindow->consumeMotionCancel();
3481 } else {
3482 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
3483 }
3484
3485 ASSERT_TRUE(mDispatcher->waitForIdle());
3486 mUnfocusedWindow->assertNoEvents();
3487 mFocusedWindow->assertNoEvents();
3488}
3489
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05003490/**
3491 * If we have no focused window, and a key comes in, we start the ANR timer.
3492 * The focused application should add a focused window before the timer runs out to prevent ANR.
3493 *
3494 * If the user touches another application during this time, the key should be dropped.
3495 * Next, if a new focused window comes in, without toggling the focused application,
3496 * then no ANR should occur.
3497 *
3498 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
3499 * but in some cases the policy may not update the focused application.
3500 */
3501TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
3502 std::shared_ptr<FakeApplicationHandle> focusedApplication =
3503 std::make_shared<FakeApplicationHandle>();
3504 focusedApplication->setDispatchingTimeout(60ms);
3505 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
3506 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
3507 mFocusedWindow->setFocusable(false);
3508
3509 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
3510 mFocusedWindow->consumeFocusEvent(false);
3511
3512 // Send a key. The ANR timer should start because there is no focused window.
3513 // 'focusedApplication' will get blamed if this timer completes.
3514 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003515 InputEventInjectionResult result =
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05003516 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003517 InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/);
3518 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05003519
3520 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
3521 // then the injected touches won't cause the focused event to get dropped.
3522 // The dispatcher only checks for whether the queue should be pruned upon queueing.
3523 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
3524 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
3525 // For this test, it means that the key would get delivered to the window once it becomes
3526 // focused.
3527 std::this_thread::sleep_for(10ms);
3528
3529 // Touch unfocused window. This should force the pending key to get dropped.
3530 NotifyMotionArgs motionArgs =
3531 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3532 ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION});
3533 mDispatcher->notifyMotion(&motionArgs);
3534
3535 // We do not consume the motion right away, because that would require dispatcher to first
3536 // process (== drop) the key event, and by that time, ANR will be raised.
3537 // Set the focused window first.
3538 mFocusedWindow->setFocusable(true);
3539 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
3540 setFocusedWindow(mFocusedWindow);
3541 mFocusedWindow->consumeFocusEvent(true);
3542 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
3543 // to another application. This could be a bug / behaviour in the policy.
3544
3545 mUnfocusedWindow->consumeMotionDown();
3546
3547 ASSERT_TRUE(mDispatcher->waitForIdle());
3548 // Should not ANR because we actually have a focused window. It was just added too slowly.
3549 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
3550}
3551
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05003552// These tests ensure we cannot send touch events to a window that's positioned behind a window
3553// that has feature NO_INPUT_CHANNEL.
3554// Layout:
3555// Top (closest to user)
3556// mNoInputWindow (above all windows)
3557// mBottomWindow
3558// Bottom (furthest from user)
3559class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
3560 virtual void SetUp() override {
3561 InputDispatcherTest::SetUp();
3562
3563 mApplication = std::make_shared<FakeApplicationHandle>();
3564 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
3565 "Window without input channel", ADISPLAY_ID_DEFAULT,
3566 std::make_optional<sp<IBinder>>(nullptr) /*token*/);
3567
3568 mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL);
3569 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
3570 // It's perfectly valid for this window to not have an associated input channel
3571
3572 mBottomWindow = new FakeWindowHandle(mApplication, mDispatcher, "Bottom window",
3573 ADISPLAY_ID_DEFAULT);
3574 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
3575
3576 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
3577 }
3578
3579protected:
3580 std::shared_ptr<FakeApplicationHandle> mApplication;
3581 sp<FakeWindowHandle> mNoInputWindow;
3582 sp<FakeWindowHandle> mBottomWindow;
3583};
3584
3585TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
3586 PointF touchedPoint = {10, 10};
3587
3588 NotifyMotionArgs motionArgs =
3589 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3590 ADISPLAY_ID_DEFAULT, {touchedPoint});
3591 mDispatcher->notifyMotion(&motionArgs);
3592
3593 mNoInputWindow->assertNoEvents();
3594 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
3595 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
3596 // and therefore should prevent mBottomWindow from receiving touches
3597 mBottomWindow->assertNoEvents();
3598}
3599
3600/**
3601 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
3602 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
3603 */
3604TEST_F(InputDispatcherMultiWindowOcclusionTests,
3605 NoInputChannelFeature_DropsTouchesWithValidChannel) {
3606 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
3607 "Window with input channel and NO_INPUT_CHANNEL",
3608 ADISPLAY_ID_DEFAULT);
3609
3610 mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL);
3611 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
3612 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
3613
3614 PointF touchedPoint = {10, 10};
3615
3616 NotifyMotionArgs motionArgs =
3617 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3618 ADISPLAY_ID_DEFAULT, {touchedPoint});
3619 mDispatcher->notifyMotion(&motionArgs);
3620
3621 mNoInputWindow->assertNoEvents();
3622 mBottomWindow->assertNoEvents();
3623}
3624
Vishnu Nair958da932020-08-21 17:12:37 -07003625class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
3626protected:
3627 std::shared_ptr<FakeApplicationHandle> mApp;
3628 sp<FakeWindowHandle> mWindow;
3629 sp<FakeWindowHandle> mMirror;
3630
3631 virtual void SetUp() override {
3632 InputDispatcherTest::SetUp();
3633 mApp = std::make_shared<FakeApplicationHandle>();
3634 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
3635 mMirror = new FakeWindowHandle(mApp, mDispatcher, "TestWindowMirror", ADISPLAY_ID_DEFAULT,
3636 mWindow->getToken());
3637 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
3638 mWindow->setFocusable(true);
3639 mMirror->setFocusable(true);
3640 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
3641 }
3642};
3643
3644TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
3645 // Request focus on a mirrored window
3646 setFocusedWindow(mMirror);
3647
3648 // window gets focused
3649 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003650 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3651 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003652 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
3653}
3654
3655// A focused & mirrored window remains focused only if the window and its mirror are both
3656// focusable.
3657TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
3658 setFocusedWindow(mMirror);
3659
3660 // window gets focused
3661 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003662 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3663 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003664 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003665 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
3666 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003667 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
3668
3669 mMirror->setFocusable(false);
3670 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
3671
3672 // window loses focus since one of the windows associated with the token in not focusable
3673 mWindow->consumeFocusEvent(false);
3674
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003675 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
3676 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07003677 mWindow->assertNoEvents();
3678}
3679
3680// A focused & mirrored window remains focused until the window and its mirror both become
3681// invisible.
3682TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
3683 setFocusedWindow(mMirror);
3684
3685 // window gets focused
3686 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003687 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3688 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003689 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003690 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
3691 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003692 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
3693
3694 mMirror->setVisible(false);
3695 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
3696
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003697 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3698 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003699 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003700 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
3701 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003702 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
3703
3704 mWindow->setVisible(false);
3705 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
3706
3707 // window loses focus only after all windows associated with the token become invisible.
3708 mWindow->consumeFocusEvent(false);
3709
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003710 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
3711 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07003712 mWindow->assertNoEvents();
3713}
3714
3715// A focused & mirrored window remains focused until both windows are removed.
3716TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
3717 setFocusedWindow(mMirror);
3718
3719 // window gets focused
3720 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003721 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3722 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003723 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003724 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
3725 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003726 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
3727
3728 // single window is removed but the window token remains focused
3729 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
3730
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003731 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
3732 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003733 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003734 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
3735 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07003736 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
3737
3738 // Both windows are removed
3739 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
3740 mWindow->consumeFocusEvent(false);
3741
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003742 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
3743 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07003744 mWindow->assertNoEvents();
3745}
3746
3747// Focus request can be pending until one window becomes visible.
3748TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
3749 // Request focus on an invisible mirror.
3750 mWindow->setVisible(false);
3751 mMirror->setVisible(false);
3752 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
3753 setFocusedWindow(mMirror);
3754
3755 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003756 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Vishnu Nair958da932020-08-21 17:12:37 -07003757 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003758 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07003759
3760 mMirror->setVisible(true);
3761 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
3762
3763 // window gets focused
3764 mWindow->consumeFocusEvent(true);
3765 // window gets the pending key event
3766 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3767}
Garfield Tane84e6f92019-08-29 17:28:41 -07003768} // namespace android::inputdispatcher