blob: b706a749fe616af31bd6b140b3677910365f46d6 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Garfield Tan0fc2fa72019-08-29 17:22:15 -070017#include "../dispatcher/InputDispatcher.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080018
Garfield Tane84e6f92019-08-29 17:28:41 -070019#include <InputDispatcherThread.h>
20
Robert Carr803535b2018-08-02 16:38:15 -070021#include <binder/Binder.h>
22
Michael Wrightd02c5b62014-02-10 15:10:22 -080023#include <gtest/gtest.h>
24#include <linux/input.h>
25
Garfield Tane84e6f92019-08-29 17:28:41 -070026namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080027
28// An arbitrary time value.
29static const nsecs_t ARBITRARY_TIME = 1234;
30
31// An arbitrary device id.
32static const int32_t DEVICE_ID = 1;
33
Jeff Brownf086ddb2014-02-11 14:28:48 -080034// An arbitrary display id.
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -080035static const int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
Jeff Brownf086ddb2014-02-11 14:28:48 -080036
Michael Wrightd02c5b62014-02-10 15:10:22 -080037// An arbitrary injector pid / uid pair that has permission to inject events.
38static const int32_t INJECTOR_PID = 999;
39static const int32_t INJECTOR_UID = 1001;
40
41
42// --- FakeInputDispatcherPolicy ---
43
44class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
45 InputDispatcherConfiguration mConfig;
46
47protected:
48 virtual ~FakeInputDispatcherPolicy() {
49 }
50
51public:
52 FakeInputDispatcherPolicy() {
Jackal Guof9696682018-10-05 12:23:23 +080053 mInputEventFiltered = false;
54 mTime = -1;
55 mAction = -1;
56 mDisplayId = -1;
chaviwfd6d3512019-03-25 13:23:49 -070057 mOnPointerDownToken.clear();
Jackal Guof9696682018-10-05 12:23:23 +080058 }
59
60 void assertFilterInputEventWasCalledWithExpectedArgs(const NotifyMotionArgs* args) {
61 ASSERT_TRUE(mInputEventFiltered)
62 << "Expected filterInputEvent() to have been called.";
63
64 ASSERT_EQ(mTime, args->eventTime)
65 << "Expected time of filtered event was not matched";
66 ASSERT_EQ(mAction, args->action)
67 << "Expected action of filtered event was not matched";
68 ASSERT_EQ(mDisplayId, args->displayId)
69 << "Expected displayId of filtered event was not matched";
70
71 reset();
72 }
73
74 void assertFilterInputEventWasCalledWithExpectedArgs(const NotifyKeyArgs* args) {
75 ASSERT_TRUE(mInputEventFiltered)
76 << "Expected filterInputEvent() to have been called.";
77
78 ASSERT_EQ(mTime, args->eventTime)
79 << "Expected time of filtered event was not matched";
80 ASSERT_EQ(mAction, args->action)
81 << "Expected action of filtered event was not matched";
82 ASSERT_EQ(mDisplayId, args->displayId)
83 << "Expected displayId of filtered event was not matched";
84
85 reset();
86 }
87
88 void assertFilterInputEventWasNotCalled() {
89 ASSERT_FALSE(mInputEventFiltered)
90 << "Expected filterInputEvent() to not have been called.";
Michael Wrightd02c5b62014-02-10 15:10:22 -080091 }
92
chaviwfd6d3512019-03-25 13:23:49 -070093 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
94 ASSERT_EQ(mOnPointerDownToken, touchedToken)
95 << "Expected token from onPointerDownOutsideFocus was not matched";
96 reset();
97 }
98
Michael Wrightd02c5b62014-02-10 15:10:22 -080099private:
Jackal Guof9696682018-10-05 12:23:23 +0800100 bool mInputEventFiltered;
101 nsecs_t mTime;
102 int32_t mAction;
103 int32_t mDisplayId;
chaviwfd6d3512019-03-25 13:23:49 -0700104 sp<IBinder> mOnPointerDownToken;
Jackal Guof9696682018-10-05 12:23:23 +0800105
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100106 virtual void notifyConfigurationChanged(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800107 }
108
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100109 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>&,
Robert Carr803535b2018-08-02 16:38:15 -0700110 const sp<IBinder>&,
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800111 const std::string&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800112 return 0;
113 }
114
Robert Carr803535b2018-08-02 16:38:15 -0700115 virtual void notifyInputChannelBroken(const sp<IBinder>&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800116 }
117
chaviw0c06c6e2019-01-09 13:27:07 -0800118 virtual void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) {
Robert Carr740167f2018-10-11 19:03:41 -0700119 }
120
Michael Wrightd02c5b62014-02-10 15:10:22 -0800121 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
122 *outConfig = mConfig;
123 }
124
Jackal Guof9696682018-10-05 12:23:23 +0800125 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) {
126 switch (inputEvent->getType()) {
127 case AINPUT_EVENT_TYPE_KEY: {
128 const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent);
129 mTime = keyEvent->getEventTime();
130 mAction = keyEvent->getAction();
131 mDisplayId = keyEvent->getDisplayId();
132 break;
133 }
134
135 case AINPUT_EVENT_TYPE_MOTION: {
136 const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent);
137 mTime = motionEvent->getEventTime();
138 mAction = motionEvent->getAction();
139 mDisplayId = motionEvent->getDisplayId();
140 break;
141 }
142 }
143
144 mInputEventFiltered = true;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800145 return true;
146 }
147
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100148 virtual void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800149 }
150
Charles Chen3611f1f2019-01-29 17:26:18 +0800151 virtual void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800152 }
153
Robert Carr803535b2018-08-02 16:38:15 -0700154 virtual nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&,
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100155 const KeyEvent*, uint32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800156 return 0;
157 }
158
Robert Carr803535b2018-08-02 16:38:15 -0700159 virtual bool dispatchUnhandledKey(const sp<IBinder>&,
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100160 const KeyEvent*, uint32_t, KeyEvent*) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800161 return false;
162 }
163
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100164 virtual void notifySwitch(nsecs_t, uint32_t, uint32_t, uint32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800165 }
166
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100167 virtual void pokeUserActivity(nsecs_t, int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800168 }
169
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100170 virtual bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800171 return false;
172 }
Jackal Guof9696682018-10-05 12:23:23 +0800173
chaviwfd6d3512019-03-25 13:23:49 -0700174 virtual void onPointerDownOutsideFocus(const sp<IBinder>& newToken) {
175 mOnPointerDownToken = newToken;
176 }
177
Jackal Guof9696682018-10-05 12:23:23 +0800178 void reset() {
179 mInputEventFiltered = false;
180 mTime = -1;
181 mAction = -1;
182 mDisplayId = -1;
chaviwfd6d3512019-03-25 13:23:49 -0700183 mOnPointerDownToken.clear();
Jackal Guof9696682018-10-05 12:23:23 +0800184 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800185};
186
187
188// --- InputDispatcherTest ---
189
190class InputDispatcherTest : public testing::Test {
191protected:
192 sp<FakeInputDispatcherPolicy> mFakePolicy;
193 sp<InputDispatcher> mDispatcher;
Arthur Hungb92218b2018-08-14 12:00:21 +0800194 sp<InputDispatcherThread> mDispatcherThread;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800195
196 virtual void SetUp() {
197 mFakePolicy = new FakeInputDispatcherPolicy();
198 mDispatcher = new InputDispatcher(mFakePolicy);
Arthur Hungb92218b2018-08-14 12:00:21 +0800199 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
200 //Start InputDispatcher thread
201 mDispatcherThread = new InputDispatcherThread(mDispatcher);
202 mDispatcherThread->run("InputDispatcherTest", PRIORITY_URGENT_DISPLAY);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800203 }
204
205 virtual void TearDown() {
Arthur Hungb92218b2018-08-14 12:00:21 +0800206 mDispatcherThread->requestExit();
207 mDispatcherThread.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800208 mFakePolicy.clear();
209 mDispatcher.clear();
210 }
211};
212
213
214TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
215 KeyEvent event;
216
217 // Rejects undefined key actions.
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100218 event.initialize(DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800219 /*action*/ -1, 0,
220 AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, ARBITRARY_TIME);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800221 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800222 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800223 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
224 << "Should reject key events with undefined action.";
225
226 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100227 event.initialize(DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800228 AKEY_EVENT_ACTION_MULTIPLE, 0,
229 AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, ARBITRARY_TIME);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800230 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800231 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800232 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
233 << "Should reject key events with ACTION_MULTIPLE.";
234}
235
236TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
237 MotionEvent event;
238 PointerProperties pointerProperties[MAX_POINTERS + 1];
239 PointerCoords pointerCoords[MAX_POINTERS + 1];
240 for (int i = 0; i <= MAX_POINTERS; i++) {
241 pointerProperties[i].clear();
242 pointerProperties[i].id = i;
243 pointerCoords[i].clear();
244 }
245
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800246 // Some constants commonly used below
247 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
248 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
249 constexpr int32_t metaState = AMETA_NONE;
250 constexpr MotionClassification classification = MotionClassification::NONE;
251
Michael Wrightd02c5b62014-02-10 15:10:22 -0800252 // Rejects undefined motion actions.
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800253 event.initialize(DEVICE_ID, source, DISPLAY_ID,
Garfield Tan00f511d2019-06-12 16:55:40 -0700254 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification, 0, 0, 0, 0,
255 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
256 ARBITRARY_TIME, ARBITRARY_TIME,
257 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800258 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800259 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800260 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
261 << "Should reject motion events with undefined action.";
262
263 // Rejects pointer down with invalid index.
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800264 event.initialize(DEVICE_ID, source, DISPLAY_ID,
Garfield Tan00f511d2019-06-12 16:55:40 -0700265 AMOTION_EVENT_ACTION_POINTER_DOWN |
266 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
267 0, 0, edgeFlags, metaState, 0, classification, 0, 0, 0, 0,
268 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
269 ARBITRARY_TIME, ARBITRARY_TIME,
270 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800271 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800272 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800273 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
274 << "Should reject motion events with pointer down index too large.";
275
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800276 event.initialize(DEVICE_ID, source, DISPLAY_ID,
Garfield Tan00f511d2019-06-12 16:55:40 -0700277 AMOTION_EVENT_ACTION_POINTER_DOWN |
278 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
279 0, 0, edgeFlags, metaState, 0, classification, 0, 0, 0, 0,
280 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
281 ARBITRARY_TIME, ARBITRARY_TIME,
282 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800283 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800284 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800285 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
286 << "Should reject motion events with pointer down index too small.";
287
288 // Rejects pointer up with invalid index.
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800289 event.initialize(DEVICE_ID, source, DISPLAY_ID,
Garfield Tan00f511d2019-06-12 16:55:40 -0700290 AMOTION_EVENT_ACTION_POINTER_UP |
291 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
292 0, 0, edgeFlags, metaState, 0, classification, 0, 0, 0, 0,
293 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
294 ARBITRARY_TIME, ARBITRARY_TIME,
295 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800296 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800297 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800298 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
299 << "Should reject motion events with pointer up index too large.";
300
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800301 event.initialize(DEVICE_ID, source, DISPLAY_ID,
Garfield Tan00f511d2019-06-12 16:55:40 -0700302 AMOTION_EVENT_ACTION_POINTER_UP |
303 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
304 0, 0, edgeFlags, metaState, 0, classification, 0, 0, 0, 0,
305 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
306 ARBITRARY_TIME, ARBITRARY_TIME,
307 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800308 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800309 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800310 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
311 << "Should reject motion events with pointer up index too small.";
312
313 // Rejects motion events with invalid number of pointers.
Garfield Tan00f511d2019-06-12 16:55:40 -0700314 event.initialize(DEVICE_ID, source, DISPLAY_ID, AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags,
315 metaState, 0, classification, 0, 0, 0, 0,
316 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
317 ARBITRARY_TIME, ARBITRARY_TIME,
318 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800319 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800320 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800321 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
322 << "Should reject motion events with 0 pointers.";
323
Garfield Tan00f511d2019-06-12 16:55:40 -0700324 event.initialize(DEVICE_ID, source, DISPLAY_ID, AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags,
325 metaState, 0, classification, 0, 0, 0, 0,
326 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
327 ARBITRARY_TIME, ARBITRARY_TIME,
328 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800329 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800330 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800331 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
332 << "Should reject motion events with more than MAX_POINTERS pointers.";
333
334 // Rejects motion events with invalid pointer ids.
335 pointerProperties[0].id = -1;
Garfield Tan00f511d2019-06-12 16:55:40 -0700336 event.initialize(DEVICE_ID, source, DISPLAY_ID, AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags,
337 metaState, 0, classification, 0, 0, 0, 0,
338 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
339 ARBITRARY_TIME, ARBITRARY_TIME,
340 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800341 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800342 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800343 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
344 << "Should reject motion events with pointer ids less than 0.";
345
346 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tan00f511d2019-06-12 16:55:40 -0700347 event.initialize(DEVICE_ID, source, DISPLAY_ID, AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags,
348 metaState, 0, classification, 0, 0, 0, 0,
349 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
350 ARBITRARY_TIME, ARBITRARY_TIME,
351 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800352 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800353 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800354 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
355 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
356
357 // Rejects motion events with duplicate pointer ids.
358 pointerProperties[0].id = 1;
359 pointerProperties[1].id = 1;
Garfield Tan00f511d2019-06-12 16:55:40 -0700360 event.initialize(DEVICE_ID, source, DISPLAY_ID, AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags,
361 metaState, 0, classification, 0, 0, 0, 0,
362 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
363 ARBITRARY_TIME, ARBITRARY_TIME,
364 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800365 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800366 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800367 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
368 << "Should reject motion events with duplicate pointer ids.";
369}
370
Arthur Hungb92218b2018-08-14 12:00:21 +0800371// --- InputDispatcherTest SetInputWindowTest ---
372static const int32_t INJECT_EVENT_TIMEOUT = 500;
373static const int32_t DISPATCHING_TIMEOUT = 100;
374
375class FakeApplicationHandle : public InputApplicationHandle {
376public:
377 FakeApplicationHandle() {}
378 virtual ~FakeApplicationHandle() {}
379
380 virtual bool updateInfo() {
Arthur Hung7a0c39a2019-03-20 16:52:24 +0800381 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Arthur Hungb92218b2018-08-14 12:00:21 +0800382 return true;
383 }
384};
385
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800386class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800387public:
Tiger Huang8664f8c2018-10-11 19:14:35 +0800388 void consumeEvent(int32_t expectedEventType, int32_t expectedDisplayId,
389 int32_t expectedFlags = 0) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800390 uint32_t consumeSeq;
391 InputEvent* event;
392 status_t status = mConsumer->consume(&mEventFactory, false /*consumeBatches*/, -1,
393 &consumeSeq, &event);
394
395 ASSERT_EQ(OK, status)
396 << mName.c_str() << ": consumer consume should return OK.";
397 ASSERT_TRUE(event != nullptr)
398 << mName.c_str() << ": consumer should have returned non-NULL event.";
399 ASSERT_EQ(expectedEventType, event->getType())
Tiger Huang8664f8c2018-10-11 19:14:35 +0800400 << mName.c_str() << ": event type should match.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800401
402 ASSERT_EQ(expectedDisplayId, event->getDisplayId())
Tiger Huang8664f8c2018-10-11 19:14:35 +0800403 << mName.c_str() << ": event displayId should be the same as expected.";
404
405 int32_t flags;
406 switch (expectedEventType) {
407 case AINPUT_EVENT_TYPE_KEY: {
408 KeyEvent* typedEvent = static_cast<KeyEvent*>(event);
409 flags = typedEvent->getFlags();
410 break;
411 }
412 case AINPUT_EVENT_TYPE_MOTION: {
413 MotionEvent* typedEvent = static_cast<MotionEvent*>(event);
414 flags = typedEvent->getFlags();
415 break;
416 }
417 default: {
418 FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType;
419 }
420 }
421 ASSERT_EQ(expectedFlags, flags)
422 << mName.c_str() << ": event flags should be the same as expected.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800423
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800424 status = mConsumer->sendFinishedSignal(consumeSeq, handled());
Arthur Hungb92218b2018-08-14 12:00:21 +0800425 ASSERT_EQ(OK, status)
426 << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
427 }
428
429 void assertNoEvents() {
430 uint32_t consumeSeq;
431 InputEvent* event;
432 status_t status = mConsumer->consume(&mEventFactory, false /*consumeBatches*/, -1,
433 &consumeSeq, &event);
434 ASSERT_NE(OK, status)
435 << mName.c_str()
436 << ": should not have received any events, so consume(..) should not return OK.";
437 }
438
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800439protected:
440 explicit FakeInputReceiver(const sp<InputDispatcher>& dispatcher,
441 const std::string name, int32_t displayId) :
442 mDispatcher(dispatcher), mName(name), mDisplayId(displayId) {
443 InputChannel::openInputChannelPair(name, mServerChannel, mClientChannel);
444 mConsumer = new InputConsumer(mClientChannel);
445 }
446
447 virtual ~FakeInputReceiver() {
448 }
449
450 // return true if the event has been handled.
451 virtual bool handled() {
452 return false;
453 }
454
Arthur Hungb92218b2018-08-14 12:00:21 +0800455 sp<InputDispatcher> mDispatcher;
456 sp<InputChannel> mServerChannel, mClientChannel;
457 InputConsumer *mConsumer;
458 PreallocatedInputEventFactory mEventFactory;
459
460 std::string mName;
Arthur Hungb92218b2018-08-14 12:00:21 +0800461 int32_t mDisplayId;
462};
463
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800464class FakeWindowHandle : public InputWindowHandle, public FakeInputReceiver {
465public:
466 static const int32_t WIDTH = 600;
467 static const int32_t HEIGHT = 800;
468
469 FakeWindowHandle(const sp<InputApplicationHandle>& inputApplicationHandle,
470 const sp<InputDispatcher>& dispatcher, const std::string name, int32_t displayId) :
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800471 FakeInputReceiver(dispatcher, name, displayId),
chaviwfd6d3512019-03-25 13:23:49 -0700472 mFocused(false), mFrame(Rect(0, 0, WIDTH, HEIGHT)), mLayoutParamFlags(0) {
Siarhei Vishniakou7c34b232019-10-11 19:08:48 -0700473 mDispatcher->registerInputChannel(mServerChannel);
chaviwfd6d3512019-03-25 13:23:49 -0700474
Robert Carr740167f2018-10-11 19:03:41 -0700475 inputApplicationHandle->updateInfo();
476 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800477 }
478
479 virtual bool updateInfo() {
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700480 mInfo.token = mServerChannel ? mServerChannel->getConnectionToken() : nullptr;
Arthur Hung3b413f22018-10-26 18:05:34 +0800481 mInfo.name = mName;
chaviwfd6d3512019-03-25 13:23:49 -0700482 mInfo.layoutParamsFlags = mLayoutParamFlags;
Arthur Hung3b413f22018-10-26 18:05:34 +0800483 mInfo.layoutParamsType = InputWindowInfo::TYPE_APPLICATION;
484 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
chaviwfd6d3512019-03-25 13:23:49 -0700485 mInfo.frameLeft = mFrame.left;
486 mInfo.frameTop = mFrame.top;
487 mInfo.frameRight = mFrame.right;
488 mInfo.frameBottom = mFrame.bottom;
Robert Carre07e1032018-11-26 12:55:53 -0800489 mInfo.globalScaleFactor = 1.0;
Garfield Tan00f511d2019-06-12 16:55:40 -0700490 mInfo.touchableRegion.clear();
chaviwfd6d3512019-03-25 13:23:49 -0700491 mInfo.addTouchableRegion(mFrame);
Arthur Hung3b413f22018-10-26 18:05:34 +0800492 mInfo.visible = true;
493 mInfo.canReceiveKeys = true;
494 mInfo.hasFocus = mFocused;
495 mInfo.hasWallpaper = false;
496 mInfo.paused = false;
497 mInfo.layer = 0;
498 mInfo.ownerPid = INJECTOR_PID;
499 mInfo.ownerUid = INJECTOR_UID;
500 mInfo.inputFeatures = 0;
501 mInfo.displayId = mDisplayId;
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800502
503 return true;
504 }
505
506 void setFocus() {
507 mFocused = true;
508 }
Arthur Hung832bc4a2019-01-28 11:43:17 +0800509
chaviwfd6d3512019-03-25 13:23:49 -0700510 void setFrame(const Rect& frame) {
511 mFrame.set(frame);
512 }
513
514 void setLayoutParamFlags(int32_t flags) {
515 mLayoutParamFlags = flags;
516 }
517
Arthur Hung832bc4a2019-01-28 11:43:17 +0800518 void releaseChannel() {
Arthur Hung6b5a2b92019-01-31 16:39:28 +0800519 mServerChannel.clear();
Arthur Hung832bc4a2019-01-28 11:43:17 +0800520 InputWindowHandle::releaseChannel();
Arthur Hung832bc4a2019-01-28 11:43:17 +0800521 }
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800522protected:
523 virtual bool handled() {
524 return true;
525 }
526
527 bool mFocused;
chaviwfd6d3512019-03-25 13:23:49 -0700528 Rect mFrame;
529 int32_t mLayoutParamFlags;
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800530};
531
Tiger Huang721e26f2018-07-24 22:26:19 +0800532static int32_t injectKeyDown(const sp<InputDispatcher>& dispatcher,
533 int32_t displayId = ADISPLAY_ID_NONE) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800534 KeyEvent event;
535 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
536
537 // Define a valid key down event.
Tiger Huang721e26f2018-07-24 22:26:19 +0800538 event.initialize(DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Arthur Hungb92218b2018-08-14 12:00:21 +0800539 AKEY_EVENT_ACTION_DOWN, /* flags */ 0,
540 AKEYCODE_A, KEY_A, AMETA_NONE, /* repeatCount */ 0, currentTime, currentTime);
541
542 // Inject event until dispatch out.
543 return dispatcher->injectInputEvent(
544 &event,
545 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT,
546 INJECT_EVENT_TIMEOUT, POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
547}
548
Garfield Tan00f511d2019-06-12 16:55:40 -0700549static int32_t injectMotionEvent(const sp<InputDispatcher>& dispatcher, int32_t action,
550 int32_t source, int32_t displayId, int32_t x, int32_t y,
551 int32_t xCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION,
552 int32_t yCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800553 MotionEvent event;
554 PointerProperties pointerProperties[1];
555 PointerCoords pointerCoords[1];
556
557 pointerProperties[0].clear();
558 pointerProperties[0].id = 0;
559 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
560
561 pointerCoords[0].clear();
chaviwfd6d3512019-03-25 13:23:49 -0700562 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
563 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Arthur Hungb92218b2018-08-14 12:00:21 +0800564
565 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
566 // Define a valid motion down event.
Garfield Tan00f511d2019-06-12 16:55:40 -0700567 event.initialize(DEVICE_ID, source, displayId, action, /* actionButton */ 0, /* flags */ 0,
568 /* edgeFlags */ 0, AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
569 /* xOffset */ 0, /* yOffset */ 0, /* xPrecision */ 0,
570 /* yPrecision */ 0, xCursorPosition, yCursorPosition, currentTime, currentTime,
571 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Arthur Hungb92218b2018-08-14 12:00:21 +0800572
573 // Inject event until dispatch out.
574 return dispatcher->injectInputEvent(
575 &event,
576 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT,
577 INJECT_EVENT_TIMEOUT, POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
578}
579
Garfield Tan00f511d2019-06-12 16:55:40 -0700580static int32_t injectMotionDown(const sp<InputDispatcher>& dispatcher, int32_t source,
581 int32_t displayId, int32_t x = 100, int32_t y = 200) {
582 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, x, y);
583}
584
Jackal Guof9696682018-10-05 12:23:23 +0800585static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
586 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
587 // Define a valid key event.
588 NotifyKeyArgs args(/* sequenceNum */ 0, currentTime, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
589 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0,
590 AKEYCODE_A, KEY_A, AMETA_NONE, currentTime);
591
592 return args;
593}
594
595static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
596 PointerProperties pointerProperties[1];
597 PointerCoords pointerCoords[1];
598
599 pointerProperties[0].clear();
600 pointerProperties[0].id = 0;
601 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
602
603 pointerCoords[0].clear();
604 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
605 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 200);
606
607 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
608 // Define a valid motion event.
609 NotifyMotionArgs args(/* sequenceNum */ 0, currentTime, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -0700610 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
611 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
Atif Niyaz21da0ff2019-06-28 13:22:51 -0700612 AMOTION_EVENT_EDGE_FLAG_NONE, 1, pointerProperties, pointerCoords,
613 /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -0700614 AMOTION_EVENT_INVALID_CURSOR_POSITION,
615 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +0800616
617 return args;
618}
619
Arthur Hungb92218b2018-08-14 12:00:21 +0800620TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
621 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800622 sp<FakeWindowHandle> window = new FakeWindowHandle(application, mDispatcher, "Fake Window",
623 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800624
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800625 std::vector<sp<InputWindowHandle>> inputWindowHandles;
626 inputWindowHandles.push_back(window);
Arthur Hungb92218b2018-08-14 12:00:21 +0800627
628 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800629 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
630 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +0800631 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
632
633 // Window should receive motion event.
634 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_DEFAULT);
635}
636
637// The foreground window should receive the first touch down event.
638TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
639 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800640 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
641 ADISPLAY_ID_DEFAULT);
642 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
643 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800644
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800645 std::vector<sp<InputWindowHandle>> inputWindowHandles;
646 inputWindowHandles.push_back(windowTop);
647 inputWindowHandles.push_back(windowSecond);
Arthur Hungb92218b2018-08-14 12:00:21 +0800648
649 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800650 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
651 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +0800652 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
653
654 // Top window should receive the touch down event. Second window should not receive anything.
655 windowTop->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_DEFAULT);
656 windowSecond->assertNoEvents();
657}
658
659TEST_F(InputDispatcherTest, SetInputWindow_FocusedWindow) {
660 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800661 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
662 ADISPLAY_ID_DEFAULT);
663 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
664 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800665
Arthur Hung7ab76b12019-01-09 19:17:20 +0800666 // Set focused application.
Tiger Huang721e26f2018-07-24 22:26:19 +0800667 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Arthur Hungb92218b2018-08-14 12:00:21 +0800668
669 // Expect one focus window exist in display.
670 windowSecond->setFocus();
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800671 std::vector<sp<InputWindowHandle>> inputWindowHandles;
672 inputWindowHandles.push_back(windowTop);
673 inputWindowHandles.push_back(windowSecond);
Arthur Hungb92218b2018-08-14 12:00:21 +0800674
675 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
676 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
677 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
678
679 // Focused window should receive event.
680 windowTop->assertNoEvents();
681 windowSecond->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE);
682}
683
Arthur Hung7ab76b12019-01-09 19:17:20 +0800684TEST_F(InputDispatcherTest, SetInputWindow_FocusPriority) {
685 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
686 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
687 ADISPLAY_ID_DEFAULT);
688 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
689 ADISPLAY_ID_DEFAULT);
690
691 // Set focused application.
692 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
693
694 // Display has two focused windows. Add them to inputWindowsHandles in z-order (top most first)
695 windowTop->setFocus();
696 windowSecond->setFocus();
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800697 std::vector<sp<InputWindowHandle>> inputWindowHandles;
698 inputWindowHandles.push_back(windowTop);
699 inputWindowHandles.push_back(windowSecond);
Arthur Hung7ab76b12019-01-09 19:17:20 +0800700
701 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
702 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
703 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
704
705 // Top focused window should receive event.
706 windowTop->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE);
707 windowSecond->assertNoEvents();
708}
709
Arthur Hung3b413f22018-10-26 18:05:34 +0800710TEST_F(InputDispatcherTest, SetInputWindow_InputWindowInfo) {
711 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
712
713 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
714 ADISPLAY_ID_DEFAULT);
715 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
716 ADISPLAY_ID_DEFAULT);
717
Arthur Hung832bc4a2019-01-28 11:43:17 +0800718 // Set focused application.
719 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Arthur Hung3b413f22018-10-26 18:05:34 +0800720
Arthur Hung832bc4a2019-01-28 11:43:17 +0800721 windowTop->setFocus();
722 windowSecond->setFocus();
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800723 std::vector<sp<InputWindowHandle>> inputWindowHandles;
724 inputWindowHandles.push_back(windowTop);
725 inputWindowHandles.push_back(windowSecond);
Arthur Hung3b413f22018-10-26 18:05:34 +0800726 // Release channel for window is no longer valid.
727 windowTop->releaseChannel();
Arthur Hung832bc4a2019-01-28 11:43:17 +0800728 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
Arthur Hung3b413f22018-10-26 18:05:34 +0800729
Arthur Hung832bc4a2019-01-28 11:43:17 +0800730 // Test inject a key down, should dispatch to a valid window.
731 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
732 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Arthur Hung3b413f22018-10-26 18:05:34 +0800733
734 // Top window is invalid, so it should not receive any input event.
735 windowTop->assertNoEvents();
Arthur Hung832bc4a2019-01-28 11:43:17 +0800736 windowSecond->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE);
Arthur Hung3b413f22018-10-26 18:05:34 +0800737}
738
Garfield Tan00f511d2019-06-12 16:55:40 -0700739TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
740 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
741
742 sp<FakeWindowHandle> windowLeft =
743 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
744 windowLeft->setFrame(Rect(0, 0, 600, 800));
745 windowLeft->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL);
746 sp<FakeWindowHandle> windowRight =
747 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
748 windowRight->setFrame(Rect(600, 0, 1200, 800));
749 windowRight->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL);
750
751 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
752
753 std::vector<sp<InputWindowHandle>> inputWindowHandles{windowLeft, windowRight};
754 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
755
756 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
757 // left window. This event should be dispatched to the left window.
758 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
759 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
760 ADISPLAY_ID_DEFAULT, 610, 400, 599, 400));
761 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_DEFAULT);
762 windowRight->assertNoEvents();
763}
764
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800765/* Test InputDispatcher for MultiDisplay */
766class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
767public:
768 static constexpr int32_t SECOND_DISPLAY_ID = 1;
769 virtual void SetUp() {
770 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +0800771
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800772 application1 = new FakeApplicationHandle();
773 windowInPrimary = new FakeWindowHandle(application1, mDispatcher, "D_1",
774 ADISPLAY_ID_DEFAULT);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800775 std::vector<sp<InputWindowHandle>> inputWindowHandles;
776 inputWindowHandles.push_back(windowInPrimary);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800777 // Set focus window for primary display, but focused display would be second one.
778 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
779 windowInPrimary->setFocus();
780 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800781
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800782 application2 = new FakeApplicationHandle();
783 windowInSecondary = new FakeWindowHandle(application2, mDispatcher, "D_2",
784 SECOND_DISPLAY_ID);
785 // Set focus to second display window.
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800786 std::vector<sp<InputWindowHandle>> inputWindowHandles_Second;
787 inputWindowHandles_Second.push_back(windowInSecondary);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800788 // Set focus display to second one.
789 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
790 // Set focus window for second display.
791 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
792 windowInSecondary->setFocus();
793 mDispatcher->setInputWindows(inputWindowHandles_Second, SECOND_DISPLAY_ID);
794 }
795
796 virtual void TearDown() {
797 InputDispatcherTest::TearDown();
798
799 application1.clear();
800 windowInPrimary.clear();
801 application2.clear();
802 windowInSecondary.clear();
803 }
804
805protected:
806 sp<FakeApplicationHandle> application1;
807 sp<FakeWindowHandle> windowInPrimary;
808 sp<FakeApplicationHandle> application2;
809 sp<FakeWindowHandle> windowInSecondary;
810};
811
812TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
813 // Test touch down on primary display.
814 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
815 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +0800816 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
817 windowInPrimary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_DEFAULT);
818 windowInSecondary->assertNoEvents();
819
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800820 // Test touch down on second display.
821 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
822 AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Arthur Hungb92218b2018-08-14 12:00:21 +0800823 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
824 windowInPrimary->assertNoEvents();
825 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, SECOND_DISPLAY_ID);
826}
827
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800828TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +0800829 // Test inject a key down with display id specified.
830 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
831 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
832 windowInPrimary->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_DEFAULT);
833 windowInSecondary->assertNoEvents();
834
835 // Test inject a key down without display id specified.
Arthur Hungb92218b2018-08-14 12:00:21 +0800836 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
837 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
838 windowInPrimary->assertNoEvents();
839 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE);
840
841 // Remove secondary display.
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800842 std::vector<sp<InputWindowHandle>> noWindows;
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800843 mDispatcher->setInputWindows(noWindows, SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +0800844
845 // Expect old focus should receive a cancel event.
Tiger Huang8664f8c2018-10-11 19:14:35 +0800846 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE,
847 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +0800848
849 // Test inject a key down, should timeout because of no target window.
850 ASSERT_EQ(INPUT_EVENT_INJECTION_TIMED_OUT, injectKeyDown(mDispatcher))
851 << "Inject key event should return INPUT_EVENT_INJECTION_TIMED_OUT";
852 windowInPrimary->assertNoEvents();
853 windowInSecondary->assertNoEvents();
854}
855
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800856class FakeMonitorReceiver : public FakeInputReceiver, public RefBase {
857public:
858 FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name,
Michael Wright3dd60e22019-03-27 22:06:44 +0000859 int32_t displayId, bool isGestureMonitor = false)
860 : FakeInputReceiver(dispatcher, name, displayId) {
Michael Wright3dd60e22019-03-27 22:06:44 +0000861 mDispatcher->registerInputMonitor(mServerChannel, displayId, isGestureMonitor);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800862 }
863};
864
865// Test per-display input monitors for motion event.
866TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
867 sp<FakeMonitorReceiver> monitorInPrimary =
868 new FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
869 sp<FakeMonitorReceiver> monitorInSecondary =
870 new FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
871
872 // Test touch down on primary display.
873 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
874 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
875 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
876 windowInPrimary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_DEFAULT);
877 monitorInPrimary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_DEFAULT);
878 windowInSecondary->assertNoEvents();
879 monitorInSecondary->assertNoEvents();
880
881 // Test touch down on second display.
882 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
883 AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
884 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
885 windowInPrimary->assertNoEvents();
886 monitorInPrimary->assertNoEvents();
887 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, SECOND_DISPLAY_ID);
888 monitorInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, SECOND_DISPLAY_ID);
889
890 // Test inject a non-pointer motion event.
891 // If specific a display, it will dispatch to the focused window of particular display,
892 // or it will dispatch to the focused window of focused display.
893 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
894 AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
895 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
896 windowInPrimary->assertNoEvents();
897 monitorInPrimary->assertNoEvents();
898 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_NONE);
899 monitorInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_NONE);
900}
901
902// Test per-display input monitors for key event.
903TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
904 //Input monitor per display.
905 sp<FakeMonitorReceiver> monitorInPrimary =
906 new FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
907 sp<FakeMonitorReceiver> monitorInSecondary =
908 new FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
909
910 // Test inject a key down.
911 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
912 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
913 windowInPrimary->assertNoEvents();
914 monitorInPrimary->assertNoEvents();
915 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE);
916 monitorInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE);
917}
918
Jackal Guof9696682018-10-05 12:23:23 +0800919class InputFilterTest : public InputDispatcherTest {
920protected:
921 static constexpr int32_t SECOND_DISPLAY_ID = 1;
922
923 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) {
924 NotifyMotionArgs motionArgs;
925
926 motionArgs = generateMotionArgs(
927 AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
928 mDispatcher->notifyMotion(&motionArgs);
929 motionArgs = generateMotionArgs(
930 AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
931 mDispatcher->notifyMotion(&motionArgs);
932
933 if (expectToBeFiltered) {
934 mFakePolicy->assertFilterInputEventWasCalledWithExpectedArgs(&motionArgs);
935 } else {
936 mFakePolicy->assertFilterInputEventWasNotCalled();
937 }
938 }
939
940 void testNotifyKey(bool expectToBeFiltered) {
941 NotifyKeyArgs keyArgs;
942
943 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
944 mDispatcher->notifyKey(&keyArgs);
945 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
946 mDispatcher->notifyKey(&keyArgs);
947
948 if (expectToBeFiltered) {
949 mFakePolicy->assertFilterInputEventWasCalledWithExpectedArgs(&keyArgs);
950 } else {
951 mFakePolicy->assertFilterInputEventWasNotCalled();
952 }
953 }
954};
955
956// Test InputFilter for MotionEvent
957TEST_F(InputFilterTest, MotionEvent_InputFilter) {
958 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
959 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
960 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
961
962 // Enable InputFilter
963 mDispatcher->setInputFilterEnabled(true);
964 // Test touch on both primary and second display, and check if both events are filtered.
965 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
966 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
967
968 // Disable InputFilter
969 mDispatcher->setInputFilterEnabled(false);
970 // Test touch on both primary and second display, and check if both events aren't filtered.
971 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
972 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
973}
974
975// Test InputFilter for KeyEvent
976TEST_F(InputFilterTest, KeyEvent_InputFilter) {
977 // Since the InputFilter is disabled by default, check if key event aren't filtered.
978 testNotifyKey(/*expectToBeFiltered*/ false);
979
980 // Enable InputFilter
981 mDispatcher->setInputFilterEnabled(true);
982 // Send a key event, and check if it is filtered.
983 testNotifyKey(/*expectToBeFiltered*/ true);
984
985 // Disable InputFilter
986 mDispatcher->setInputFilterEnabled(false);
987 // Send a key event, and check if it isn't filtered.
988 testNotifyKey(/*expectToBeFiltered*/ false);
989}
990
chaviwfd6d3512019-03-25 13:23:49 -0700991class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
992 virtual void SetUp() {
993 InputDispatcherTest::SetUp();
994
995 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
996 mUnfocusedWindow = new FakeWindowHandle(application, mDispatcher, "Top",
997 ADISPLAY_ID_DEFAULT);
998 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
999 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
1000 // window.
1001 mUnfocusedWindow->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL);
1002
1003 mWindowFocused = new FakeWindowHandle(application, mDispatcher, "Second",
1004 ADISPLAY_ID_DEFAULT);
1005 mWindowFocused->setFrame(Rect(50, 50, 100, 100));
1006 mWindowFocused->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL);
1007 mWindowFocusedTouchPoint = 60;
1008
1009 // Set focused application.
1010 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1011 mWindowFocused->setFocus();
1012
1013 // Expect one focus window exist in display.
1014 std::vector<sp<InputWindowHandle>> inputWindowHandles;
1015 inputWindowHandles.push_back(mUnfocusedWindow);
1016 inputWindowHandles.push_back(mWindowFocused);
1017 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
1018 }
1019
1020 virtual void TearDown() {
1021 InputDispatcherTest::TearDown();
1022
1023 mUnfocusedWindow.clear();
1024 mWindowFocused.clear();
1025 }
1026
1027protected:
1028 sp<FakeWindowHandle> mUnfocusedWindow;
1029 sp<FakeWindowHandle> mWindowFocused;
1030 int32_t mWindowFocusedTouchPoint;
1031};
1032
1033// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
1034// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
1035// the onPointerDownOutsideFocus callback.
1036TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
1037 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1038 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, 20, 20))
1039 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1040 // Call monitor to wait for the command queue to get flushed.
1041 mDispatcher->monitor();
1042
1043 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
1044}
1045
1046// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
1047// DOWN on the window that doesn't have focus. Ensure no window received the
1048// onPointerDownOutsideFocus callback.
1049TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
1050 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1051 AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, 20, 20))
1052 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1053 // Call monitor to wait for the command queue to get flushed.
1054 mDispatcher->monitor();
1055
1056 mFakePolicy->assertOnPointerDownEquals(nullptr);
1057}
1058
1059// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
1060// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
1061TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
1062 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
1063 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1064 // Call monitor to wait for the command queue to get flushed.
1065 mDispatcher->monitor();
1066
1067 mFakePolicy->assertOnPointerDownEquals(nullptr);
1068}
1069
1070// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
1071// DOWN on the window that already has focus. Ensure no window received the
1072// onPointerDownOutsideFocus callback.
1073TEST_F(InputDispatcherOnPointerDownOutsideFocus,
1074 OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
1075 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1076 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, mWindowFocusedTouchPoint,
1077 mWindowFocusedTouchPoint))
1078 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1079 // Call monitor to wait for the command queue to get flushed.
1080 mDispatcher->monitor();
1081
1082 mFakePolicy->assertOnPointerDownEquals(nullptr);
1083}
1084
Garfield Tane84e6f92019-08-29 17:28:41 -07001085} // namespace android::inputdispatcher