blob: 2a29e0dbca8805fa24091df8c153668b42f6d76b [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() {
chaviwfd6d3512019-03-25 13:23:49 -070053 mOnPointerDownToken.clear();
Jackal Guof9696682018-10-05 12:23:23 +080054 }
55
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080056 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
57 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
58 ASSERT_EQ(mFilteredEvent->getType(), AINPUT_EVENT_TYPE_KEY);
Jackal Guof9696682018-10-05 12:23:23 +080059
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080060 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*mFilteredEvent);
61 ASSERT_EQ(keyEvent.getEventTime(), args.eventTime);
62 ASSERT_EQ(keyEvent.getAction(), args.action);
63 ASSERT_EQ(keyEvent.getDisplayId(), args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080064
65 reset();
66 }
67
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080068 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args) {
69 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
70 ASSERT_EQ(mFilteredEvent->getType(), AINPUT_EVENT_TYPE_MOTION);
Jackal Guof9696682018-10-05 12:23:23 +080071
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080072 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*mFilteredEvent);
73 ASSERT_EQ(motionEvent.getEventTime(), args.eventTime);
74 ASSERT_EQ(motionEvent.getAction(), args.action);
75 ASSERT_EQ(motionEvent.getDisplayId(), args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080076
77 reset();
78 }
79
80 void assertFilterInputEventWasNotCalled() {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080081 ASSERT_EQ(nullptr, mFilteredEvent)
Jackal Guof9696682018-10-05 12:23:23 +080082 << "Expected filterInputEvent() to not have been called.";
Michael Wrightd02c5b62014-02-10 15:10:22 -080083 }
84
chaviwfd6d3512019-03-25 13:23:49 -070085 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
86 ASSERT_EQ(mOnPointerDownToken, touchedToken)
87 << "Expected token from onPointerDownOutsideFocus was not matched";
88 reset();
89 }
90
Michael Wrightd02c5b62014-02-10 15:10:22 -080091private:
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080092 std::unique_ptr<InputEvent> mFilteredEvent;
chaviwfd6d3512019-03-25 13:23:49 -070093 sp<IBinder> mOnPointerDownToken;
Jackal Guof9696682018-10-05 12:23:23 +080094
Narayan Kamath39efe3e2014-10-17 10:37:08 +010095 virtual void notifyConfigurationChanged(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080096 }
97
Narayan Kamath39efe3e2014-10-17 10:37:08 +010098 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>&,
Robert Carr803535b2018-08-02 16:38:15 -070099 const sp<IBinder>&,
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800100 const std::string&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800101 return 0;
102 }
103
Robert Carr803535b2018-08-02 16:38:15 -0700104 virtual void notifyInputChannelBroken(const sp<IBinder>&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800105 }
106
chaviw0c06c6e2019-01-09 13:27:07 -0800107 virtual void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) {
Robert Carr740167f2018-10-11 19:03:41 -0700108 }
109
Michael Wrightd02c5b62014-02-10 15:10:22 -0800110 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
111 *outConfig = mConfig;
112 }
113
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800114 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Jackal Guof9696682018-10-05 12:23:23 +0800115 switch (inputEvent->getType()) {
116 case AINPUT_EVENT_TYPE_KEY: {
117 const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800118 mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800119 break;
120 }
121
122 case AINPUT_EVENT_TYPE_MOTION: {
123 const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800124 mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800125 break;
126 }
127 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800128 return true;
129 }
130
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100131 virtual void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800132 }
133
Charles Chen3611f1f2019-01-29 17:26:18 +0800134 virtual void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800135 }
136
Robert Carr803535b2018-08-02 16:38:15 -0700137 virtual nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&,
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100138 const KeyEvent*, uint32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800139 return 0;
140 }
141
Robert Carr803535b2018-08-02 16:38:15 -0700142 virtual bool dispatchUnhandledKey(const sp<IBinder>&,
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100143 const KeyEvent*, uint32_t, KeyEvent*) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800144 return false;
145 }
146
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100147 virtual void notifySwitch(nsecs_t, uint32_t, uint32_t, uint32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800148 }
149
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100150 virtual void pokeUserActivity(nsecs_t, int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800151 }
152
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100153 virtual bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800154 return false;
155 }
Jackal Guof9696682018-10-05 12:23:23 +0800156
chaviwfd6d3512019-03-25 13:23:49 -0700157 virtual void onPointerDownOutsideFocus(const sp<IBinder>& newToken) {
158 mOnPointerDownToken = newToken;
159 }
160
Jackal Guof9696682018-10-05 12:23:23 +0800161 void reset() {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800162 mFilteredEvent = nullptr;
chaviwfd6d3512019-03-25 13:23:49 -0700163 mOnPointerDownToken.clear();
Jackal Guof9696682018-10-05 12:23:23 +0800164 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800165};
166
167
168// --- InputDispatcherTest ---
169
170class InputDispatcherTest : public testing::Test {
171protected:
172 sp<FakeInputDispatcherPolicy> mFakePolicy;
173 sp<InputDispatcher> mDispatcher;
Arthur Hungb92218b2018-08-14 12:00:21 +0800174 sp<InputDispatcherThread> mDispatcherThread;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800175
176 virtual void SetUp() {
177 mFakePolicy = new FakeInputDispatcherPolicy();
178 mDispatcher = new InputDispatcher(mFakePolicy);
Arthur Hungb92218b2018-08-14 12:00:21 +0800179 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
180 //Start InputDispatcher thread
181 mDispatcherThread = new InputDispatcherThread(mDispatcher);
182 mDispatcherThread->run("InputDispatcherTest", PRIORITY_URGENT_DISPLAY);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800183 }
184
185 virtual void TearDown() {
Arthur Hungb92218b2018-08-14 12:00:21 +0800186 mDispatcherThread->requestExit();
187 mDispatcherThread.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188 mFakePolicy.clear();
189 mDispatcher.clear();
190 }
191};
192
193
194TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
195 KeyEvent event;
196
197 // Rejects undefined key actions.
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100198 event.initialize(DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800199 /*action*/ -1, 0,
200 AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, ARBITRARY_TIME);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800201 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800202 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800203 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
204 << "Should reject key events with undefined action.";
205
206 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100207 event.initialize(DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800208 AKEY_EVENT_ACTION_MULTIPLE, 0,
209 AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, ARBITRARY_TIME);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800210 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800211 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800212 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
213 << "Should reject key events with ACTION_MULTIPLE.";
214}
215
216TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
217 MotionEvent event;
218 PointerProperties pointerProperties[MAX_POINTERS + 1];
219 PointerCoords pointerCoords[MAX_POINTERS + 1];
220 for (int i = 0; i <= MAX_POINTERS; i++) {
221 pointerProperties[i].clear();
222 pointerProperties[i].id = i;
223 pointerCoords[i].clear();
224 }
225
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800226 // Some constants commonly used below
227 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
228 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
229 constexpr int32_t metaState = AMETA_NONE;
230 constexpr MotionClassification classification = MotionClassification::NONE;
231
Michael Wrightd02c5b62014-02-10 15:10:22 -0800232 // Rejects undefined motion actions.
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800233 event.initialize(DEVICE_ID, source, DISPLAY_ID,
Garfield Tan00f511d2019-06-12 16:55:40 -0700234 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification, 0, 0, 0, 0,
235 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
236 ARBITRARY_TIME, ARBITRARY_TIME,
237 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800238 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800239 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800240 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
241 << "Should reject motion events with undefined action.";
242
243 // Rejects pointer down with invalid index.
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800244 event.initialize(DEVICE_ID, source, DISPLAY_ID,
Garfield Tan00f511d2019-06-12 16:55:40 -0700245 AMOTION_EVENT_ACTION_POINTER_DOWN |
246 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
247 0, 0, edgeFlags, metaState, 0, classification, 0, 0, 0, 0,
248 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
249 ARBITRARY_TIME, ARBITRARY_TIME,
250 /*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,
Garfield Tan00f511d2019-06-12 16:55:40 -0700257 AMOTION_EVENT_ACTION_POINTER_DOWN |
258 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
259 0, 0, edgeFlags, metaState, 0, classification, 0, 0, 0, 0,
260 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
261 ARBITRARY_TIME, ARBITRARY_TIME,
262 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800263 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800264 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800265 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
266 << "Should reject motion events with pointer down index too small.";
267
268 // Rejects pointer up with invalid index.
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800269 event.initialize(DEVICE_ID, source, DISPLAY_ID,
Garfield Tan00f511d2019-06-12 16:55:40 -0700270 AMOTION_EVENT_ACTION_POINTER_UP |
271 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
272 0, 0, edgeFlags, metaState, 0, classification, 0, 0, 0, 0,
273 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
274 ARBITRARY_TIME, ARBITRARY_TIME,
275 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800276 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800277 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800278 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
279 << "Should reject motion events with pointer up index too large.";
280
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800281 event.initialize(DEVICE_ID, source, DISPLAY_ID,
Garfield Tan00f511d2019-06-12 16:55:40 -0700282 AMOTION_EVENT_ACTION_POINTER_UP |
283 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
284 0, 0, edgeFlags, metaState, 0, classification, 0, 0, 0, 0,
285 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
286 ARBITRARY_TIME, ARBITRARY_TIME,
287 /*pointerCount*/ 1, 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 pointer up index too small.";
292
293 // Rejects motion events with invalid number of pointers.
Garfield Tan00f511d2019-06-12 16:55:40 -0700294 event.initialize(DEVICE_ID, source, DISPLAY_ID, AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags,
295 metaState, 0, classification, 0, 0, 0, 0,
296 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
297 ARBITRARY_TIME, ARBITRARY_TIME,
298 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800299 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800300 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800301 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
302 << "Should reject motion events with 0 pointers.";
303
Garfield Tan00f511d2019-06-12 16:55:40 -0700304 event.initialize(DEVICE_ID, source, DISPLAY_ID, AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags,
305 metaState, 0, classification, 0, 0, 0, 0,
306 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
307 ARBITRARY_TIME, ARBITRARY_TIME,
308 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800309 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800310 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800311 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
312 << "Should reject motion events with more than MAX_POINTERS pointers.";
313
314 // Rejects motion events with invalid pointer ids.
315 pointerProperties[0].id = -1;
Garfield Tan00f511d2019-06-12 16:55:40 -0700316 event.initialize(DEVICE_ID, source, DISPLAY_ID, AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags,
317 metaState, 0, classification, 0, 0, 0, 0,
318 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
319 ARBITRARY_TIME, ARBITRARY_TIME,
320 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800321 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800322 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800323 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
324 << "Should reject motion events with pointer ids less than 0.";
325
326 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tan00f511d2019-06-12 16:55:40 -0700327 event.initialize(DEVICE_ID, source, DISPLAY_ID, AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags,
328 metaState, 0, classification, 0, 0, 0, 0,
329 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
330 ARBITRARY_TIME, ARBITRARY_TIME,
331 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800332 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800333 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800334 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
335 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
336
337 // Rejects motion events with duplicate pointer ids.
338 pointerProperties[0].id = 1;
339 pointerProperties[1].id = 1;
Garfield Tan00f511d2019-06-12 16:55:40 -0700340 event.initialize(DEVICE_ID, source, DISPLAY_ID, AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags,
341 metaState, 0, classification, 0, 0, 0, 0,
342 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
343 ARBITRARY_TIME, ARBITRARY_TIME,
344 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800345 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800346 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800347 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
348 << "Should reject motion events with duplicate pointer ids.";
349}
350
Arthur Hungb92218b2018-08-14 12:00:21 +0800351// --- InputDispatcherTest SetInputWindowTest ---
352static const int32_t INJECT_EVENT_TIMEOUT = 500;
353static const int32_t DISPATCHING_TIMEOUT = 100;
354
355class FakeApplicationHandle : public InputApplicationHandle {
356public:
357 FakeApplicationHandle() {}
358 virtual ~FakeApplicationHandle() {}
359
360 virtual bool updateInfo() {
Arthur Hung7a0c39a2019-03-20 16:52:24 +0800361 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Arthur Hungb92218b2018-08-14 12:00:21 +0800362 return true;
363 }
364};
365
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800366class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800367public:
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800368 void consumeEvent(int32_t expectedEventType, int32_t expectedAction, int32_t expectedDisplayId,
369 int32_t expectedFlags) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800370 uint32_t consumeSeq;
371 InputEvent* event;
372 status_t status = mConsumer->consume(&mEventFactory, false /*consumeBatches*/, -1,
373 &consumeSeq, &event);
374
375 ASSERT_EQ(OK, status)
376 << mName.c_str() << ": consumer consume should return OK.";
377 ASSERT_TRUE(event != nullptr)
378 << mName.c_str() << ": consumer should have returned non-NULL event.";
379 ASSERT_EQ(expectedEventType, event->getType())
Tiger Huang8664f8c2018-10-11 19:14:35 +0800380 << mName.c_str() << ": event type should match.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800381
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800382 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
Tiger Huang8664f8c2018-10-11 19:14:35 +0800383
Tiger Huang8664f8c2018-10-11 19:14:35 +0800384 switch (expectedEventType) {
385 case AINPUT_EVENT_TYPE_KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800386 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
387 EXPECT_EQ(expectedAction, keyEvent.getAction());
388 EXPECT_EQ(expectedFlags, keyEvent.getFlags());
Tiger Huang8664f8c2018-10-11 19:14:35 +0800389 break;
390 }
391 case AINPUT_EVENT_TYPE_MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800392 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
393 EXPECT_EQ(expectedAction, motionEvent.getAction());
394 EXPECT_EQ(expectedFlags, motionEvent.getFlags());
Tiger Huang8664f8c2018-10-11 19:14:35 +0800395 break;
396 }
397 default: {
398 FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType;
399 }
400 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800401
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800402 status = mConsumer->sendFinishedSignal(consumeSeq, handled());
Arthur Hungb92218b2018-08-14 12:00:21 +0800403 ASSERT_EQ(OK, status)
404 << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
405 }
406
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800407 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
408 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
409 expectedFlags);
410 }
411
412 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
413 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
414 expectedFlags);
415 }
416
Arthur Hungb92218b2018-08-14 12:00:21 +0800417 void assertNoEvents() {
418 uint32_t consumeSeq;
419 InputEvent* event;
420 status_t status = mConsumer->consume(&mEventFactory, false /*consumeBatches*/, -1,
421 &consumeSeq, &event);
422 ASSERT_NE(OK, status)
423 << mName.c_str()
424 << ": should not have received any events, so consume(..) should not return OK.";
425 }
426
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800427protected:
428 explicit FakeInputReceiver(const sp<InputDispatcher>& dispatcher,
429 const std::string name, int32_t displayId) :
430 mDispatcher(dispatcher), mName(name), mDisplayId(displayId) {
431 InputChannel::openInputChannelPair(name, mServerChannel, mClientChannel);
432 mConsumer = new InputConsumer(mClientChannel);
433 }
434
435 virtual ~FakeInputReceiver() {
436 }
437
438 // return true if the event has been handled.
439 virtual bool handled() {
440 return false;
441 }
442
Arthur Hungb92218b2018-08-14 12:00:21 +0800443 sp<InputDispatcher> mDispatcher;
444 sp<InputChannel> mServerChannel, mClientChannel;
445 InputConsumer *mConsumer;
446 PreallocatedInputEventFactory mEventFactory;
447
448 std::string mName;
Arthur Hungb92218b2018-08-14 12:00:21 +0800449 int32_t mDisplayId;
450};
451
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800452class FakeWindowHandle : public InputWindowHandle, public FakeInputReceiver {
453public:
454 static const int32_t WIDTH = 600;
455 static const int32_t HEIGHT = 800;
456
457 FakeWindowHandle(const sp<InputApplicationHandle>& inputApplicationHandle,
458 const sp<InputDispatcher>& dispatcher, const std::string name, int32_t displayId) :
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800459 FakeInputReceiver(dispatcher, name, displayId),
chaviwfd6d3512019-03-25 13:23:49 -0700460 mFocused(false), mFrame(Rect(0, 0, WIDTH, HEIGHT)), mLayoutParamFlags(0) {
Siarhei Vishniakou7c34b232019-10-11 19:08:48 -0700461 mDispatcher->registerInputChannel(mServerChannel);
chaviwfd6d3512019-03-25 13:23:49 -0700462
Robert Carr740167f2018-10-11 19:03:41 -0700463 inputApplicationHandle->updateInfo();
464 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800465 }
466
467 virtual bool updateInfo() {
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700468 mInfo.token = mServerChannel ? mServerChannel->getConnectionToken() : nullptr;
Arthur Hung3b413f22018-10-26 18:05:34 +0800469 mInfo.name = mName;
chaviwfd6d3512019-03-25 13:23:49 -0700470 mInfo.layoutParamsFlags = mLayoutParamFlags;
Arthur Hung3b413f22018-10-26 18:05:34 +0800471 mInfo.layoutParamsType = InputWindowInfo::TYPE_APPLICATION;
472 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
chaviwfd6d3512019-03-25 13:23:49 -0700473 mInfo.frameLeft = mFrame.left;
474 mInfo.frameTop = mFrame.top;
475 mInfo.frameRight = mFrame.right;
476 mInfo.frameBottom = mFrame.bottom;
Robert Carre07e1032018-11-26 12:55:53 -0800477 mInfo.globalScaleFactor = 1.0;
Garfield Tan00f511d2019-06-12 16:55:40 -0700478 mInfo.touchableRegion.clear();
chaviwfd6d3512019-03-25 13:23:49 -0700479 mInfo.addTouchableRegion(mFrame);
Arthur Hung3b413f22018-10-26 18:05:34 +0800480 mInfo.visible = true;
481 mInfo.canReceiveKeys = true;
482 mInfo.hasFocus = mFocused;
483 mInfo.hasWallpaper = false;
484 mInfo.paused = false;
485 mInfo.layer = 0;
486 mInfo.ownerPid = INJECTOR_PID;
487 mInfo.ownerUid = INJECTOR_UID;
488 mInfo.inputFeatures = 0;
489 mInfo.displayId = mDisplayId;
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800490
491 return true;
492 }
493
494 void setFocus() {
495 mFocused = true;
496 }
Arthur Hung832bc4a2019-01-28 11:43:17 +0800497
chaviwfd6d3512019-03-25 13:23:49 -0700498 void setFrame(const Rect& frame) {
499 mFrame.set(frame);
500 }
501
502 void setLayoutParamFlags(int32_t flags) {
503 mLayoutParamFlags = flags;
504 }
505
Arthur Hung832bc4a2019-01-28 11:43:17 +0800506 void releaseChannel() {
Arthur Hung6b5a2b92019-01-31 16:39:28 +0800507 mServerChannel.clear();
Arthur Hung832bc4a2019-01-28 11:43:17 +0800508 InputWindowHandle::releaseChannel();
Arthur Hung832bc4a2019-01-28 11:43:17 +0800509 }
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800510protected:
511 virtual bool handled() {
512 return true;
513 }
514
515 bool mFocused;
chaviwfd6d3512019-03-25 13:23:49 -0700516 Rect mFrame;
517 int32_t mLayoutParamFlags;
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800518};
519
Tiger Huang721e26f2018-07-24 22:26:19 +0800520static int32_t injectKeyDown(const sp<InputDispatcher>& dispatcher,
521 int32_t displayId = ADISPLAY_ID_NONE) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800522 KeyEvent event;
523 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
524
525 // Define a valid key down event.
Tiger Huang721e26f2018-07-24 22:26:19 +0800526 event.initialize(DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Arthur Hungb92218b2018-08-14 12:00:21 +0800527 AKEY_EVENT_ACTION_DOWN, /* flags */ 0,
528 AKEYCODE_A, KEY_A, AMETA_NONE, /* repeatCount */ 0, currentTime, currentTime);
529
530 // Inject event until dispatch out.
531 return dispatcher->injectInputEvent(
532 &event,
533 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT,
534 INJECT_EVENT_TIMEOUT, POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
535}
536
Garfield Tan00f511d2019-06-12 16:55:40 -0700537static int32_t injectMotionEvent(const sp<InputDispatcher>& dispatcher, int32_t action,
538 int32_t source, int32_t displayId, int32_t x, int32_t y,
539 int32_t xCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION,
540 int32_t yCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800541 MotionEvent event;
542 PointerProperties pointerProperties[1];
543 PointerCoords pointerCoords[1];
544
545 pointerProperties[0].clear();
546 pointerProperties[0].id = 0;
547 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
548
549 pointerCoords[0].clear();
chaviwfd6d3512019-03-25 13:23:49 -0700550 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
551 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Arthur Hungb92218b2018-08-14 12:00:21 +0800552
553 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
554 // Define a valid motion down event.
Garfield Tan00f511d2019-06-12 16:55:40 -0700555 event.initialize(DEVICE_ID, source, displayId, action, /* actionButton */ 0, /* flags */ 0,
556 /* edgeFlags */ 0, AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
557 /* xOffset */ 0, /* yOffset */ 0, /* xPrecision */ 0,
558 /* yPrecision */ 0, xCursorPosition, yCursorPosition, currentTime, currentTime,
559 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Arthur Hungb92218b2018-08-14 12:00:21 +0800560
561 // Inject event until dispatch out.
562 return dispatcher->injectInputEvent(
563 &event,
564 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT,
565 INJECT_EVENT_TIMEOUT, POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
566}
567
Garfield Tan00f511d2019-06-12 16:55:40 -0700568static int32_t injectMotionDown(const sp<InputDispatcher>& dispatcher, int32_t source,
569 int32_t displayId, int32_t x = 100, int32_t y = 200) {
570 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, x, y);
571}
572
Jackal Guof9696682018-10-05 12:23:23 +0800573static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
574 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
575 // Define a valid key event.
576 NotifyKeyArgs args(/* sequenceNum */ 0, currentTime, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
577 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0,
578 AKEYCODE_A, KEY_A, AMETA_NONE, currentTime);
579
580 return args;
581}
582
583static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
584 PointerProperties pointerProperties[1];
585 PointerCoords pointerCoords[1];
586
587 pointerProperties[0].clear();
588 pointerProperties[0].id = 0;
589 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
590
591 pointerCoords[0].clear();
592 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
593 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 200);
594
595 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
596 // Define a valid motion event.
597 NotifyMotionArgs args(/* sequenceNum */ 0, currentTime, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -0700598 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
599 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
Atif Niyaz21da0ff2019-06-28 13:22:51 -0700600 AMOTION_EVENT_EDGE_FLAG_NONE, 1, pointerProperties, pointerCoords,
601 /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -0700602 AMOTION_EVENT_INVALID_CURSOR_POSITION,
603 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +0800604
605 return args;
606}
607
Arthur Hungb92218b2018-08-14 12:00:21 +0800608TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
609 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800610 sp<FakeWindowHandle> window = new FakeWindowHandle(application, mDispatcher, "Fake Window",
611 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800612
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800613 std::vector<sp<InputWindowHandle>> inputWindowHandles;
614 inputWindowHandles.push_back(window);
Arthur Hungb92218b2018-08-14 12:00:21 +0800615
616 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800617 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
618 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +0800619 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
620
621 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800622 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800623}
624
625// The foreground window should receive the first touch down event.
626TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
627 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800628 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
629 ADISPLAY_ID_DEFAULT);
630 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
631 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800632
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800633 std::vector<sp<InputWindowHandle>> inputWindowHandles;
634 inputWindowHandles.push_back(windowTop);
635 inputWindowHandles.push_back(windowSecond);
Arthur Hungb92218b2018-08-14 12:00:21 +0800636
637 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800638 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
639 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +0800640 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
641
642 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800643 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800644 windowSecond->assertNoEvents();
645}
646
647TEST_F(InputDispatcherTest, SetInputWindow_FocusedWindow) {
648 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800649 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
650 ADISPLAY_ID_DEFAULT);
651 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
652 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800653
Arthur Hung7ab76b12019-01-09 19:17:20 +0800654 // Set focused application.
Tiger Huang721e26f2018-07-24 22:26:19 +0800655 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Arthur Hungb92218b2018-08-14 12:00:21 +0800656
657 // Expect one focus window exist in display.
658 windowSecond->setFocus();
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800659 std::vector<sp<InputWindowHandle>> inputWindowHandles;
660 inputWindowHandles.push_back(windowTop);
661 inputWindowHandles.push_back(windowSecond);
Arthur Hungb92218b2018-08-14 12:00:21 +0800662
663 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
664 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
665 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
666
667 // Focused window should receive event.
668 windowTop->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800669 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +0800670}
671
Arthur Hung7ab76b12019-01-09 19:17:20 +0800672TEST_F(InputDispatcherTest, SetInputWindow_FocusPriority) {
673 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
674 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
675 ADISPLAY_ID_DEFAULT);
676 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
677 ADISPLAY_ID_DEFAULT);
678
679 // Set focused application.
680 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
681
682 // Display has two focused windows. Add them to inputWindowsHandles in z-order (top most first)
683 windowTop->setFocus();
684 windowSecond->setFocus();
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800685 std::vector<sp<InputWindowHandle>> inputWindowHandles;
686 inputWindowHandles.push_back(windowTop);
687 inputWindowHandles.push_back(windowSecond);
Arthur Hung7ab76b12019-01-09 19:17:20 +0800688
689 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
690 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
691 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
692
693 // Top focused window should receive event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800694 windowTop->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung7ab76b12019-01-09 19:17:20 +0800695 windowSecond->assertNoEvents();
696}
697
Arthur Hung3b413f22018-10-26 18:05:34 +0800698TEST_F(InputDispatcherTest, SetInputWindow_InputWindowInfo) {
699 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
700
701 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
702 ADISPLAY_ID_DEFAULT);
703 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
704 ADISPLAY_ID_DEFAULT);
705
Arthur Hung832bc4a2019-01-28 11:43:17 +0800706 // Set focused application.
707 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Arthur Hung3b413f22018-10-26 18:05:34 +0800708
Arthur Hung832bc4a2019-01-28 11:43:17 +0800709 windowTop->setFocus();
710 windowSecond->setFocus();
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800711 std::vector<sp<InputWindowHandle>> inputWindowHandles;
712 inputWindowHandles.push_back(windowTop);
713 inputWindowHandles.push_back(windowSecond);
Arthur Hung3b413f22018-10-26 18:05:34 +0800714 // Release channel for window is no longer valid.
715 windowTop->releaseChannel();
Arthur Hung832bc4a2019-01-28 11:43:17 +0800716 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
Arthur Hung3b413f22018-10-26 18:05:34 +0800717
Arthur Hung832bc4a2019-01-28 11:43:17 +0800718 // Test inject a key down, should dispatch to a valid window.
719 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
720 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Arthur Hung3b413f22018-10-26 18:05:34 +0800721
722 // Top window is invalid, so it should not receive any input event.
723 windowTop->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800724 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung3b413f22018-10-26 18:05:34 +0800725}
726
Garfield Tan00f511d2019-06-12 16:55:40 -0700727TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
728 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
729
730 sp<FakeWindowHandle> windowLeft =
731 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
732 windowLeft->setFrame(Rect(0, 0, 600, 800));
733 windowLeft->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL);
734 sp<FakeWindowHandle> windowRight =
735 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
736 windowRight->setFrame(Rect(600, 0, 1200, 800));
737 windowRight->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL);
738
739 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
740
741 std::vector<sp<InputWindowHandle>> inputWindowHandles{windowLeft, windowRight};
742 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
743
744 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
745 // left window. This event should be dispatched to the left window.
746 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
747 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
748 ADISPLAY_ID_DEFAULT, 610, 400, 599, 400));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800749 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -0700750 windowRight->assertNoEvents();
751}
752
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800753/* Test InputDispatcher for MultiDisplay */
754class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
755public:
756 static constexpr int32_t SECOND_DISPLAY_ID = 1;
757 virtual void SetUp() {
758 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +0800759
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800760 application1 = new FakeApplicationHandle();
761 windowInPrimary = new FakeWindowHandle(application1, mDispatcher, "D_1",
762 ADISPLAY_ID_DEFAULT);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800763 std::vector<sp<InputWindowHandle>> inputWindowHandles;
764 inputWindowHandles.push_back(windowInPrimary);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800765 // Set focus window for primary display, but focused display would be second one.
766 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
767 windowInPrimary->setFocus();
768 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800769
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800770 application2 = new FakeApplicationHandle();
771 windowInSecondary = new FakeWindowHandle(application2, mDispatcher, "D_2",
772 SECOND_DISPLAY_ID);
773 // Set focus to second display window.
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800774 std::vector<sp<InputWindowHandle>> inputWindowHandles_Second;
775 inputWindowHandles_Second.push_back(windowInSecondary);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800776 // Set focus display to second one.
777 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
778 // Set focus window for second display.
779 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
780 windowInSecondary->setFocus();
781 mDispatcher->setInputWindows(inputWindowHandles_Second, SECOND_DISPLAY_ID);
782 }
783
784 virtual void TearDown() {
785 InputDispatcherTest::TearDown();
786
787 application1.clear();
788 windowInPrimary.clear();
789 application2.clear();
790 windowInSecondary.clear();
791 }
792
793protected:
794 sp<FakeApplicationHandle> application1;
795 sp<FakeWindowHandle> windowInPrimary;
796 sp<FakeApplicationHandle> application2;
797 sp<FakeWindowHandle> windowInSecondary;
798};
799
800TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
801 // Test touch down on primary display.
802 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
803 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +0800804 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800805 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800806 windowInSecondary->assertNoEvents();
807
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800808 // Test touch down on second display.
809 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
810 AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Arthur Hungb92218b2018-08-14 12:00:21 +0800811 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
812 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800813 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +0800814}
815
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800816TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +0800817 // Test inject a key down with display id specified.
818 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
819 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800820 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +0800821 windowInSecondary->assertNoEvents();
822
823 // Test inject a key down without display id specified.
Arthur Hungb92218b2018-08-14 12:00:21 +0800824 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
825 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
826 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800827 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +0800828
829 // Remove secondary display.
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800830 std::vector<sp<InputWindowHandle>> noWindows;
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800831 mDispatcher->setInputWindows(noWindows, SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +0800832
833 // Expect old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800834 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
835 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +0800836
837 // Test inject a key down, should timeout because of no target window.
838 ASSERT_EQ(INPUT_EVENT_INJECTION_TIMED_OUT, injectKeyDown(mDispatcher))
839 << "Inject key event should return INPUT_EVENT_INJECTION_TIMED_OUT";
840 windowInPrimary->assertNoEvents();
841 windowInSecondary->assertNoEvents();
842}
843
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800844class FakeMonitorReceiver : public FakeInputReceiver, public RefBase {
845public:
846 FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name,
Michael Wright3dd60e22019-03-27 22:06:44 +0000847 int32_t displayId, bool isGestureMonitor = false)
848 : FakeInputReceiver(dispatcher, name, displayId) {
Michael Wright3dd60e22019-03-27 22:06:44 +0000849 mDispatcher->registerInputMonitor(mServerChannel, displayId, isGestureMonitor);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800850 }
851};
852
853// Test per-display input monitors for motion event.
854TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
855 sp<FakeMonitorReceiver> monitorInPrimary =
856 new FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
857 sp<FakeMonitorReceiver> monitorInSecondary =
858 new FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
859
860 // Test touch down on primary display.
861 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
862 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
863 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800864 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
865 monitorInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800866 windowInSecondary->assertNoEvents();
867 monitorInSecondary->assertNoEvents();
868
869 // Test touch down on second display.
870 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
871 AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
872 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
873 windowInPrimary->assertNoEvents();
874 monitorInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800875 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
876 monitorInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800877
878 // Test inject a non-pointer motion event.
879 // If specific a display, it will dispatch to the focused window of particular display,
880 // or it will dispatch to the focused window of focused display.
881 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
882 AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
883 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
884 windowInPrimary->assertNoEvents();
885 monitorInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800886 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
887 monitorInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800888}
889
890// Test per-display input monitors for key event.
891TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
892 //Input monitor per display.
893 sp<FakeMonitorReceiver> monitorInPrimary =
894 new FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
895 sp<FakeMonitorReceiver> monitorInSecondary =
896 new FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
897
898 // Test inject a key down.
899 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
900 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
901 windowInPrimary->assertNoEvents();
902 monitorInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800903 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
904 monitorInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800905}
906
Jackal Guof9696682018-10-05 12:23:23 +0800907class InputFilterTest : public InputDispatcherTest {
908protected:
909 static constexpr int32_t SECOND_DISPLAY_ID = 1;
910
911 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) {
912 NotifyMotionArgs motionArgs;
913
914 motionArgs = generateMotionArgs(
915 AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
916 mDispatcher->notifyMotion(&motionArgs);
917 motionArgs = generateMotionArgs(
918 AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
919 mDispatcher->notifyMotion(&motionArgs);
920
921 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800922 mFakePolicy->assertFilterInputEventWasCalled(motionArgs);
Jackal Guof9696682018-10-05 12:23:23 +0800923 } else {
924 mFakePolicy->assertFilterInputEventWasNotCalled();
925 }
926 }
927
928 void testNotifyKey(bool expectToBeFiltered) {
929 NotifyKeyArgs keyArgs;
930
931 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
932 mDispatcher->notifyKey(&keyArgs);
933 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
934 mDispatcher->notifyKey(&keyArgs);
935
936 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800937 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +0800938 } else {
939 mFakePolicy->assertFilterInputEventWasNotCalled();
940 }
941 }
942};
943
944// Test InputFilter for MotionEvent
945TEST_F(InputFilterTest, MotionEvent_InputFilter) {
946 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
947 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
948 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
949
950 // Enable InputFilter
951 mDispatcher->setInputFilterEnabled(true);
952 // Test touch on both primary and second display, and check if both events are filtered.
953 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
954 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
955
956 // Disable InputFilter
957 mDispatcher->setInputFilterEnabled(false);
958 // Test touch on both primary and second display, and check if both events aren't filtered.
959 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
960 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
961}
962
963// Test InputFilter for KeyEvent
964TEST_F(InputFilterTest, KeyEvent_InputFilter) {
965 // Since the InputFilter is disabled by default, check if key event aren't filtered.
966 testNotifyKey(/*expectToBeFiltered*/ false);
967
968 // Enable InputFilter
969 mDispatcher->setInputFilterEnabled(true);
970 // Send a key event, and check if it is filtered.
971 testNotifyKey(/*expectToBeFiltered*/ true);
972
973 // Disable InputFilter
974 mDispatcher->setInputFilterEnabled(false);
975 // Send a key event, and check if it isn't filtered.
976 testNotifyKey(/*expectToBeFiltered*/ false);
977}
978
chaviwfd6d3512019-03-25 13:23:49 -0700979class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
980 virtual void SetUp() {
981 InputDispatcherTest::SetUp();
982
983 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
984 mUnfocusedWindow = new FakeWindowHandle(application, mDispatcher, "Top",
985 ADISPLAY_ID_DEFAULT);
986 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
987 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
988 // window.
989 mUnfocusedWindow->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL);
990
991 mWindowFocused = new FakeWindowHandle(application, mDispatcher, "Second",
992 ADISPLAY_ID_DEFAULT);
993 mWindowFocused->setFrame(Rect(50, 50, 100, 100));
994 mWindowFocused->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL);
995 mWindowFocusedTouchPoint = 60;
996
997 // Set focused application.
998 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
999 mWindowFocused->setFocus();
1000
1001 // Expect one focus window exist in display.
1002 std::vector<sp<InputWindowHandle>> inputWindowHandles;
1003 inputWindowHandles.push_back(mUnfocusedWindow);
1004 inputWindowHandles.push_back(mWindowFocused);
1005 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
1006 }
1007
1008 virtual void TearDown() {
1009 InputDispatcherTest::TearDown();
1010
1011 mUnfocusedWindow.clear();
1012 mWindowFocused.clear();
1013 }
1014
1015protected:
1016 sp<FakeWindowHandle> mUnfocusedWindow;
1017 sp<FakeWindowHandle> mWindowFocused;
1018 int32_t mWindowFocusedTouchPoint;
1019};
1020
1021// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
1022// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
1023// the onPointerDownOutsideFocus callback.
1024TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
1025 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1026 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, 20, 20))
1027 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1028 // Call monitor to wait for the command queue to get flushed.
1029 mDispatcher->monitor();
1030
1031 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
1032}
1033
1034// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
1035// DOWN on the window that doesn't have focus. Ensure no window received the
1036// onPointerDownOutsideFocus callback.
1037TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
1038 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1039 AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, 20, 20))
1040 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1041 // Call monitor to wait for the command queue to get flushed.
1042 mDispatcher->monitor();
1043
1044 mFakePolicy->assertOnPointerDownEquals(nullptr);
1045}
1046
1047// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
1048// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
1049TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
1050 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
1051 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1052 // Call monitor to wait for the command queue to get flushed.
1053 mDispatcher->monitor();
1054
1055 mFakePolicy->assertOnPointerDownEquals(nullptr);
1056}
1057
1058// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
1059// DOWN on the window that already has focus. Ensure no window received the
1060// onPointerDownOutsideFocus callback.
1061TEST_F(InputDispatcherOnPointerDownOutsideFocus,
1062 OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
1063 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1064 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, mWindowFocusedTouchPoint,
1065 mWindowFocusedTouchPoint))
1066 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1067 // Call monitor to wait for the command queue to get flushed.
1068 mDispatcher->monitor();
1069
1070 mFakePolicy->assertOnPointerDownEquals(nullptr);
1071}
1072
Garfield Tane84e6f92019-08-29 17:28:41 -07001073} // namespace android::inputdispatcher