blob: 1fc78b289f7db8f4033b63399b6b084dd50d1f67 [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() {
343 if (!mInfo) {
344 mInfo = new InputApplicationInfo();
345 }
346 mInfo->dispatchingTimeout = DISPATCHING_TIMEOUT;
347 return true;
348 }
349};
350
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800351class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800352public:
Tiger Huang8664f8c2018-10-11 19:14:35 +0800353 void consumeEvent(int32_t expectedEventType, int32_t expectedDisplayId,
354 int32_t expectedFlags = 0) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800355 uint32_t consumeSeq;
356 InputEvent* event;
357 status_t status = mConsumer->consume(&mEventFactory, false /*consumeBatches*/, -1,
358 &consumeSeq, &event);
359
360 ASSERT_EQ(OK, status)
361 << mName.c_str() << ": consumer consume should return OK.";
362 ASSERT_TRUE(event != nullptr)
363 << mName.c_str() << ": consumer should have returned non-NULL event.";
364 ASSERT_EQ(expectedEventType, event->getType())
Tiger Huang8664f8c2018-10-11 19:14:35 +0800365 << mName.c_str() << ": event type should match.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800366
367 ASSERT_EQ(expectedDisplayId, event->getDisplayId())
Tiger Huang8664f8c2018-10-11 19:14:35 +0800368 << mName.c_str() << ": event displayId should be the same as expected.";
369
370 int32_t flags;
371 switch (expectedEventType) {
372 case AINPUT_EVENT_TYPE_KEY: {
373 KeyEvent* typedEvent = static_cast<KeyEvent*>(event);
374 flags = typedEvent->getFlags();
375 break;
376 }
377 case AINPUT_EVENT_TYPE_MOTION: {
378 MotionEvent* typedEvent = static_cast<MotionEvent*>(event);
379 flags = typedEvent->getFlags();
380 break;
381 }
382 default: {
383 FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType;
384 }
385 }
386 ASSERT_EQ(expectedFlags, flags)
387 << mName.c_str() << ": event flags should be the same as expected.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800388
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800389 status = mConsumer->sendFinishedSignal(consumeSeq, handled());
Arthur Hungb92218b2018-08-14 12:00:21 +0800390 ASSERT_EQ(OK, status)
391 << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
392 }
393
394 void assertNoEvents() {
395 uint32_t consumeSeq;
396 InputEvent* event;
397 status_t status = mConsumer->consume(&mEventFactory, false /*consumeBatches*/, -1,
398 &consumeSeq, &event);
399 ASSERT_NE(OK, status)
400 << mName.c_str()
401 << ": should not have received any events, so consume(..) should not return OK.";
402 }
403
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800404protected:
405 explicit FakeInputReceiver(const sp<InputDispatcher>& dispatcher,
406 const std::string name, int32_t displayId) :
407 mDispatcher(dispatcher), mName(name), mDisplayId(displayId) {
408 InputChannel::openInputChannelPair(name, mServerChannel, mClientChannel);
409 mConsumer = new InputConsumer(mClientChannel);
410 }
411
412 virtual ~FakeInputReceiver() {
413 }
414
415 // return true if the event has been handled.
416 virtual bool handled() {
417 return false;
418 }
419
Arthur Hungb92218b2018-08-14 12:00:21 +0800420 sp<InputDispatcher> mDispatcher;
421 sp<InputChannel> mServerChannel, mClientChannel;
Robert Carr5c8a0262018-10-03 16:30:44 -0700422 sp<IBinder> mToken;
Arthur Hungb92218b2018-08-14 12:00:21 +0800423 InputConsumer *mConsumer;
424 PreallocatedInputEventFactory mEventFactory;
425
426 std::string mName;
Arthur Hungb92218b2018-08-14 12:00:21 +0800427 int32_t mDisplayId;
428};
429
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800430class FakeWindowHandle : public InputWindowHandle, public FakeInputReceiver {
431public:
432 static const int32_t WIDTH = 600;
433 static const int32_t HEIGHT = 800;
434
435 FakeWindowHandle(const sp<InputApplicationHandle>& inputApplicationHandle,
436 const sp<InputDispatcher>& dispatcher, const std::string name, int32_t displayId) :
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800437 FakeInputReceiver(dispatcher, name, displayId),
438 mFocused(false) {
Robert Carr4e670e52018-08-15 13:26:12 -0700439 mServerChannel->setToken(new BBinder());
Robert Carr803535b2018-08-02 16:38:15 -0700440 mDispatcher->registerInputChannel(mServerChannel, displayId);
Robert Carr740167f2018-10-11 19:03:41 -0700441
442 inputApplicationHandle->updateInfo();
443 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800444 }
445
446 virtual bool updateInfo() {
Robert Carr5c8a0262018-10-03 16:30:44 -0700447 mInfo.token = mServerChannel->getToken();
Arthur Hung3b413f22018-10-26 18:05:34 +0800448 mInfo.name = mName;
449 mInfo.layoutParamsFlags = 0;
450 mInfo.layoutParamsType = InputWindowInfo::TYPE_APPLICATION;
451 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
452 mInfo.frameLeft = 0;
453 mInfo.frameTop = 0;
454 mInfo.frameRight = WIDTH;
455 mInfo.frameBottom = HEIGHT;
Robert Carre07e1032018-11-26 12:55:53 -0800456 mInfo.globalScaleFactor = 1.0;
Arthur Hung3b413f22018-10-26 18:05:34 +0800457 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
458 mInfo.visible = true;
459 mInfo.canReceiveKeys = true;
460 mInfo.hasFocus = mFocused;
461 mInfo.hasWallpaper = false;
462 mInfo.paused = false;
463 mInfo.layer = 0;
464 mInfo.ownerPid = INJECTOR_PID;
465 mInfo.ownerUid = INJECTOR_UID;
466 mInfo.inputFeatures = 0;
467 mInfo.displayId = mDisplayId;
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800468
469 return true;
470 }
471
472 void setFocus() {
473 mFocused = true;
474 }
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800475protected:
476 virtual bool handled() {
477 return true;
478 }
479
480 bool mFocused;
481};
482
Tiger Huang721e26f2018-07-24 22:26:19 +0800483static int32_t injectKeyDown(const sp<InputDispatcher>& dispatcher,
484 int32_t displayId = ADISPLAY_ID_NONE) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800485 KeyEvent event;
486 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
487
488 // Define a valid key down event.
Tiger Huang721e26f2018-07-24 22:26:19 +0800489 event.initialize(DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Arthur Hungb92218b2018-08-14 12:00:21 +0800490 AKEY_EVENT_ACTION_DOWN, /* flags */ 0,
491 AKEYCODE_A, KEY_A, AMETA_NONE, /* repeatCount */ 0, currentTime, currentTime);
492
493 // Inject event until dispatch out.
494 return dispatcher->injectInputEvent(
495 &event,
496 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT,
497 INJECT_EVENT_TIMEOUT, POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
498}
499
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800500static int32_t injectMotionDown(const sp<InputDispatcher>& dispatcher, int32_t source,
501 int32_t displayId) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800502 MotionEvent event;
503 PointerProperties pointerProperties[1];
504 PointerCoords pointerCoords[1];
505
506 pointerProperties[0].clear();
507 pointerProperties[0].id = 0;
508 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
509
510 pointerCoords[0].clear();
511 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
512 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 200);
513
514 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
515 // Define a valid motion down event.
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800516 event.initialize(DEVICE_ID, source, displayId,
Arthur Hungb92218b2018-08-14 12:00:21 +0800517 AMOTION_EVENT_ACTION_DOWN, /* actionButton */0, /* flags */ 0, /* edgeFlags */ 0,
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800518 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
519 /* xOffset */ 0, /* yOffset */ 0, /* xPrecision */ 0,
Arthur Hungb92218b2018-08-14 12:00:21 +0800520 /* yPrecision */ 0, currentTime, currentTime, /*pointerCount*/ 1, pointerProperties,
521 pointerCoords);
522
523 // Inject event until dispatch out.
524 return dispatcher->injectInputEvent(
525 &event,
526 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT,
527 INJECT_EVENT_TIMEOUT, POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
528}
529
Jackal Guof9696682018-10-05 12:23:23 +0800530static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
531 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
532 // Define a valid key event.
533 NotifyKeyArgs args(/* sequenceNum */ 0, currentTime, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
534 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0,
535 AKEYCODE_A, KEY_A, AMETA_NONE, currentTime);
536
537 return args;
538}
539
540static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
541 PointerProperties pointerProperties[1];
542 PointerCoords pointerCoords[1];
543
544 pointerProperties[0].clear();
545 pointerProperties[0].id = 0;
546 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
547
548 pointerCoords[0].clear();
549 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
550 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 200);
551
552 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
553 // Define a valid motion event.
554 NotifyMotionArgs args(/* sequenceNum */ 0, currentTime, DEVICE_ID, source, displayId,
555 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
556 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
557 AMOTION_EVENT_EDGE_FLAG_NONE, /* deviceTimestamp */ 0, 1, pointerProperties,
558 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0, currentTime,
559 /* videoFrames */ {});
560
561 return args;
562}
563
Arthur Hungb92218b2018-08-14 12:00:21 +0800564TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
565 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800566 sp<FakeWindowHandle> window = new FakeWindowHandle(application, mDispatcher, "Fake Window",
567 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800568
569 Vector<sp<InputWindowHandle>> inputWindowHandles;
570 inputWindowHandles.add(window);
571
572 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800573 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
574 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +0800575 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
576
577 // Window should receive motion event.
578 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_DEFAULT);
579}
580
581// The foreground window should receive the first touch down event.
582TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
583 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800584 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
585 ADISPLAY_ID_DEFAULT);
586 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
587 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800588
589 Vector<sp<InputWindowHandle>> inputWindowHandles;
590 inputWindowHandles.add(windowTop);
591 inputWindowHandles.add(windowSecond);
592
593 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800594 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
595 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +0800596 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
597
598 // Top window should receive the touch down event. Second window should not receive anything.
599 windowTop->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_DEFAULT);
600 windowSecond->assertNoEvents();
601}
602
603TEST_F(InputDispatcherTest, SetInputWindow_FocusedWindow) {
604 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800605 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
606 ADISPLAY_ID_DEFAULT);
607 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
608 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800609
Arthur Hung7ab76b12019-01-09 19:17:20 +0800610 // Set focused application.
Tiger Huang721e26f2018-07-24 22:26:19 +0800611 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Arthur Hungb92218b2018-08-14 12:00:21 +0800612
613 // Expect one focus window exist in display.
614 windowSecond->setFocus();
615 Vector<sp<InputWindowHandle>> inputWindowHandles;
616 inputWindowHandles.add(windowTop);
617 inputWindowHandles.add(windowSecond);
618
619 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
620 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
621 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
622
623 // Focused window should receive event.
624 windowTop->assertNoEvents();
625 windowSecond->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE);
626}
627
Arthur Hung7ab76b12019-01-09 19:17:20 +0800628TEST_F(InputDispatcherTest, SetInputWindow_FocusPriority) {
629 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
630 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
631 ADISPLAY_ID_DEFAULT);
632 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
633 ADISPLAY_ID_DEFAULT);
634
635 // Set focused application.
636 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
637
638 // Display has two focused windows. Add them to inputWindowsHandles in z-order (top most first)
639 windowTop->setFocus();
640 windowSecond->setFocus();
641 Vector<sp<InputWindowHandle>> inputWindowHandles;
642 inputWindowHandles.add(windowTop);
643 inputWindowHandles.add(windowSecond);
644
645 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
646 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
647 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
648
649 // Top focused window should receive event.
650 windowTop->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE);
651 windowSecond->assertNoEvents();
652}
653
Arthur Hung3b413f22018-10-26 18:05:34 +0800654TEST_F(InputDispatcherTest, SetInputWindow_InputWindowInfo) {
655 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
656
657 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
658 ADISPLAY_ID_DEFAULT);
659 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
660 ADISPLAY_ID_DEFAULT);
661
662 windowTop->setFocus();
663
664 Vector<sp<InputWindowHandle>> inputWindowHandles;
665 inputWindowHandles.add(windowTop);
666 inputWindowHandles.add(windowSecond);
667
668 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
669
670 // Release channel for window is no longer valid.
671 windowTop->releaseChannel();
672
673 // Test inject a motion down, should timeout because of no target channel.
674 ASSERT_EQ(INPUT_EVENT_INJECTION_TIMED_OUT, injectKeyDown(mDispatcher))
675 << "Inject key event should return INPUT_EVENT_INJECTION_TIMED_OUT";
676
677 // Top window is invalid, so it should not receive any input event.
678 windowTop->assertNoEvents();
679 windowSecond->assertNoEvents();
680}
681
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800682/* Test InputDispatcher for MultiDisplay */
683class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
684public:
685 static constexpr int32_t SECOND_DISPLAY_ID = 1;
686 virtual void SetUp() {
687 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +0800688
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800689 application1 = new FakeApplicationHandle();
690 windowInPrimary = new FakeWindowHandle(application1, mDispatcher, "D_1",
691 ADISPLAY_ID_DEFAULT);
692 Vector<sp<InputWindowHandle>> inputWindowHandles;
693 inputWindowHandles.push(windowInPrimary);
694 // Set focus window for primary display, but focused display would be second one.
695 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
696 windowInPrimary->setFocus();
697 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800698
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800699 application2 = new FakeApplicationHandle();
700 windowInSecondary = new FakeWindowHandle(application2, mDispatcher, "D_2",
701 SECOND_DISPLAY_ID);
702 // Set focus to second display window.
703 Vector<sp<InputWindowHandle>> inputWindowHandles_Second;
704 inputWindowHandles_Second.push(windowInSecondary);
705 // Set focus display to second one.
706 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
707 // Set focus window for second display.
708 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
709 windowInSecondary->setFocus();
710 mDispatcher->setInputWindows(inputWindowHandles_Second, SECOND_DISPLAY_ID);
711 }
712
713 virtual void TearDown() {
714 InputDispatcherTest::TearDown();
715
716 application1.clear();
717 windowInPrimary.clear();
718 application2.clear();
719 windowInSecondary.clear();
720 }
721
722protected:
723 sp<FakeApplicationHandle> application1;
724 sp<FakeWindowHandle> windowInPrimary;
725 sp<FakeApplicationHandle> application2;
726 sp<FakeWindowHandle> windowInSecondary;
727};
728
729TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
730 // Test touch down on primary display.
731 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
732 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +0800733 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
734 windowInPrimary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_DEFAULT);
735 windowInSecondary->assertNoEvents();
736
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800737 // Test touch down on second display.
738 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
739 AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Arthur Hungb92218b2018-08-14 12:00:21 +0800740 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
741 windowInPrimary->assertNoEvents();
742 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, SECOND_DISPLAY_ID);
743}
744
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800745TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +0800746 // Test inject a key down with display id specified.
747 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
748 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
749 windowInPrimary->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_DEFAULT);
750 windowInSecondary->assertNoEvents();
751
752 // Test inject a key down without display id specified.
Arthur Hungb92218b2018-08-14 12:00:21 +0800753 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
754 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
755 windowInPrimary->assertNoEvents();
756 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE);
757
758 // Remove secondary display.
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800759 Vector<sp<InputWindowHandle>> noWindows;
760 mDispatcher->setInputWindows(noWindows, SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +0800761
762 // Expect old focus should receive a cancel event.
Tiger Huang8664f8c2018-10-11 19:14:35 +0800763 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE,
764 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +0800765
766 // Test inject a key down, should timeout because of no target window.
767 ASSERT_EQ(INPUT_EVENT_INJECTION_TIMED_OUT, injectKeyDown(mDispatcher))
768 << "Inject key event should return INPUT_EVENT_INJECTION_TIMED_OUT";
769 windowInPrimary->assertNoEvents();
770 windowInSecondary->assertNoEvents();
771}
772
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800773class FakeMonitorReceiver : public FakeInputReceiver, public RefBase {
774public:
775 FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name,
776 int32_t displayId) : FakeInputReceiver(dispatcher, name, displayId) {
Robert Carr803535b2018-08-02 16:38:15 -0700777 mDispatcher->registerInputChannel(mServerChannel, displayId);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800778 }
779};
780
781// Test per-display input monitors for motion event.
782TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
783 sp<FakeMonitorReceiver> monitorInPrimary =
784 new FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
785 sp<FakeMonitorReceiver> monitorInSecondary =
786 new FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
787
788 // Test touch down on primary display.
789 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
790 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
791 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
792 windowInPrimary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_DEFAULT);
793 monitorInPrimary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_DEFAULT);
794 windowInSecondary->assertNoEvents();
795 monitorInSecondary->assertNoEvents();
796
797 // Test touch down on second display.
798 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
799 AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
800 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
801 windowInPrimary->assertNoEvents();
802 monitorInPrimary->assertNoEvents();
803 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, SECOND_DISPLAY_ID);
804 monitorInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, SECOND_DISPLAY_ID);
805
806 // Test inject a non-pointer motion event.
807 // If specific a display, it will dispatch to the focused window of particular display,
808 // or it will dispatch to the focused window of focused display.
809 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
810 AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
811 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
812 windowInPrimary->assertNoEvents();
813 monitorInPrimary->assertNoEvents();
814 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_NONE);
815 monitorInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_NONE);
816}
817
818// Test per-display input monitors for key event.
819TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
820 //Input monitor per display.
821 sp<FakeMonitorReceiver> monitorInPrimary =
822 new FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
823 sp<FakeMonitorReceiver> monitorInSecondary =
824 new FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
825
826 // Test inject a key down.
827 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
828 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
829 windowInPrimary->assertNoEvents();
830 monitorInPrimary->assertNoEvents();
831 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE);
832 monitorInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE);
833}
834
Jackal Guof9696682018-10-05 12:23:23 +0800835class InputFilterTest : public InputDispatcherTest {
836protected:
837 static constexpr int32_t SECOND_DISPLAY_ID = 1;
838
839 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) {
840 NotifyMotionArgs motionArgs;
841
842 motionArgs = generateMotionArgs(
843 AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
844 mDispatcher->notifyMotion(&motionArgs);
845 motionArgs = generateMotionArgs(
846 AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
847 mDispatcher->notifyMotion(&motionArgs);
848
849 if (expectToBeFiltered) {
850 mFakePolicy->assertFilterInputEventWasCalledWithExpectedArgs(&motionArgs);
851 } else {
852 mFakePolicy->assertFilterInputEventWasNotCalled();
853 }
854 }
855
856 void testNotifyKey(bool expectToBeFiltered) {
857 NotifyKeyArgs keyArgs;
858
859 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
860 mDispatcher->notifyKey(&keyArgs);
861 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
862 mDispatcher->notifyKey(&keyArgs);
863
864 if (expectToBeFiltered) {
865 mFakePolicy->assertFilterInputEventWasCalledWithExpectedArgs(&keyArgs);
866 } else {
867 mFakePolicy->assertFilterInputEventWasNotCalled();
868 }
869 }
870};
871
872// Test InputFilter for MotionEvent
873TEST_F(InputFilterTest, MotionEvent_InputFilter) {
874 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
875 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
876 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
877
878 // Enable InputFilter
879 mDispatcher->setInputFilterEnabled(true);
880 // Test touch on both primary and second display, and check if both events are filtered.
881 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
882 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
883
884 // Disable InputFilter
885 mDispatcher->setInputFilterEnabled(false);
886 // Test touch on both primary and second display, and check if both events aren't filtered.
887 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
888 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
889}
890
891// Test InputFilter for KeyEvent
892TEST_F(InputFilterTest, KeyEvent_InputFilter) {
893 // Since the InputFilter is disabled by default, check if key event aren't filtered.
894 testNotifyKey(/*expectToBeFiltered*/ false);
895
896 // Enable InputFilter
897 mDispatcher->setInputFilterEnabled(true);
898 // Send a key event, and check if it is filtered.
899 testNotifyKey(/*expectToBeFiltered*/ true);
900
901 // Disable InputFilter
902 mDispatcher->setInputFilterEnabled(false);
903 // Send a key event, and check if it isn't filtered.
904 testNotifyKey(/*expectToBeFiltered*/ false);
905}
906
Michael Wrightd02c5b62014-02-10 15:10:22 -0800907} // namespace android