blob: 47397795ecc4ae5c7fee84bb54a0ba98686bd230 [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
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800639 std::vector<sp<InputWindowHandle>> inputWindowHandles;
640 inputWindowHandles.push_back(window);
Arthur Hungb92218b2018-08-14 12:00:21 +0800641
642 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800643 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
644 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +0800645 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
646
647 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800648 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800649}
650
651// The foreground window should receive the first touch down event.
652TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
653 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800654 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
655 ADISPLAY_ID_DEFAULT);
656 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
657 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800658
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);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800664 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
665 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +0800666 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
667
668 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800669 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800670 windowSecond->assertNoEvents();
671}
672
673TEST_F(InputDispatcherTest, SetInputWindow_FocusedWindow) {
674 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800675 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
676 ADISPLAY_ID_DEFAULT);
677 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
678 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800679
Arthur Hung7ab76b12019-01-09 19:17:20 +0800680 // Set focused application.
Tiger Huang721e26f2018-07-24 22:26:19 +0800681 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Arthur Hungb92218b2018-08-14 12:00:21 +0800682
683 // Expect one focus window exist in display.
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 Hungb92218b2018-08-14 12:00:21 +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 // Focused window should receive event.
694 windowTop->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800695 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +0800696}
697
Arthur Hung7ab76b12019-01-09 19:17:20 +0800698TEST_F(InputDispatcherTest, SetInputWindow_FocusPriority) {
699 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
700 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
701 ADISPLAY_ID_DEFAULT);
702 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
703 ADISPLAY_ID_DEFAULT);
704
705 // Set focused application.
706 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
707
708 // Display has two focused windows. Add them to inputWindowsHandles in z-order (top most first)
709 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 Hung7ab76b12019-01-09 19:17:20 +0800714
715 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
716 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
717 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
718
719 // Top focused window should receive event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800720 windowTop->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung7ab76b12019-01-09 19:17:20 +0800721 windowSecond->assertNoEvents();
722}
723
Arthur Hung3b413f22018-10-26 18:05:34 +0800724TEST_F(InputDispatcherTest, SetInputWindow_InputWindowInfo) {
725 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
726
727 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
728 ADISPLAY_ID_DEFAULT);
729 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
730 ADISPLAY_ID_DEFAULT);
731
Arthur Hung832bc4a2019-01-28 11:43:17 +0800732 // Set focused application.
733 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Arthur Hung3b413f22018-10-26 18:05:34 +0800734
Arthur Hung832bc4a2019-01-28 11:43:17 +0800735 windowTop->setFocus();
736 windowSecond->setFocus();
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800737 std::vector<sp<InputWindowHandle>> inputWindowHandles;
738 inputWindowHandles.push_back(windowTop);
739 inputWindowHandles.push_back(windowSecond);
Arthur Hung3b413f22018-10-26 18:05:34 +0800740 // Release channel for window is no longer valid.
741 windowTop->releaseChannel();
Arthur Hung832bc4a2019-01-28 11:43:17 +0800742 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
Arthur Hung3b413f22018-10-26 18:05:34 +0800743
Arthur Hung832bc4a2019-01-28 11:43:17 +0800744 // Test inject a key down, should dispatch to a valid window.
745 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
746 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Arthur Hung3b413f22018-10-26 18:05:34 +0800747
748 // Top window is invalid, so it should not receive any input event.
749 windowTop->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800750 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung3b413f22018-10-26 18:05:34 +0800751}
752
Garfield Tan00f511d2019-06-12 16:55:40 -0700753TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
754 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
755
756 sp<FakeWindowHandle> windowLeft =
757 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
758 windowLeft->setFrame(Rect(0, 0, 600, 800));
759 windowLeft->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL);
760 sp<FakeWindowHandle> windowRight =
761 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
762 windowRight->setFrame(Rect(600, 0, 1200, 800));
763 windowRight->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL);
764
765 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
766
767 std::vector<sp<InputWindowHandle>> inputWindowHandles{windowLeft, windowRight};
768 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
769
770 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
771 // left window. This event should be dispatched to the left window.
772 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
773 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
774 ADISPLAY_ID_DEFAULT, 610, 400, 599, 400));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800775 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -0700776 windowRight->assertNoEvents();
777}
778
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800779/* Test InputDispatcher for MultiDisplay */
780class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
781public:
782 static constexpr int32_t SECOND_DISPLAY_ID = 1;
783 virtual void SetUp() {
784 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +0800785
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800786 application1 = new FakeApplicationHandle();
787 windowInPrimary = new FakeWindowHandle(application1, mDispatcher, "D_1",
788 ADISPLAY_ID_DEFAULT);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800789 std::vector<sp<InputWindowHandle>> inputWindowHandles;
790 inputWindowHandles.push_back(windowInPrimary);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800791 // Set focus window for primary display, but focused display would be second one.
792 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
793 windowInPrimary->setFocus();
794 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800795
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800796 application2 = new FakeApplicationHandle();
797 windowInSecondary = new FakeWindowHandle(application2, mDispatcher, "D_2",
798 SECOND_DISPLAY_ID);
799 // Set focus to second display window.
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800800 std::vector<sp<InputWindowHandle>> inputWindowHandles_Second;
801 inputWindowHandles_Second.push_back(windowInSecondary);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800802 // Set focus display to second one.
803 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
804 // Set focus window for second display.
805 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
806 windowInSecondary->setFocus();
807 mDispatcher->setInputWindows(inputWindowHandles_Second, SECOND_DISPLAY_ID);
808 }
809
810 virtual void TearDown() {
811 InputDispatcherTest::TearDown();
812
813 application1.clear();
814 windowInPrimary.clear();
815 application2.clear();
816 windowInSecondary.clear();
817 }
818
819protected:
820 sp<FakeApplicationHandle> application1;
821 sp<FakeWindowHandle> windowInPrimary;
822 sp<FakeApplicationHandle> application2;
823 sp<FakeWindowHandle> windowInSecondary;
824};
825
826TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
827 // Test touch down on primary display.
828 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
829 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +0800830 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800831 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800832 windowInSecondary->assertNoEvents();
833
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800834 // Test touch down on second display.
835 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
836 AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Arthur Hungb92218b2018-08-14 12:00:21 +0800837 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
838 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800839 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +0800840}
841
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800842TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +0800843 // Test inject a key down with display id specified.
844 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
845 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800846 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +0800847 windowInSecondary->assertNoEvents();
848
849 // Test inject a key down without display id specified.
Arthur Hungb92218b2018-08-14 12:00:21 +0800850 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
851 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
852 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800853 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +0800854
855 // Remove secondary display.
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800856 std::vector<sp<InputWindowHandle>> noWindows;
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800857 mDispatcher->setInputWindows(noWindows, SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +0800858
859 // Expect old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800860 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
861 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +0800862
863 // Test inject a key down, should timeout because of no target window.
864 ASSERT_EQ(INPUT_EVENT_INJECTION_TIMED_OUT, injectKeyDown(mDispatcher))
865 << "Inject key event should return INPUT_EVENT_INJECTION_TIMED_OUT";
866 windowInPrimary->assertNoEvents();
867 windowInSecondary->assertNoEvents();
868}
869
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800870class FakeMonitorReceiver : public FakeInputReceiver, public RefBase {
871public:
872 FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name,
Michael Wright3dd60e22019-03-27 22:06:44 +0000873 int32_t displayId, bool isGestureMonitor = false)
874 : FakeInputReceiver(dispatcher, name, displayId) {
Michael Wright3dd60e22019-03-27 22:06:44 +0000875 mDispatcher->registerInputMonitor(mServerChannel, displayId, isGestureMonitor);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800876 }
877};
878
879// Test per-display input monitors for motion event.
880TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
881 sp<FakeMonitorReceiver> monitorInPrimary =
882 new FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
883 sp<FakeMonitorReceiver> monitorInSecondary =
884 new FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
885
886 // Test touch down on primary display.
887 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
888 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
889 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800890 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
891 monitorInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800892 windowInSecondary->assertNoEvents();
893 monitorInSecondary->assertNoEvents();
894
895 // Test touch down on second display.
896 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
897 AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
898 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
899 windowInPrimary->assertNoEvents();
900 monitorInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800901 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
902 monitorInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800903
904 // Test inject a non-pointer motion event.
905 // If specific a display, it will dispatch to the focused window of particular display,
906 // or it will dispatch to the focused window of focused display.
907 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
908 AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
909 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
910 windowInPrimary->assertNoEvents();
911 monitorInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800912 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
913 monitorInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800914}
915
916// Test per-display input monitors for key event.
917TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
918 //Input monitor per display.
919 sp<FakeMonitorReceiver> monitorInPrimary =
920 new FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
921 sp<FakeMonitorReceiver> monitorInSecondary =
922 new FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
923
924 // Test inject a key down.
925 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
926 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
927 windowInPrimary->assertNoEvents();
928 monitorInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800929 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
930 monitorInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800931}
932
Jackal Guof9696682018-10-05 12:23:23 +0800933class InputFilterTest : public InputDispatcherTest {
934protected:
935 static constexpr int32_t SECOND_DISPLAY_ID = 1;
936
937 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) {
938 NotifyMotionArgs motionArgs;
939
940 motionArgs = generateMotionArgs(
941 AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
942 mDispatcher->notifyMotion(&motionArgs);
943 motionArgs = generateMotionArgs(
944 AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
945 mDispatcher->notifyMotion(&motionArgs);
946
947 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800948 mFakePolicy->assertFilterInputEventWasCalled(motionArgs);
Jackal Guof9696682018-10-05 12:23:23 +0800949 } else {
950 mFakePolicy->assertFilterInputEventWasNotCalled();
951 }
952 }
953
954 void testNotifyKey(bool expectToBeFiltered) {
955 NotifyKeyArgs keyArgs;
956
957 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
958 mDispatcher->notifyKey(&keyArgs);
959 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
960 mDispatcher->notifyKey(&keyArgs);
961
962 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800963 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +0800964 } else {
965 mFakePolicy->assertFilterInputEventWasNotCalled();
966 }
967 }
968};
969
970// Test InputFilter for MotionEvent
971TEST_F(InputFilterTest, MotionEvent_InputFilter) {
972 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
973 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
974 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
975
976 // Enable InputFilter
977 mDispatcher->setInputFilterEnabled(true);
978 // Test touch on both primary and second display, and check if both events are filtered.
979 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
980 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
981
982 // Disable InputFilter
983 mDispatcher->setInputFilterEnabled(false);
984 // Test touch on both primary and second display, and check if both events aren't filtered.
985 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
986 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
987}
988
989// Test InputFilter for KeyEvent
990TEST_F(InputFilterTest, KeyEvent_InputFilter) {
991 // Since the InputFilter is disabled by default, check if key event aren't filtered.
992 testNotifyKey(/*expectToBeFiltered*/ false);
993
994 // Enable InputFilter
995 mDispatcher->setInputFilterEnabled(true);
996 // Send a key event, and check if it is filtered.
997 testNotifyKey(/*expectToBeFiltered*/ true);
998
999 // Disable InputFilter
1000 mDispatcher->setInputFilterEnabled(false);
1001 // Send a key event, and check if it isn't filtered.
1002 testNotifyKey(/*expectToBeFiltered*/ false);
1003}
1004
chaviwfd6d3512019-03-25 13:23:49 -07001005class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
1006 virtual void SetUp() {
1007 InputDispatcherTest::SetUp();
1008
1009 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1010 mUnfocusedWindow = new FakeWindowHandle(application, mDispatcher, "Top",
1011 ADISPLAY_ID_DEFAULT);
1012 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
1013 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
1014 // window.
1015 mUnfocusedWindow->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL);
1016
1017 mWindowFocused = new FakeWindowHandle(application, mDispatcher, "Second",
1018 ADISPLAY_ID_DEFAULT);
1019 mWindowFocused->setFrame(Rect(50, 50, 100, 100));
1020 mWindowFocused->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL);
1021 mWindowFocusedTouchPoint = 60;
1022
1023 // Set focused application.
1024 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1025 mWindowFocused->setFocus();
1026
1027 // Expect one focus window exist in display.
1028 std::vector<sp<InputWindowHandle>> inputWindowHandles;
1029 inputWindowHandles.push_back(mUnfocusedWindow);
1030 inputWindowHandles.push_back(mWindowFocused);
1031 mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT);
1032 }
1033
1034 virtual void TearDown() {
1035 InputDispatcherTest::TearDown();
1036
1037 mUnfocusedWindow.clear();
1038 mWindowFocused.clear();
1039 }
1040
1041protected:
1042 sp<FakeWindowHandle> mUnfocusedWindow;
1043 sp<FakeWindowHandle> mWindowFocused;
1044 int32_t mWindowFocusedTouchPoint;
1045};
1046
1047// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
1048// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
1049// the onPointerDownOutsideFocus callback.
1050TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
1051 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1052 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, 20, 20))
1053 << "Inject motion 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(mUnfocusedWindow->getToken());
1058}
1059
1060// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
1061// DOWN on the window that doesn't have focus. Ensure no window received the
1062// onPointerDownOutsideFocus callback.
1063TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
1064 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1065 AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, 20, 20))
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
1073// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
1074// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
1075TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
1076 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
1077 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1078 // Call monitor to wait for the command queue to get flushed.
1079 mDispatcher->monitor();
1080
1081 mFakePolicy->assertOnPointerDownEquals(nullptr);
1082}
1083
1084// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
1085// DOWN on the window that already has focus. Ensure no window received the
1086// onPointerDownOutsideFocus callback.
1087TEST_F(InputDispatcherOnPointerDownOutsideFocus,
1088 OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
1089 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1090 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, mWindowFocusedTouchPoint,
1091 mWindowFocusedTouchPoint))
1092 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1093 // Call monitor to wait for the command queue to get flushed.
1094 mDispatcher->monitor();
1095
1096 mFakePolicy->assertOnPointerDownEquals(nullptr);
1097}
1098
Garfield Tane84e6f92019-08-29 17:28:41 -07001099} // namespace android::inputdispatcher