blob: ebce47f1e74f4e1224222e4c5f0273864ec6dfe9 [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) {
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080057 assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_KEY, args.eventTime, args.action,
58 args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080059 }
60
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080061 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args) {
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080062 assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_MOTION, args.eventTime, args.action,
63 args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080064 }
65
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080066 void assertFilterInputEventWasNotCalled() { ASSERT_EQ(nullptr, mFilteredEvent); }
Michael Wrightd02c5b62014-02-10 15:10:22 -080067
chaviwfd6d3512019-03-25 13:23:49 -070068 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
69 ASSERT_EQ(mOnPointerDownToken, touchedToken)
70 << "Expected token from onPointerDownOutsideFocus was not matched";
71 reset();
72 }
73
Michael Wrightd02c5b62014-02-10 15:10:22 -080074private:
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080075 std::unique_ptr<InputEvent> mFilteredEvent;
chaviwfd6d3512019-03-25 13:23:49 -070076 sp<IBinder> mOnPointerDownToken;
Jackal Guof9696682018-10-05 12:23:23 +080077
Narayan Kamath39efe3e2014-10-17 10:37:08 +010078 virtual void notifyConfigurationChanged(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080079 }
80
Narayan Kamath39efe3e2014-10-17 10:37:08 +010081 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>&,
Robert Carr803535b2018-08-02 16:38:15 -070082 const sp<IBinder>&,
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080083 const std::string&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080084 return 0;
85 }
86
Robert Carr803535b2018-08-02 16:38:15 -070087 virtual void notifyInputChannelBroken(const sp<IBinder>&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080088 }
89
chaviw0c06c6e2019-01-09 13:27:07 -080090 virtual void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) {
Robert Carr740167f2018-10-11 19:03:41 -070091 }
92
Michael Wrightd02c5b62014-02-10 15:10:22 -080093 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
94 *outConfig = mConfig;
95 }
96
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080097 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Jackal Guof9696682018-10-05 12:23:23 +080098 switch (inputEvent->getType()) {
99 case AINPUT_EVENT_TYPE_KEY: {
100 const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800101 mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800102 break;
103 }
104
105 case AINPUT_EVENT_TYPE_MOTION: {
106 const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800107 mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800108 break;
109 }
110 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800111 return true;
112 }
113
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100114 virtual void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800115 }
116
Charles Chen3611f1f2019-01-29 17:26:18 +0800117 virtual void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800118 }
119
Robert Carr803535b2018-08-02 16:38:15 -0700120 virtual nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&,
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100121 const KeyEvent*, uint32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800122 return 0;
123 }
124
Robert Carr803535b2018-08-02 16:38:15 -0700125 virtual bool dispatchUnhandledKey(const sp<IBinder>&,
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100126 const KeyEvent*, uint32_t, KeyEvent*) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800127 return false;
128 }
129
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100130 virtual void notifySwitch(nsecs_t, uint32_t, uint32_t, uint32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800131 }
132
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100133 virtual void pokeUserActivity(nsecs_t, int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800134 }
135
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100136 virtual bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800137 return false;
138 }
Jackal Guof9696682018-10-05 12:23:23 +0800139
chaviwfd6d3512019-03-25 13:23:49 -0700140 virtual void onPointerDownOutsideFocus(const sp<IBinder>& newToken) {
141 mOnPointerDownToken = newToken;
142 }
143
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800144 void assertFilterInputEventWasCalled(int type, nsecs_t eventTime, int32_t action,
145 int32_t displayId) {
146 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
147 ASSERT_EQ(mFilteredEvent->getType(), type);
148
149 if (type == AINPUT_EVENT_TYPE_KEY) {
150 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*mFilteredEvent);
151 EXPECT_EQ(keyEvent.getEventTime(), eventTime);
152 EXPECT_EQ(keyEvent.getAction(), action);
153 EXPECT_EQ(keyEvent.getDisplayId(), displayId);
154 } else if (type == AINPUT_EVENT_TYPE_MOTION) {
155 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*mFilteredEvent);
156 EXPECT_EQ(motionEvent.getEventTime(), eventTime);
157 EXPECT_EQ(motionEvent.getAction(), action);
158 EXPECT_EQ(motionEvent.getDisplayId(), displayId);
159 } else {
160 FAIL() << "Unknown type: " << type;
161 }
162
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800163 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800164 }
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800165
166 void reset() { mOnPointerDownToken.clear(); }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800167};
168
169
170// --- InputDispatcherTest ---
171
172class InputDispatcherTest : public testing::Test {
173protected:
174 sp<FakeInputDispatcherPolicy> mFakePolicy;
175 sp<InputDispatcher> mDispatcher;
Arthur Hungb92218b2018-08-14 12:00:21 +0800176 sp<InputDispatcherThread> mDispatcherThread;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800177
178 virtual void SetUp() {
179 mFakePolicy = new FakeInputDispatcherPolicy();
180 mDispatcher = new InputDispatcher(mFakePolicy);
Arthur Hungb92218b2018-08-14 12:00:21 +0800181 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
182 //Start InputDispatcher thread
183 mDispatcherThread = new InputDispatcherThread(mDispatcher);
184 mDispatcherThread->run("InputDispatcherTest", PRIORITY_URGENT_DISPLAY);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800185 }
186
187 virtual void TearDown() {
Arthur Hungb92218b2018-08-14 12:00:21 +0800188 mDispatcherThread->requestExit();
189 mDispatcherThread.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800190 mFakePolicy.clear();
191 mDispatcher.clear();
192 }
193};
194
195
196TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
197 KeyEvent event;
198
199 // Rejects undefined key actions.
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100200 event.initialize(DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800201 /*action*/ -1, 0,
202 AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, ARBITRARY_TIME);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800203 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800204 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800205 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
206 << "Should reject key events with undefined action.";
207
208 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100209 event.initialize(DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800210 AKEY_EVENT_ACTION_MULTIPLE, 0,
211 AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, ARBITRARY_TIME);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800212 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800213 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800214 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
215 << "Should reject key events with ACTION_MULTIPLE.";
216}
217
218TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
219 MotionEvent event;
220 PointerProperties pointerProperties[MAX_POINTERS + 1];
221 PointerCoords pointerCoords[MAX_POINTERS + 1];
222 for (int i = 0; i <= MAX_POINTERS; i++) {
223 pointerProperties[i].clear();
224 pointerProperties[i].id = i;
225 pointerCoords[i].clear();
226 }
227
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800228 // Some constants commonly used below
229 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
230 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
231 constexpr int32_t metaState = AMETA_NONE;
232 constexpr MotionClassification classification = MotionClassification::NONE;
233
Michael Wrightd02c5b62014-02-10 15:10:22 -0800234 // Rejects undefined motion actions.
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800235 event.initialize(DEVICE_ID, source, DISPLAY_ID,
Garfield Tan00f511d2019-06-12 16:55:40 -0700236 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification, 0, 0, 0, 0,
237 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
238 ARBITRARY_TIME, ARBITRARY_TIME,
239 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800240 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800241 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800242 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
243 << "Should reject motion events with undefined action.";
244
245 // Rejects pointer down with invalid index.
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800246 event.initialize(DEVICE_ID, source, DISPLAY_ID,
Garfield Tan00f511d2019-06-12 16:55:40 -0700247 AMOTION_EVENT_ACTION_POINTER_DOWN |
248 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
249 0, 0, edgeFlags, metaState, 0, classification, 0, 0, 0, 0,
250 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
251 ARBITRARY_TIME, ARBITRARY_TIME,
252 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800253 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800254 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800255 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
256 << "Should reject motion events with pointer down index too large.";
257
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800258 event.initialize(DEVICE_ID, source, DISPLAY_ID,
Garfield Tan00f511d2019-06-12 16:55:40 -0700259 AMOTION_EVENT_ACTION_POINTER_DOWN |
260 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
261 0, 0, edgeFlags, metaState, 0, classification, 0, 0, 0, 0,
262 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
263 ARBITRARY_TIME, ARBITRARY_TIME,
264 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800265 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800266 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800267 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
268 << "Should reject motion events with pointer down index too small.";
269
270 // Rejects pointer up with invalid index.
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800271 event.initialize(DEVICE_ID, source, DISPLAY_ID,
Garfield Tan00f511d2019-06-12 16:55:40 -0700272 AMOTION_EVENT_ACTION_POINTER_UP |
273 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
274 0, 0, edgeFlags, metaState, 0, classification, 0, 0, 0, 0,
275 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
276 ARBITRARY_TIME, ARBITRARY_TIME,
277 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800278 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800279 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800280 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
281 << "Should reject motion events with pointer up index too large.";
282
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800283 event.initialize(DEVICE_ID, source, DISPLAY_ID,
Garfield Tan00f511d2019-06-12 16:55:40 -0700284 AMOTION_EVENT_ACTION_POINTER_UP |
285 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
286 0, 0, edgeFlags, metaState, 0, classification, 0, 0, 0, 0,
287 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
288 ARBITRARY_TIME, ARBITRARY_TIME,
289 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800290 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800291 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800292 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
293 << "Should reject motion events with pointer up index too small.";
294
295 // Rejects motion events with invalid number of pointers.
Garfield Tan00f511d2019-06-12 16:55:40 -0700296 event.initialize(DEVICE_ID, source, DISPLAY_ID, AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags,
297 metaState, 0, classification, 0, 0, 0, 0,
298 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
299 ARBITRARY_TIME, ARBITRARY_TIME,
300 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800301 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800302 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800303 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
304 << "Should reject motion events with 0 pointers.";
305
Garfield Tan00f511d2019-06-12 16:55:40 -0700306 event.initialize(DEVICE_ID, source, DISPLAY_ID, AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags,
307 metaState, 0, classification, 0, 0, 0, 0,
308 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
309 ARBITRARY_TIME, ARBITRARY_TIME,
310 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800311 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800312 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800313 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
314 << "Should reject motion events with more than MAX_POINTERS pointers.";
315
316 // Rejects motion events with invalid pointer ids.
317 pointerProperties[0].id = -1;
Garfield Tan00f511d2019-06-12 16:55:40 -0700318 event.initialize(DEVICE_ID, source, DISPLAY_ID, AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags,
319 metaState, 0, classification, 0, 0, 0, 0,
320 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
321 ARBITRARY_TIME, ARBITRARY_TIME,
322 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800323 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800324 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800325 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
326 << "Should reject motion events with pointer ids less than 0.";
327
328 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tan00f511d2019-06-12 16:55:40 -0700329 event.initialize(DEVICE_ID, source, DISPLAY_ID, AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags,
330 metaState, 0, classification, 0, 0, 0, 0,
331 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
332 ARBITRARY_TIME, ARBITRARY_TIME,
333 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800334 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800335 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800336 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
337 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
338
339 // Rejects motion events with duplicate pointer ids.
340 pointerProperties[0].id = 1;
341 pointerProperties[1].id = 1;
Garfield Tan00f511d2019-06-12 16:55:40 -0700342 event.initialize(DEVICE_ID, source, DISPLAY_ID, AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags,
343 metaState, 0, classification, 0, 0, 0, 0,
344 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
345 ARBITRARY_TIME, ARBITRARY_TIME,
346 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800347 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800348 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800349 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
350 << "Should reject motion events with duplicate pointer ids.";
351}
352
Arthur Hungb92218b2018-08-14 12:00:21 +0800353// --- InputDispatcherTest SetInputWindowTest ---
354static const int32_t INJECT_EVENT_TIMEOUT = 500;
355static const int32_t DISPATCHING_TIMEOUT = 100;
356
357class FakeApplicationHandle : public InputApplicationHandle {
358public:
359 FakeApplicationHandle() {}
360 virtual ~FakeApplicationHandle() {}
361
362 virtual bool updateInfo() {
Arthur Hung7a0c39a2019-03-20 16:52:24 +0800363 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Arthur Hungb92218b2018-08-14 12:00:21 +0800364 return true;
365 }
366};
367
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800368class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800369public:
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800370 InputEvent* consume() {
Arthur Hungb92218b2018-08-14 12:00:21 +0800371 uint32_t consumeSeq;
372 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800373
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800374 std::chrono::time_point start = std::chrono::steady_clock::now();
375 status_t status = WOULD_BLOCK;
376 while (status == WOULD_BLOCK) {
377 status = mConsumer->consume(&mEventFactory, false /*consumeBatches*/, -1, &consumeSeq,
378 &event);
379 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
380 if (elapsed > 100ms) {
381 break;
382 }
383 }
384
385 if (status == WOULD_BLOCK) {
386 // Just means there's no event available.
387 return nullptr;
388 }
389
390 if (status != OK) {
391 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
392 return nullptr;
393 }
394 if (event == nullptr) {
395 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
396 return nullptr;
397 }
398
399 status = mConsumer->sendFinishedSignal(consumeSeq, handled());
400 if (status != OK) {
401 ADD_FAILURE() << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
402 }
403 return event;
404 }
405
406 void consumeEvent(int32_t expectedEventType, int32_t expectedAction, int32_t expectedDisplayId,
407 int32_t expectedFlags) {
408 InputEvent* event = consume();
409
410 ASSERT_NE(nullptr, event) << mName.c_str()
411 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800412 ASSERT_EQ(expectedEventType, event->getType())
Tiger Huang8664f8c2018-10-11 19:14:35 +0800413 << mName.c_str() << ": event type should match.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800414
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800415 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
Tiger Huang8664f8c2018-10-11 19:14:35 +0800416
Tiger Huang8664f8c2018-10-11 19:14:35 +0800417 switch (expectedEventType) {
418 case AINPUT_EVENT_TYPE_KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800419 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
420 EXPECT_EQ(expectedAction, keyEvent.getAction());
421 EXPECT_EQ(expectedFlags, keyEvent.getFlags());
Tiger Huang8664f8c2018-10-11 19:14:35 +0800422 break;
423 }
424 case AINPUT_EVENT_TYPE_MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800425 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
426 EXPECT_EQ(expectedAction, motionEvent.getAction());
427 EXPECT_EQ(expectedFlags, motionEvent.getFlags());
Tiger Huang8664f8c2018-10-11 19:14:35 +0800428 break;
429 }
430 default: {
431 FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType;
432 }
433 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800434 }
435
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800436 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
437 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
438 expectedFlags);
439 }
440
441 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
442 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
443 expectedFlags);
444 }
445
Arthur Hungb92218b2018-08-14 12:00:21 +0800446 void assertNoEvents() {
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800447 InputEvent* event = consume();
448 ASSERT_EQ(nullptr, event)
Arthur Hungb92218b2018-08-14 12:00:21 +0800449 << mName.c_str()
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800450 << ": should not have received any events, so consume() should return NULL";
Arthur Hungb92218b2018-08-14 12:00:21 +0800451 }
452
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800453protected:
454 explicit FakeInputReceiver(const sp<InputDispatcher>& dispatcher,
455 const std::string name, int32_t displayId) :
456 mDispatcher(dispatcher), mName(name), mDisplayId(displayId) {
457 InputChannel::openInputChannelPair(name, mServerChannel, mClientChannel);
458 mConsumer = new InputConsumer(mClientChannel);
459 }
460
461 virtual ~FakeInputReceiver() {
462 }
463
464 // return true if the event has been handled.
465 virtual bool handled() {
466 return false;
467 }
468
Arthur Hungb92218b2018-08-14 12:00:21 +0800469 sp<InputDispatcher> mDispatcher;
470 sp<InputChannel> mServerChannel, mClientChannel;
471 InputConsumer *mConsumer;
472 PreallocatedInputEventFactory mEventFactory;
473
474 std::string mName;
Arthur Hungb92218b2018-08-14 12:00:21 +0800475 int32_t mDisplayId;
476};
477
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800478class FakeWindowHandle : public InputWindowHandle, public FakeInputReceiver {
479public:
480 static const int32_t WIDTH = 600;
481 static const int32_t HEIGHT = 800;
482
483 FakeWindowHandle(const sp<InputApplicationHandle>& inputApplicationHandle,
484 const sp<InputDispatcher>& dispatcher, const std::string name, int32_t displayId) :
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800485 FakeInputReceiver(dispatcher, name, displayId),
chaviwfd6d3512019-03-25 13:23:49 -0700486 mFocused(false), mFrame(Rect(0, 0, WIDTH, HEIGHT)), mLayoutParamFlags(0) {
Siarhei Vishniakou7c34b232019-10-11 19:08:48 -0700487 mDispatcher->registerInputChannel(mServerChannel);
chaviwfd6d3512019-03-25 13:23:49 -0700488
Robert Carr740167f2018-10-11 19:03:41 -0700489 inputApplicationHandle->updateInfo();
490 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800491 }
492
493 virtual bool updateInfo() {
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700494 mInfo.token = mServerChannel ? mServerChannel->getConnectionToken() : nullptr;
Arthur Hung3b413f22018-10-26 18:05:34 +0800495 mInfo.name = mName;
chaviwfd6d3512019-03-25 13:23:49 -0700496 mInfo.layoutParamsFlags = mLayoutParamFlags;
Arthur Hung3b413f22018-10-26 18:05:34 +0800497 mInfo.layoutParamsType = InputWindowInfo::TYPE_APPLICATION;
498 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
chaviwfd6d3512019-03-25 13:23:49 -0700499 mInfo.frameLeft = mFrame.left;
500 mInfo.frameTop = mFrame.top;
501 mInfo.frameRight = mFrame.right;
502 mInfo.frameBottom = mFrame.bottom;
Robert Carre07e1032018-11-26 12:55:53 -0800503 mInfo.globalScaleFactor = 1.0;
Garfield Tan00f511d2019-06-12 16:55:40 -0700504 mInfo.touchableRegion.clear();
chaviwfd6d3512019-03-25 13:23:49 -0700505 mInfo.addTouchableRegion(mFrame);
Arthur Hung3b413f22018-10-26 18:05:34 +0800506 mInfo.visible = true;
507 mInfo.canReceiveKeys = true;
508 mInfo.hasFocus = mFocused;
509 mInfo.hasWallpaper = false;
510 mInfo.paused = false;
511 mInfo.layer = 0;
512 mInfo.ownerPid = INJECTOR_PID;
513 mInfo.ownerUid = INJECTOR_UID;
514 mInfo.inputFeatures = 0;
515 mInfo.displayId = mDisplayId;
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800516
517 return true;
518 }
519
520 void setFocus() {
521 mFocused = true;
522 }
Arthur Hung832bc4a2019-01-28 11:43:17 +0800523
chaviwfd6d3512019-03-25 13:23:49 -0700524 void setFrame(const Rect& frame) {
525 mFrame.set(frame);
526 }
527
528 void setLayoutParamFlags(int32_t flags) {
529 mLayoutParamFlags = flags;
530 }
531
Arthur Hung832bc4a2019-01-28 11:43:17 +0800532 void releaseChannel() {
Arthur Hung6b5a2b92019-01-31 16:39:28 +0800533 mServerChannel.clear();
Arthur Hung832bc4a2019-01-28 11:43:17 +0800534 InputWindowHandle::releaseChannel();
Arthur Hung832bc4a2019-01-28 11:43:17 +0800535 }
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800536protected:
537 virtual bool handled() {
538 return true;
539 }
540
541 bool mFocused;
chaviwfd6d3512019-03-25 13:23:49 -0700542 Rect mFrame;
543 int32_t mLayoutParamFlags;
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800544};
545
Tiger Huang721e26f2018-07-24 22:26:19 +0800546static int32_t injectKeyDown(const sp<InputDispatcher>& dispatcher,
547 int32_t displayId = ADISPLAY_ID_NONE) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800548 KeyEvent event;
549 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
550
551 // Define a valid key down event.
Tiger Huang721e26f2018-07-24 22:26:19 +0800552 event.initialize(DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Arthur Hungb92218b2018-08-14 12:00:21 +0800553 AKEY_EVENT_ACTION_DOWN, /* flags */ 0,
554 AKEYCODE_A, KEY_A, AMETA_NONE, /* repeatCount */ 0, currentTime, currentTime);
555
556 // Inject event until dispatch out.
557 return dispatcher->injectInputEvent(
558 &event,
559 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT,
560 INJECT_EVENT_TIMEOUT, POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
561}
562
Garfield Tan00f511d2019-06-12 16:55:40 -0700563static int32_t injectMotionEvent(const sp<InputDispatcher>& dispatcher, int32_t action,
564 int32_t source, int32_t displayId, int32_t x, int32_t y,
565 int32_t xCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION,
566 int32_t yCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800567 MotionEvent event;
568 PointerProperties pointerProperties[1];
569 PointerCoords pointerCoords[1];
570
571 pointerProperties[0].clear();
572 pointerProperties[0].id = 0;
573 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
574
575 pointerCoords[0].clear();
chaviwfd6d3512019-03-25 13:23:49 -0700576 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
577 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Arthur Hungb92218b2018-08-14 12:00:21 +0800578
579 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
580 // Define a valid motion down event.
Garfield Tan00f511d2019-06-12 16:55:40 -0700581 event.initialize(DEVICE_ID, source, displayId, action, /* actionButton */ 0, /* flags */ 0,
582 /* edgeFlags */ 0, AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
583 /* xOffset */ 0, /* yOffset */ 0, /* xPrecision */ 0,
584 /* yPrecision */ 0, xCursorPosition, yCursorPosition, currentTime, currentTime,
585 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Arthur Hungb92218b2018-08-14 12:00:21 +0800586
587 // Inject event until dispatch out.
588 return dispatcher->injectInputEvent(
589 &event,
590 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT,
591 INJECT_EVENT_TIMEOUT, POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
592}
593
Garfield Tan00f511d2019-06-12 16:55:40 -0700594static int32_t injectMotionDown(const sp<InputDispatcher>& dispatcher, int32_t source,
595 int32_t displayId, int32_t x = 100, int32_t y = 200) {
596 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, x, y);
597}
598
Jackal Guof9696682018-10-05 12:23:23 +0800599static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
600 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
601 // Define a valid key event.
602 NotifyKeyArgs args(/* sequenceNum */ 0, currentTime, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
603 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0,
604 AKEYCODE_A, KEY_A, AMETA_NONE, currentTime);
605
606 return args;
607}
608
609static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
610 PointerProperties pointerProperties[1];
611 PointerCoords pointerCoords[1];
612
613 pointerProperties[0].clear();
614 pointerProperties[0].id = 0;
615 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
616
617 pointerCoords[0].clear();
618 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
619 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 200);
620
621 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
622 // Define a valid motion event.
623 NotifyMotionArgs args(/* sequenceNum */ 0, currentTime, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -0700624 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
625 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
Atif Niyaz21da0ff2019-06-28 13:22:51 -0700626 AMOTION_EVENT_EDGE_FLAG_NONE, 1, pointerProperties, pointerCoords,
627 /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -0700628 AMOTION_EVENT_INVALID_CURSOR_POSITION,
629 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +0800630
631 return args;
632}
633
Arthur Hungb92218b2018-08-14 12:00:21 +0800634TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
635 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800636 sp<FakeWindowHandle> window = new FakeWindowHandle(application, mDispatcher, "Fake Window",
637 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800638
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -0800639 mDispatcher->setInputWindows({window}, ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800640 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
641 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +0800642 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
643
644 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800645 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800646}
647
648// The foreground window should receive the first touch down event.
649TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
650 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800651 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
652 ADISPLAY_ID_DEFAULT);
653 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
654 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800655
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -0800656 mDispatcher->setInputWindows({windowTop, windowSecond}, ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800657 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
658 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +0800659 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
660
661 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800662 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800663 windowSecond->assertNoEvents();
664}
665
666TEST_F(InputDispatcherTest, SetInputWindow_FocusedWindow) {
667 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800668 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
669 ADISPLAY_ID_DEFAULT);
670 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
671 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800672
Arthur Hung7ab76b12019-01-09 19:17:20 +0800673 // Set focused application.
Tiger Huang721e26f2018-07-24 22:26:19 +0800674 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Arthur Hungb92218b2018-08-14 12:00:21 +0800675
676 // Expect one focus window exist in display.
677 windowSecond->setFocus();
Arthur Hungb92218b2018-08-14 12:00:21 +0800678
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -0800679 mDispatcher->setInputWindows({windowTop, windowSecond}, ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800680 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
681 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
682
683 // Focused window should receive event.
684 windowTop->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800685 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +0800686}
687
Arthur Hung7ab76b12019-01-09 19:17:20 +0800688TEST_F(InputDispatcherTest, SetInputWindow_FocusPriority) {
689 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
690 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
691 ADISPLAY_ID_DEFAULT);
692 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
693 ADISPLAY_ID_DEFAULT);
694
695 // Set focused application.
696 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
697
698 // Display has two focused windows. Add them to inputWindowsHandles in z-order (top most first)
699 windowTop->setFocus();
700 windowSecond->setFocus();
Arthur Hung7ab76b12019-01-09 19:17:20 +0800701
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -0800702 mDispatcher->setInputWindows({windowTop, windowSecond}, ADISPLAY_ID_DEFAULT);
Arthur Hung7ab76b12019-01-09 19:17:20 +0800703 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
704 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
705
706 // Top focused window should receive event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800707 windowTop->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung7ab76b12019-01-09 19:17:20 +0800708 windowSecond->assertNoEvents();
709}
710
Arthur Hung3b413f22018-10-26 18:05:34 +0800711TEST_F(InputDispatcherTest, SetInputWindow_InputWindowInfo) {
712 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
713
714 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
715 ADISPLAY_ID_DEFAULT);
716 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
717 ADISPLAY_ID_DEFAULT);
718
Arthur Hung832bc4a2019-01-28 11:43:17 +0800719 // Set focused application.
720 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Arthur Hung3b413f22018-10-26 18:05:34 +0800721
Arthur Hung832bc4a2019-01-28 11:43:17 +0800722 windowTop->setFocus();
723 windowSecond->setFocus();
Arthur Hung3b413f22018-10-26 18:05:34 +0800724 // Release channel for window is no longer valid.
725 windowTop->releaseChannel();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -0800726 mDispatcher->setInputWindows({windowTop, windowSecond}, ADISPLAY_ID_DEFAULT);
Arthur Hung3b413f22018-10-26 18:05:34 +0800727
Arthur Hung832bc4a2019-01-28 11:43:17 +0800728 // Test inject a key down, should dispatch to a valid window.
729 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
730 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Arthur Hung3b413f22018-10-26 18:05:34 +0800731
732 // Top window is invalid, so it should not receive any input event.
733 windowTop->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800734 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung3b413f22018-10-26 18:05:34 +0800735}
736
Garfield Tan00f511d2019-06-12 16:55:40 -0700737TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
738 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
739
740 sp<FakeWindowHandle> windowLeft =
741 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
742 windowLeft->setFrame(Rect(0, 0, 600, 800));
743 windowLeft->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL);
744 sp<FakeWindowHandle> windowRight =
745 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
746 windowRight->setFrame(Rect(600, 0, 1200, 800));
747 windowRight->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL);
748
749 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
750
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -0800751 mDispatcher->setInputWindows({windowLeft, windowRight}, ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -0700752
753 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
754 // left window. This event should be dispatched to the left window.
755 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
756 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
757 ADISPLAY_ID_DEFAULT, 610, 400, 599, 400));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800758 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -0700759 windowRight->assertNoEvents();
760}
761
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800762/* Test InputDispatcher for MultiDisplay */
763class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
764public:
765 static constexpr int32_t SECOND_DISPLAY_ID = 1;
766 virtual void SetUp() {
767 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +0800768
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800769 application1 = new FakeApplicationHandle();
770 windowInPrimary = new FakeWindowHandle(application1, mDispatcher, "D_1",
771 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -0800772
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800773 // Set focus window for primary display, but focused display would be second one.
774 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
775 windowInPrimary->setFocus();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -0800776 mDispatcher->setInputWindows({windowInPrimary}, ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800777
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800778 application2 = new FakeApplicationHandle();
779 windowInSecondary = new FakeWindowHandle(application2, mDispatcher, "D_2",
780 SECOND_DISPLAY_ID);
781 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800782 // Set focus display to second one.
783 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
784 // Set focus window for second display.
785 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
786 windowInSecondary->setFocus();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -0800787 mDispatcher->setInputWindows({windowInSecondary}, SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800788 }
789
790 virtual void TearDown() {
791 InputDispatcherTest::TearDown();
792
793 application1.clear();
794 windowInPrimary.clear();
795 application2.clear();
796 windowInSecondary.clear();
797 }
798
799protected:
800 sp<FakeApplicationHandle> application1;
801 sp<FakeWindowHandle> windowInPrimary;
802 sp<FakeApplicationHandle> application2;
803 sp<FakeWindowHandle> windowInSecondary;
804};
805
806TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
807 // Test touch down on primary display.
808 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
809 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +0800810 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800811 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800812 windowInSecondary->assertNoEvents();
813
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800814 // Test touch down on second display.
815 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
816 AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Arthur Hungb92218b2018-08-14 12:00:21 +0800817 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
818 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800819 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +0800820}
821
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800822TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +0800823 // Test inject a key down with display id specified.
824 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
825 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800826 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +0800827 windowInSecondary->assertNoEvents();
828
829 // Test inject a key down without display id specified.
Arthur Hungb92218b2018-08-14 12:00:21 +0800830 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
831 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
832 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800833 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +0800834
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -0800835 // Remove all windows in secondary display.
836 mDispatcher->setInputWindows({}, SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +0800837
838 // Expect old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800839 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
840 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +0800841
842 // Test inject a key down, should timeout because of no target window.
843 ASSERT_EQ(INPUT_EVENT_INJECTION_TIMED_OUT, injectKeyDown(mDispatcher))
844 << "Inject key event should return INPUT_EVENT_INJECTION_TIMED_OUT";
845 windowInPrimary->assertNoEvents();
846 windowInSecondary->assertNoEvents();
847}
848
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800849class FakeMonitorReceiver : public FakeInputReceiver, public RefBase {
850public:
851 FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name,
Michael Wright3dd60e22019-03-27 22:06:44 +0000852 int32_t displayId, bool isGestureMonitor = false)
853 : FakeInputReceiver(dispatcher, name, displayId) {
Michael Wright3dd60e22019-03-27 22:06:44 +0000854 mDispatcher->registerInputMonitor(mServerChannel, displayId, isGestureMonitor);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800855 }
856};
857
858// Test per-display input monitors for motion event.
859TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
860 sp<FakeMonitorReceiver> monitorInPrimary =
861 new FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
862 sp<FakeMonitorReceiver> monitorInSecondary =
863 new FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
864
865 // Test touch down on primary display.
866 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
867 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
868 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800869 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
870 monitorInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800871 windowInSecondary->assertNoEvents();
872 monitorInSecondary->assertNoEvents();
873
874 // Test touch down on second display.
875 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
876 AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
877 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
878 windowInPrimary->assertNoEvents();
879 monitorInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800880 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
881 monitorInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800882
883 // Test inject a non-pointer motion event.
884 // If specific a display, it will dispatch to the focused window of particular display,
885 // or it will dispatch to the focused window of focused display.
886 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
887 AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
888 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
889 windowInPrimary->assertNoEvents();
890 monitorInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800891 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
892 monitorInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800893}
894
895// Test per-display input monitors for key event.
896TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
897 //Input monitor per display.
898 sp<FakeMonitorReceiver> monitorInPrimary =
899 new FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
900 sp<FakeMonitorReceiver> monitorInSecondary =
901 new FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
902
903 // Test inject a key down.
904 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
905 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
906 windowInPrimary->assertNoEvents();
907 monitorInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800908 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
909 monitorInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800910}
911
Jackal Guof9696682018-10-05 12:23:23 +0800912class InputFilterTest : public InputDispatcherTest {
913protected:
914 static constexpr int32_t SECOND_DISPLAY_ID = 1;
915
916 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) {
917 NotifyMotionArgs motionArgs;
918
919 motionArgs = generateMotionArgs(
920 AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
921 mDispatcher->notifyMotion(&motionArgs);
922 motionArgs = generateMotionArgs(
923 AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
924 mDispatcher->notifyMotion(&motionArgs);
925
926 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800927 mFakePolicy->assertFilterInputEventWasCalled(motionArgs);
Jackal Guof9696682018-10-05 12:23:23 +0800928 } else {
929 mFakePolicy->assertFilterInputEventWasNotCalled();
930 }
931 }
932
933 void testNotifyKey(bool expectToBeFiltered) {
934 NotifyKeyArgs keyArgs;
935
936 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
937 mDispatcher->notifyKey(&keyArgs);
938 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
939 mDispatcher->notifyKey(&keyArgs);
940
941 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800942 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +0800943 } else {
944 mFakePolicy->assertFilterInputEventWasNotCalled();
945 }
946 }
947};
948
949// Test InputFilter for MotionEvent
950TEST_F(InputFilterTest, MotionEvent_InputFilter) {
951 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
952 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
953 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
954
955 // Enable InputFilter
956 mDispatcher->setInputFilterEnabled(true);
957 // Test touch on both primary and second display, and check if both events are filtered.
958 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
959 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
960
961 // Disable InputFilter
962 mDispatcher->setInputFilterEnabled(false);
963 // Test touch on both primary and second display, and check if both events aren't filtered.
964 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
965 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
966}
967
968// Test InputFilter for KeyEvent
969TEST_F(InputFilterTest, KeyEvent_InputFilter) {
970 // Since the InputFilter is disabled by default, check if key event aren't filtered.
971 testNotifyKey(/*expectToBeFiltered*/ false);
972
973 // Enable InputFilter
974 mDispatcher->setInputFilterEnabled(true);
975 // Send a key event, and check if it is filtered.
976 testNotifyKey(/*expectToBeFiltered*/ true);
977
978 // Disable InputFilter
979 mDispatcher->setInputFilterEnabled(false);
980 // Send a key event, and check if it isn't filtered.
981 testNotifyKey(/*expectToBeFiltered*/ false);
982}
983
chaviwfd6d3512019-03-25 13:23:49 -0700984class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
985 virtual void SetUp() {
986 InputDispatcherTest::SetUp();
987
988 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
989 mUnfocusedWindow = new FakeWindowHandle(application, mDispatcher, "Top",
990 ADISPLAY_ID_DEFAULT);
991 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
992 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
993 // window.
994 mUnfocusedWindow->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL);
995
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -0800996 mFocusedWindow =
997 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
998 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
999 mFocusedWindow->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL);
1000 mFocusedWindowTouchPoint = 60;
chaviwfd6d3512019-03-25 13:23:49 -07001001
1002 // Set focused application.
1003 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08001004 mFocusedWindow->setFocus();
chaviwfd6d3512019-03-25 13:23:49 -07001005
1006 // Expect one focus window exist in display.
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08001007 mDispatcher->setInputWindows({mUnfocusedWindow, mFocusedWindow}, ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07001008 }
1009
1010 virtual void TearDown() {
1011 InputDispatcherTest::TearDown();
1012
1013 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08001014 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07001015 }
1016
1017protected:
1018 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08001019 sp<FakeWindowHandle> mFocusedWindow;
1020 int32_t mFocusedWindowTouchPoint;
chaviwfd6d3512019-03-25 13:23:49 -07001021};
1022
1023// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
1024// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
1025// the onPointerDownOutsideFocus callback.
1026TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
1027 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1028 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, 20, 20))
1029 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1030 // Call monitor to wait for the command queue to get flushed.
1031 mDispatcher->monitor();
1032
1033 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
1034}
1035
1036// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
1037// DOWN on the window that doesn't have focus. Ensure no window received the
1038// onPointerDownOutsideFocus callback.
1039TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
1040 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1041 AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, 20, 20))
1042 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1043 // Call monitor to wait for the command queue to get flushed.
1044 mDispatcher->monitor();
1045
1046 mFakePolicy->assertOnPointerDownEquals(nullptr);
1047}
1048
1049// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
1050// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
1051TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
1052 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
1053 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1054 // Call monitor to wait for the command queue to get flushed.
1055 mDispatcher->monitor();
1056
1057 mFakePolicy->assertOnPointerDownEquals(nullptr);
1058}
1059
1060// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
1061// DOWN on the window that already has focus. Ensure no window received the
1062// onPointerDownOutsideFocus callback.
1063TEST_F(InputDispatcherOnPointerDownOutsideFocus,
1064 OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08001065 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1066 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1067 mFocusedWindowTouchPoint, mFocusedWindowTouchPoint))
chaviwfd6d3512019-03-25 13:23:49 -07001068 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1069 // Call monitor to wait for the command queue to get flushed.
1070 mDispatcher->monitor();
1071
1072 mFakePolicy->assertOnPointerDownEquals(nullptr);
1073}
1074
Garfield Tane84e6f92019-08-29 17:28:41 -07001075} // namespace android::inputdispatcher