blob: 365d43dd16cbd068c87035bc7c25c13c9f0fc0ab [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 Tan1c7bc862020-01-28 13:24:04 -080019#include <android-base/stringprintf.h>
Robert Carr803535b2018-08-02 16:38:15 -070020#include <binder/Binder.h>
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080021#include <input/Input.h>
Robert Carr803535b2018-08-02 16:38:15 -070022
Michael Wrightd02c5b62014-02-10 15:10:22 -080023#include <gtest/gtest.h>
24#include <linux/input.h>
Garfield Tan1c7bc862020-01-28 13:24:04 -080025#include <cinttypes>
26#include <unordered_set>
chaviwd1c23182019-12-20 18:44:56 -080027#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080028
Garfield Tan1c7bc862020-01-28 13:24:04 -080029using android::base::StringPrintf;
30
Garfield Tane84e6f92019-08-29 17:28:41 -070031namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080032
33// An arbitrary time value.
34static const nsecs_t ARBITRARY_TIME = 1234;
35
36// An arbitrary device id.
37static const int32_t DEVICE_ID = 1;
38
Jeff Brownf086ddb2014-02-11 14:28:48 -080039// An arbitrary display id.
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -080040static const int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
Jeff Brownf086ddb2014-02-11 14:28:48 -080041
Michael Wrightd02c5b62014-02-10 15:10:22 -080042// An arbitrary injector pid / uid pair that has permission to inject events.
43static const int32_t INJECTOR_PID = 999;
44static const int32_t INJECTOR_UID = 1001;
45
chaviwd1c23182019-12-20 18:44:56 -080046struct PointF {
47 float x;
48 float y;
49};
Michael Wrightd02c5b62014-02-10 15:10:22 -080050
Gang Wang342c9272020-01-13 13:15:04 -050051/**
52 * Return a DOWN key event with KEYCODE_A.
53 */
54static KeyEvent getTestKeyEvent() {
55 KeyEvent event;
56
Garfield Tanfbe732e2020-01-24 11:26:14 -080057 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
58 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
59 ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -050060 return event;
61}
62
Michael Wrightd02c5b62014-02-10 15:10:22 -080063// --- FakeInputDispatcherPolicy ---
64
65class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
66 InputDispatcherConfiguration mConfig;
67
68protected:
69 virtual ~FakeInputDispatcherPolicy() {
70 }
71
72public:
73 FakeInputDispatcherPolicy() {
Jackal Guof9696682018-10-05 12:23:23 +080074 }
75
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080076 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080077 assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_KEY, args.eventTime, args.action,
78 args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080079 }
80
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080081 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args) {
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080082 assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_MOTION, args.eventTime, args.action,
83 args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080084 }
85
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080086 void assertFilterInputEventWasNotCalled() { ASSERT_EQ(nullptr, mFilteredEvent); }
Michael Wrightd02c5b62014-02-10 15:10:22 -080087
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -080088 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
89 ASSERT_TRUE(mConfigurationChangedTime)
90 << "Timed out waiting for configuration changed call";
91 ASSERT_EQ(*mConfigurationChangedTime, when);
92 mConfigurationChangedTime = std::nullopt;
93 }
94
95 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
96 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -080097 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -080098 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
99 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
100 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
101 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
102 mLastNotifySwitch = std::nullopt;
103 }
104
chaviwfd6d3512019-03-25 13:23:49 -0700105 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800106 ASSERT_EQ(touchedToken, mOnPointerDownToken);
107 mOnPointerDownToken.clear();
108 }
109
110 void assertOnPointerDownWasNotCalled() {
111 ASSERT_TRUE(mOnPointerDownToken == nullptr)
112 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700113 }
114
Garfield Tan1c7bc862020-01-28 13:24:04 -0800115 void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) {
116 mConfig.keyRepeatTimeout = timeout;
117 mConfig.keyRepeatDelay = delay;
118 }
119
Michael Wrightd02c5b62014-02-10 15:10:22 -0800120private:
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800121 std::unique_ptr<InputEvent> mFilteredEvent;
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800122 std::optional<nsecs_t> mConfigurationChangedTime;
chaviwfd6d3512019-03-25 13:23:49 -0700123 sp<IBinder> mOnPointerDownToken;
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800124 std::optional<NotifySwitchArgs> mLastNotifySwitch;
Jackal Guof9696682018-10-05 12:23:23 +0800125
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800126 virtual void notifyConfigurationChanged(nsecs_t when) override {
127 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800128 }
129
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100130 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>&,
Robert Carr803535b2018-08-02 16:38:15 -0700131 const sp<IBinder>&,
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800132 const std::string&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800133 return 0;
134 }
135
Robert Carr803535b2018-08-02 16:38:15 -0700136 virtual void notifyInputChannelBroken(const sp<IBinder>&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800137 }
138
chaviw0c06c6e2019-01-09 13:27:07 -0800139 virtual void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) {
Robert Carr740167f2018-10-11 19:03:41 -0700140 }
141
Michael Wrightd02c5b62014-02-10 15:10:22 -0800142 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
143 *outConfig = mConfig;
144 }
145
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800146 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Jackal Guof9696682018-10-05 12:23:23 +0800147 switch (inputEvent->getType()) {
148 case AINPUT_EVENT_TYPE_KEY: {
149 const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800150 mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800151 break;
152 }
153
154 case AINPUT_EVENT_TYPE_MOTION: {
155 const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800156 mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800157 break;
158 }
159 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800160 return true;
161 }
162
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100163 virtual void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800164 }
165
Charles Chen3611f1f2019-01-29 17:26:18 +0800166 virtual void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800167 }
168
Robert Carr803535b2018-08-02 16:38:15 -0700169 virtual nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&,
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100170 const KeyEvent*, uint32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800171 return 0;
172 }
173
Robert Carr803535b2018-08-02 16:38:15 -0700174 virtual bool dispatchUnhandledKey(const sp<IBinder>&,
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100175 const KeyEvent*, uint32_t, KeyEvent*) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800176 return false;
177 }
178
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800179 virtual void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
180 uint32_t policyFlags) override {
181 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
182 * essentially a passthrough for notifySwitch.
183 */
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800184 mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800185 }
186
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100187 virtual void pokeUserActivity(nsecs_t, int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188 }
189
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100190 virtual bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800191 return false;
192 }
Jackal Guof9696682018-10-05 12:23:23 +0800193
chaviwfd6d3512019-03-25 13:23:49 -0700194 virtual void onPointerDownOutsideFocus(const sp<IBinder>& newToken) {
195 mOnPointerDownToken = newToken;
196 }
197
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800198 void assertFilterInputEventWasCalled(int type, nsecs_t eventTime, int32_t action,
199 int32_t displayId) {
200 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
201 ASSERT_EQ(mFilteredEvent->getType(), type);
202
203 if (type == AINPUT_EVENT_TYPE_KEY) {
204 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*mFilteredEvent);
205 EXPECT_EQ(keyEvent.getEventTime(), eventTime);
206 EXPECT_EQ(keyEvent.getAction(), action);
207 EXPECT_EQ(keyEvent.getDisplayId(), displayId);
208 } else if (type == AINPUT_EVENT_TYPE_MOTION) {
209 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*mFilteredEvent);
210 EXPECT_EQ(motionEvent.getEventTime(), eventTime);
211 EXPECT_EQ(motionEvent.getAction(), action);
212 EXPECT_EQ(motionEvent.getDisplayId(), displayId);
213 } else {
214 FAIL() << "Unknown type: " << type;
215 }
216
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800217 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800218 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800219};
220
Gang Wang342c9272020-01-13 13:15:04 -0500221// --- HmacKeyManagerTest ---
222
223class HmacKeyManagerTest : public testing::Test {
224protected:
225 HmacKeyManager mHmacKeyManager;
226};
227
228/**
229 * Ensure that separate calls to sign the same data are generating the same key.
230 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
231 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
232 * tests.
233 */
234TEST_F(HmacKeyManagerTest, GeneratedHmac_IsConsistent) {
235 KeyEvent event = getTestKeyEvent();
236 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
237
238 std::array<uint8_t, 32> hmac1 = mHmacKeyManager.sign(verifiedEvent);
239 std::array<uint8_t, 32> hmac2 = mHmacKeyManager.sign(verifiedEvent);
240 ASSERT_EQ(hmac1, hmac2);
241}
242
243/**
244 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
245 */
246TEST_F(HmacKeyManagerTest, GeneratedHmac_ChangesWhenFieldsChange) {
247 KeyEvent event = getTestKeyEvent();
248 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
249 std::array<uint8_t, 32> initialHmac = mHmacKeyManager.sign(verifiedEvent);
250
251 verifiedEvent.deviceId += 1;
252 ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent));
253
254 verifiedEvent.source += 1;
255 ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent));
256
257 verifiedEvent.eventTimeNanos += 1;
258 ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent));
259
260 verifiedEvent.displayId += 1;
261 ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent));
262
263 verifiedEvent.action += 1;
264 ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent));
265
266 verifiedEvent.downTimeNanos += 1;
267 ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent));
268
269 verifiedEvent.flags += 1;
270 ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent));
271
272 verifiedEvent.keyCode += 1;
273 ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent));
274
275 verifiedEvent.scanCode += 1;
276 ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent));
277
278 verifiedEvent.metaState += 1;
279 ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent));
280
281 verifiedEvent.repeatCount += 1;
282 ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent));
283}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800284
285// --- InputDispatcherTest ---
286
287class InputDispatcherTest : public testing::Test {
288protected:
289 sp<FakeInputDispatcherPolicy> mFakePolicy;
290 sp<InputDispatcher> mDispatcher;
291
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700292 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800293 mFakePolicy = new FakeInputDispatcherPolicy();
294 mDispatcher = new InputDispatcher(mFakePolicy);
Arthur Hungb92218b2018-08-14 12:00:21 +0800295 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
296 //Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700297 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800298 }
299
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700300 virtual void TearDown() override {
301 ASSERT_EQ(OK, mDispatcher->stop());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800302 mFakePolicy.clear();
303 mDispatcher.clear();
304 }
305};
306
307
308TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
309 KeyEvent event;
310
311 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800312 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
313 INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600314 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
315 ARBITRARY_TIME);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800316 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800317 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800318 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
319 << "Should reject key events with undefined action.";
320
321 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800322 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
323 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600324 ARBITRARY_TIME, ARBITRARY_TIME);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800325 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800326 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800327 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
328 << "Should reject key events with ACTION_MULTIPLE.";
329}
330
331TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
332 MotionEvent event;
333 PointerProperties pointerProperties[MAX_POINTERS + 1];
334 PointerCoords pointerCoords[MAX_POINTERS + 1];
335 for (int i = 0; i <= MAX_POINTERS; i++) {
336 pointerProperties[i].clear();
337 pointerProperties[i].id = i;
338 pointerCoords[i].clear();
339 }
340
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800341 // Some constants commonly used below
342 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
343 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
344 constexpr int32_t metaState = AMETA_NONE;
345 constexpr MotionClassification classification = MotionClassification::NONE;
346
Michael Wrightd02c5b62014-02-10 15:10:22 -0800347 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800348 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600349 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification, 1 /* xScale */,
350 1 /* yScale */, 0, 0, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
351 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700352 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800353 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800354 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800355 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
356 << "Should reject motion events with undefined action.";
357
358 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800359 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700360 AMOTION_EVENT_ACTION_POINTER_DOWN |
361 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600362 0, 0, edgeFlags, metaState, 0, classification, 1 /* xScale */, 1 /* yScale */,
363 0, 0, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
364 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700365 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800366 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800367 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800368 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
369 << "Should reject motion events with pointer down index too large.";
370
Garfield Tanfbe732e2020-01-24 11:26:14 -0800371 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700372 AMOTION_EVENT_ACTION_POINTER_DOWN |
373 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600374 0, 0, edgeFlags, metaState, 0, classification, 1 /* xScale */, 1 /* yScale */,
375 0, 0, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
376 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700377 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800378 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800379 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800380 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
381 << "Should reject motion events with pointer down index too small.";
382
383 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800384 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700385 AMOTION_EVENT_ACTION_POINTER_UP |
386 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600387 0, 0, edgeFlags, metaState, 0, classification, 1 /* xScale */, 1 /* yScale */,
388 0, 0, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
389 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700390 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800391 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800392 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800393 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
394 << "Should reject motion events with pointer up index too large.";
395
Garfield Tanfbe732e2020-01-24 11:26:14 -0800396 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700397 AMOTION_EVENT_ACTION_POINTER_UP |
398 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600399 0, 0, edgeFlags, metaState, 0, classification, 1 /* xScale */, 1 /* yScale */,
400 0, 0, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
401 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700402 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800403 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800404 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800405 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
406 << "Should reject motion events with pointer up index too small.";
407
408 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800409 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
410 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
411 1 /* xScale */, 1 /* yScale */, 0, 0, 0, 0,
412 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
413 ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700414 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800415 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800416 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800417 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
418 << "Should reject motion events with 0 pointers.";
419
Garfield Tanfbe732e2020-01-24 11:26:14 -0800420 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
421 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
422 1 /* xScale */, 1 /* yScale */, 0, 0, 0, 0,
423 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
424 ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700425 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800426 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800427 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800428 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
429 << "Should reject motion events with more than MAX_POINTERS pointers.";
430
431 // Rejects motion events with invalid pointer ids.
432 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800433 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
434 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
435 1 /* xScale */, 1 /* yScale */, 0, 0, 0, 0,
436 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
437 ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700438 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800439 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800440 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800441 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
442 << "Should reject motion events with pointer ids less than 0.";
443
444 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800445 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
446 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
447 1 /* xScale */, 1 /* yScale */, 0, 0, 0, 0,
448 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
449 ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700450 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800451 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800452 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800453 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
454 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
455
456 // Rejects motion events with duplicate pointer ids.
457 pointerProperties[0].id = 1;
458 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800459 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
460 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
461 1 /* xScale */, 1 /* yScale */, 0, 0, 0, 0,
462 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
463 ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700464 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800465 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800466 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800467 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
468 << "Should reject motion events with duplicate pointer ids.";
469}
470
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800471/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
472
473TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
474 constexpr nsecs_t eventTime = 20;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800475 NotifyConfigurationChangedArgs args(10 /*id*/, eventTime);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800476 mDispatcher->notifyConfigurationChanged(&args);
477 ASSERT_TRUE(mDispatcher->waitForIdle());
478
479 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
480}
481
482TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800483 NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/,
484 2 /*switchMask*/);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800485 mDispatcher->notifySwitch(&args);
486
487 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
488 args.policyFlags |= POLICY_FLAG_TRUSTED;
489 mFakePolicy->assertNotifySwitchWasCalled(args);
490}
491
Arthur Hungb92218b2018-08-14 12:00:21 +0800492// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800493static constexpr int32_t INJECT_EVENT_TIMEOUT = 500;
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -0700494static constexpr nsecs_t DISPATCHING_TIMEOUT = seconds_to_nanoseconds(5);
Arthur Hungb92218b2018-08-14 12:00:21 +0800495
496class FakeApplicationHandle : public InputApplicationHandle {
497public:
498 FakeApplicationHandle() {}
499 virtual ~FakeApplicationHandle() {}
500
501 virtual bool updateInfo() {
Arthur Hung7a0c39a2019-03-20 16:52:24 +0800502 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Arthur Hungb92218b2018-08-14 12:00:21 +0800503 return true;
504 }
505};
506
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800507class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800508public:
chaviwd1c23182019-12-20 18:44:56 -0800509 explicit FakeInputReceiver(const sp<InputChannel>& clientChannel, const std::string name)
510 : mName(name) {
511 mConsumer = std::make_unique<InputConsumer>(clientChannel);
512 }
513
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800514 InputEvent* consume() {
Arthur Hungb92218b2018-08-14 12:00:21 +0800515 uint32_t consumeSeq;
516 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800517
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800518 std::chrono::time_point start = std::chrono::steady_clock::now();
519 status_t status = WOULD_BLOCK;
520 while (status == WOULD_BLOCK) {
chaviw81e2bb92019-12-18 15:03:51 -0800521 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800522 &event);
523 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
524 if (elapsed > 100ms) {
525 break;
526 }
527 }
528
529 if (status == WOULD_BLOCK) {
530 // Just means there's no event available.
531 return nullptr;
532 }
533
534 if (status != OK) {
535 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
536 return nullptr;
537 }
538 if (event == nullptr) {
539 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
540 return nullptr;
541 }
542
chaviwd1c23182019-12-20 18:44:56 -0800543 status = mConsumer->sendFinishedSignal(consumeSeq, true);
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800544 if (status != OK) {
545 ADD_FAILURE() << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
546 }
547 return event;
548 }
549
550 void consumeEvent(int32_t expectedEventType, int32_t expectedAction, int32_t expectedDisplayId,
551 int32_t expectedFlags) {
552 InputEvent* event = consume();
553
554 ASSERT_NE(nullptr, event) << mName.c_str()
555 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800556 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800557 << mName.c_str() << "expected " << inputEventTypeToString(expectedEventType)
558 << " event, got " << inputEventTypeToString(event->getType()) << " event";
Arthur Hungb92218b2018-08-14 12:00:21 +0800559
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800560 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
Tiger Huang8664f8c2018-10-11 19:14:35 +0800561
Tiger Huang8664f8c2018-10-11 19:14:35 +0800562 switch (expectedEventType) {
563 case AINPUT_EVENT_TYPE_KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800564 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
565 EXPECT_EQ(expectedAction, keyEvent.getAction());
566 EXPECT_EQ(expectedFlags, keyEvent.getFlags());
Tiger Huang8664f8c2018-10-11 19:14:35 +0800567 break;
568 }
569 case AINPUT_EVENT_TYPE_MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800570 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
571 EXPECT_EQ(expectedAction, motionEvent.getAction());
572 EXPECT_EQ(expectedFlags, motionEvent.getFlags());
Tiger Huang8664f8c2018-10-11 19:14:35 +0800573 break;
574 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100575 case AINPUT_EVENT_TYPE_FOCUS: {
576 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
577 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800578 default: {
579 FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType;
580 }
581 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800582 }
583
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100584 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
585 InputEvent* event = consume();
586 ASSERT_NE(nullptr, event) << mName.c_str()
587 << ": consumer should have returned non-NULL event.";
588 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
589 << "Got " << inputEventTypeToString(event->getType())
590 << " event instead of FOCUS event";
591
592 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
593 << mName.c_str() << ": event displayId should always be NONE.";
594
595 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
596 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
597 EXPECT_EQ(inTouchMode, focusEvent->getInTouchMode());
598 }
599
chaviwd1c23182019-12-20 18:44:56 -0800600 void assertNoEvents() {
601 InputEvent* event = consume();
602 ASSERT_EQ(nullptr, event)
603 << mName.c_str()
604 << ": should not have received any events, so consume() should return NULL";
605 }
606
607 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
608
609protected:
610 std::unique_ptr<InputConsumer> mConsumer;
611 PreallocatedInputEventFactory mEventFactory;
612
613 std::string mName;
614};
615
616class FakeWindowHandle : public InputWindowHandle {
617public:
618 static const int32_t WIDTH = 600;
619 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -0800620
621 FakeWindowHandle(const sp<InputApplicationHandle>& inputApplicationHandle,
622 const sp<InputDispatcher>& dispatcher, const std::string name,
623 int32_t displayId, sp<IBinder> token = nullptr)
624 : mName(name) {
625 if (token == nullptr) {
626 sp<InputChannel> serverChannel, clientChannel;
627 InputChannel::openInputChannelPair(name, serverChannel, clientChannel);
628 mInputReceiver = std::make_unique<FakeInputReceiver>(clientChannel, name);
629 dispatcher->registerInputChannel(serverChannel);
630 token = serverChannel->getConnectionToken();
631 }
632
633 inputApplicationHandle->updateInfo();
634 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
635
636 mInfo.token = token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700637 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -0800638 mInfo.name = name;
639 mInfo.layoutParamsFlags = 0;
640 mInfo.layoutParamsType = InputWindowInfo::TYPE_APPLICATION;
641 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
642 mInfo.frameLeft = 0;
643 mInfo.frameTop = 0;
644 mInfo.frameRight = WIDTH;
645 mInfo.frameBottom = HEIGHT;
646 mInfo.globalScaleFactor = 1.0;
647 mInfo.touchableRegion.clear();
648 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
649 mInfo.visible = true;
650 mInfo.canReceiveKeys = true;
651 mInfo.hasFocus = false;
652 mInfo.hasWallpaper = false;
653 mInfo.paused = false;
chaviwd1c23182019-12-20 18:44:56 -0800654 mInfo.ownerPid = INJECTOR_PID;
655 mInfo.ownerUid = INJECTOR_UID;
656 mInfo.inputFeatures = 0;
657 mInfo.displayId = displayId;
658 }
659
660 virtual bool updateInfo() { return true; }
661
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100662 void setFocus(bool hasFocus) { mInfo.hasFocus = hasFocus; }
chaviwd1c23182019-12-20 18:44:56 -0800663
664 void setFrame(const Rect& frame) {
665 mInfo.frameLeft = frame.left;
666 mInfo.frameTop = frame.top;
667 mInfo.frameRight = frame.right;
668 mInfo.frameBottom = frame.bottom;
669 mInfo.touchableRegion.clear();
670 mInfo.addTouchableRegion(frame);
671 }
672
673 void setLayoutParamFlags(int32_t flags) { mInfo.layoutParamsFlags = flags; }
674
chaviwaf87b3e2019-10-01 16:59:28 -0700675 void setWindowScale(float xScale, float yScale) {
676 mInfo.windowXScale = xScale;
677 mInfo.windowYScale = yScale;
678 }
679
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800680 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
681 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
682 expectedFlags);
683 }
684
Svet Ganov5d3bc372020-01-26 23:11:07 -0800685 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
686 int32_t expectedFlags = 0) {
687 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId,
688 expectedFlags);
689 }
690
691 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
692 int32_t expectedFlags = 0) {
693 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId,
694 expectedFlags);
695 }
696
697 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
698 int32_t expectedFlags = 0) {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800699 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
700 expectedFlags);
701 }
702
Svet Ganov5d3bc372020-01-26 23:11:07 -0800703 void consumeMotionPointerDown(int32_t pointerIdx,
704 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, int32_t expectedFlags = 0) {
705 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN
706 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
707 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
708 }
709
710 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
711 int32_t expectedFlags = 0) {
712 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP
713 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
714 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
715 }
716
717 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
718 int32_t expectedFlags = 0) {
Michael Wright3a240c42019-12-10 20:53:41 +0000719 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
720 expectedFlags);
721 }
722
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100723 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
724 ASSERT_NE(mInputReceiver, nullptr)
725 << "Cannot consume events from a window with no receiver";
726 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
727 }
728
chaviwd1c23182019-12-20 18:44:56 -0800729 void consumeEvent(int32_t expectedEventType, int32_t expectedAction, int32_t expectedDisplayId,
730 int32_t expectedFlags) {
731 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
732 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
733 expectedFlags);
734 }
735
chaviwaf87b3e2019-10-01 16:59:28 -0700736 InputEvent* consume() {
737 if (mInputReceiver == nullptr) {
738 return nullptr;
739 }
740 return mInputReceiver->consume();
741 }
742
Arthur Hungb92218b2018-08-14 12:00:21 +0800743 void assertNoEvents() {
chaviwd1c23182019-12-20 18:44:56 -0800744 ASSERT_NE(mInputReceiver, nullptr)
745 << "Call 'assertNoEvents' on a window with an InputReceiver";
746 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +0800747 }
748
chaviwaf87b3e2019-10-01 16:59:28 -0700749 sp<IBinder> getToken() { return mInfo.token; }
750
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100751 const std::string& getName() { return mName; }
752
chaviwd1c23182019-12-20 18:44:56 -0800753private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100754 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -0800755 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700756 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800757};
758
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700759std::atomic<int32_t> FakeWindowHandle::sId{1};
760
Tiger Huang721e26f2018-07-24 22:26:19 +0800761static int32_t injectKeyDown(const sp<InputDispatcher>& dispatcher,
762 int32_t displayId = ADISPLAY_ID_NONE) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800763 KeyEvent event;
764 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
765
766 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800767 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
768 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A,
769 AMETA_NONE,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600770 /* repeatCount */ 0, currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +0800771
772 // Inject event until dispatch out.
773 return dispatcher->injectInputEvent(
774 &event,
775 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT,
776 INJECT_EVENT_TIMEOUT, POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
777}
778
Garfield Tan00f511d2019-06-12 16:55:40 -0700779static int32_t injectMotionEvent(const sp<InputDispatcher>& dispatcher, int32_t action,
780 int32_t source, int32_t displayId, int32_t x, int32_t y,
781 int32_t xCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION,
782 int32_t yCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800783 MotionEvent event;
784 PointerProperties pointerProperties[1];
785 PointerCoords pointerCoords[1];
786
787 pointerProperties[0].clear();
788 pointerProperties[0].id = 0;
789 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
790
791 pointerCoords[0].clear();
chaviwfd6d3512019-03-25 13:23:49 -0700792 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
793 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Arthur Hungb92218b2018-08-14 12:00:21 +0800794
795 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
796 // Define a valid motion down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800797 event.initialize(InputEvent::nextId(), DEVICE_ID, source, displayId, INVALID_HMAC, action,
798 /* actionButton */ 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600799 /* flags */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -0700800 /* edgeFlags */ 0, AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600801 /* xScale */ 1, /* yScale */ 1, /* xOffset */ 0, /* yOffset */ 0,
802 /* xPrecision */ 0, /* yPrecision */ 0, xCursorPosition, yCursorPosition,
803 currentTime, currentTime,
Garfield Tan00f511d2019-06-12 16:55:40 -0700804 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Arthur Hungb92218b2018-08-14 12:00:21 +0800805
806 // Inject event until dispatch out.
807 return dispatcher->injectInputEvent(
808 &event,
809 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT,
810 INJECT_EVENT_TIMEOUT, POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
811}
812
Garfield Tan00f511d2019-06-12 16:55:40 -0700813static int32_t injectMotionDown(const sp<InputDispatcher>& dispatcher, int32_t source,
814 int32_t displayId, int32_t x = 100, int32_t y = 200) {
815 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, x, y);
816}
817
Michael Wright3a240c42019-12-10 20:53:41 +0000818static int32_t injectMotionUp(const sp<InputDispatcher>& dispatcher, int32_t source,
819 int32_t displayId, int32_t x = 100, int32_t y = 200) {
820 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, x, y);
821}
822
Jackal Guof9696682018-10-05 12:23:23 +0800823static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
824 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
825 // Define a valid key event.
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800826 NotifyKeyArgs args(/* id */ 0, currentTime, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
827 POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, KEY_A,
828 AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +0800829
830 return args;
831}
832
chaviwd1c23182019-12-20 18:44:56 -0800833static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId,
834 const std::vector<PointF>& points) {
835 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -0700836 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
837 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
838 }
839
chaviwd1c23182019-12-20 18:44:56 -0800840 PointerProperties pointerProperties[pointerCount];
841 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +0800842
chaviwd1c23182019-12-20 18:44:56 -0800843 for (size_t i = 0; i < pointerCount; i++) {
844 pointerProperties[i].clear();
845 pointerProperties[i].id = i;
846 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jackal Guof9696682018-10-05 12:23:23 +0800847
chaviwd1c23182019-12-20 18:44:56 -0800848 pointerCoords[i].clear();
849 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
850 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
851 }
Jackal Guof9696682018-10-05 12:23:23 +0800852
853 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
854 // Define a valid motion event.
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800855 NotifyMotionArgs args(/* id */ 0, currentTime, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -0700856 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
857 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -0800858 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
859 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -0700860 AMOTION_EVENT_INVALID_CURSOR_POSITION,
861 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +0800862
863 return args;
864}
865
chaviwd1c23182019-12-20 18:44:56 -0800866static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
867 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
868}
869
Arthur Hungb92218b2018-08-14 12:00:21 +0800870TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
871 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800872 sp<FakeWindowHandle> window = new FakeWindowHandle(application, mDispatcher, "Fake Window",
873 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800874
Arthur Hung72d8dc32020-03-28 00:48:39 +0000875 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800876 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
877 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +0800878 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
879
880 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800881 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800882}
883
884// The foreground window should receive the first touch down event.
885TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
886 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800887 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
888 ADISPLAY_ID_DEFAULT);
889 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
890 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800891
Arthur Hung72d8dc32020-03-28 00:48:39 +0000892 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800893 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
894 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +0800895 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
896
897 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800898 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800899 windowSecond->assertNoEvents();
900}
901
902TEST_F(InputDispatcherTest, SetInputWindow_FocusedWindow) {
903 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800904 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
905 ADISPLAY_ID_DEFAULT);
906 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
907 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800908
Arthur Hung7ab76b12019-01-09 19:17:20 +0800909 // Set focused application.
Tiger Huang721e26f2018-07-24 22:26:19 +0800910 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Arthur Hungb92218b2018-08-14 12:00:21 +0800911
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100912 // Display should have only one focused window
913 windowSecond->setFocus(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +0000914 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100915
916 windowSecond->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +0800917 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
918 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
919
920 // Focused window should receive event.
921 windowTop->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800922 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +0800923}
924
Arthur Hung7ab76b12019-01-09 19:17:20 +0800925TEST_F(InputDispatcherTest, SetInputWindow_FocusPriority) {
926 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
927 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
928 ADISPLAY_ID_DEFAULT);
929 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
930 ADISPLAY_ID_DEFAULT);
931
932 // Set focused application.
933 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
934
935 // Display has two focused windows. Add them to inputWindowsHandles in z-order (top most first)
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100936 windowTop->setFocus(true);
937 windowSecond->setFocus(true);
Arthur Hung7ab76b12019-01-09 19:17:20 +0800938
Arthur Hung72d8dc32020-03-28 00:48:39 +0000939 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100940 windowTop->consumeFocusEvent(true);
Arthur Hung7ab76b12019-01-09 19:17:20 +0800941 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
942 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
943
944 // Top focused window should receive event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800945 windowTop->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung7ab76b12019-01-09 19:17:20 +0800946 windowSecond->assertNoEvents();
947}
948
Arthur Hung3b413f22018-10-26 18:05:34 +0800949TEST_F(InputDispatcherTest, SetInputWindow_InputWindowInfo) {
950 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
951
952 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
953 ADISPLAY_ID_DEFAULT);
954 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
955 ADISPLAY_ID_DEFAULT);
956
Arthur Hung832bc4a2019-01-28 11:43:17 +0800957 // Set focused application.
958 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Arthur Hung3b413f22018-10-26 18:05:34 +0800959
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100960 windowTop->setFocus(true);
961 windowSecond->setFocus(true);
Arthur Hung3b413f22018-10-26 18:05:34 +0800962 // Release channel for window is no longer valid.
963 windowTop->releaseChannel();
Arthur Hung72d8dc32020-03-28 00:48:39 +0000964 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100965 windowSecond->consumeFocusEvent(true);
Arthur Hung3b413f22018-10-26 18:05:34 +0800966
Arthur Hung832bc4a2019-01-28 11:43:17 +0800967 // Test inject a key down, should dispatch to a valid window.
968 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
969 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Arthur Hung3b413f22018-10-26 18:05:34 +0800970
971 // Top window is invalid, so it should not receive any input event.
972 windowTop->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800973 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung3b413f22018-10-26 18:05:34 +0800974}
975
Garfield Tan00f511d2019-06-12 16:55:40 -0700976TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
977 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
978
979 sp<FakeWindowHandle> windowLeft =
980 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
981 windowLeft->setFrame(Rect(0, 0, 600, 800));
982 windowLeft->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL);
983 sp<FakeWindowHandle> windowRight =
984 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
985 windowRight->setFrame(Rect(600, 0, 1200, 800));
986 windowRight->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL);
987
988 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
989
Arthur Hung72d8dc32020-03-28 00:48:39 +0000990 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -0700991
992 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
993 // left window. This event should be dispatched to the left window.
994 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
995 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
996 ADISPLAY_ID_DEFAULT, 610, 400, 599, 400));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800997 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -0700998 windowRight->assertNoEvents();
999}
1000
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001001TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
1002 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1003 sp<FakeWindowHandle> window =
1004 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001005 window->setFocus(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001006
Arthur Hung72d8dc32020-03-28 00:48:39 +00001007 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001008 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001009
1010 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1011 mDispatcher->notifyKey(&keyArgs);
1012
1013 // Window should receive key down event.
1014 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1015
1016 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
1017 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001018 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001019 mDispatcher->notifyDeviceReset(&args);
1020 window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
1021 AKEY_EVENT_FLAG_CANCELED);
1022}
1023
1024TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
1025 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1026 sp<FakeWindowHandle> window =
1027 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1028
Arthur Hung72d8dc32020-03-28 00:48:39 +00001029 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001030
1031 NotifyMotionArgs motionArgs =
1032 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1033 ADISPLAY_ID_DEFAULT);
1034 mDispatcher->notifyMotion(&motionArgs);
1035
1036 // Window should receive motion down event.
1037 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1038
1039 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
1040 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001041 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001042 mDispatcher->notifyDeviceReset(&args);
1043 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT,
1044 0 /*expectedFlags*/);
1045}
1046
Svet Ganov5d3bc372020-01-26 23:11:07 -08001047TEST_F(InputDispatcherTest, TransferTouchFocus_OnePointer) {
1048 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1049
1050 // Create a couple of windows
1051 sp<FakeWindowHandle> firstWindow = new FakeWindowHandle(application, mDispatcher,
1052 "First Window", ADISPLAY_ID_DEFAULT);
1053 sp<FakeWindowHandle> secondWindow = new FakeWindowHandle(application, mDispatcher,
1054 "Second Window", ADISPLAY_ID_DEFAULT);
1055
1056 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001057 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001058
1059 // Send down to the first window
1060 NotifyMotionArgs downMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
1061 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT);
1062 mDispatcher->notifyMotion(&downMotionArgs);
1063 // Only the first window should get the down event
1064 firstWindow->consumeMotionDown();
1065 secondWindow->assertNoEvents();
1066
1067 // Transfer touch focus to the second window
1068 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1069 // The first window gets cancel and the second gets down
1070 firstWindow->consumeMotionCancel();
1071 secondWindow->consumeMotionDown();
1072
1073 // Send up event to the second window
1074 NotifyMotionArgs upMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP,
1075 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT);
1076 mDispatcher->notifyMotion(&upMotionArgs);
1077 // The first window gets no events and the second gets up
1078 firstWindow->assertNoEvents();
1079 secondWindow->consumeMotionUp();
1080}
1081
1082TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointerNoSplitTouch) {
1083 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1084
1085 PointF touchPoint = {10, 10};
1086
1087 // Create a couple of windows
1088 sp<FakeWindowHandle> firstWindow = new FakeWindowHandle(application, mDispatcher,
1089 "First Window", ADISPLAY_ID_DEFAULT);
1090 sp<FakeWindowHandle> secondWindow = new FakeWindowHandle(application, mDispatcher,
1091 "Second Window", ADISPLAY_ID_DEFAULT);
1092
1093 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001094 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001095
1096 // Send down to the first window
1097 NotifyMotionArgs downMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
1098 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {touchPoint});
1099 mDispatcher->notifyMotion(&downMotionArgs);
1100 // Only the first window should get the down event
1101 firstWindow->consumeMotionDown();
1102 secondWindow->assertNoEvents();
1103
1104 // Send pointer down to the first window
1105 NotifyMotionArgs pointerDownMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN
1106 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1107 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint});
1108 mDispatcher->notifyMotion(&pointerDownMotionArgs);
1109 // Only the first window should get the pointer down event
1110 firstWindow->consumeMotionPointerDown(1);
1111 secondWindow->assertNoEvents();
1112
1113 // Transfer touch focus to the second window
1114 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1115 // The first window gets cancel and the second gets down and pointer down
1116 firstWindow->consumeMotionCancel();
1117 secondWindow->consumeMotionDown();
1118 secondWindow->consumeMotionPointerDown(1);
1119
1120 // Send pointer up to the second window
1121 NotifyMotionArgs pointerUpMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP
1122 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1123 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint});
1124 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1125 // The first window gets nothing and the second gets pointer up
1126 firstWindow->assertNoEvents();
1127 secondWindow->consumeMotionPointerUp(1);
1128
1129 // Send up event to the second window
1130 NotifyMotionArgs upMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP,
1131 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT);
1132 mDispatcher->notifyMotion(&upMotionArgs);
1133 // The first window gets nothing and the second gets up
1134 firstWindow->assertNoEvents();
1135 secondWindow->consumeMotionUp();
1136}
1137
1138TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
1139 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1140
1141 // Create a non touch modal window that supports split touch
1142 sp<FakeWindowHandle> firstWindow = new FakeWindowHandle(application, mDispatcher,
1143 "First Window", ADISPLAY_ID_DEFAULT);
1144 firstWindow->setFrame(Rect(0, 0, 600, 400));
1145 firstWindow->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL
1146 | InputWindowInfo::FLAG_SPLIT_TOUCH);
1147
1148 // Create a non touch modal window that supports split touch
1149 sp<FakeWindowHandle> secondWindow = new FakeWindowHandle(application, mDispatcher,
1150 "Second Window", ADISPLAY_ID_DEFAULT);
1151 secondWindow->setFrame(Rect(0, 400, 600, 800));
1152 secondWindow->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL
1153 | InputWindowInfo::FLAG_SPLIT_TOUCH);
1154
1155 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001156 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001157
1158 PointF pointInFirst = {300, 200};
1159 PointF pointInSecond = {300, 600};
1160
1161 // Send down to the first window
1162 NotifyMotionArgs firstDownMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
1163 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {pointInFirst});
1164 mDispatcher->notifyMotion(&firstDownMotionArgs);
1165 // Only the first window should get the down event
1166 firstWindow->consumeMotionDown();
1167 secondWindow->assertNoEvents();
1168
1169 // Send down to the second window
1170 NotifyMotionArgs secondDownMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN
1171 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1172 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {pointInFirst, pointInSecond});
1173 mDispatcher->notifyMotion(&secondDownMotionArgs);
1174 // The first window gets a move and the second a down
1175 firstWindow->consumeMotionMove();
1176 secondWindow->consumeMotionDown();
1177
1178 // Transfer touch focus to the second window
1179 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1180 // The first window gets cancel and the new gets pointer down (it already saw down)
1181 firstWindow->consumeMotionCancel();
1182 secondWindow->consumeMotionPointerDown(1);
1183
1184 // Send pointer up to the second window
1185 NotifyMotionArgs pointerUpMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP
1186 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1187 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {pointInFirst, pointInSecond});
1188 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1189 // The first window gets nothing and the second gets pointer up
1190 firstWindow->assertNoEvents();
1191 secondWindow->consumeMotionPointerUp(1);
1192
1193 // Send up event to the second window
1194 NotifyMotionArgs upMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP,
1195 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT);
1196 mDispatcher->notifyMotion(&upMotionArgs);
1197 // The first window gets nothing and the second gets up
1198 firstWindow->assertNoEvents();
1199 secondWindow->consumeMotionUp();
1200}
1201
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001202TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
1203 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1204 sp<FakeWindowHandle> window =
1205 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1206
1207 window->setFocus(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001208 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001209
1210 window->consumeFocusEvent(true);
1211
1212 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1213 mDispatcher->notifyKey(&keyArgs);
1214
1215 // Window should receive key down event.
1216 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1217}
1218
1219TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
1220 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1221 sp<FakeWindowHandle> window =
1222 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1223
Arthur Hung72d8dc32020-03-28 00:48:39 +00001224 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001225
1226 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1227 mDispatcher->notifyKey(&keyArgs);
1228 mDispatcher->waitForIdle();
1229
1230 window->assertNoEvents();
1231}
1232
1233// If a window is touchable, but does not have focus, it should receive motion events, but not keys
1234TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
1235 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1236 sp<FakeWindowHandle> window =
1237 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1238
Arthur Hung72d8dc32020-03-28 00:48:39 +00001239 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001240
1241 // Send key
1242 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1243 mDispatcher->notifyKey(&keyArgs);
1244 // Send motion
1245 NotifyMotionArgs motionArgs =
1246 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1247 ADISPLAY_ID_DEFAULT);
1248 mDispatcher->notifyMotion(&motionArgs);
1249
1250 // Window should receive only the motion event
1251 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1252 window->assertNoEvents(); // Key event or focus event will not be received
1253}
1254
chaviwd1c23182019-12-20 18:44:56 -08001255class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00001256public:
1257 FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name,
chaviwd1c23182019-12-20 18:44:56 -08001258 int32_t displayId, bool isGestureMonitor = false) {
1259 sp<InputChannel> serverChannel, clientChannel;
1260 InputChannel::openInputChannelPair(name, serverChannel, clientChannel);
1261 mInputReceiver = std::make_unique<FakeInputReceiver>(clientChannel, name);
1262 dispatcher->registerInputMonitor(serverChannel, displayId, isGestureMonitor);
Michael Wright3a240c42019-12-10 20:53:41 +00001263 }
1264
chaviwd1c23182019-12-20 18:44:56 -08001265 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
1266
1267 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1268 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN,
1269 expectedDisplayId, expectedFlags);
1270 }
1271
1272 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1273 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
1274 expectedDisplayId, expectedFlags);
1275 }
1276
1277 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1278 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP,
1279 expectedDisplayId, expectedFlags);
1280 }
1281
1282 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
1283
1284private:
1285 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00001286};
1287
1288// Tests for gesture monitors
1289TEST_F(InputDispatcherTest, GestureMonitor_ReceivesMotionEvents) {
1290 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1291 sp<FakeWindowHandle> window =
1292 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001293 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00001294
chaviwd1c23182019-12-20 18:44:56 -08001295 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
1296 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00001297
1298 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1299 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1300 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1301 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001302 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00001303}
1304
1305TEST_F(InputDispatcherTest, GestureMonitor_DoesNotReceiveKeyEvents) {
1306 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1307 sp<FakeWindowHandle> window =
1308 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1309
1310 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001311 window->setFocus(true);
Michael Wright3a240c42019-12-10 20:53:41 +00001312
Arthur Hung72d8dc32020-03-28 00:48:39 +00001313 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001314 window->consumeFocusEvent(true);
Michael Wright3a240c42019-12-10 20:53:41 +00001315
chaviwd1c23182019-12-20 18:44:56 -08001316 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
1317 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00001318
1319 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
1320 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1321 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001322 monitor.assertNoEvents();
Michael Wright3a240c42019-12-10 20:53:41 +00001323}
1324
1325TEST_F(InputDispatcherTest, GestureMonitor_CanPilferAfterWindowIsRemovedMidStream) {
1326 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1327 sp<FakeWindowHandle> window =
1328 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001329 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00001330
chaviwd1c23182019-12-20 18:44:56 -08001331 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
1332 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00001333
1334 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1335 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1336 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1337 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001338 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00001339
1340 window->releaseChannel();
1341
chaviwd1c23182019-12-20 18:44:56 -08001342 mDispatcher->pilferPointers(monitor.getToken());
Michael Wright3a240c42019-12-10 20:53:41 +00001343
1344 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1345 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1346 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08001347 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00001348}
1349
chaviw81e2bb92019-12-18 15:03:51 -08001350TEST_F(InputDispatcherTest, TestMoveEvent) {
1351 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1352 sp<FakeWindowHandle> window =
1353 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1354
Arthur Hung72d8dc32020-03-28 00:48:39 +00001355 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08001356
1357 NotifyMotionArgs motionArgs =
1358 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1359 ADISPLAY_ID_DEFAULT);
1360
1361 mDispatcher->notifyMotion(&motionArgs);
1362 // Window should receive motion down event.
1363 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1364
1365 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001366 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08001367 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1368 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
1369 motionArgs.pointerCoords[0].getX() - 10);
1370
1371 mDispatcher->notifyMotion(&motionArgs);
1372 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
1373 0 /*expectedFlags*/);
1374}
1375
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001376/**
1377 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
1378 * the device default right away. In the test scenario, we check both the default value,
1379 * and the action of enabling / disabling.
1380 */
1381TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
1382 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1383 sp<FakeWindowHandle> window =
1384 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
1385
1386 // Set focused application.
1387 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1388 window->setFocus(true);
1389
1390 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00001391 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001392 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
1393
1394 SCOPED_TRACE("Remove the window to trigger focus loss");
1395 window->setFocus(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001396 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001397 window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/);
1398
1399 SCOPED_TRACE("Disable touch mode");
1400 mDispatcher->setInTouchMode(false);
1401 window->setFocus(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001402 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001403 window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/);
1404
1405 SCOPED_TRACE("Remove the window to trigger focus loss");
1406 window->setFocus(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001407 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001408 window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/);
1409
1410 SCOPED_TRACE("Enable touch mode again");
1411 mDispatcher->setInTouchMode(true);
1412 window->setFocus(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001413 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001414 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
1415
1416 window->assertNoEvents();
1417}
1418
Gang Wange9087892020-01-07 12:17:14 -05001419TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
1420 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1421 sp<FakeWindowHandle> window =
1422 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
1423
1424 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1425 window->setFocus(true);
1426
Arthur Hung72d8dc32020-03-28 00:48:39 +00001427 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Gang Wange9087892020-01-07 12:17:14 -05001428 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
1429
1430 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
1431 mDispatcher->notifyKey(&keyArgs);
1432
1433 InputEvent* event = window->consume();
1434 ASSERT_NE(event, nullptr);
1435
1436 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
1437 ASSERT_NE(verified, nullptr);
1438 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
1439
1440 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
1441 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
1442 ASSERT_EQ(keyArgs.source, verified->source);
1443 ASSERT_EQ(keyArgs.displayId, verified->displayId);
1444
1445 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
1446
1447 ASSERT_EQ(keyArgs.action, verifiedKey.action);
1448 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05001449 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
1450 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
1451 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
1452 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
1453 ASSERT_EQ(0, verifiedKey.repeatCount);
1454}
1455
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08001456TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
1457 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1458 sp<FakeWindowHandle> window =
1459 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
1460
1461 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1462
Arthur Hung72d8dc32020-03-28 00:48:39 +00001463 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08001464
1465 NotifyMotionArgs motionArgs =
1466 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1467 ADISPLAY_ID_DEFAULT);
1468 mDispatcher->notifyMotion(&motionArgs);
1469
1470 InputEvent* event = window->consume();
1471 ASSERT_NE(event, nullptr);
1472
1473 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
1474 ASSERT_NE(verified, nullptr);
1475 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
1476
1477 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
1478 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
1479 EXPECT_EQ(motionArgs.source, verified->source);
1480 EXPECT_EQ(motionArgs.displayId, verified->displayId);
1481
1482 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
1483
1484 EXPECT_EQ(motionArgs.pointerCoords[0].getX(), verifiedMotion.rawX);
1485 EXPECT_EQ(motionArgs.pointerCoords[0].getY(), verifiedMotion.rawY);
1486 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
1487 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
1488 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
1489 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
1490 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
1491}
1492
Garfield Tan1c7bc862020-01-28 13:24:04 -08001493class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
1494protected:
1495 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
1496 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
1497
1498 sp<FakeApplicationHandle> mApp;
1499 sp<FakeWindowHandle> mWindow;
1500
1501 virtual void SetUp() override {
1502 mFakePolicy = new FakeInputDispatcherPolicy();
1503 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
1504 mDispatcher = new InputDispatcher(mFakePolicy);
1505 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
1506 ASSERT_EQ(OK, mDispatcher->start());
1507
1508 setUpWindow();
1509 }
1510
1511 void setUpWindow() {
1512 mApp = new FakeApplicationHandle();
1513 mWindow = new FakeWindowHandle(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1514
1515 mWindow->setFocus(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001516 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Garfield Tan1c7bc862020-01-28 13:24:04 -08001517
1518 mWindow->consumeFocusEvent(true);
1519 }
1520
1521 void sendAndConsumeKeyDown() {
1522 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1523 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
1524 mDispatcher->notifyKey(&keyArgs);
1525
1526 // Window should receive key down event.
1527 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1528 }
1529
1530 void expectKeyRepeatOnce(int32_t repeatCount) {
1531 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
1532 InputEvent* repeatEvent = mWindow->consume();
1533 ASSERT_NE(nullptr, repeatEvent);
1534
1535 uint32_t eventType = repeatEvent->getType();
1536 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType);
1537
1538 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
1539 uint32_t eventAction = repeatKeyEvent->getAction();
1540 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
1541 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
1542 }
1543
1544 void sendAndConsumeKeyUp() {
1545 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
1546 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
1547 mDispatcher->notifyKey(&keyArgs);
1548
1549 // Window should receive key down event.
1550 mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
1551 0 /*expectedFlags*/);
1552 }
1553};
1554
1555TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
1556 sendAndConsumeKeyDown();
1557 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
1558 expectKeyRepeatOnce(repeatCount);
1559 }
1560}
1561
1562TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
1563 sendAndConsumeKeyDown();
1564 expectKeyRepeatOnce(1 /*repeatCount*/);
1565 sendAndConsumeKeyUp();
1566 mWindow->assertNoEvents();
1567}
1568
1569TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
1570 sendAndConsumeKeyDown();
1571 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
1572 InputEvent* repeatEvent = mWindow->consume();
1573 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
1574 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
1575 IdGenerator::getSource(repeatEvent->getId()));
1576 }
1577}
1578
1579TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
1580 sendAndConsumeKeyDown();
1581
1582 std::unordered_set<int32_t> idSet;
1583 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
1584 InputEvent* repeatEvent = mWindow->consume();
1585 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
1586 int32_t id = repeatEvent->getId();
1587 EXPECT_EQ(idSet.end(), idSet.find(id));
1588 idSet.insert(id);
1589 }
1590}
1591
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001592/* Test InputDispatcher for MultiDisplay */
1593class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
1594public:
1595 static constexpr int32_t SECOND_DISPLAY_ID = 1;
Prabir Pradhan3608aad2019-10-02 17:08:26 -07001596 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001597 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08001598
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001599 application1 = new FakeApplicationHandle();
1600 windowInPrimary = new FakeWindowHandle(application1, mDispatcher, "D_1",
1601 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08001602
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001603 // Set focus window for primary display, but focused display would be second one.
1604 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001605 windowInPrimary->setFocus(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001606 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001607 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08001608
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001609 application2 = new FakeApplicationHandle();
1610 windowInSecondary = new FakeWindowHandle(application2, mDispatcher, "D_2",
1611 SECOND_DISPLAY_ID);
1612 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001613 // Set focus display to second one.
1614 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
1615 // Set focus window for second display.
1616 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001617 windowInSecondary->setFocus(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001618 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001619 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001620 }
1621
Prabir Pradhan3608aad2019-10-02 17:08:26 -07001622 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001623 InputDispatcherTest::TearDown();
1624
1625 application1.clear();
1626 windowInPrimary.clear();
1627 application2.clear();
1628 windowInSecondary.clear();
1629 }
1630
1631protected:
1632 sp<FakeApplicationHandle> application1;
1633 sp<FakeWindowHandle> windowInPrimary;
1634 sp<FakeApplicationHandle> application2;
1635 sp<FakeWindowHandle> windowInSecondary;
1636};
1637
1638TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
1639 // Test touch down on primary display.
1640 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1641 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +08001642 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001643 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001644 windowInSecondary->assertNoEvents();
1645
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001646 // Test touch down on second display.
1647 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1648 AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Arthur Hungb92218b2018-08-14 12:00:21 +08001649 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1650 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001651 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08001652}
1653
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001654TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08001655 // Test inject a key down with display id specified.
1656 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
1657 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001658 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08001659 windowInSecondary->assertNoEvents();
1660
1661 // Test inject a key down without display id specified.
Arthur Hungb92218b2018-08-14 12:00:21 +08001662 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
1663 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1664 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001665 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08001666
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08001667 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00001668 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08001669
1670 // Expect old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001671 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
1672 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08001673
1674 // Test inject a key down, should timeout because of no target window.
1675 ASSERT_EQ(INPUT_EVENT_INJECTION_TIMED_OUT, injectKeyDown(mDispatcher))
1676 << "Inject key event should return INPUT_EVENT_INJECTION_TIMED_OUT";
1677 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001678 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08001679 windowInSecondary->assertNoEvents();
1680}
1681
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001682// Test per-display input monitors for motion event.
1683TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08001684 FakeMonitorReceiver monitorInPrimary =
1685 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
1686 FakeMonitorReceiver monitorInSecondary =
1687 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001688
1689 // Test touch down on primary display.
1690 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1691 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1692 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001693 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001694 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001695 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08001696 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001697
1698 // Test touch down on second display.
1699 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1700 AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
1701 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1702 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08001703 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001704 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08001705 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001706
1707 // Test inject a non-pointer motion event.
1708 // If specific a display, it will dispatch to the focused window of particular display,
1709 // or it will dispatch to the focused window of focused display.
1710 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1711 AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
1712 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1713 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08001714 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001715 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08001716 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001717}
1718
1719// Test per-display input monitors for key event.
1720TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
1721 //Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08001722 FakeMonitorReceiver monitorInPrimary =
1723 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
1724 FakeMonitorReceiver monitorInSecondary =
1725 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001726
1727 // Test inject a key down.
1728 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
1729 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1730 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08001731 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001732 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08001733 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001734}
1735
Jackal Guof9696682018-10-05 12:23:23 +08001736class InputFilterTest : public InputDispatcherTest {
1737protected:
1738 static constexpr int32_t SECOND_DISPLAY_ID = 1;
1739
1740 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) {
1741 NotifyMotionArgs motionArgs;
1742
1743 motionArgs = generateMotionArgs(
1744 AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
1745 mDispatcher->notifyMotion(&motionArgs);
1746 motionArgs = generateMotionArgs(
1747 AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
1748 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001749 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08001750 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08001751 mFakePolicy->assertFilterInputEventWasCalled(motionArgs);
Jackal Guof9696682018-10-05 12:23:23 +08001752 } else {
1753 mFakePolicy->assertFilterInputEventWasNotCalled();
1754 }
1755 }
1756
1757 void testNotifyKey(bool expectToBeFiltered) {
1758 NotifyKeyArgs keyArgs;
1759
1760 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
1761 mDispatcher->notifyKey(&keyArgs);
1762 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
1763 mDispatcher->notifyKey(&keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001764 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08001765
1766 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08001767 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08001768 } else {
1769 mFakePolicy->assertFilterInputEventWasNotCalled();
1770 }
1771 }
1772};
1773
1774// Test InputFilter for MotionEvent
1775TEST_F(InputFilterTest, MotionEvent_InputFilter) {
1776 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
1777 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
1778 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
1779
1780 // Enable InputFilter
1781 mDispatcher->setInputFilterEnabled(true);
1782 // Test touch on both primary and second display, and check if both events are filtered.
1783 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
1784 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
1785
1786 // Disable InputFilter
1787 mDispatcher->setInputFilterEnabled(false);
1788 // Test touch on both primary and second display, and check if both events aren't filtered.
1789 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
1790 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
1791}
1792
1793// Test InputFilter for KeyEvent
1794TEST_F(InputFilterTest, KeyEvent_InputFilter) {
1795 // Since the InputFilter is disabled by default, check if key event aren't filtered.
1796 testNotifyKey(/*expectToBeFiltered*/ false);
1797
1798 // Enable InputFilter
1799 mDispatcher->setInputFilterEnabled(true);
1800 // Send a key event, and check if it is filtered.
1801 testNotifyKey(/*expectToBeFiltered*/ true);
1802
1803 // Disable InputFilter
1804 mDispatcher->setInputFilterEnabled(false);
1805 // Send a key event, and check if it isn't filtered.
1806 testNotifyKey(/*expectToBeFiltered*/ false);
1807}
1808
chaviwfd6d3512019-03-25 13:23:49 -07001809class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07001810 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07001811 InputDispatcherTest::SetUp();
1812
1813 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1814 mUnfocusedWindow = new FakeWindowHandle(application, mDispatcher, "Top",
1815 ADISPLAY_ID_DEFAULT);
1816 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
1817 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
1818 // window.
1819 mUnfocusedWindow->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL);
1820
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08001821 mFocusedWindow =
1822 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
1823 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
1824 mFocusedWindow->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL);
1825 mFocusedWindowTouchPoint = 60;
chaviwfd6d3512019-03-25 13:23:49 -07001826
1827 // Set focused application.
1828 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001829 mFocusedWindow->setFocus(true);
chaviwfd6d3512019-03-25 13:23:49 -07001830
1831 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00001832 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001833 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07001834 }
1835
Prabir Pradhan3608aad2019-10-02 17:08:26 -07001836 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07001837 InputDispatcherTest::TearDown();
1838
1839 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08001840 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07001841 }
1842
1843protected:
1844 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08001845 sp<FakeWindowHandle> mFocusedWindow;
1846 int32_t mFocusedWindowTouchPoint;
chaviwfd6d3512019-03-25 13:23:49 -07001847};
1848
1849// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
1850// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
1851// the onPointerDownOutsideFocus callback.
1852TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
1853 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1854 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, 20, 20))
1855 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07001856 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07001857
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001858 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07001859 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
1860}
1861
1862// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
1863// DOWN on the window that doesn't have focus. Ensure no window received the
1864// onPointerDownOutsideFocus callback.
1865TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
1866 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1867 AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, 20, 20))
1868 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07001869 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07001870
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001871 ASSERT_TRUE(mDispatcher->waitForIdle());
1872 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07001873}
1874
1875// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
1876// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
1877TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
1878 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
1879 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07001880 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07001881
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001882 ASSERT_TRUE(mDispatcher->waitForIdle());
1883 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07001884}
1885
1886// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
1887// DOWN on the window that already has focus. Ensure no window received the
1888// onPointerDownOutsideFocus callback.
1889TEST_F(InputDispatcherOnPointerDownOutsideFocus,
1890 OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08001891 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1892 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1893 mFocusedWindowTouchPoint, mFocusedWindowTouchPoint))
chaviwfd6d3512019-03-25 13:23:49 -07001894 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07001895 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07001896
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001897 ASSERT_TRUE(mDispatcher->waitForIdle());
1898 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07001899}
1900
chaviwaf87b3e2019-10-01 16:59:28 -07001901// These tests ensures we can send touch events to a single client when there are multiple input
1902// windows that point to the same client token.
1903class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
1904 virtual void SetUp() override {
1905 InputDispatcherTest::SetUp();
1906
1907 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1908 mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1",
1909 ADISPLAY_ID_DEFAULT);
1910 // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window.
1911 // We also need FLAG_SPLIT_TOUCH or we won't be able to get touches for both windows.
1912 mWindow1->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL |
1913 InputWindowInfo::FLAG_SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07001914 mWindow1->setFrame(Rect(0, 0, 100, 100));
1915
1916 mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2",
1917 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
1918 mWindow2->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL |
1919 InputWindowInfo::FLAG_SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07001920 mWindow2->setFrame(Rect(100, 100, 200, 200));
1921
Arthur Hung72d8dc32020-03-28 00:48:39 +00001922 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07001923 }
1924
1925protected:
1926 sp<FakeWindowHandle> mWindow1;
1927 sp<FakeWindowHandle> mWindow2;
1928
1929 // Helper function to convert the point from screen coordinates into the window's space
1930 static PointF getPointInWindow(const InputWindowInfo* windowInfo, const PointF& point) {
1931 float x = windowInfo->windowXScale * (point.x - windowInfo->frameLeft);
1932 float y = windowInfo->windowYScale * (point.y - windowInfo->frameTop);
1933 return {x, y};
1934 }
1935
1936 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
1937 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001938 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07001939 InputEvent* event = window->consume();
1940
1941 ASSERT_NE(nullptr, event) << name.c_str()
1942 << ": consumer should have returned non-NULL event.";
1943
1944 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
1945 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
1946 << " event, got " << inputEventTypeToString(event->getType()) << " event";
1947
1948 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
1949 EXPECT_EQ(expectedAction, motionEvent.getAction());
1950
1951 for (size_t i = 0; i < points.size(); i++) {
1952 float expectedX = points[i].x;
1953 float expectedY = points[i].y;
1954
1955 EXPECT_EQ(expectedX, motionEvent.getX(i))
1956 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
1957 << ", got " << motionEvent.getX(i);
1958 EXPECT_EQ(expectedY, motionEvent.getY(i))
1959 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
1960 << ", got " << motionEvent.getY(i);
1961 }
1962 }
1963};
1964
1965TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
1966 // Touch Window 1
1967 PointF touchedPoint = {10, 10};
1968 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
1969
1970 NotifyMotionArgs motionArgs =
1971 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1972 ADISPLAY_ID_DEFAULT, {touchedPoint});
1973 mDispatcher->notifyMotion(&motionArgs);
1974 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, {expectedPoint});
1975
1976 // Release touch on Window 1
1977 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1978 ADISPLAY_ID_DEFAULT, {touchedPoint});
1979 mDispatcher->notifyMotion(&motionArgs);
1980 // consume the UP event
1981 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_UP, {expectedPoint});
1982
1983 // Touch Window 2
1984 touchedPoint = {150, 150};
1985 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
1986
1987 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1988 ADISPLAY_ID_DEFAULT, {touchedPoint});
1989 mDispatcher->notifyMotion(&motionArgs);
1990
1991 // Consuming from window1 since it's the window that has the InputReceiver
1992 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, {expectedPoint});
1993}
1994
1995TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentScale) {
1996 mWindow2->setWindowScale(0.5f, 0.5f);
1997
1998 // Touch Window 1
1999 PointF touchedPoint = {10, 10};
2000 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
2001
2002 NotifyMotionArgs motionArgs =
2003 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2004 ADISPLAY_ID_DEFAULT, {touchedPoint});
2005 mDispatcher->notifyMotion(&motionArgs);
2006 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, {expectedPoint});
2007
2008 // Release touch on Window 1
2009 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2010 ADISPLAY_ID_DEFAULT, {touchedPoint});
2011 mDispatcher->notifyMotion(&motionArgs);
2012 // consume the UP event
2013 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_UP, {expectedPoint});
2014
2015 // Touch Window 2
2016 touchedPoint = {150, 150};
2017 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
2018
2019 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2020 ADISPLAY_ID_DEFAULT, {touchedPoint});
2021 mDispatcher->notifyMotion(&motionArgs);
2022
2023 // Consuming from window1 since it's the window that has the InputReceiver
2024 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, {expectedPoint});
2025}
2026
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002027TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentScale) {
2028 mWindow2->setWindowScale(0.5f, 0.5f);
2029
2030 // Touch Window 1
2031 std::vector<PointF> touchedPoints = {PointF{10, 10}};
2032 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
2033
2034 NotifyMotionArgs motionArgs =
2035 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2036 ADISPLAY_ID_DEFAULT, touchedPoints);
2037 mDispatcher->notifyMotion(&motionArgs);
2038 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, expectedPoints);
2039
2040 // Touch Window 2
2041 int32_t actionPointerDown =
2042 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
2043 touchedPoints.emplace_back(PointF{150, 150});
2044 expectedPoints.emplace_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
2045
2046 motionArgs = generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN,
2047 ADISPLAY_ID_DEFAULT, touchedPoints);
2048 mDispatcher->notifyMotion(&motionArgs);
2049
2050 // Consuming from window1 since it's the window that has the InputReceiver
2051 consumeMotionEvent(mWindow1, actionPointerDown, expectedPoints);
2052}
2053
2054TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentScale) {
2055 mWindow2->setWindowScale(0.5f, 0.5f);
2056
2057 // Touch Window 1
2058 std::vector<PointF> touchedPoints = {PointF{10, 10}};
2059 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
2060
2061 NotifyMotionArgs motionArgs =
2062 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2063 ADISPLAY_ID_DEFAULT, touchedPoints);
2064 mDispatcher->notifyMotion(&motionArgs);
2065 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, expectedPoints);
2066
2067 // Touch Window 2
2068 int32_t actionPointerDown =
2069 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
2070 touchedPoints.emplace_back(PointF{150, 150});
2071 expectedPoints.emplace_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
2072
2073 motionArgs = generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN,
2074 ADISPLAY_ID_DEFAULT, touchedPoints);
2075 mDispatcher->notifyMotion(&motionArgs);
2076
2077 // Consuming from window1 since it's the window that has the InputReceiver
2078 consumeMotionEvent(mWindow1, actionPointerDown, expectedPoints);
2079
2080 // Move both windows
2081 touchedPoints = {{20, 20}, {175, 175}};
2082 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
2083 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
2084
2085 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2086 ADISPLAY_ID_DEFAULT, touchedPoints);
2087 mDispatcher->notifyMotion(&motionArgs);
2088
2089 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_MOVE, expectedPoints);
2090}
2091
2092TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
2093 mWindow1->setWindowScale(0.5f, 0.5f);
2094
2095 // Touch Window 1
2096 std::vector<PointF> touchedPoints = {PointF{10, 10}};
2097 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
2098
2099 NotifyMotionArgs motionArgs =
2100 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2101 ADISPLAY_ID_DEFAULT, touchedPoints);
2102 mDispatcher->notifyMotion(&motionArgs);
2103 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, expectedPoints);
2104
2105 // Touch Window 2
2106 int32_t actionPointerDown =
2107 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
2108 touchedPoints.emplace_back(PointF{150, 150});
2109 expectedPoints.emplace_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
2110
2111 motionArgs = generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN,
2112 ADISPLAY_ID_DEFAULT, touchedPoints);
2113 mDispatcher->notifyMotion(&motionArgs);
2114
2115 // Consuming from window1 since it's the window that has the InputReceiver
2116 consumeMotionEvent(mWindow1, actionPointerDown, expectedPoints);
2117
2118 // Move both windows
2119 touchedPoints = {{20, 20}, {175, 175}};
2120 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
2121 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
2122
2123 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2124 ADISPLAY_ID_DEFAULT, touchedPoints);
2125 mDispatcher->notifyMotion(&motionArgs);
2126
2127 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_MOVE, expectedPoints);
2128}
2129
Garfield Tane84e6f92019-08-29 17:28:41 -07002130} // namespace android::inputdispatcher