blob: 094452a0c06492185ea493f8b3ec738143dcd2d1 [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
Robert Carr803535b2018-08-02 16:38:15 -070019#include <binder/Binder.h>
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080020#include <input/Input.h>
Robert Carr803535b2018-08-02 16:38:15 -070021
Michael Wrightd02c5b62014-02-10 15:10:22 -080022#include <gtest/gtest.h>
23#include <linux/input.h>
chaviwd1c23182019-12-20 18:44:56 -080024#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080025
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
chaviwd1c23182019-12-20 18:44:56 -080041struct PointF {
42 float x;
43 float y;
44};
Michael Wrightd02c5b62014-02-10 15:10:22 -080045
46// --- FakeInputDispatcherPolicy ---
47
48class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
49 InputDispatcherConfiguration mConfig;
50
51protected:
52 virtual ~FakeInputDispatcherPolicy() {
53 }
54
55public:
56 FakeInputDispatcherPolicy() {
Jackal Guof9696682018-10-05 12:23:23 +080057 }
58
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080059 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080060 assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_KEY, args.eventTime, args.action,
61 args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080062 }
63
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080064 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args) {
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080065 assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_MOTION, args.eventTime, args.action,
66 args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080067 }
68
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080069 void assertFilterInputEventWasNotCalled() { ASSERT_EQ(nullptr, mFilteredEvent); }
Michael Wrightd02c5b62014-02-10 15:10:22 -080070
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -080071 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
72 ASSERT_TRUE(mConfigurationChangedTime)
73 << "Timed out waiting for configuration changed call";
74 ASSERT_EQ(*mConfigurationChangedTime, when);
75 mConfigurationChangedTime = std::nullopt;
76 }
77
78 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
79 ASSERT_TRUE(mLastNotifySwitch);
80 // We do not check sequenceNum because it is not exposed to the policy
81 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
82 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
83 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
84 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
85 mLastNotifySwitch = std::nullopt;
86 }
87
chaviwfd6d3512019-03-25 13:23:49 -070088 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -080089 ASSERT_EQ(touchedToken, mOnPointerDownToken);
90 mOnPointerDownToken.clear();
91 }
92
93 void assertOnPointerDownWasNotCalled() {
94 ASSERT_TRUE(mOnPointerDownToken == nullptr)
95 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -070096 }
97
Michael Wrightd02c5b62014-02-10 15:10:22 -080098private:
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080099 std::unique_ptr<InputEvent> mFilteredEvent;
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800100 std::optional<nsecs_t> mConfigurationChangedTime;
chaviwfd6d3512019-03-25 13:23:49 -0700101 sp<IBinder> mOnPointerDownToken;
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800102 std::optional<NotifySwitchArgs> mLastNotifySwitch;
Jackal Guof9696682018-10-05 12:23:23 +0800103
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800104 virtual void notifyConfigurationChanged(nsecs_t when) override {
105 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800106 }
107
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100108 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>&,
Robert Carr803535b2018-08-02 16:38:15 -0700109 const sp<IBinder>&,
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800110 const std::string&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800111 return 0;
112 }
113
Robert Carr803535b2018-08-02 16:38:15 -0700114 virtual void notifyInputChannelBroken(const sp<IBinder>&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800115 }
116
chaviw0c06c6e2019-01-09 13:27:07 -0800117 virtual void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) {
Robert Carr740167f2018-10-11 19:03:41 -0700118 }
119
Michael Wrightd02c5b62014-02-10 15:10:22 -0800120 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
121 *outConfig = mConfig;
122 }
123
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800124 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Jackal Guof9696682018-10-05 12:23:23 +0800125 switch (inputEvent->getType()) {
126 case AINPUT_EVENT_TYPE_KEY: {
127 const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800128 mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800129 break;
130 }
131
132 case AINPUT_EVENT_TYPE_MOTION: {
133 const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800134 mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800135 break;
136 }
137 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800138 return true;
139 }
140
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100141 virtual void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800142 }
143
Charles Chen3611f1f2019-01-29 17:26:18 +0800144 virtual void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800145 }
146
Robert Carr803535b2018-08-02 16:38:15 -0700147 virtual nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&,
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100148 const KeyEvent*, uint32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800149 return 0;
150 }
151
Robert Carr803535b2018-08-02 16:38:15 -0700152 virtual bool dispatchUnhandledKey(const sp<IBinder>&,
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100153 const KeyEvent*, uint32_t, KeyEvent*) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800154 return false;
155 }
156
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800157 virtual void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
158 uint32_t policyFlags) override {
159 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
160 * essentially a passthrough for notifySwitch.
161 */
162 mLastNotifySwitch =
163 NotifySwitchArgs(1 /*sequenceNum*/, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800164 }
165
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100166 virtual void pokeUserActivity(nsecs_t, int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800167 }
168
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100169 virtual bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170 return false;
171 }
Jackal Guof9696682018-10-05 12:23:23 +0800172
chaviwfd6d3512019-03-25 13:23:49 -0700173 virtual void onPointerDownOutsideFocus(const sp<IBinder>& newToken) {
174 mOnPointerDownToken = newToken;
175 }
176
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800177 void assertFilterInputEventWasCalled(int type, nsecs_t eventTime, int32_t action,
178 int32_t displayId) {
179 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
180 ASSERT_EQ(mFilteredEvent->getType(), type);
181
182 if (type == AINPUT_EVENT_TYPE_KEY) {
183 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*mFilteredEvent);
184 EXPECT_EQ(keyEvent.getEventTime(), eventTime);
185 EXPECT_EQ(keyEvent.getAction(), action);
186 EXPECT_EQ(keyEvent.getDisplayId(), displayId);
187 } else if (type == AINPUT_EVENT_TYPE_MOTION) {
188 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*mFilteredEvent);
189 EXPECT_EQ(motionEvent.getEventTime(), eventTime);
190 EXPECT_EQ(motionEvent.getAction(), action);
191 EXPECT_EQ(motionEvent.getDisplayId(), displayId);
192 } else {
193 FAIL() << "Unknown type: " << type;
194 }
195
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800196 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800197 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800198};
199
200
201// --- InputDispatcherTest ---
202
203class InputDispatcherTest : public testing::Test {
204protected:
205 sp<FakeInputDispatcherPolicy> mFakePolicy;
206 sp<InputDispatcher> mDispatcher;
207
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700208 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800209 mFakePolicy = new FakeInputDispatcherPolicy();
210 mDispatcher = new InputDispatcher(mFakePolicy);
Arthur Hungb92218b2018-08-14 12:00:21 +0800211 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
212 //Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700213 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800214 }
215
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700216 virtual void TearDown() override {
217 ASSERT_EQ(OK, mDispatcher->stop());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800218 mFakePolicy.clear();
219 mDispatcher.clear();
220 }
221};
222
223
224TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
225 KeyEvent event;
226
227 // Rejects undefined key actions.
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600228 event.initialize(DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, INVALID_HMAC,
229 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
230 ARBITRARY_TIME);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800231 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800232 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800233 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
234 << "Should reject key events with undefined action.";
235
236 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600237 event.initialize(DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, INVALID_HMAC,
238 AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
239 ARBITRARY_TIME, ARBITRARY_TIME);
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 key events with ACTION_MULTIPLE.";
244}
245
246TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
247 MotionEvent event;
248 PointerProperties pointerProperties[MAX_POINTERS + 1];
249 PointerCoords pointerCoords[MAX_POINTERS + 1];
250 for (int i = 0; i <= MAX_POINTERS; i++) {
251 pointerProperties[i].clear();
252 pointerProperties[i].id = i;
253 pointerCoords[i].clear();
254 }
255
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800256 // Some constants commonly used below
257 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
258 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
259 constexpr int32_t metaState = AMETA_NONE;
260 constexpr MotionClassification classification = MotionClassification::NONE;
261
Michael Wrightd02c5b62014-02-10 15:10:22 -0800262 // Rejects undefined motion actions.
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600263 event.initialize(DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
264 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification, 1 /* xScale */,
265 1 /* yScale */, 0, 0, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
266 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700267 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800268 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800269 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800270 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
271 << "Should reject motion events with undefined action.";
272
273 // Rejects pointer down with invalid index.
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600274 event.initialize(DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700275 AMOTION_EVENT_ACTION_POINTER_DOWN |
276 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600277 0, 0, edgeFlags, metaState, 0, classification, 1 /* xScale */, 1 /* yScale */,
278 0, 0, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
279 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700280 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800281 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800282 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800283 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
284 << "Should reject motion events with pointer down index too large.";
285
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600286 event.initialize(DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700287 AMOTION_EVENT_ACTION_POINTER_DOWN |
288 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600289 0, 0, edgeFlags, metaState, 0, classification, 1 /* xScale */, 1 /* yScale */,
290 0, 0, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
291 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700292 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800293 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800294 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800295 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
296 << "Should reject motion events with pointer down index too small.";
297
298 // Rejects pointer up with invalid index.
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600299 event.initialize(DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700300 AMOTION_EVENT_ACTION_POINTER_UP |
301 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600302 0, 0, edgeFlags, metaState, 0, classification, 1 /* xScale */, 1 /* yScale */,
303 0, 0, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
304 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700305 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800306 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800307 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800308 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
309 << "Should reject motion events with pointer up index too large.";
310
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600311 event.initialize(DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700312 AMOTION_EVENT_ACTION_POINTER_UP |
313 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600314 0, 0, edgeFlags, metaState, 0, classification, 1 /* xScale */, 1 /* yScale */,
315 0, 0, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
316 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700317 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800318 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800319 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800320 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
321 << "Should reject motion events with pointer up index too small.";
322
323 // Rejects motion events with invalid number of pointers.
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600324 event.initialize(DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
325 edgeFlags, metaState, 0, classification, 1 /* xScale */, 1 /* yScale */, 0, 0,
326 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
327 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700328 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800329 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800330 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800331 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
332 << "Should reject motion events with 0 pointers.";
333
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600334 event.initialize(DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
335 edgeFlags, metaState, 0, classification, 1 /* xScale */, 1 /* yScale */, 0, 0,
336 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
337 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700338 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800339 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800340 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800341 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
342 << "Should reject motion events with more than MAX_POINTERS pointers.";
343
344 // Rejects motion events with invalid pointer ids.
345 pointerProperties[0].id = -1;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600346 event.initialize(DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
347 edgeFlags, metaState, 0, classification, 1 /* xScale */, 1 /* yScale */, 0, 0,
348 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
349 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700350 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800351 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800352 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800353 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
354 << "Should reject motion events with pointer ids less than 0.";
355
356 pointerProperties[0].id = MAX_POINTER_ID + 1;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600357 event.initialize(DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
358 edgeFlags, metaState, 0, classification, 1 /* xScale */, 1 /* yScale */, 0, 0,
359 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
360 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700361 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800362 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800363 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800364 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
365 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
366
367 // Rejects motion events with duplicate pointer ids.
368 pointerProperties[0].id = 1;
369 pointerProperties[1].id = 1;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600370 event.initialize(DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
371 edgeFlags, metaState, 0, classification, 1 /* xScale */, 1 /* yScale */, 0, 0,
372 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
373 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700374 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800375 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800376 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800377 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
378 << "Should reject motion events with duplicate pointer ids.";
379}
380
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800381/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
382
383TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
384 constexpr nsecs_t eventTime = 20;
385 NotifyConfigurationChangedArgs args(10 /*sequenceNum*/, eventTime);
386 mDispatcher->notifyConfigurationChanged(&args);
387 ASSERT_TRUE(mDispatcher->waitForIdle());
388
389 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
390}
391
392TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
393 NotifySwitchArgs args(10 /*sequenceNum*/, 20 /*eventTime*/, 0 /*policyFlags*/,
394 1 /*switchValues*/, 2 /*switchMask*/);
395 mDispatcher->notifySwitch(&args);
396
397 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
398 args.policyFlags |= POLICY_FLAG_TRUSTED;
399 mFakePolicy->assertNotifySwitchWasCalled(args);
400}
401
Arthur Hungb92218b2018-08-14 12:00:21 +0800402// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800403static constexpr int32_t INJECT_EVENT_TIMEOUT = 500;
404static constexpr int32_t DISPATCHING_TIMEOUT = 100;
Arthur Hungb92218b2018-08-14 12:00:21 +0800405
406class FakeApplicationHandle : public InputApplicationHandle {
407public:
408 FakeApplicationHandle() {}
409 virtual ~FakeApplicationHandle() {}
410
411 virtual bool updateInfo() {
Arthur Hung7a0c39a2019-03-20 16:52:24 +0800412 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Arthur Hungb92218b2018-08-14 12:00:21 +0800413 return true;
414 }
415};
416
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800417class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800418public:
chaviwd1c23182019-12-20 18:44:56 -0800419 explicit FakeInputReceiver(const sp<InputChannel>& clientChannel, const std::string name)
420 : mName(name) {
421 mConsumer = std::make_unique<InputConsumer>(clientChannel);
422 }
423
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800424 InputEvent* consume() {
Arthur Hungb92218b2018-08-14 12:00:21 +0800425 uint32_t consumeSeq;
426 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800427
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800428 std::chrono::time_point start = std::chrono::steady_clock::now();
429 status_t status = WOULD_BLOCK;
430 while (status == WOULD_BLOCK) {
chaviw81e2bb92019-12-18 15:03:51 -0800431 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800432 &event);
433 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
434 if (elapsed > 100ms) {
435 break;
436 }
437 }
438
439 if (status == WOULD_BLOCK) {
440 // Just means there's no event available.
441 return nullptr;
442 }
443
444 if (status != OK) {
445 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
446 return nullptr;
447 }
448 if (event == nullptr) {
449 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
450 return nullptr;
451 }
452
chaviwd1c23182019-12-20 18:44:56 -0800453 status = mConsumer->sendFinishedSignal(consumeSeq, true);
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800454 if (status != OK) {
455 ADD_FAILURE() << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
456 }
457 return event;
458 }
459
460 void consumeEvent(int32_t expectedEventType, int32_t expectedAction, int32_t expectedDisplayId,
461 int32_t expectedFlags) {
462 InputEvent* event = consume();
463
464 ASSERT_NE(nullptr, event) << mName.c_str()
465 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800466 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800467 << mName.c_str() << "expected " << inputEventTypeToString(expectedEventType)
468 << " event, got " << inputEventTypeToString(event->getType()) << " event";
Arthur Hungb92218b2018-08-14 12:00:21 +0800469
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800470 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
Tiger Huang8664f8c2018-10-11 19:14:35 +0800471
Tiger Huang8664f8c2018-10-11 19:14:35 +0800472 switch (expectedEventType) {
473 case AINPUT_EVENT_TYPE_KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800474 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
475 EXPECT_EQ(expectedAction, keyEvent.getAction());
476 EXPECT_EQ(expectedFlags, keyEvent.getFlags());
Tiger Huang8664f8c2018-10-11 19:14:35 +0800477 break;
478 }
479 case AINPUT_EVENT_TYPE_MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800480 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
481 EXPECT_EQ(expectedAction, motionEvent.getAction());
482 EXPECT_EQ(expectedFlags, motionEvent.getFlags());
Tiger Huang8664f8c2018-10-11 19:14:35 +0800483 break;
484 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100485 case AINPUT_EVENT_TYPE_FOCUS: {
486 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
487 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800488 default: {
489 FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType;
490 }
491 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800492 }
493
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100494 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
495 InputEvent* event = consume();
496 ASSERT_NE(nullptr, event) << mName.c_str()
497 << ": consumer should have returned non-NULL event.";
498 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
499 << "Got " << inputEventTypeToString(event->getType())
500 << " event instead of FOCUS event";
501
502 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
503 << mName.c_str() << ": event displayId should always be NONE.";
504
505 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
506 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
507 EXPECT_EQ(inTouchMode, focusEvent->getInTouchMode());
508 }
509
chaviwd1c23182019-12-20 18:44:56 -0800510 void assertNoEvents() {
511 InputEvent* event = consume();
512 ASSERT_EQ(nullptr, event)
513 << mName.c_str()
514 << ": should not have received any events, so consume() should return NULL";
515 }
516
517 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
518
519protected:
520 std::unique_ptr<InputConsumer> mConsumer;
521 PreallocatedInputEventFactory mEventFactory;
522
523 std::string mName;
524};
525
526class FakeWindowHandle : public InputWindowHandle {
527public:
528 static const int32_t WIDTH = 600;
529 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -0800530
531 FakeWindowHandle(const sp<InputApplicationHandle>& inputApplicationHandle,
532 const sp<InputDispatcher>& dispatcher, const std::string name,
533 int32_t displayId, sp<IBinder> token = nullptr)
534 : mName(name) {
535 if (token == nullptr) {
536 sp<InputChannel> serverChannel, clientChannel;
537 InputChannel::openInputChannelPair(name, serverChannel, clientChannel);
538 mInputReceiver = std::make_unique<FakeInputReceiver>(clientChannel, name);
539 dispatcher->registerInputChannel(serverChannel);
540 token = serverChannel->getConnectionToken();
541 }
542
543 inputApplicationHandle->updateInfo();
544 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
545
546 mInfo.token = token;
chaviwaf87b3e2019-10-01 16:59:28 -0700547 mInfo.id = 0;
chaviwd1c23182019-12-20 18:44:56 -0800548 mInfo.name = name;
549 mInfo.layoutParamsFlags = 0;
550 mInfo.layoutParamsType = InputWindowInfo::TYPE_APPLICATION;
551 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
552 mInfo.frameLeft = 0;
553 mInfo.frameTop = 0;
554 mInfo.frameRight = WIDTH;
555 mInfo.frameBottom = HEIGHT;
556 mInfo.globalScaleFactor = 1.0;
557 mInfo.touchableRegion.clear();
558 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
559 mInfo.visible = true;
560 mInfo.canReceiveKeys = true;
561 mInfo.hasFocus = false;
562 mInfo.hasWallpaper = false;
563 mInfo.paused = false;
chaviwd1c23182019-12-20 18:44:56 -0800564 mInfo.ownerPid = INJECTOR_PID;
565 mInfo.ownerUid = INJECTOR_UID;
566 mInfo.inputFeatures = 0;
567 mInfo.displayId = displayId;
568 }
569
570 virtual bool updateInfo() { return true; }
571
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100572 void setFocus(bool hasFocus) { mInfo.hasFocus = hasFocus; }
chaviwd1c23182019-12-20 18:44:56 -0800573
574 void setFrame(const Rect& frame) {
575 mInfo.frameLeft = frame.left;
576 mInfo.frameTop = frame.top;
577 mInfo.frameRight = frame.right;
578 mInfo.frameBottom = frame.bottom;
579 mInfo.touchableRegion.clear();
580 mInfo.addTouchableRegion(frame);
581 }
582
583 void setLayoutParamFlags(int32_t flags) { mInfo.layoutParamsFlags = flags; }
584
chaviwaf87b3e2019-10-01 16:59:28 -0700585 void setId(int32_t id) { mInfo.id = id; }
586
587 void setWindowScale(float xScale, float yScale) {
588 mInfo.windowXScale = xScale;
589 mInfo.windowYScale = yScale;
590 }
591
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800592 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
593 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
594 expectedFlags);
595 }
596
597 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
598 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
599 expectedFlags);
600 }
601
Michael Wright3a240c42019-12-10 20:53:41 +0000602 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
603 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
604 expectedFlags);
605 }
606
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100607 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
608 ASSERT_NE(mInputReceiver, nullptr)
609 << "Cannot consume events from a window with no receiver";
610 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
611 }
612
chaviwd1c23182019-12-20 18:44:56 -0800613 void consumeEvent(int32_t expectedEventType, int32_t expectedAction, int32_t expectedDisplayId,
614 int32_t expectedFlags) {
615 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
616 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
617 expectedFlags);
618 }
619
chaviwaf87b3e2019-10-01 16:59:28 -0700620 InputEvent* consume() {
621 if (mInputReceiver == nullptr) {
622 return nullptr;
623 }
624 return mInputReceiver->consume();
625 }
626
Arthur Hungb92218b2018-08-14 12:00:21 +0800627 void assertNoEvents() {
chaviwd1c23182019-12-20 18:44:56 -0800628 ASSERT_NE(mInputReceiver, nullptr)
629 << "Call 'assertNoEvents' on a window with an InputReceiver";
630 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +0800631 }
632
chaviwaf87b3e2019-10-01 16:59:28 -0700633 sp<IBinder> getToken() { return mInfo.token; }
634
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100635 const std::string& getName() { return mName; }
636
chaviwd1c23182019-12-20 18:44:56 -0800637private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100638 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -0800639 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800640};
641
Tiger Huang721e26f2018-07-24 22:26:19 +0800642static int32_t injectKeyDown(const sp<InputDispatcher>& dispatcher,
643 int32_t displayId = ADISPLAY_ID_NONE) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800644 KeyEvent event;
645 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
646
647 // Define a valid key down event.
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600648 event.initialize(DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, INVALID_HMAC,
649 AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE,
650 /* repeatCount */ 0, currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +0800651
652 // Inject event until dispatch out.
653 return dispatcher->injectInputEvent(
654 &event,
655 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT,
656 INJECT_EVENT_TIMEOUT, POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
657}
658
Garfield Tan00f511d2019-06-12 16:55:40 -0700659static int32_t injectMotionEvent(const sp<InputDispatcher>& dispatcher, int32_t action,
660 int32_t source, int32_t displayId, int32_t x, int32_t y,
661 int32_t xCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION,
662 int32_t yCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800663 MotionEvent event;
664 PointerProperties pointerProperties[1];
665 PointerCoords pointerCoords[1];
666
667 pointerProperties[0].clear();
668 pointerProperties[0].id = 0;
669 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
670
671 pointerCoords[0].clear();
chaviwfd6d3512019-03-25 13:23:49 -0700672 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
673 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Arthur Hungb92218b2018-08-14 12:00:21 +0800674
675 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
676 // Define a valid motion down event.
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600677 event.initialize(DEVICE_ID, source, displayId, INVALID_HMAC, action, /* actionButton */ 0,
678 /* flags */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -0700679 /* edgeFlags */ 0, AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600680 /* xScale */ 1, /* yScale */ 1, /* xOffset */ 0, /* yOffset */ 0,
681 /* xPrecision */ 0, /* yPrecision */ 0, xCursorPosition, yCursorPosition,
682 currentTime, currentTime,
Garfield Tan00f511d2019-06-12 16:55:40 -0700683 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Arthur Hungb92218b2018-08-14 12:00:21 +0800684
685 // Inject event until dispatch out.
686 return dispatcher->injectInputEvent(
687 &event,
688 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT,
689 INJECT_EVENT_TIMEOUT, POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
690}
691
Garfield Tan00f511d2019-06-12 16:55:40 -0700692static int32_t injectMotionDown(const sp<InputDispatcher>& dispatcher, int32_t source,
693 int32_t displayId, int32_t x = 100, int32_t y = 200) {
694 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, x, y);
695}
696
Michael Wright3a240c42019-12-10 20:53:41 +0000697static int32_t injectMotionUp(const sp<InputDispatcher>& dispatcher, int32_t source,
698 int32_t displayId, int32_t x = 100, int32_t y = 200) {
699 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, x, y);
700}
701
Jackal Guof9696682018-10-05 12:23:23 +0800702static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
703 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
704 // Define a valid key event.
705 NotifyKeyArgs args(/* sequenceNum */ 0, currentTime, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
706 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0,
707 AKEYCODE_A, KEY_A, AMETA_NONE, currentTime);
708
709 return args;
710}
711
chaviwd1c23182019-12-20 18:44:56 -0800712static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId,
713 const std::vector<PointF>& points) {
714 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -0700715 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
716 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
717 }
718
chaviwd1c23182019-12-20 18:44:56 -0800719 PointerProperties pointerProperties[pointerCount];
720 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +0800721
chaviwd1c23182019-12-20 18:44:56 -0800722 for (size_t i = 0; i < pointerCount; i++) {
723 pointerProperties[i].clear();
724 pointerProperties[i].id = i;
725 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jackal Guof9696682018-10-05 12:23:23 +0800726
chaviwd1c23182019-12-20 18:44:56 -0800727 pointerCoords[i].clear();
728 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
729 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
730 }
Jackal Guof9696682018-10-05 12:23:23 +0800731
732 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
733 // Define a valid motion event.
734 NotifyMotionArgs args(/* sequenceNum */ 0, currentTime, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -0700735 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
736 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -0800737 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
738 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -0700739 AMOTION_EVENT_INVALID_CURSOR_POSITION,
740 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +0800741
742 return args;
743}
744
chaviwd1c23182019-12-20 18:44:56 -0800745static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
746 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
747}
748
Arthur Hungb92218b2018-08-14 12:00:21 +0800749TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
750 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800751 sp<FakeWindowHandle> window = new FakeWindowHandle(application, mDispatcher, "Fake Window",
752 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800753
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -0800754 mDispatcher->setInputWindows({window}, ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800755 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
756 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +0800757 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
758
759 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800760 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800761}
762
763// The foreground window should receive the first touch down event.
764TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
765 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800766 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
767 ADISPLAY_ID_DEFAULT);
768 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
769 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800770
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -0800771 mDispatcher->setInputWindows({windowTop, windowSecond}, ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800772 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
773 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +0800774 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
775
776 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800777 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800778 windowSecond->assertNoEvents();
779}
780
781TEST_F(InputDispatcherTest, SetInputWindow_FocusedWindow) {
782 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800783 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
784 ADISPLAY_ID_DEFAULT);
785 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
786 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800787
Arthur Hung7ab76b12019-01-09 19:17:20 +0800788 // Set focused application.
Tiger Huang721e26f2018-07-24 22:26:19 +0800789 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Arthur Hungb92218b2018-08-14 12:00:21 +0800790
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100791 // Display should have only one focused window
792 windowSecond->setFocus(true);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -0800793 mDispatcher->setInputWindows({windowTop, windowSecond}, ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100794
795 windowSecond->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +0800796 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
797 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
798
799 // Focused window should receive event.
800 windowTop->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800801 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +0800802}
803
Arthur Hung7ab76b12019-01-09 19:17:20 +0800804TEST_F(InputDispatcherTest, SetInputWindow_FocusPriority) {
805 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
806 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
807 ADISPLAY_ID_DEFAULT);
808 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
809 ADISPLAY_ID_DEFAULT);
810
811 // Set focused application.
812 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
813
814 // Display has two focused windows. Add them to inputWindowsHandles in z-order (top most first)
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100815 windowTop->setFocus(true);
816 windowSecond->setFocus(true);
Arthur Hung7ab76b12019-01-09 19:17:20 +0800817
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -0800818 mDispatcher->setInputWindows({windowTop, windowSecond}, ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100819 windowTop->consumeFocusEvent(true);
Arthur Hung7ab76b12019-01-09 19:17:20 +0800820 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
821 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
822
823 // Top focused window should receive event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800824 windowTop->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung7ab76b12019-01-09 19:17:20 +0800825 windowSecond->assertNoEvents();
826}
827
Arthur Hung3b413f22018-10-26 18:05:34 +0800828TEST_F(InputDispatcherTest, SetInputWindow_InputWindowInfo) {
829 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
830
831 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
832 ADISPLAY_ID_DEFAULT);
833 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
834 ADISPLAY_ID_DEFAULT);
835
Arthur Hung832bc4a2019-01-28 11:43:17 +0800836 // Set focused application.
837 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Arthur Hung3b413f22018-10-26 18:05:34 +0800838
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100839 windowTop->setFocus(true);
840 windowSecond->setFocus(true);
Arthur Hung3b413f22018-10-26 18:05:34 +0800841 // Release channel for window is no longer valid.
842 windowTop->releaseChannel();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -0800843 mDispatcher->setInputWindows({windowTop, windowSecond}, ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100844 windowSecond->consumeFocusEvent(true);
Arthur Hung3b413f22018-10-26 18:05:34 +0800845
Arthur Hung832bc4a2019-01-28 11:43:17 +0800846 // Test inject a key down, should dispatch to a valid window.
847 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
848 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Arthur Hung3b413f22018-10-26 18:05:34 +0800849
850 // Top window is invalid, so it should not receive any input event.
851 windowTop->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800852 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung3b413f22018-10-26 18:05:34 +0800853}
854
Garfield Tan00f511d2019-06-12 16:55:40 -0700855TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
856 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
857
858 sp<FakeWindowHandle> windowLeft =
859 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
860 windowLeft->setFrame(Rect(0, 0, 600, 800));
861 windowLeft->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL);
862 sp<FakeWindowHandle> windowRight =
863 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
864 windowRight->setFrame(Rect(600, 0, 1200, 800));
865 windowRight->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL);
866
867 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
868
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -0800869 mDispatcher->setInputWindows({windowLeft, windowRight}, ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -0700870
871 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
872 // left window. This event should be dispatched to the left window.
873 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
874 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
875 ADISPLAY_ID_DEFAULT, 610, 400, 599, 400));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800876 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -0700877 windowRight->assertNoEvents();
878}
879
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800880TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
881 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
882 sp<FakeWindowHandle> window =
883 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100884 window->setFocus(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800885
886 mDispatcher->setInputWindows({window}, ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100887 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800888
889 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
890 mDispatcher->notifyKey(&keyArgs);
891
892 // Window should receive key down event.
893 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
894
895 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
896 // on the app side.
897 NotifyDeviceResetArgs args(10 /*sequenceNum*/, 20 /*eventTime*/, DEVICE_ID);
898 mDispatcher->notifyDeviceReset(&args);
899 window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
900 AKEY_EVENT_FLAG_CANCELED);
901}
902
903TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
904 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
905 sp<FakeWindowHandle> window =
906 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
907
908 mDispatcher->setInputWindows({window}, ADISPLAY_ID_DEFAULT);
909
910 NotifyMotionArgs motionArgs =
911 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
912 ADISPLAY_ID_DEFAULT);
913 mDispatcher->notifyMotion(&motionArgs);
914
915 // Window should receive motion down event.
916 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
917
918 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
919 // on the app side.
920 NotifyDeviceResetArgs args(10 /*sequenceNum*/, 20 /*eventTime*/, DEVICE_ID);
921 mDispatcher->notifyDeviceReset(&args);
922 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT,
923 0 /*expectedFlags*/);
924}
925
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100926TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
927 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
928 sp<FakeWindowHandle> window =
929 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
930
931 window->setFocus(true);
932 mDispatcher->setInputWindows({window}, ADISPLAY_ID_DEFAULT);
933
934 window->consumeFocusEvent(true);
935
936 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
937 mDispatcher->notifyKey(&keyArgs);
938
939 // Window should receive key down event.
940 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
941}
942
943TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
944 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
945 sp<FakeWindowHandle> window =
946 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
947
948 mDispatcher->setInputWindows({window}, ADISPLAY_ID_DEFAULT);
949
950 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
951 mDispatcher->notifyKey(&keyArgs);
952 mDispatcher->waitForIdle();
953
954 window->assertNoEvents();
955}
956
957// If a window is touchable, but does not have focus, it should receive motion events, but not keys
958TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
959 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
960 sp<FakeWindowHandle> window =
961 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
962
963 mDispatcher->setInputWindows({window}, ADISPLAY_ID_DEFAULT);
964
965 // Send key
966 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
967 mDispatcher->notifyKey(&keyArgs);
968 // Send motion
969 NotifyMotionArgs motionArgs =
970 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
971 ADISPLAY_ID_DEFAULT);
972 mDispatcher->notifyMotion(&motionArgs);
973
974 // Window should receive only the motion event
975 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
976 window->assertNoEvents(); // Key event or focus event will not be received
977}
978
chaviwd1c23182019-12-20 18:44:56 -0800979class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +0000980public:
981 FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name,
chaviwd1c23182019-12-20 18:44:56 -0800982 int32_t displayId, bool isGestureMonitor = false) {
983 sp<InputChannel> serverChannel, clientChannel;
984 InputChannel::openInputChannelPair(name, serverChannel, clientChannel);
985 mInputReceiver = std::make_unique<FakeInputReceiver>(clientChannel, name);
986 dispatcher->registerInputMonitor(serverChannel, displayId, isGestureMonitor);
Michael Wright3a240c42019-12-10 20:53:41 +0000987 }
988
chaviwd1c23182019-12-20 18:44:56 -0800989 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
990
991 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
992 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN,
993 expectedDisplayId, expectedFlags);
994 }
995
996 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
997 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
998 expectedDisplayId, expectedFlags);
999 }
1000
1001 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1002 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP,
1003 expectedDisplayId, expectedFlags);
1004 }
1005
1006 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
1007
1008private:
1009 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00001010};
1011
1012// Tests for gesture monitors
1013TEST_F(InputDispatcherTest, GestureMonitor_ReceivesMotionEvents) {
1014 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1015 sp<FakeWindowHandle> window =
1016 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1017 mDispatcher->setInputWindows({window}, ADISPLAY_ID_DEFAULT);
1018
chaviwd1c23182019-12-20 18:44:56 -08001019 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
1020 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00001021
1022 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1023 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1024 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1025 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001026 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00001027}
1028
1029TEST_F(InputDispatcherTest, GestureMonitor_DoesNotReceiveKeyEvents) {
1030 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1031 sp<FakeWindowHandle> window =
1032 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1033
1034 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001035 window->setFocus(true);
Michael Wright3a240c42019-12-10 20:53:41 +00001036
1037 mDispatcher->setInputWindows({window}, ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001038 window->consumeFocusEvent(true);
Michael Wright3a240c42019-12-10 20:53:41 +00001039
chaviwd1c23182019-12-20 18:44:56 -08001040 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
1041 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00001042
1043 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
1044 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1045 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001046 monitor.assertNoEvents();
Michael Wright3a240c42019-12-10 20:53:41 +00001047}
1048
1049TEST_F(InputDispatcherTest, GestureMonitor_CanPilferAfterWindowIsRemovedMidStream) {
1050 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1051 sp<FakeWindowHandle> window =
1052 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1053 mDispatcher->setInputWindows({window}, ADISPLAY_ID_DEFAULT);
1054
chaviwd1c23182019-12-20 18:44:56 -08001055 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
1056 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00001057
1058 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1059 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1060 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1061 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001062 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00001063
1064 window->releaseChannel();
1065
chaviwd1c23182019-12-20 18:44:56 -08001066 mDispatcher->pilferPointers(monitor.getToken());
Michael Wright3a240c42019-12-10 20:53:41 +00001067
1068 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1069 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1070 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08001071 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00001072}
1073
chaviw81e2bb92019-12-18 15:03:51 -08001074TEST_F(InputDispatcherTest, TestMoveEvent) {
1075 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1076 sp<FakeWindowHandle> window =
1077 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1078
1079 mDispatcher->setInputWindows({window}, ADISPLAY_ID_DEFAULT);
1080
1081 NotifyMotionArgs motionArgs =
1082 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1083 ADISPLAY_ID_DEFAULT);
1084
1085 mDispatcher->notifyMotion(&motionArgs);
1086 // Window should receive motion down event.
1087 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1088
1089 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
1090 motionArgs.sequenceNum += 1;
1091 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1092 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
1093 motionArgs.pointerCoords[0].getX() - 10);
1094
1095 mDispatcher->notifyMotion(&motionArgs);
1096 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
1097 0 /*expectedFlags*/);
1098}
1099
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001100/**
1101 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
1102 * the device default right away. In the test scenario, we check both the default value,
1103 * and the action of enabling / disabling.
1104 */
1105TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
1106 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1107 sp<FakeWindowHandle> window =
1108 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
1109
1110 // Set focused application.
1111 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1112 window->setFocus(true);
1113
1114 SCOPED_TRACE("Check default value of touch mode");
1115 mDispatcher->setInputWindows({window}, ADISPLAY_ID_DEFAULT);
1116 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
1117
1118 SCOPED_TRACE("Remove the window to trigger focus loss");
1119 window->setFocus(false);
1120 mDispatcher->setInputWindows({window}, ADISPLAY_ID_DEFAULT);
1121 window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/);
1122
1123 SCOPED_TRACE("Disable touch mode");
1124 mDispatcher->setInTouchMode(false);
1125 window->setFocus(true);
1126 mDispatcher->setInputWindows({window}, ADISPLAY_ID_DEFAULT);
1127 window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/);
1128
1129 SCOPED_TRACE("Remove the window to trigger focus loss");
1130 window->setFocus(false);
1131 mDispatcher->setInputWindows({window}, ADISPLAY_ID_DEFAULT);
1132 window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/);
1133
1134 SCOPED_TRACE("Enable touch mode again");
1135 mDispatcher->setInTouchMode(true);
1136 window->setFocus(true);
1137 mDispatcher->setInputWindows({window}, ADISPLAY_ID_DEFAULT);
1138 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
1139
1140 window->assertNoEvents();
1141}
1142
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001143/* Test InputDispatcher for MultiDisplay */
1144class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
1145public:
1146 static constexpr int32_t SECOND_DISPLAY_ID = 1;
Prabir Pradhan3608aad2019-10-02 17:08:26 -07001147 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001148 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08001149
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001150 application1 = new FakeApplicationHandle();
1151 windowInPrimary = new FakeWindowHandle(application1, mDispatcher, "D_1",
1152 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08001153
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001154 // Set focus window for primary display, but focused display would be second one.
1155 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001156 windowInPrimary->setFocus(true);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08001157 mDispatcher->setInputWindows({windowInPrimary}, ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001158 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08001159
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001160 application2 = new FakeApplicationHandle();
1161 windowInSecondary = new FakeWindowHandle(application2, mDispatcher, "D_2",
1162 SECOND_DISPLAY_ID);
1163 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001164 // Set focus display to second one.
1165 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
1166 // Set focus window for second display.
1167 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001168 windowInSecondary->setFocus(true);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08001169 mDispatcher->setInputWindows({windowInSecondary}, SECOND_DISPLAY_ID);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001170 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001171 }
1172
Prabir Pradhan3608aad2019-10-02 17:08:26 -07001173 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001174 InputDispatcherTest::TearDown();
1175
1176 application1.clear();
1177 windowInPrimary.clear();
1178 application2.clear();
1179 windowInSecondary.clear();
1180 }
1181
1182protected:
1183 sp<FakeApplicationHandle> application1;
1184 sp<FakeWindowHandle> windowInPrimary;
1185 sp<FakeApplicationHandle> application2;
1186 sp<FakeWindowHandle> windowInSecondary;
1187};
1188
1189TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
1190 // Test touch down on primary display.
1191 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1192 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +08001193 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001194 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001195 windowInSecondary->assertNoEvents();
1196
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001197 // Test touch down on second display.
1198 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1199 AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Arthur Hungb92218b2018-08-14 12:00:21 +08001200 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1201 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001202 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08001203}
1204
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001205TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08001206 // Test inject a key down with display id specified.
1207 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
1208 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001209 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08001210 windowInSecondary->assertNoEvents();
1211
1212 // Test inject a key down without display id specified.
Arthur Hungb92218b2018-08-14 12:00:21 +08001213 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
1214 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1215 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001216 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08001217
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08001218 // Remove all windows in secondary display.
1219 mDispatcher->setInputWindows({}, SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08001220
1221 // Expect old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001222 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
1223 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08001224
1225 // Test inject a key down, should timeout because of no target window.
1226 ASSERT_EQ(INPUT_EVENT_INJECTION_TIMED_OUT, injectKeyDown(mDispatcher))
1227 << "Inject key event should return INPUT_EVENT_INJECTION_TIMED_OUT";
1228 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001229 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08001230 windowInSecondary->assertNoEvents();
1231}
1232
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001233// Test per-display input monitors for motion event.
1234TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08001235 FakeMonitorReceiver monitorInPrimary =
1236 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
1237 FakeMonitorReceiver monitorInSecondary =
1238 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001239
1240 // Test touch down on primary display.
1241 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1242 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1243 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001244 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001245 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001246 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08001247 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001248
1249 // Test touch down on second display.
1250 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1251 AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
1252 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1253 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08001254 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001255 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08001256 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001257
1258 // Test inject a non-pointer motion event.
1259 // If specific a display, it will dispatch to the focused window of particular display,
1260 // or it will dispatch to the focused window of focused display.
1261 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1262 AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
1263 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1264 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08001265 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001266 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08001267 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001268}
1269
1270// Test per-display input monitors for key event.
1271TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
1272 //Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08001273 FakeMonitorReceiver monitorInPrimary =
1274 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
1275 FakeMonitorReceiver monitorInSecondary =
1276 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001277
1278 // Test inject a key down.
1279 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
1280 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1281 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08001282 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001283 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08001284 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001285}
1286
Jackal Guof9696682018-10-05 12:23:23 +08001287class InputFilterTest : public InputDispatcherTest {
1288protected:
1289 static constexpr int32_t SECOND_DISPLAY_ID = 1;
1290
1291 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) {
1292 NotifyMotionArgs motionArgs;
1293
1294 motionArgs = generateMotionArgs(
1295 AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
1296 mDispatcher->notifyMotion(&motionArgs);
1297 motionArgs = generateMotionArgs(
1298 AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
1299 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001300 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08001301 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08001302 mFakePolicy->assertFilterInputEventWasCalled(motionArgs);
Jackal Guof9696682018-10-05 12:23:23 +08001303 } else {
1304 mFakePolicy->assertFilterInputEventWasNotCalled();
1305 }
1306 }
1307
1308 void testNotifyKey(bool expectToBeFiltered) {
1309 NotifyKeyArgs keyArgs;
1310
1311 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
1312 mDispatcher->notifyKey(&keyArgs);
1313 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
1314 mDispatcher->notifyKey(&keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001315 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08001316
1317 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08001318 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08001319 } else {
1320 mFakePolicy->assertFilterInputEventWasNotCalled();
1321 }
1322 }
1323};
1324
1325// Test InputFilter for MotionEvent
1326TEST_F(InputFilterTest, MotionEvent_InputFilter) {
1327 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
1328 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
1329 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
1330
1331 // Enable InputFilter
1332 mDispatcher->setInputFilterEnabled(true);
1333 // Test touch on both primary and second display, and check if both events are filtered.
1334 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
1335 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
1336
1337 // Disable InputFilter
1338 mDispatcher->setInputFilterEnabled(false);
1339 // Test touch on both primary and second display, and check if both events aren't filtered.
1340 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
1341 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
1342}
1343
1344// Test InputFilter for KeyEvent
1345TEST_F(InputFilterTest, KeyEvent_InputFilter) {
1346 // Since the InputFilter is disabled by default, check if key event aren't filtered.
1347 testNotifyKey(/*expectToBeFiltered*/ false);
1348
1349 // Enable InputFilter
1350 mDispatcher->setInputFilterEnabled(true);
1351 // Send a key event, and check if it is filtered.
1352 testNotifyKey(/*expectToBeFiltered*/ true);
1353
1354 // Disable InputFilter
1355 mDispatcher->setInputFilterEnabled(false);
1356 // Send a key event, and check if it isn't filtered.
1357 testNotifyKey(/*expectToBeFiltered*/ false);
1358}
1359
chaviwfd6d3512019-03-25 13:23:49 -07001360class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07001361 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07001362 InputDispatcherTest::SetUp();
1363
1364 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1365 mUnfocusedWindow = new FakeWindowHandle(application, mDispatcher, "Top",
1366 ADISPLAY_ID_DEFAULT);
1367 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
1368 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
1369 // window.
1370 mUnfocusedWindow->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL);
1371
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08001372 mFocusedWindow =
1373 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
1374 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
1375 mFocusedWindow->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL);
1376 mFocusedWindowTouchPoint = 60;
chaviwfd6d3512019-03-25 13:23:49 -07001377
1378 // Set focused application.
1379 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001380 mFocusedWindow->setFocus(true);
chaviwfd6d3512019-03-25 13:23:49 -07001381
1382 // Expect one focus window exist in display.
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08001383 mDispatcher->setInputWindows({mUnfocusedWindow, mFocusedWindow}, ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001384 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07001385 }
1386
Prabir Pradhan3608aad2019-10-02 17:08:26 -07001387 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07001388 InputDispatcherTest::TearDown();
1389
1390 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08001391 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07001392 }
1393
1394protected:
1395 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08001396 sp<FakeWindowHandle> mFocusedWindow;
1397 int32_t mFocusedWindowTouchPoint;
chaviwfd6d3512019-03-25 13:23:49 -07001398};
1399
1400// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
1401// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
1402// the onPointerDownOutsideFocus callback.
1403TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
1404 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1405 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, 20, 20))
1406 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
chaviwfd6d3512019-03-25 13:23:49 -07001407
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001408 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07001409 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
1410}
1411
1412// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
1413// DOWN on the window that doesn't have focus. Ensure no window received the
1414// onPointerDownOutsideFocus callback.
1415TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
1416 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1417 AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, 20, 20))
1418 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
chaviwfd6d3512019-03-25 13:23:49 -07001419
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001420 ASSERT_TRUE(mDispatcher->waitForIdle());
1421 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07001422}
1423
1424// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
1425// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
1426TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
1427 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
1428 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
chaviwfd6d3512019-03-25 13:23:49 -07001429
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001430 ASSERT_TRUE(mDispatcher->waitForIdle());
1431 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07001432}
1433
1434// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
1435// DOWN on the window that already has focus. Ensure no window received the
1436// onPointerDownOutsideFocus callback.
1437TEST_F(InputDispatcherOnPointerDownOutsideFocus,
1438 OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08001439 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1440 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1441 mFocusedWindowTouchPoint, mFocusedWindowTouchPoint))
chaviwfd6d3512019-03-25 13:23:49 -07001442 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
chaviwfd6d3512019-03-25 13:23:49 -07001443
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001444 ASSERT_TRUE(mDispatcher->waitForIdle());
1445 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07001446}
1447
chaviwaf87b3e2019-10-01 16:59:28 -07001448// These tests ensures we can send touch events to a single client when there are multiple input
1449// windows that point to the same client token.
1450class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
1451 virtual void SetUp() override {
1452 InputDispatcherTest::SetUp();
1453
1454 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1455 mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1",
1456 ADISPLAY_ID_DEFAULT);
1457 // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window.
1458 // We also need FLAG_SPLIT_TOUCH or we won't be able to get touches for both windows.
1459 mWindow1->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL |
1460 InputWindowInfo::FLAG_SPLIT_TOUCH);
1461 mWindow1->setId(0);
1462 mWindow1->setFrame(Rect(0, 0, 100, 100));
1463
1464 mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2",
1465 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
1466 mWindow2->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL |
1467 InputWindowInfo::FLAG_SPLIT_TOUCH);
1468 mWindow2->setId(1);
1469 mWindow2->setFrame(Rect(100, 100, 200, 200));
1470
1471 mDispatcher->setInputWindows({mWindow1, mWindow2}, ADISPLAY_ID_DEFAULT);
1472 }
1473
1474protected:
1475 sp<FakeWindowHandle> mWindow1;
1476 sp<FakeWindowHandle> mWindow2;
1477
1478 // Helper function to convert the point from screen coordinates into the window's space
1479 static PointF getPointInWindow(const InputWindowInfo* windowInfo, const PointF& point) {
1480 float x = windowInfo->windowXScale * (point.x - windowInfo->frameLeft);
1481 float y = windowInfo->windowYScale * (point.y - windowInfo->frameTop);
1482 return {x, y};
1483 }
1484
1485 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
1486 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001487 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07001488 InputEvent* event = window->consume();
1489
1490 ASSERT_NE(nullptr, event) << name.c_str()
1491 << ": consumer should have returned non-NULL event.";
1492
1493 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
1494 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
1495 << " event, got " << inputEventTypeToString(event->getType()) << " event";
1496
1497 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
1498 EXPECT_EQ(expectedAction, motionEvent.getAction());
1499
1500 for (size_t i = 0; i < points.size(); i++) {
1501 float expectedX = points[i].x;
1502 float expectedY = points[i].y;
1503
1504 EXPECT_EQ(expectedX, motionEvent.getX(i))
1505 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
1506 << ", got " << motionEvent.getX(i);
1507 EXPECT_EQ(expectedY, motionEvent.getY(i))
1508 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
1509 << ", got " << motionEvent.getY(i);
1510 }
1511 }
1512};
1513
1514TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
1515 // Touch Window 1
1516 PointF touchedPoint = {10, 10};
1517 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
1518
1519 NotifyMotionArgs motionArgs =
1520 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1521 ADISPLAY_ID_DEFAULT, {touchedPoint});
1522 mDispatcher->notifyMotion(&motionArgs);
1523 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, {expectedPoint});
1524
1525 // Release touch on Window 1
1526 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1527 ADISPLAY_ID_DEFAULT, {touchedPoint});
1528 mDispatcher->notifyMotion(&motionArgs);
1529 // consume the UP event
1530 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_UP, {expectedPoint});
1531
1532 // Touch Window 2
1533 touchedPoint = {150, 150};
1534 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
1535
1536 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1537 ADISPLAY_ID_DEFAULT, {touchedPoint});
1538 mDispatcher->notifyMotion(&motionArgs);
1539
1540 // Consuming from window1 since it's the window that has the InputReceiver
1541 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, {expectedPoint});
1542}
1543
1544TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentScale) {
1545 mWindow2->setWindowScale(0.5f, 0.5f);
1546
1547 // Touch Window 1
1548 PointF touchedPoint = {10, 10};
1549 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
1550
1551 NotifyMotionArgs motionArgs =
1552 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1553 ADISPLAY_ID_DEFAULT, {touchedPoint});
1554 mDispatcher->notifyMotion(&motionArgs);
1555 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, {expectedPoint});
1556
1557 // Release touch on Window 1
1558 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1559 ADISPLAY_ID_DEFAULT, {touchedPoint});
1560 mDispatcher->notifyMotion(&motionArgs);
1561 // consume the UP event
1562 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_UP, {expectedPoint});
1563
1564 // Touch Window 2
1565 touchedPoint = {150, 150};
1566 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
1567
1568 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1569 ADISPLAY_ID_DEFAULT, {touchedPoint});
1570 mDispatcher->notifyMotion(&motionArgs);
1571
1572 // Consuming from window1 since it's the window that has the InputReceiver
1573 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, {expectedPoint});
1574}
1575
Chavi Weingarten65f98b82020-01-16 18:56:50 +00001576TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentScale) {
1577 mWindow2->setWindowScale(0.5f, 0.5f);
1578
1579 // Touch Window 1
1580 std::vector<PointF> touchedPoints = {PointF{10, 10}};
1581 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
1582
1583 NotifyMotionArgs motionArgs =
1584 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1585 ADISPLAY_ID_DEFAULT, touchedPoints);
1586 mDispatcher->notifyMotion(&motionArgs);
1587 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, expectedPoints);
1588
1589 // Touch Window 2
1590 int32_t actionPointerDown =
1591 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
1592 touchedPoints.emplace_back(PointF{150, 150});
1593 expectedPoints.emplace_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
1594
1595 motionArgs = generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN,
1596 ADISPLAY_ID_DEFAULT, touchedPoints);
1597 mDispatcher->notifyMotion(&motionArgs);
1598
1599 // Consuming from window1 since it's the window that has the InputReceiver
1600 consumeMotionEvent(mWindow1, actionPointerDown, expectedPoints);
1601}
1602
1603TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentScale) {
1604 mWindow2->setWindowScale(0.5f, 0.5f);
1605
1606 // Touch Window 1
1607 std::vector<PointF> touchedPoints = {PointF{10, 10}};
1608 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
1609
1610 NotifyMotionArgs motionArgs =
1611 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1612 ADISPLAY_ID_DEFAULT, touchedPoints);
1613 mDispatcher->notifyMotion(&motionArgs);
1614 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, expectedPoints);
1615
1616 // Touch Window 2
1617 int32_t actionPointerDown =
1618 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
1619 touchedPoints.emplace_back(PointF{150, 150});
1620 expectedPoints.emplace_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
1621
1622 motionArgs = generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN,
1623 ADISPLAY_ID_DEFAULT, touchedPoints);
1624 mDispatcher->notifyMotion(&motionArgs);
1625
1626 // Consuming from window1 since it's the window that has the InputReceiver
1627 consumeMotionEvent(mWindow1, actionPointerDown, expectedPoints);
1628
1629 // Move both windows
1630 touchedPoints = {{20, 20}, {175, 175}};
1631 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
1632 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
1633
1634 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1635 ADISPLAY_ID_DEFAULT, touchedPoints);
1636 mDispatcher->notifyMotion(&motionArgs);
1637
1638 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_MOVE, expectedPoints);
1639}
1640
1641TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
1642 mWindow1->setWindowScale(0.5f, 0.5f);
1643
1644 // Touch Window 1
1645 std::vector<PointF> touchedPoints = {PointF{10, 10}};
1646 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
1647
1648 NotifyMotionArgs motionArgs =
1649 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1650 ADISPLAY_ID_DEFAULT, touchedPoints);
1651 mDispatcher->notifyMotion(&motionArgs);
1652 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, expectedPoints);
1653
1654 // Touch Window 2
1655 int32_t actionPointerDown =
1656 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
1657 touchedPoints.emplace_back(PointF{150, 150});
1658 expectedPoints.emplace_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
1659
1660 motionArgs = generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN,
1661 ADISPLAY_ID_DEFAULT, touchedPoints);
1662 mDispatcher->notifyMotion(&motionArgs);
1663
1664 // Consuming from window1 since it's the window that has the InputReceiver
1665 consumeMotionEvent(mWindow1, actionPointerDown, expectedPoints);
1666
1667 // Move both windows
1668 touchedPoints = {{20, 20}, {175, 175}};
1669 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
1670 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
1671
1672 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1673 ADISPLAY_ID_DEFAULT, touchedPoints);
1674 mDispatcher->notifyMotion(&motionArgs);
1675
1676 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_MOVE, expectedPoints);
1677}
1678
Garfield Tane84e6f92019-08-29 17:28:41 -07001679} // namespace android::inputdispatcher