blob: e0e211fd6ee5d6b22dce89358d1390e9456b314a [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
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100141 virtual void interceptMotionBeforeQueueing(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 Hung832bc4a2019-01-28 11:43:17 +0800475
476 void releaseChannel() {
477 InputWindowHandle::releaseChannel();
478 mDispatcher->unregisterInputChannel(mServerChannel);
479 }
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800480protected:
481 virtual bool handled() {
482 return true;
483 }
484
485 bool mFocused;
486};
487
Tiger Huang721e26f2018-07-24 22:26:19 +0800488static int32_t injectKeyDown(const sp<InputDispatcher>& dispatcher,
489 int32_t displayId = ADISPLAY_ID_NONE) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800490 KeyEvent event;
491 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
492
493 // Define a valid key down event.
Tiger Huang721e26f2018-07-24 22:26:19 +0800494 event.initialize(DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Arthur Hungb92218b2018-08-14 12:00:21 +0800495 AKEY_EVENT_ACTION_DOWN, /* flags */ 0,
496 AKEYCODE_A, KEY_A, AMETA_NONE, /* repeatCount */ 0, currentTime, currentTime);
497
498 // Inject event until dispatch out.
499 return dispatcher->injectInputEvent(
500 &event,
501 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT,
502 INJECT_EVENT_TIMEOUT, POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
503}
504
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800505static int32_t injectMotionDown(const sp<InputDispatcher>& dispatcher, int32_t source,
506 int32_t displayId) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800507 MotionEvent event;
508 PointerProperties pointerProperties[1];
509 PointerCoords pointerCoords[1];
510
511 pointerProperties[0].clear();
512 pointerProperties[0].id = 0;
513 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
514
515 pointerCoords[0].clear();
516 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
517 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 200);
518
519 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
520 // Define a valid motion down event.
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800521 event.initialize(DEVICE_ID, source, displayId,
Arthur Hungb92218b2018-08-14 12:00:21 +0800522 AMOTION_EVENT_ACTION_DOWN, /* actionButton */0, /* flags */ 0, /* edgeFlags */ 0,
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800523 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
524 /* xOffset */ 0, /* yOffset */ 0, /* xPrecision */ 0,
Arthur Hungb92218b2018-08-14 12:00:21 +0800525 /* yPrecision */ 0, currentTime, currentTime, /*pointerCount*/ 1, pointerProperties,
526 pointerCoords);
527
528 // Inject event until dispatch out.
529 return dispatcher->injectInputEvent(
530 &event,
531 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT,
532 INJECT_EVENT_TIMEOUT, POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
533}
534
Jackal Guof9696682018-10-05 12:23:23 +0800535static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
536 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
537 // Define a valid key event.
538 NotifyKeyArgs args(/* sequenceNum */ 0, currentTime, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
539 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0,
540 AKEYCODE_A, KEY_A, AMETA_NONE, currentTime);
541
542 return args;
543}
544
545static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
546 PointerProperties pointerProperties[1];
547 PointerCoords pointerCoords[1];
548
549 pointerProperties[0].clear();
550 pointerProperties[0].id = 0;
551 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
552
553 pointerCoords[0].clear();
554 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
555 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 200);
556
557 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
558 // Define a valid motion event.
559 NotifyMotionArgs args(/* sequenceNum */ 0, currentTime, DEVICE_ID, source, displayId,
560 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
561 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
562 AMOTION_EVENT_EDGE_FLAG_NONE, /* deviceTimestamp */ 0, 1, pointerProperties,
563 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0, currentTime,
564 /* videoFrames */ {});
565
566 return args;
567}
568
Arthur Hungb92218b2018-08-14 12:00:21 +0800569TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
570 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800571 sp<FakeWindowHandle> window = new FakeWindowHandle(application, mDispatcher, "Fake Window",
572 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800573
574 Vector<sp<InputWindowHandle>> inputWindowHandles;
575 inputWindowHandles.add(window);
576
577 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800578 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
579 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +0800580 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
581
582 // Window should receive motion event.
583 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_DEFAULT);
584}
585
586// The foreground window should receive the first touch down event.
587TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
588 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800589 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
590 ADISPLAY_ID_DEFAULT);
591 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
592 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800593
594 Vector<sp<InputWindowHandle>> inputWindowHandles;
595 inputWindowHandles.add(windowTop);
596 inputWindowHandles.add(windowSecond);
597
598 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800599 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
600 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +0800601 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
602
603 // Top window should receive the touch down event. Second window should not receive anything.
604 windowTop->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_DEFAULT);
605 windowSecond->assertNoEvents();
606}
607
608TEST_F(InputDispatcherTest, SetInputWindow_FocusedWindow) {
609 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800610 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
611 ADISPLAY_ID_DEFAULT);
612 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
613 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800614
Arthur Hung7ab76b12019-01-09 19:17:20 +0800615 // Set focused application.
Tiger Huang721e26f2018-07-24 22:26:19 +0800616 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Arthur Hungb92218b2018-08-14 12:00:21 +0800617
618 // Expect one focus window exist in display.
619 windowSecond->setFocus();
620 Vector<sp<InputWindowHandle>> inputWindowHandles;
621 inputWindowHandles.add(windowTop);
622 inputWindowHandles.add(windowSecond);
623
624 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
625 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
626 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
627
628 // Focused window should receive event.
629 windowTop->assertNoEvents();
630 windowSecond->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE);
631}
632
Arthur Hung7ab76b12019-01-09 19:17:20 +0800633TEST_F(InputDispatcherTest, SetInputWindow_FocusPriority) {
634 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
635 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
636 ADISPLAY_ID_DEFAULT);
637 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
638 ADISPLAY_ID_DEFAULT);
639
640 // Set focused application.
641 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
642
643 // Display has two focused windows. Add them to inputWindowsHandles in z-order (top most first)
644 windowTop->setFocus();
645 windowSecond->setFocus();
646 Vector<sp<InputWindowHandle>> inputWindowHandles;
647 inputWindowHandles.add(windowTop);
648 inputWindowHandles.add(windowSecond);
649
650 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
651 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
652 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
653
654 // Top focused window should receive event.
655 windowTop->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE);
656 windowSecond->assertNoEvents();
657}
658
Arthur Hung3b413f22018-10-26 18:05:34 +0800659TEST_F(InputDispatcherTest, SetInputWindow_InputWindowInfo) {
660 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
661
662 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
663 ADISPLAY_ID_DEFAULT);
664 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
665 ADISPLAY_ID_DEFAULT);
666
Arthur Hung832bc4a2019-01-28 11:43:17 +0800667 // Set focused application.
668 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Arthur Hung3b413f22018-10-26 18:05:34 +0800669
Arthur Hung832bc4a2019-01-28 11:43:17 +0800670 windowTop->setFocus();
671 windowSecond->setFocus();
Arthur Hung3b413f22018-10-26 18:05:34 +0800672 Vector<sp<InputWindowHandle>> inputWindowHandles;
673 inputWindowHandles.add(windowTop);
674 inputWindowHandles.add(windowSecond);
Arthur Hung3b413f22018-10-26 18:05:34 +0800675 // Release channel for window is no longer valid.
676 windowTop->releaseChannel();
Arthur Hung832bc4a2019-01-28 11:43:17 +0800677 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
Arthur Hung3b413f22018-10-26 18:05:34 +0800678
Arthur Hung832bc4a2019-01-28 11:43:17 +0800679 // Test inject a key down, should dispatch to a valid window.
680 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
681 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Arthur Hung3b413f22018-10-26 18:05:34 +0800682
683 // Top window is invalid, so it should not receive any input event.
684 windowTop->assertNoEvents();
Arthur Hung832bc4a2019-01-28 11:43:17 +0800685 windowSecond->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE);
Arthur Hung3b413f22018-10-26 18:05:34 +0800686}
687
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800688/* Test InputDispatcher for MultiDisplay */
689class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
690public:
691 static constexpr int32_t SECOND_DISPLAY_ID = 1;
692 virtual void SetUp() {
693 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +0800694
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800695 application1 = new FakeApplicationHandle();
696 windowInPrimary = new FakeWindowHandle(application1, mDispatcher, "D_1",
697 ADISPLAY_ID_DEFAULT);
698 Vector<sp<InputWindowHandle>> inputWindowHandles;
699 inputWindowHandles.push(windowInPrimary);
700 // Set focus window for primary display, but focused display would be second one.
701 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
702 windowInPrimary->setFocus();
703 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800704
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800705 application2 = new FakeApplicationHandle();
706 windowInSecondary = new FakeWindowHandle(application2, mDispatcher, "D_2",
707 SECOND_DISPLAY_ID);
708 // Set focus to second display window.
709 Vector<sp<InputWindowHandle>> inputWindowHandles_Second;
710 inputWindowHandles_Second.push(windowInSecondary);
711 // Set focus display to second one.
712 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
713 // Set focus window for second display.
714 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
715 windowInSecondary->setFocus();
716 mDispatcher->setInputWindows(inputWindowHandles_Second, SECOND_DISPLAY_ID);
717 }
718
719 virtual void TearDown() {
720 InputDispatcherTest::TearDown();
721
722 application1.clear();
723 windowInPrimary.clear();
724 application2.clear();
725 windowInSecondary.clear();
726 }
727
728protected:
729 sp<FakeApplicationHandle> application1;
730 sp<FakeWindowHandle> windowInPrimary;
731 sp<FakeApplicationHandle> application2;
732 sp<FakeWindowHandle> windowInSecondary;
733};
734
735TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
736 // Test touch down on primary display.
737 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
738 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +0800739 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
740 windowInPrimary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_DEFAULT);
741 windowInSecondary->assertNoEvents();
742
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800743 // Test touch down on second display.
744 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
745 AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Arthur Hungb92218b2018-08-14 12:00:21 +0800746 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
747 windowInPrimary->assertNoEvents();
748 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, SECOND_DISPLAY_ID);
749}
750
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800751TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +0800752 // Test inject a key down with display id specified.
753 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
754 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
755 windowInPrimary->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_DEFAULT);
756 windowInSecondary->assertNoEvents();
757
758 // Test inject a key down without display id specified.
Arthur Hungb92218b2018-08-14 12:00:21 +0800759 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
760 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
761 windowInPrimary->assertNoEvents();
762 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE);
763
764 // Remove secondary display.
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800765 Vector<sp<InputWindowHandle>> noWindows;
766 mDispatcher->setInputWindows(noWindows, SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +0800767
768 // Expect old focus should receive a cancel event.
Tiger Huang8664f8c2018-10-11 19:14:35 +0800769 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE,
770 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +0800771
772 // Test inject a key down, should timeout because of no target window.
773 ASSERT_EQ(INPUT_EVENT_INJECTION_TIMED_OUT, injectKeyDown(mDispatcher))
774 << "Inject key event should return INPUT_EVENT_INJECTION_TIMED_OUT";
775 windowInPrimary->assertNoEvents();
776 windowInSecondary->assertNoEvents();
777}
778
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800779class FakeMonitorReceiver : public FakeInputReceiver, public RefBase {
780public:
781 FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name,
782 int32_t displayId) : FakeInputReceiver(dispatcher, name, displayId) {
Robert Carr803535b2018-08-02 16:38:15 -0700783 mDispatcher->registerInputChannel(mServerChannel, displayId);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800784 }
785};
786
787// Test per-display input monitors for motion event.
788TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
789 sp<FakeMonitorReceiver> monitorInPrimary =
790 new FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
791 sp<FakeMonitorReceiver> monitorInSecondary =
792 new FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
793
794 // Test touch down on primary display.
795 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
796 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
797 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
798 windowInPrimary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_DEFAULT);
799 monitorInPrimary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_DEFAULT);
800 windowInSecondary->assertNoEvents();
801 monitorInSecondary->assertNoEvents();
802
803 // Test touch down on second display.
804 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
805 AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
806 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
807 windowInPrimary->assertNoEvents();
808 monitorInPrimary->assertNoEvents();
809 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, SECOND_DISPLAY_ID);
810 monitorInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, SECOND_DISPLAY_ID);
811
812 // Test inject a non-pointer motion event.
813 // If specific a display, it will dispatch to the focused window of particular display,
814 // or it will dispatch to the focused window of focused display.
815 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
816 AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
817 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
818 windowInPrimary->assertNoEvents();
819 monitorInPrimary->assertNoEvents();
820 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_NONE);
821 monitorInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_NONE);
822}
823
824// Test per-display input monitors for key event.
825TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
826 //Input monitor per display.
827 sp<FakeMonitorReceiver> monitorInPrimary =
828 new FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
829 sp<FakeMonitorReceiver> monitorInSecondary =
830 new FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
831
832 // Test inject a key down.
833 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
834 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
835 windowInPrimary->assertNoEvents();
836 monitorInPrimary->assertNoEvents();
837 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE);
838 monitorInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE);
839}
840
Jackal Guof9696682018-10-05 12:23:23 +0800841class InputFilterTest : public InputDispatcherTest {
842protected:
843 static constexpr int32_t SECOND_DISPLAY_ID = 1;
844
845 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) {
846 NotifyMotionArgs motionArgs;
847
848 motionArgs = generateMotionArgs(
849 AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
850 mDispatcher->notifyMotion(&motionArgs);
851 motionArgs = generateMotionArgs(
852 AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
853 mDispatcher->notifyMotion(&motionArgs);
854
855 if (expectToBeFiltered) {
856 mFakePolicy->assertFilterInputEventWasCalledWithExpectedArgs(&motionArgs);
857 } else {
858 mFakePolicy->assertFilterInputEventWasNotCalled();
859 }
860 }
861
862 void testNotifyKey(bool expectToBeFiltered) {
863 NotifyKeyArgs keyArgs;
864
865 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
866 mDispatcher->notifyKey(&keyArgs);
867 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
868 mDispatcher->notifyKey(&keyArgs);
869
870 if (expectToBeFiltered) {
871 mFakePolicy->assertFilterInputEventWasCalledWithExpectedArgs(&keyArgs);
872 } else {
873 mFakePolicy->assertFilterInputEventWasNotCalled();
874 }
875 }
876};
877
878// Test InputFilter for MotionEvent
879TEST_F(InputFilterTest, MotionEvent_InputFilter) {
880 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
881 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
882 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
883
884 // Enable InputFilter
885 mDispatcher->setInputFilterEnabled(true);
886 // Test touch on both primary and second display, and check if both events are filtered.
887 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
888 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
889
890 // Disable InputFilter
891 mDispatcher->setInputFilterEnabled(false);
892 // Test touch on both primary and second display, and check if both events aren't filtered.
893 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
894 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
895}
896
897// Test InputFilter for KeyEvent
898TEST_F(InputFilterTest, KeyEvent_InputFilter) {
899 // Since the InputFilter is disabled by default, check if key event aren't filtered.
900 testNotifyKey(/*expectToBeFiltered*/ false);
901
902 // Enable InputFilter
903 mDispatcher->setInputFilterEnabled(true);
904 // Send a key event, and check if it is filtered.
905 testNotifyKey(/*expectToBeFiltered*/ true);
906
907 // Disable InputFilter
908 mDispatcher->setInputFilterEnabled(false);
909 // Send a key event, and check if it isn't filtered.
910 testNotifyKey(/*expectToBeFiltered*/ false);
911}
912
Michael Wrightd02c5b62014-02-10 15:10:22 -0800913} // namespace android