blob: 38a8c6fe22f01234f4afba36a256faf56989d220 [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
17#include "../InputDispatcher.h"
18
Robert Carr803535b2018-08-02 16:38:15 -070019#include <binder/Binder.h>
20
Michael Wrightd02c5b62014-02-10 15:10:22 -080021#include <gtest/gtest.h>
22#include <linux/input.h>
23
24namespace android {
25
26// An arbitrary time value.
27static const nsecs_t ARBITRARY_TIME = 1234;
28
29// An arbitrary device id.
30static const int32_t DEVICE_ID = 1;
31
Jeff Brownf086ddb2014-02-11 14:28:48 -080032// An arbitrary display id.
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -080033static const int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
Jeff Brownf086ddb2014-02-11 14:28:48 -080034
Michael Wrightd02c5b62014-02-10 15:10:22 -080035// An arbitrary injector pid / uid pair that has permission to inject events.
36static const int32_t INJECTOR_PID = 999;
37static const int32_t INJECTOR_UID = 1001;
38
39
40// --- FakeInputDispatcherPolicy ---
41
42class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
43 InputDispatcherConfiguration mConfig;
44
45protected:
46 virtual ~FakeInputDispatcherPolicy() {
47 }
48
49public:
50 FakeInputDispatcherPolicy() {
Jackal Guof9696682018-10-05 12:23:23 +080051 mInputEventFiltered = false;
52 mTime = -1;
53 mAction = -1;
54 mDisplayId = -1;
55 }
56
57 void assertFilterInputEventWasCalledWithExpectedArgs(const NotifyMotionArgs* args) {
58 ASSERT_TRUE(mInputEventFiltered)
59 << "Expected filterInputEvent() to have been called.";
60
61 ASSERT_EQ(mTime, args->eventTime)
62 << "Expected time of filtered event was not matched";
63 ASSERT_EQ(mAction, args->action)
64 << "Expected action of filtered event was not matched";
65 ASSERT_EQ(mDisplayId, args->displayId)
66 << "Expected displayId of filtered event was not matched";
67
68 reset();
69 }
70
71 void assertFilterInputEventWasCalledWithExpectedArgs(const NotifyKeyArgs* args) {
72 ASSERT_TRUE(mInputEventFiltered)
73 << "Expected filterInputEvent() to have been called.";
74
75 ASSERT_EQ(mTime, args->eventTime)
76 << "Expected time of filtered event was not matched";
77 ASSERT_EQ(mAction, args->action)
78 << "Expected action of filtered event was not matched";
79 ASSERT_EQ(mDisplayId, args->displayId)
80 << "Expected displayId of filtered event was not matched";
81
82 reset();
83 }
84
85 void assertFilterInputEventWasNotCalled() {
86 ASSERT_FALSE(mInputEventFiltered)
87 << "Expected filterInputEvent() to not have been called.";
Michael Wrightd02c5b62014-02-10 15:10:22 -080088 }
89
90private:
Jackal Guof9696682018-10-05 12:23:23 +080091 bool mInputEventFiltered;
92 nsecs_t mTime;
93 int32_t mAction;
94 int32_t mDisplayId;
95
Narayan Kamath39efe3e2014-10-17 10:37:08 +010096 virtual void notifyConfigurationChanged(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080097 }
98
Narayan Kamath39efe3e2014-10-17 10:37:08 +010099 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>&,
Robert Carr803535b2018-08-02 16:38:15 -0700100 const sp<IBinder>&,
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800101 const std::string&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800102 return 0;
103 }
104
Robert Carr803535b2018-08-02 16:38:15 -0700105 virtual void notifyInputChannelBroken(const sp<IBinder>&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800106 }
107
chaviw0c06c6e2019-01-09 13:27:07 -0800108 virtual void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) {
Robert Carr740167f2018-10-11 19:03:41 -0700109 }
110
Michael Wrightd02c5b62014-02-10 15:10:22 -0800111 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
112 *outConfig = mConfig;
113 }
114
Jackal Guof9696682018-10-05 12:23:23 +0800115 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) {
116 switch (inputEvent->getType()) {
117 case AINPUT_EVENT_TYPE_KEY: {
118 const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent);
119 mTime = keyEvent->getEventTime();
120 mAction = keyEvent->getAction();
121 mDisplayId = keyEvent->getDisplayId();
122 break;
123 }
124
125 case AINPUT_EVENT_TYPE_MOTION: {
126 const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent);
127 mTime = motionEvent->getEventTime();
128 mAction = motionEvent->getAction();
129 mDisplayId = motionEvent->getDisplayId();
130 break;
131 }
132 }
133
134 mInputEventFiltered = true;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800135 return true;
136 }
137
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100138 virtual void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800139 }
140
Charles Chen3611f1f2019-01-29 17:26:18 +0800141 virtual void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800142 }
143
Robert Carr803535b2018-08-02 16:38:15 -0700144 virtual nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&,
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100145 const KeyEvent*, uint32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800146 return 0;
147 }
148
Robert Carr803535b2018-08-02 16:38:15 -0700149 virtual bool dispatchUnhandledKey(const sp<IBinder>&,
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100150 const KeyEvent*, uint32_t, KeyEvent*) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800151 return false;
152 }
153
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100154 virtual void notifySwitch(nsecs_t, uint32_t, uint32_t, uint32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800155 }
156
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100157 virtual void pokeUserActivity(nsecs_t, int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800158 }
159
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100160 virtual bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800161 return false;
162 }
Jackal Guof9696682018-10-05 12:23:23 +0800163
164 void reset() {
165 mInputEventFiltered = false;
166 mTime = -1;
167 mAction = -1;
168 mDisplayId = -1;
169 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170};
171
172
173// --- InputDispatcherTest ---
174
175class InputDispatcherTest : public testing::Test {
176protected:
177 sp<FakeInputDispatcherPolicy> mFakePolicy;
178 sp<InputDispatcher> mDispatcher;
Arthur Hungb92218b2018-08-14 12:00:21 +0800179 sp<InputDispatcherThread> mDispatcherThread;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800180
181 virtual void SetUp() {
182 mFakePolicy = new FakeInputDispatcherPolicy();
183 mDispatcher = new InputDispatcher(mFakePolicy);
Arthur Hungb92218b2018-08-14 12:00:21 +0800184 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
185 //Start InputDispatcher thread
186 mDispatcherThread = new InputDispatcherThread(mDispatcher);
187 mDispatcherThread->run("InputDispatcherTest", PRIORITY_URGENT_DISPLAY);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188 }
189
190 virtual void TearDown() {
Arthur Hungb92218b2018-08-14 12:00:21 +0800191 mDispatcherThread->requestExit();
192 mDispatcherThread.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800193 mFakePolicy.clear();
194 mDispatcher.clear();
195 }
196};
197
198
199TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
200 KeyEvent event;
201
202 // Rejects undefined key actions.
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100203 event.initialize(DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800204 /*action*/ -1, 0,
205 AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, ARBITRARY_TIME);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800206 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800207 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800208 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
209 << "Should reject key events with undefined action.";
210
211 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100212 event.initialize(DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800213 AKEY_EVENT_ACTION_MULTIPLE, 0,
214 AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, ARBITRARY_TIME);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800215 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800216 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800217 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
218 << "Should reject key events with ACTION_MULTIPLE.";
219}
220
221TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
222 MotionEvent event;
223 PointerProperties pointerProperties[MAX_POINTERS + 1];
224 PointerCoords pointerCoords[MAX_POINTERS + 1];
225 for (int i = 0; i <= MAX_POINTERS; i++) {
226 pointerProperties[i].clear();
227 pointerProperties[i].id = i;
228 pointerCoords[i].clear();
229 }
230
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800231 // Some constants commonly used below
232 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
233 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
234 constexpr int32_t metaState = AMETA_NONE;
235 constexpr MotionClassification classification = MotionClassification::NONE;
236
Michael Wrightd02c5b62014-02-10 15:10:22 -0800237 // Rejects undefined motion actions.
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800238 event.initialize(DEVICE_ID, source, DISPLAY_ID,
239 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification, 0, 0, 0, 0,
240 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800241 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800242 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800243 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
244 << "Should reject motion events with undefined action.";
245
246 // Rejects pointer down with invalid index.
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800247 event.initialize(DEVICE_ID, source, DISPLAY_ID,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800248 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800249 0, 0, edgeFlags, metaState, 0, classification, 0, 0, 0, 0,
250 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800251 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800252 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800253 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
254 << "Should reject motion events with pointer down index too large.";
255
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800256 event.initialize(DEVICE_ID, source, DISPLAY_ID,
Dan Albert8b10c652016-02-02 17:08:05 -0800257 AMOTION_EVENT_ACTION_POINTER_DOWN | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800258 0, 0, edgeFlags, metaState, 0, classification, 0, 0, 0, 0,
259 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800260 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800261 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800262 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
263 << "Should reject motion events with pointer down index too small.";
264
265 // Rejects pointer up with invalid index.
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800266 event.initialize(DEVICE_ID, source, DISPLAY_ID,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800267 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800268 0, 0, edgeFlags, metaState, 0, classification, 0, 0, 0, 0,
269 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800270 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800271 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800272 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
273 << "Should reject motion events with pointer up index too large.";
274
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800275 event.initialize(DEVICE_ID, source, DISPLAY_ID,
Dan Albert8b10c652016-02-02 17:08:05 -0800276 AMOTION_EVENT_ACTION_POINTER_UP | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800277 0, 0, edgeFlags, metaState, 0, classification, 0, 0, 0, 0,
278 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800279 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800280 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800281 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
282 << "Should reject motion events with pointer up index too small.";
283
284 // Rejects motion events with invalid number of pointers.
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800285 event.initialize(DEVICE_ID, source, DISPLAY_ID,
286 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, 0, 0, 0, 0,
287 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 0, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800288 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800289 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800290 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
291 << "Should reject motion events with 0 pointers.";
292
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800293 event.initialize(DEVICE_ID, source, DISPLAY_ID,
294 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, 0, 0, 0, 0,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800295 ARBITRARY_TIME, ARBITRARY_TIME,
296 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800297 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800298 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800299 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
300 << "Should reject motion events with more than MAX_POINTERS pointers.";
301
302 // Rejects motion events with invalid pointer ids.
303 pointerProperties[0].id = -1;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800304 event.initialize(DEVICE_ID, source, DISPLAY_ID,
305 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, 0, 0, 0, 0,
306 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800307 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800308 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800309 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
310 << "Should reject motion events with pointer ids less than 0.";
311
312 pointerProperties[0].id = MAX_POINTER_ID + 1;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800313 event.initialize(DEVICE_ID, source, DISPLAY_ID,
314 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, 0, 0, 0, 0,
315 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800316 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800317 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800318 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
319 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
320
321 // Rejects motion events with duplicate pointer ids.
322 pointerProperties[0].id = 1;
323 pointerProperties[1].id = 1;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800324 event.initialize(DEVICE_ID, source, DISPLAY_ID,
325 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, 0, 0, 0, 0,
326 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 2, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800327 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800328 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800329 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
330 << "Should reject motion events with duplicate pointer ids.";
331}
332
Arthur Hungb92218b2018-08-14 12:00:21 +0800333// --- InputDispatcherTest SetInputWindowTest ---
334static const int32_t INJECT_EVENT_TIMEOUT = 500;
335static const int32_t DISPATCHING_TIMEOUT = 100;
336
337class FakeApplicationHandle : public InputApplicationHandle {
338public:
339 FakeApplicationHandle() {}
340 virtual ~FakeApplicationHandle() {}
341
342 virtual bool updateInfo() {
Arthur Hung7a0c39a2019-03-20 16:52:24 +0800343 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Arthur Hungb92218b2018-08-14 12:00:21 +0800344 return true;
345 }
346};
347
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800348class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800349public:
Tiger Huang8664f8c2018-10-11 19:14:35 +0800350 void consumeEvent(int32_t expectedEventType, int32_t expectedDisplayId,
351 int32_t expectedFlags = 0) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800352 uint32_t consumeSeq;
353 InputEvent* event;
354 status_t status = mConsumer->consume(&mEventFactory, false /*consumeBatches*/, -1,
355 &consumeSeq, &event);
356
357 ASSERT_EQ(OK, status)
358 << mName.c_str() << ": consumer consume should return OK.";
359 ASSERT_TRUE(event != nullptr)
360 << mName.c_str() << ": consumer should have returned non-NULL event.";
361 ASSERT_EQ(expectedEventType, event->getType())
Tiger Huang8664f8c2018-10-11 19:14:35 +0800362 << mName.c_str() << ": event type should match.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800363
364 ASSERT_EQ(expectedDisplayId, event->getDisplayId())
Tiger Huang8664f8c2018-10-11 19:14:35 +0800365 << mName.c_str() << ": event displayId should be the same as expected.";
366
367 int32_t flags;
368 switch (expectedEventType) {
369 case AINPUT_EVENT_TYPE_KEY: {
370 KeyEvent* typedEvent = static_cast<KeyEvent*>(event);
371 flags = typedEvent->getFlags();
372 break;
373 }
374 case AINPUT_EVENT_TYPE_MOTION: {
375 MotionEvent* typedEvent = static_cast<MotionEvent*>(event);
376 flags = typedEvent->getFlags();
377 break;
378 }
379 default: {
380 FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType;
381 }
382 }
383 ASSERT_EQ(expectedFlags, flags)
384 << mName.c_str() << ": event flags should be the same as expected.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800385
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800386 status = mConsumer->sendFinishedSignal(consumeSeq, handled());
Arthur Hungb92218b2018-08-14 12:00:21 +0800387 ASSERT_EQ(OK, status)
388 << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
389 }
390
391 void assertNoEvents() {
392 uint32_t consumeSeq;
393 InputEvent* event;
394 status_t status = mConsumer->consume(&mEventFactory, false /*consumeBatches*/, -1,
395 &consumeSeq, &event);
396 ASSERT_NE(OK, status)
397 << mName.c_str()
398 << ": should not have received any events, so consume(..) should not return OK.";
399 }
400
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800401protected:
402 explicit FakeInputReceiver(const sp<InputDispatcher>& dispatcher,
403 const std::string name, int32_t displayId) :
404 mDispatcher(dispatcher), mName(name), mDisplayId(displayId) {
405 InputChannel::openInputChannelPair(name, mServerChannel, mClientChannel);
406 mConsumer = new InputConsumer(mClientChannel);
407 }
408
409 virtual ~FakeInputReceiver() {
410 }
411
412 // return true if the event has been handled.
413 virtual bool handled() {
414 return false;
415 }
416
Arthur Hungb92218b2018-08-14 12:00:21 +0800417 sp<InputDispatcher> mDispatcher;
418 sp<InputChannel> mServerChannel, mClientChannel;
419 InputConsumer *mConsumer;
420 PreallocatedInputEventFactory mEventFactory;
421
422 std::string mName;
Arthur Hungb92218b2018-08-14 12:00:21 +0800423 int32_t mDisplayId;
424};
425
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800426class FakeWindowHandle : public InputWindowHandle, public FakeInputReceiver {
427public:
428 static const int32_t WIDTH = 600;
429 static const int32_t HEIGHT = 800;
430
431 FakeWindowHandle(const sp<InputApplicationHandle>& inputApplicationHandle,
432 const sp<InputDispatcher>& dispatcher, const std::string name, int32_t displayId) :
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800433 FakeInputReceiver(dispatcher, name, displayId),
434 mFocused(false) {
Robert Carr4e670e52018-08-15 13:26:12 -0700435 mServerChannel->setToken(new BBinder());
Robert Carr803535b2018-08-02 16:38:15 -0700436 mDispatcher->registerInputChannel(mServerChannel, displayId);
Michael Wright3dd60e22019-03-27 22:06:44 +0000437
Robert Carr740167f2018-10-11 19:03:41 -0700438 inputApplicationHandle->updateInfo();
439 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800440 }
441
442 virtual bool updateInfo() {
Arthur Hung6b5a2b92019-01-31 16:39:28 +0800443 mInfo.token = mServerChannel ? mServerChannel->getToken() : nullptr;
Arthur Hung3b413f22018-10-26 18:05:34 +0800444 mInfo.name = mName;
445 mInfo.layoutParamsFlags = 0;
446 mInfo.layoutParamsType = InputWindowInfo::TYPE_APPLICATION;
447 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
448 mInfo.frameLeft = 0;
449 mInfo.frameTop = 0;
450 mInfo.frameRight = WIDTH;
451 mInfo.frameBottom = HEIGHT;
Robert Carre07e1032018-11-26 12:55:53 -0800452 mInfo.globalScaleFactor = 1.0;
Arthur Hung3b413f22018-10-26 18:05:34 +0800453 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
454 mInfo.visible = true;
455 mInfo.canReceiveKeys = true;
456 mInfo.hasFocus = mFocused;
457 mInfo.hasWallpaper = false;
458 mInfo.paused = false;
459 mInfo.layer = 0;
460 mInfo.ownerPid = INJECTOR_PID;
461 mInfo.ownerUid = INJECTOR_UID;
462 mInfo.inputFeatures = 0;
463 mInfo.displayId = mDisplayId;
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800464
465 return true;
466 }
467
468 void setFocus() {
469 mFocused = true;
470 }
Arthur Hung832bc4a2019-01-28 11:43:17 +0800471
472 void releaseChannel() {
Arthur Hung6b5a2b92019-01-31 16:39:28 +0800473 mServerChannel.clear();
Arthur Hung832bc4a2019-01-28 11:43:17 +0800474 InputWindowHandle::releaseChannel();
Arthur Hung832bc4a2019-01-28 11:43:17 +0800475 }
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800476protected:
477 virtual bool handled() {
478 return true;
479 }
480
481 bool mFocused;
482};
483
Tiger Huang721e26f2018-07-24 22:26:19 +0800484static int32_t injectKeyDown(const sp<InputDispatcher>& dispatcher,
485 int32_t displayId = ADISPLAY_ID_NONE) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800486 KeyEvent event;
487 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
488
489 // Define a valid key down event.
Tiger Huang721e26f2018-07-24 22:26:19 +0800490 event.initialize(DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Arthur Hungb92218b2018-08-14 12:00:21 +0800491 AKEY_EVENT_ACTION_DOWN, /* flags */ 0,
492 AKEYCODE_A, KEY_A, AMETA_NONE, /* repeatCount */ 0, currentTime, currentTime);
493
494 // Inject event until dispatch out.
495 return dispatcher->injectInputEvent(
496 &event,
497 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT,
498 INJECT_EVENT_TIMEOUT, POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
499}
500
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800501static int32_t injectMotionDown(const sp<InputDispatcher>& dispatcher, int32_t source,
502 int32_t displayId) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800503 MotionEvent event;
504 PointerProperties pointerProperties[1];
505 PointerCoords pointerCoords[1];
506
507 pointerProperties[0].clear();
508 pointerProperties[0].id = 0;
509 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
510
511 pointerCoords[0].clear();
512 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
513 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 200);
514
515 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
516 // Define a valid motion down event.
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800517 event.initialize(DEVICE_ID, source, displayId,
Arthur Hungb92218b2018-08-14 12:00:21 +0800518 AMOTION_EVENT_ACTION_DOWN, /* actionButton */0, /* flags */ 0, /* edgeFlags */ 0,
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800519 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
520 /* xOffset */ 0, /* yOffset */ 0, /* xPrecision */ 0,
Arthur Hungb92218b2018-08-14 12:00:21 +0800521 /* yPrecision */ 0, currentTime, currentTime, /*pointerCount*/ 1, pointerProperties,
522 pointerCoords);
523
524 // Inject event until dispatch out.
525 return dispatcher->injectInputEvent(
526 &event,
527 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT,
528 INJECT_EVENT_TIMEOUT, POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
529}
530
Jackal Guof9696682018-10-05 12:23:23 +0800531static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
532 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
533 // Define a valid key event.
534 NotifyKeyArgs args(/* sequenceNum */ 0, currentTime, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
535 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0,
536 AKEYCODE_A, KEY_A, AMETA_NONE, currentTime);
537
538 return args;
539}
540
541static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
542 PointerProperties pointerProperties[1];
543 PointerCoords pointerCoords[1];
544
545 pointerProperties[0].clear();
546 pointerProperties[0].id = 0;
547 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
548
549 pointerCoords[0].clear();
550 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
551 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 200);
552
553 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
554 // Define a valid motion event.
555 NotifyMotionArgs args(/* sequenceNum */ 0, currentTime, DEVICE_ID, source, displayId,
556 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
557 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
558 AMOTION_EVENT_EDGE_FLAG_NONE, /* deviceTimestamp */ 0, 1, pointerProperties,
559 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0, currentTime,
560 /* videoFrames */ {});
561
562 return args;
563}
564
Arthur Hungb92218b2018-08-14 12:00:21 +0800565TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
566 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800567 sp<FakeWindowHandle> window = new FakeWindowHandle(application, mDispatcher, "Fake Window",
568 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800569
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800570 std::vector<sp<InputWindowHandle>> inputWindowHandles;
571 inputWindowHandles.push_back(window);
Arthur Hungb92218b2018-08-14 12:00:21 +0800572
573 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800574 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
575 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +0800576 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
577
578 // Window should receive motion event.
579 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_DEFAULT);
580}
581
582// The foreground window should receive the first touch down event.
583TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
584 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800585 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
586 ADISPLAY_ID_DEFAULT);
587 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
588 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800589
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800590 std::vector<sp<InputWindowHandle>> inputWindowHandles;
591 inputWindowHandles.push_back(windowTop);
592 inputWindowHandles.push_back(windowSecond);
Arthur Hungb92218b2018-08-14 12:00:21 +0800593
594 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800595 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
596 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +0800597 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
598
599 // Top window should receive the touch down event. Second window should not receive anything.
600 windowTop->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_DEFAULT);
601 windowSecond->assertNoEvents();
602}
603
604TEST_F(InputDispatcherTest, SetInputWindow_FocusedWindow) {
605 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800606 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
607 ADISPLAY_ID_DEFAULT);
608 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
609 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800610
Arthur Hung7ab76b12019-01-09 19:17:20 +0800611 // Set focused application.
Tiger Huang721e26f2018-07-24 22:26:19 +0800612 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Arthur Hungb92218b2018-08-14 12:00:21 +0800613
614 // Expect one focus window exist in display.
615 windowSecond->setFocus();
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800616 std::vector<sp<InputWindowHandle>> inputWindowHandles;
617 inputWindowHandles.push_back(windowTop);
618 inputWindowHandles.push_back(windowSecond);
Arthur Hungb92218b2018-08-14 12:00:21 +0800619
620 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
621 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
622 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
623
624 // Focused window should receive event.
625 windowTop->assertNoEvents();
626 windowSecond->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE);
627}
628
Arthur Hung7ab76b12019-01-09 19:17:20 +0800629TEST_F(InputDispatcherTest, SetInputWindow_FocusPriority) {
630 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
631 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
632 ADISPLAY_ID_DEFAULT);
633 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
634 ADISPLAY_ID_DEFAULT);
635
636 // Set focused application.
637 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
638
639 // Display has two focused windows. Add them to inputWindowsHandles in z-order (top most first)
640 windowTop->setFocus();
641 windowSecond->setFocus();
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800642 std::vector<sp<InputWindowHandle>> inputWindowHandles;
643 inputWindowHandles.push_back(windowTop);
644 inputWindowHandles.push_back(windowSecond);
Arthur Hung7ab76b12019-01-09 19:17:20 +0800645
646 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
647 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
648 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
649
650 // Top focused window should receive event.
651 windowTop->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE);
652 windowSecond->assertNoEvents();
653}
654
Arthur Hung3b413f22018-10-26 18:05:34 +0800655TEST_F(InputDispatcherTest, SetInputWindow_InputWindowInfo) {
656 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
657
658 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
659 ADISPLAY_ID_DEFAULT);
660 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
661 ADISPLAY_ID_DEFAULT);
662
Arthur Hung832bc4a2019-01-28 11:43:17 +0800663 // Set focused application.
664 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Arthur Hung3b413f22018-10-26 18:05:34 +0800665
Arthur Hung832bc4a2019-01-28 11:43:17 +0800666 windowTop->setFocus();
667 windowSecond->setFocus();
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800668 std::vector<sp<InputWindowHandle>> inputWindowHandles;
669 inputWindowHandles.push_back(windowTop);
670 inputWindowHandles.push_back(windowSecond);
Arthur Hung3b413f22018-10-26 18:05:34 +0800671 // Release channel for window is no longer valid.
672 windowTop->releaseChannel();
Arthur Hung832bc4a2019-01-28 11:43:17 +0800673 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
Arthur Hung3b413f22018-10-26 18:05:34 +0800674
Arthur Hung832bc4a2019-01-28 11:43:17 +0800675 // Test inject a key down, should dispatch to a valid window.
676 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
677 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Arthur Hung3b413f22018-10-26 18:05:34 +0800678
679 // Top window is invalid, so it should not receive any input event.
680 windowTop->assertNoEvents();
Arthur Hung832bc4a2019-01-28 11:43:17 +0800681 windowSecond->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE);
Arthur Hung3b413f22018-10-26 18:05:34 +0800682}
683
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800684/* Test InputDispatcher for MultiDisplay */
685class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
686public:
687 static constexpr int32_t SECOND_DISPLAY_ID = 1;
688 virtual void SetUp() {
689 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +0800690
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800691 application1 = new FakeApplicationHandle();
692 windowInPrimary = new FakeWindowHandle(application1, mDispatcher, "D_1",
693 ADISPLAY_ID_DEFAULT);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800694 std::vector<sp<InputWindowHandle>> inputWindowHandles;
695 inputWindowHandles.push_back(windowInPrimary);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800696 // Set focus window for primary display, but focused display would be second one.
697 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
698 windowInPrimary->setFocus();
699 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800700
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800701 application2 = new FakeApplicationHandle();
702 windowInSecondary = new FakeWindowHandle(application2, mDispatcher, "D_2",
703 SECOND_DISPLAY_ID);
704 // Set focus to second display window.
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800705 std::vector<sp<InputWindowHandle>> inputWindowHandles_Second;
706 inputWindowHandles_Second.push_back(windowInSecondary);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800707 // Set focus display to second one.
708 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
709 // Set focus window for second display.
710 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
711 windowInSecondary->setFocus();
712 mDispatcher->setInputWindows(inputWindowHandles_Second, SECOND_DISPLAY_ID);
713 }
714
715 virtual void TearDown() {
716 InputDispatcherTest::TearDown();
717
718 application1.clear();
719 windowInPrimary.clear();
720 application2.clear();
721 windowInSecondary.clear();
722 }
723
724protected:
725 sp<FakeApplicationHandle> application1;
726 sp<FakeWindowHandle> windowInPrimary;
727 sp<FakeApplicationHandle> application2;
728 sp<FakeWindowHandle> windowInSecondary;
729};
730
731TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
732 // Test touch down on primary display.
733 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
734 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +0800735 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
736 windowInPrimary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_DEFAULT);
737 windowInSecondary->assertNoEvents();
738
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800739 // Test touch down on second display.
740 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
741 AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Arthur Hungb92218b2018-08-14 12:00:21 +0800742 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
743 windowInPrimary->assertNoEvents();
744 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, SECOND_DISPLAY_ID);
745}
746
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800747TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +0800748 // Test inject a key down with display id specified.
749 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
750 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
751 windowInPrimary->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_DEFAULT);
752 windowInSecondary->assertNoEvents();
753
754 // Test inject a key down without display id specified.
Arthur Hungb92218b2018-08-14 12:00:21 +0800755 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
756 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
757 windowInPrimary->assertNoEvents();
758 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE);
759
760 // Remove secondary display.
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800761 std::vector<sp<InputWindowHandle>> noWindows;
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800762 mDispatcher->setInputWindows(noWindows, SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +0800763
764 // Expect old focus should receive a cancel event.
Tiger Huang8664f8c2018-10-11 19:14:35 +0800765 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE,
766 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +0800767
768 // Test inject a key down, should timeout because of no target window.
769 ASSERT_EQ(INPUT_EVENT_INJECTION_TIMED_OUT, injectKeyDown(mDispatcher))
770 << "Inject key event should return INPUT_EVENT_INJECTION_TIMED_OUT";
771 windowInPrimary->assertNoEvents();
772 windowInSecondary->assertNoEvents();
773}
774
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800775class FakeMonitorReceiver : public FakeInputReceiver, public RefBase {
776public:
777 FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name,
Michael Wright3dd60e22019-03-27 22:06:44 +0000778 int32_t displayId, bool isGestureMonitor = false)
779 : FakeInputReceiver(dispatcher, name, displayId) {
780 mServerChannel->setToken(new BBinder());
781 mDispatcher->registerInputMonitor(mServerChannel, displayId, isGestureMonitor);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800782 }
783};
784
785// Test per-display input monitors for motion event.
786TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
787 sp<FakeMonitorReceiver> monitorInPrimary =
788 new FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
789 sp<FakeMonitorReceiver> monitorInSecondary =
790 new FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
791
792 // Test touch down on primary display.
793 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
794 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
795 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
796 windowInPrimary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_DEFAULT);
797 monitorInPrimary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_DEFAULT);
798 windowInSecondary->assertNoEvents();
799 monitorInSecondary->assertNoEvents();
800
801 // Test touch down on second display.
802 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
803 AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
804 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
805 windowInPrimary->assertNoEvents();
806 monitorInPrimary->assertNoEvents();
807 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, SECOND_DISPLAY_ID);
808 monitorInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, SECOND_DISPLAY_ID);
809
810 // Test inject a non-pointer motion event.
811 // If specific a display, it will dispatch to the focused window of particular display,
812 // or it will dispatch to the focused window of focused display.
813 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
814 AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
815 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
816 windowInPrimary->assertNoEvents();
817 monitorInPrimary->assertNoEvents();
818 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_NONE);
819 monitorInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_NONE);
820}
821
822// Test per-display input monitors for key event.
823TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
824 //Input monitor per display.
825 sp<FakeMonitorReceiver> monitorInPrimary =
826 new FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
827 sp<FakeMonitorReceiver> monitorInSecondary =
828 new FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
829
830 // Test inject a key down.
831 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
832 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
833 windowInPrimary->assertNoEvents();
834 monitorInPrimary->assertNoEvents();
835 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE);
836 monitorInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE);
837}
838
Jackal Guof9696682018-10-05 12:23:23 +0800839class InputFilterTest : public InputDispatcherTest {
840protected:
841 static constexpr int32_t SECOND_DISPLAY_ID = 1;
842
843 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) {
844 NotifyMotionArgs motionArgs;
845
846 motionArgs = generateMotionArgs(
847 AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
848 mDispatcher->notifyMotion(&motionArgs);
849 motionArgs = generateMotionArgs(
850 AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
851 mDispatcher->notifyMotion(&motionArgs);
852
853 if (expectToBeFiltered) {
854 mFakePolicy->assertFilterInputEventWasCalledWithExpectedArgs(&motionArgs);
855 } else {
856 mFakePolicy->assertFilterInputEventWasNotCalled();
857 }
858 }
859
860 void testNotifyKey(bool expectToBeFiltered) {
861 NotifyKeyArgs keyArgs;
862
863 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
864 mDispatcher->notifyKey(&keyArgs);
865 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
866 mDispatcher->notifyKey(&keyArgs);
867
868 if (expectToBeFiltered) {
869 mFakePolicy->assertFilterInputEventWasCalledWithExpectedArgs(&keyArgs);
870 } else {
871 mFakePolicy->assertFilterInputEventWasNotCalled();
872 }
873 }
874};
875
876// Test InputFilter for MotionEvent
877TEST_F(InputFilterTest, MotionEvent_InputFilter) {
878 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
879 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
880 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
881
882 // Enable InputFilter
883 mDispatcher->setInputFilterEnabled(true);
884 // Test touch on both primary and second display, and check if both events are filtered.
885 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
886 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
887
888 // Disable InputFilter
889 mDispatcher->setInputFilterEnabled(false);
890 // Test touch on both primary and second display, and check if both events aren't filtered.
891 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
892 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
893}
894
895// Test InputFilter for KeyEvent
896TEST_F(InputFilterTest, KeyEvent_InputFilter) {
897 // Since the InputFilter is disabled by default, check if key event aren't filtered.
898 testNotifyKey(/*expectToBeFiltered*/ false);
899
900 // Enable InputFilter
901 mDispatcher->setInputFilterEnabled(true);
902 // Send a key event, and check if it is filtered.
903 testNotifyKey(/*expectToBeFiltered*/ true);
904
905 // Disable InputFilter
906 mDispatcher->setInputFilterEnabled(false);
907 // Send a key event, and check if it isn't filtered.
908 testNotifyKey(/*expectToBeFiltered*/ false);
909}
910
Michael Wrightd02c5b62014-02-10 15:10:22 -0800911} // namespace android