blob: 29f3dac03e43ce3cd4ebea91062d5b3a6940bf1c [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;
494static constexpr int32_t DISPATCHING_TIMEOUT = 100;
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;
chaviwaf87b3e2019-10-01 16:59:28 -0700637 mInfo.id = 0;
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 setId(int32_t id) { mInfo.id = id; }
676
677 void setWindowScale(float xScale, float yScale) {
678 mInfo.windowXScale = xScale;
679 mInfo.windowYScale = yScale;
680 }
681
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800682 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
683 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
684 expectedFlags);
685 }
686
Svet Ganov5d3bc372020-01-26 23:11:07 -0800687 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
688 int32_t expectedFlags = 0) {
689 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId,
690 expectedFlags);
691 }
692
693 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
694 int32_t expectedFlags = 0) {
695 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId,
696 expectedFlags);
697 }
698
699 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
700 int32_t expectedFlags = 0) {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800701 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
702 expectedFlags);
703 }
704
Svet Ganov5d3bc372020-01-26 23:11:07 -0800705 void consumeMotionPointerDown(int32_t pointerIdx,
706 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, int32_t expectedFlags = 0) {
707 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN
708 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
709 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
710 }
711
712 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
713 int32_t expectedFlags = 0) {
714 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP
715 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
716 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
717 }
718
719 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
720 int32_t expectedFlags = 0) {
Michael Wright3a240c42019-12-10 20:53:41 +0000721 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
722 expectedFlags);
723 }
724
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100725 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
726 ASSERT_NE(mInputReceiver, nullptr)
727 << "Cannot consume events from a window with no receiver";
728 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
729 }
730
chaviwd1c23182019-12-20 18:44:56 -0800731 void consumeEvent(int32_t expectedEventType, int32_t expectedAction, int32_t expectedDisplayId,
732 int32_t expectedFlags) {
733 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
734 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
735 expectedFlags);
736 }
737
chaviwaf87b3e2019-10-01 16:59:28 -0700738 InputEvent* consume() {
739 if (mInputReceiver == nullptr) {
740 return nullptr;
741 }
742 return mInputReceiver->consume();
743 }
744
Arthur Hungb92218b2018-08-14 12:00:21 +0800745 void assertNoEvents() {
chaviwd1c23182019-12-20 18:44:56 -0800746 ASSERT_NE(mInputReceiver, nullptr)
747 << "Call 'assertNoEvents' on a window with an InputReceiver";
748 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +0800749 }
750
chaviwaf87b3e2019-10-01 16:59:28 -0700751 sp<IBinder> getToken() { return mInfo.token; }
752
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100753 const std::string& getName() { return mName; }
754
chaviwd1c23182019-12-20 18:44:56 -0800755private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100756 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -0800757 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800758};
759
Tiger Huang721e26f2018-07-24 22:26:19 +0800760static int32_t injectKeyDown(const sp<InputDispatcher>& dispatcher,
761 int32_t displayId = ADISPLAY_ID_NONE) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800762 KeyEvent event;
763 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
764
765 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800766 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
767 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A,
768 AMETA_NONE,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600769 /* repeatCount */ 0, currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +0800770
771 // Inject event until dispatch out.
772 return dispatcher->injectInputEvent(
773 &event,
774 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT,
775 INJECT_EVENT_TIMEOUT, POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
776}
777
Garfield Tan00f511d2019-06-12 16:55:40 -0700778static int32_t injectMotionEvent(const sp<InputDispatcher>& dispatcher, int32_t action,
779 int32_t source, int32_t displayId, int32_t x, int32_t y,
780 int32_t xCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION,
781 int32_t yCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800782 MotionEvent event;
783 PointerProperties pointerProperties[1];
784 PointerCoords pointerCoords[1];
785
786 pointerProperties[0].clear();
787 pointerProperties[0].id = 0;
788 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
789
790 pointerCoords[0].clear();
chaviwfd6d3512019-03-25 13:23:49 -0700791 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
792 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Arthur Hungb92218b2018-08-14 12:00:21 +0800793
794 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
795 // Define a valid motion down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800796 event.initialize(InputEvent::nextId(), DEVICE_ID, source, displayId, INVALID_HMAC, action,
797 /* actionButton */ 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600798 /* flags */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -0700799 /* edgeFlags */ 0, AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600800 /* xScale */ 1, /* yScale */ 1, /* xOffset */ 0, /* yOffset */ 0,
801 /* xPrecision */ 0, /* yPrecision */ 0, xCursorPosition, yCursorPosition,
802 currentTime, currentTime,
Garfield Tan00f511d2019-06-12 16:55:40 -0700803 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Arthur Hungb92218b2018-08-14 12:00:21 +0800804
805 // Inject event until dispatch out.
806 return dispatcher->injectInputEvent(
807 &event,
808 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT,
809 INJECT_EVENT_TIMEOUT, POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
810}
811
Garfield Tan00f511d2019-06-12 16:55:40 -0700812static int32_t injectMotionDown(const sp<InputDispatcher>& dispatcher, int32_t source,
813 int32_t displayId, int32_t x = 100, int32_t y = 200) {
814 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, x, y);
815}
816
Michael Wright3a240c42019-12-10 20:53:41 +0000817static int32_t injectMotionUp(const sp<InputDispatcher>& dispatcher, int32_t source,
818 int32_t displayId, int32_t x = 100, int32_t y = 200) {
819 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, x, y);
820}
821
Jackal Guof9696682018-10-05 12:23:23 +0800822static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
823 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
824 // Define a valid key event.
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800825 NotifyKeyArgs args(/* id */ 0, currentTime, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
826 POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, KEY_A,
827 AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +0800828
829 return args;
830}
831
chaviwd1c23182019-12-20 18:44:56 -0800832static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId,
833 const std::vector<PointF>& points) {
834 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -0700835 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
836 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
837 }
838
chaviwd1c23182019-12-20 18:44:56 -0800839 PointerProperties pointerProperties[pointerCount];
840 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +0800841
chaviwd1c23182019-12-20 18:44:56 -0800842 for (size_t i = 0; i < pointerCount; i++) {
843 pointerProperties[i].clear();
844 pointerProperties[i].id = i;
845 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jackal Guof9696682018-10-05 12:23:23 +0800846
chaviwd1c23182019-12-20 18:44:56 -0800847 pointerCoords[i].clear();
848 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
849 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
850 }
Jackal Guof9696682018-10-05 12:23:23 +0800851
852 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
853 // Define a valid motion event.
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800854 NotifyMotionArgs args(/* id */ 0, currentTime, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -0700855 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
856 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -0800857 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
858 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -0700859 AMOTION_EVENT_INVALID_CURSOR_POSITION,
860 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +0800861
862 return args;
863}
864
chaviwd1c23182019-12-20 18:44:56 -0800865static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
866 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
867}
868
Arthur Hungb92218b2018-08-14 12:00:21 +0800869TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
870 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800871 sp<FakeWindowHandle> window = new FakeWindowHandle(application, mDispatcher, "Fake Window",
872 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800873
Arthur Hung72d8dc32020-03-28 00:48:39 +0000874 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800875 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
876 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +0800877 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
878
879 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800880 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800881}
882
883// The foreground window should receive the first touch down event.
884TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
885 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800886 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
887 ADISPLAY_ID_DEFAULT);
888 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
889 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800890
Arthur Hung72d8dc32020-03-28 00:48:39 +0000891 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800892 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
893 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +0800894 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
895
896 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800897 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800898 windowSecond->assertNoEvents();
899}
900
901TEST_F(InputDispatcherTest, SetInputWindow_FocusedWindow) {
902 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800903 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
904 ADISPLAY_ID_DEFAULT);
905 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
906 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800907
Arthur Hung7ab76b12019-01-09 19:17:20 +0800908 // Set focused application.
Tiger Huang721e26f2018-07-24 22:26:19 +0800909 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Arthur Hungb92218b2018-08-14 12:00:21 +0800910
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100911 // Display should have only one focused window
912 windowSecond->setFocus(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +0000913 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100914
915 windowSecond->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +0800916 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
917 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
918
919 // Focused window should receive event.
920 windowTop->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800921 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +0800922}
923
Arthur Hung7ab76b12019-01-09 19:17:20 +0800924TEST_F(InputDispatcherTest, SetInputWindow_FocusPriority) {
925 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
926 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
927 ADISPLAY_ID_DEFAULT);
928 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
929 ADISPLAY_ID_DEFAULT);
930
931 // Set focused application.
932 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
933
934 // Display has two focused windows. Add them to inputWindowsHandles in z-order (top most first)
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100935 windowTop->setFocus(true);
936 windowSecond->setFocus(true);
Arthur Hung7ab76b12019-01-09 19:17:20 +0800937
Arthur Hung72d8dc32020-03-28 00:48:39 +0000938 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100939 windowTop->consumeFocusEvent(true);
Arthur Hung7ab76b12019-01-09 19:17:20 +0800940 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
941 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
942
943 // Top focused window should receive event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800944 windowTop->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung7ab76b12019-01-09 19:17:20 +0800945 windowSecond->assertNoEvents();
946}
947
Arthur Hung3b413f22018-10-26 18:05:34 +0800948TEST_F(InputDispatcherTest, SetInputWindow_InputWindowInfo) {
949 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
950
951 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
952 ADISPLAY_ID_DEFAULT);
953 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
954 ADISPLAY_ID_DEFAULT);
955
Arthur Hung832bc4a2019-01-28 11:43:17 +0800956 // Set focused application.
957 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Arthur Hung3b413f22018-10-26 18:05:34 +0800958
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100959 windowTop->setFocus(true);
960 windowSecond->setFocus(true);
Arthur Hung3b413f22018-10-26 18:05:34 +0800961 // Release channel for window is no longer valid.
962 windowTop->releaseChannel();
Arthur Hung72d8dc32020-03-28 00:48:39 +0000963 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100964 windowSecond->consumeFocusEvent(true);
Arthur Hung3b413f22018-10-26 18:05:34 +0800965
Arthur Hung832bc4a2019-01-28 11:43:17 +0800966 // Test inject a key down, should dispatch to a valid window.
967 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
968 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Arthur Hung3b413f22018-10-26 18:05:34 +0800969
970 // Top window is invalid, so it should not receive any input event.
971 windowTop->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800972 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung3b413f22018-10-26 18:05:34 +0800973}
974
Garfield Tan00f511d2019-06-12 16:55:40 -0700975TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
976 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
977
978 sp<FakeWindowHandle> windowLeft =
979 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
980 windowLeft->setFrame(Rect(0, 0, 600, 800));
981 windowLeft->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL);
982 sp<FakeWindowHandle> windowRight =
983 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
984 windowRight->setFrame(Rect(600, 0, 1200, 800));
985 windowRight->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL);
986
987 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
988
Arthur Hung72d8dc32020-03-28 00:48:39 +0000989 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -0700990
991 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
992 // left window. This event should be dispatched to the left window.
993 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
994 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
995 ADISPLAY_ID_DEFAULT, 610, 400, 599, 400));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800996 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -0700997 windowRight->assertNoEvents();
998}
999
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001000TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
1001 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1002 sp<FakeWindowHandle> window =
1003 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001004 window->setFocus(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001005
Arthur Hung72d8dc32020-03-28 00:48:39 +00001006 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001007 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001008
1009 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1010 mDispatcher->notifyKey(&keyArgs);
1011
1012 // Window should receive key down event.
1013 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1014
1015 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
1016 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001017 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001018 mDispatcher->notifyDeviceReset(&args);
1019 window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
1020 AKEY_EVENT_FLAG_CANCELED);
1021}
1022
1023TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
1024 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1025 sp<FakeWindowHandle> window =
1026 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1027
Arthur Hung72d8dc32020-03-28 00:48:39 +00001028 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001029
1030 NotifyMotionArgs motionArgs =
1031 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1032 ADISPLAY_ID_DEFAULT);
1033 mDispatcher->notifyMotion(&motionArgs);
1034
1035 // Window should receive motion down event.
1036 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1037
1038 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
1039 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001040 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001041 mDispatcher->notifyDeviceReset(&args);
1042 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT,
1043 0 /*expectedFlags*/);
1044}
1045
Svet Ganov5d3bc372020-01-26 23:11:07 -08001046TEST_F(InputDispatcherTest, TransferTouchFocus_OnePointer) {
1047 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1048
1049 // Create a couple of windows
1050 sp<FakeWindowHandle> firstWindow = new FakeWindowHandle(application, mDispatcher,
1051 "First Window", ADISPLAY_ID_DEFAULT);
1052 sp<FakeWindowHandle> secondWindow = new FakeWindowHandle(application, mDispatcher,
1053 "Second Window", ADISPLAY_ID_DEFAULT);
1054
1055 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001056 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001057
1058 // Send down to the first window
1059 NotifyMotionArgs downMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
1060 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT);
1061 mDispatcher->notifyMotion(&downMotionArgs);
1062 // Only the first window should get the down event
1063 firstWindow->consumeMotionDown();
1064 secondWindow->assertNoEvents();
1065
1066 // Transfer touch focus to the second window
1067 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1068 // The first window gets cancel and the second gets down
1069 firstWindow->consumeMotionCancel();
1070 secondWindow->consumeMotionDown();
1071
1072 // Send up event to the second window
1073 NotifyMotionArgs upMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP,
1074 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT);
1075 mDispatcher->notifyMotion(&upMotionArgs);
1076 // The first window gets no events and the second gets up
1077 firstWindow->assertNoEvents();
1078 secondWindow->consumeMotionUp();
1079}
1080
1081TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointerNoSplitTouch) {
1082 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1083
1084 PointF touchPoint = {10, 10};
1085
1086 // Create a couple of windows
1087 sp<FakeWindowHandle> firstWindow = new FakeWindowHandle(application, mDispatcher,
1088 "First Window", ADISPLAY_ID_DEFAULT);
1089 sp<FakeWindowHandle> secondWindow = new FakeWindowHandle(application, mDispatcher,
1090 "Second Window", ADISPLAY_ID_DEFAULT);
1091
1092 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001093 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001094
1095 // Send down to the first window
1096 NotifyMotionArgs downMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
1097 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {touchPoint});
1098 mDispatcher->notifyMotion(&downMotionArgs);
1099 // Only the first window should get the down event
1100 firstWindow->consumeMotionDown();
1101 secondWindow->assertNoEvents();
1102
1103 // Send pointer down to the first window
1104 NotifyMotionArgs pointerDownMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN
1105 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1106 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint});
1107 mDispatcher->notifyMotion(&pointerDownMotionArgs);
1108 // Only the first window should get the pointer down event
1109 firstWindow->consumeMotionPointerDown(1);
1110 secondWindow->assertNoEvents();
1111
1112 // Transfer touch focus to the second window
1113 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1114 // The first window gets cancel and the second gets down and pointer down
1115 firstWindow->consumeMotionCancel();
1116 secondWindow->consumeMotionDown();
1117 secondWindow->consumeMotionPointerDown(1);
1118
1119 // Send pointer up to the second window
1120 NotifyMotionArgs pointerUpMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP
1121 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1122 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint});
1123 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1124 // The first window gets nothing and the second gets pointer up
1125 firstWindow->assertNoEvents();
1126 secondWindow->consumeMotionPointerUp(1);
1127
1128 // Send up event to the second window
1129 NotifyMotionArgs upMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP,
1130 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT);
1131 mDispatcher->notifyMotion(&upMotionArgs);
1132 // The first window gets nothing and the second gets up
1133 firstWindow->assertNoEvents();
1134 secondWindow->consumeMotionUp();
1135}
1136
1137TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
1138 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1139
1140 // Create a non touch modal window that supports split touch
1141 sp<FakeWindowHandle> firstWindow = new FakeWindowHandle(application, mDispatcher,
1142 "First Window", ADISPLAY_ID_DEFAULT);
1143 firstWindow->setFrame(Rect(0, 0, 600, 400));
1144 firstWindow->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL
1145 | InputWindowInfo::FLAG_SPLIT_TOUCH);
1146
1147 // Create a non touch modal window that supports split touch
1148 sp<FakeWindowHandle> secondWindow = new FakeWindowHandle(application, mDispatcher,
1149 "Second Window", ADISPLAY_ID_DEFAULT);
1150 secondWindow->setFrame(Rect(0, 400, 600, 800));
1151 secondWindow->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL
1152 | InputWindowInfo::FLAG_SPLIT_TOUCH);
1153
1154 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001155 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001156
1157 PointF pointInFirst = {300, 200};
1158 PointF pointInSecond = {300, 600};
1159
1160 // Send down to the first window
1161 NotifyMotionArgs firstDownMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
1162 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {pointInFirst});
1163 mDispatcher->notifyMotion(&firstDownMotionArgs);
1164 // Only the first window should get the down event
1165 firstWindow->consumeMotionDown();
1166 secondWindow->assertNoEvents();
1167
1168 // Send down to the second window
1169 NotifyMotionArgs secondDownMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN
1170 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1171 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {pointInFirst, pointInSecond});
1172 mDispatcher->notifyMotion(&secondDownMotionArgs);
1173 // The first window gets a move and the second a down
1174 firstWindow->consumeMotionMove();
1175 secondWindow->consumeMotionDown();
1176
1177 // Transfer touch focus to the second window
1178 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1179 // The first window gets cancel and the new gets pointer down (it already saw down)
1180 firstWindow->consumeMotionCancel();
1181 secondWindow->consumeMotionPointerDown(1);
1182
1183 // Send pointer up to the second window
1184 NotifyMotionArgs pointerUpMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP
1185 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1186 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {pointInFirst, pointInSecond});
1187 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1188 // The first window gets nothing and the second gets pointer up
1189 firstWindow->assertNoEvents();
1190 secondWindow->consumeMotionPointerUp(1);
1191
1192 // Send up event to the second window
1193 NotifyMotionArgs upMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP,
1194 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT);
1195 mDispatcher->notifyMotion(&upMotionArgs);
1196 // The first window gets nothing and the second gets up
1197 firstWindow->assertNoEvents();
1198 secondWindow->consumeMotionUp();
1199}
1200
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001201TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
1202 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1203 sp<FakeWindowHandle> window =
1204 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1205
1206 window->setFocus(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001207 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001208
1209 window->consumeFocusEvent(true);
1210
1211 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1212 mDispatcher->notifyKey(&keyArgs);
1213
1214 // Window should receive key down event.
1215 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1216}
1217
1218TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
1219 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1220 sp<FakeWindowHandle> window =
1221 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1222
Arthur Hung72d8dc32020-03-28 00:48:39 +00001223 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001224
1225 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1226 mDispatcher->notifyKey(&keyArgs);
1227 mDispatcher->waitForIdle();
1228
1229 window->assertNoEvents();
1230}
1231
1232// If a window is touchable, but does not have focus, it should receive motion events, but not keys
1233TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
1234 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1235 sp<FakeWindowHandle> window =
1236 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1237
Arthur Hung72d8dc32020-03-28 00:48:39 +00001238 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001239
1240 // Send key
1241 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1242 mDispatcher->notifyKey(&keyArgs);
1243 // Send motion
1244 NotifyMotionArgs motionArgs =
1245 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1246 ADISPLAY_ID_DEFAULT);
1247 mDispatcher->notifyMotion(&motionArgs);
1248
1249 // Window should receive only the motion event
1250 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1251 window->assertNoEvents(); // Key event or focus event will not be received
1252}
1253
chaviwd1c23182019-12-20 18:44:56 -08001254class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00001255public:
1256 FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name,
chaviwd1c23182019-12-20 18:44:56 -08001257 int32_t displayId, bool isGestureMonitor = false) {
1258 sp<InputChannel> serverChannel, clientChannel;
1259 InputChannel::openInputChannelPair(name, serverChannel, clientChannel);
1260 mInputReceiver = std::make_unique<FakeInputReceiver>(clientChannel, name);
1261 dispatcher->registerInputMonitor(serverChannel, displayId, isGestureMonitor);
Michael Wright3a240c42019-12-10 20:53:41 +00001262 }
1263
chaviwd1c23182019-12-20 18:44:56 -08001264 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
1265
1266 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1267 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN,
1268 expectedDisplayId, expectedFlags);
1269 }
1270
1271 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1272 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
1273 expectedDisplayId, expectedFlags);
1274 }
1275
1276 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1277 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP,
1278 expectedDisplayId, expectedFlags);
1279 }
1280
1281 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
1282
1283private:
1284 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00001285};
1286
1287// Tests for gesture monitors
1288TEST_F(InputDispatcherTest, GestureMonitor_ReceivesMotionEvents) {
1289 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1290 sp<FakeWindowHandle> window =
1291 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001292 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00001293
chaviwd1c23182019-12-20 18:44:56 -08001294 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
1295 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00001296
1297 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1298 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1299 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1300 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001301 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00001302}
1303
1304TEST_F(InputDispatcherTest, GestureMonitor_DoesNotReceiveKeyEvents) {
1305 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1306 sp<FakeWindowHandle> window =
1307 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1308
1309 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001310 window->setFocus(true);
Michael Wright3a240c42019-12-10 20:53:41 +00001311
Arthur Hung72d8dc32020-03-28 00:48:39 +00001312 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001313 window->consumeFocusEvent(true);
Michael Wright3a240c42019-12-10 20:53:41 +00001314
chaviwd1c23182019-12-20 18:44:56 -08001315 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
1316 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00001317
1318 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
1319 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1320 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001321 monitor.assertNoEvents();
Michael Wright3a240c42019-12-10 20:53:41 +00001322}
1323
1324TEST_F(InputDispatcherTest, GestureMonitor_CanPilferAfterWindowIsRemovedMidStream) {
1325 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1326 sp<FakeWindowHandle> window =
1327 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001328 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00001329
chaviwd1c23182019-12-20 18:44:56 -08001330 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
1331 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00001332
1333 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1334 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1335 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1336 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001337 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00001338
1339 window->releaseChannel();
1340
chaviwd1c23182019-12-20 18:44:56 -08001341 mDispatcher->pilferPointers(monitor.getToken());
Michael Wright3a240c42019-12-10 20:53:41 +00001342
1343 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1344 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1345 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08001346 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00001347}
1348
chaviw81e2bb92019-12-18 15:03:51 -08001349TEST_F(InputDispatcherTest, TestMoveEvent) {
1350 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1351 sp<FakeWindowHandle> window =
1352 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1353
Arthur Hung72d8dc32020-03-28 00:48:39 +00001354 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08001355
1356 NotifyMotionArgs motionArgs =
1357 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1358 ADISPLAY_ID_DEFAULT);
1359
1360 mDispatcher->notifyMotion(&motionArgs);
1361 // Window should receive motion down event.
1362 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1363
1364 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001365 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08001366 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1367 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
1368 motionArgs.pointerCoords[0].getX() - 10);
1369
1370 mDispatcher->notifyMotion(&motionArgs);
1371 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
1372 0 /*expectedFlags*/);
1373}
1374
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001375/**
1376 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
1377 * the device default right away. In the test scenario, we check both the default value,
1378 * and the action of enabling / disabling.
1379 */
1380TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
1381 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1382 sp<FakeWindowHandle> window =
1383 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
1384
1385 // Set focused application.
1386 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1387 window->setFocus(true);
1388
1389 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00001390 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001391 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
1392
1393 SCOPED_TRACE("Remove the window to trigger focus loss");
1394 window->setFocus(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001395 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001396 window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/);
1397
1398 SCOPED_TRACE("Disable touch mode");
1399 mDispatcher->setInTouchMode(false);
1400 window->setFocus(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001401 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001402 window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/);
1403
1404 SCOPED_TRACE("Remove the window to trigger focus loss");
1405 window->setFocus(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001406 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001407 window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/);
1408
1409 SCOPED_TRACE("Enable touch mode again");
1410 mDispatcher->setInTouchMode(true);
1411 window->setFocus(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001412 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001413 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
1414
1415 window->assertNoEvents();
1416}
1417
Gang Wange9087892020-01-07 12:17:14 -05001418TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
1419 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1420 sp<FakeWindowHandle> window =
1421 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
1422
1423 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1424 window->setFocus(true);
1425
Arthur Hung72d8dc32020-03-28 00:48:39 +00001426 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Gang Wange9087892020-01-07 12:17:14 -05001427 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
1428
1429 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
1430 mDispatcher->notifyKey(&keyArgs);
1431
1432 InputEvent* event = window->consume();
1433 ASSERT_NE(event, nullptr);
1434
1435 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
1436 ASSERT_NE(verified, nullptr);
1437 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
1438
1439 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
1440 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
1441 ASSERT_EQ(keyArgs.source, verified->source);
1442 ASSERT_EQ(keyArgs.displayId, verified->displayId);
1443
1444 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
1445
1446 ASSERT_EQ(keyArgs.action, verifiedKey.action);
1447 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05001448 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
1449 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
1450 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
1451 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
1452 ASSERT_EQ(0, verifiedKey.repeatCount);
1453}
1454
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08001455TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
1456 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1457 sp<FakeWindowHandle> window =
1458 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
1459
1460 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1461
Arthur Hung72d8dc32020-03-28 00:48:39 +00001462 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08001463
1464 NotifyMotionArgs motionArgs =
1465 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1466 ADISPLAY_ID_DEFAULT);
1467 mDispatcher->notifyMotion(&motionArgs);
1468
1469 InputEvent* event = window->consume();
1470 ASSERT_NE(event, nullptr);
1471
1472 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
1473 ASSERT_NE(verified, nullptr);
1474 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
1475
1476 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
1477 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
1478 EXPECT_EQ(motionArgs.source, verified->source);
1479 EXPECT_EQ(motionArgs.displayId, verified->displayId);
1480
1481 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
1482
1483 EXPECT_EQ(motionArgs.pointerCoords[0].getX(), verifiedMotion.rawX);
1484 EXPECT_EQ(motionArgs.pointerCoords[0].getY(), verifiedMotion.rawY);
1485 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
1486 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
1487 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
1488 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
1489 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
1490}
1491
Garfield Tan1c7bc862020-01-28 13:24:04 -08001492class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
1493protected:
1494 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
1495 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
1496
1497 sp<FakeApplicationHandle> mApp;
1498 sp<FakeWindowHandle> mWindow;
1499
1500 virtual void SetUp() override {
1501 mFakePolicy = new FakeInputDispatcherPolicy();
1502 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
1503 mDispatcher = new InputDispatcher(mFakePolicy);
1504 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
1505 ASSERT_EQ(OK, mDispatcher->start());
1506
1507 setUpWindow();
1508 }
1509
1510 void setUpWindow() {
1511 mApp = new FakeApplicationHandle();
1512 mWindow = new FakeWindowHandle(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1513
1514 mWindow->setFocus(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001515 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Garfield Tan1c7bc862020-01-28 13:24:04 -08001516
1517 mWindow->consumeFocusEvent(true);
1518 }
1519
1520 void sendAndConsumeKeyDown() {
1521 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1522 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
1523 mDispatcher->notifyKey(&keyArgs);
1524
1525 // Window should receive key down event.
1526 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1527 }
1528
1529 void expectKeyRepeatOnce(int32_t repeatCount) {
1530 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
1531 InputEvent* repeatEvent = mWindow->consume();
1532 ASSERT_NE(nullptr, repeatEvent);
1533
1534 uint32_t eventType = repeatEvent->getType();
1535 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType);
1536
1537 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
1538 uint32_t eventAction = repeatKeyEvent->getAction();
1539 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
1540 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
1541 }
1542
1543 void sendAndConsumeKeyUp() {
1544 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
1545 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
1546 mDispatcher->notifyKey(&keyArgs);
1547
1548 // Window should receive key down event.
1549 mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
1550 0 /*expectedFlags*/);
1551 }
1552};
1553
1554TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
1555 sendAndConsumeKeyDown();
1556 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
1557 expectKeyRepeatOnce(repeatCount);
1558 }
1559}
1560
1561TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
1562 sendAndConsumeKeyDown();
1563 expectKeyRepeatOnce(1 /*repeatCount*/);
1564 sendAndConsumeKeyUp();
1565 mWindow->assertNoEvents();
1566}
1567
1568TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
1569 sendAndConsumeKeyDown();
1570 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
1571 InputEvent* repeatEvent = mWindow->consume();
1572 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
1573 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
1574 IdGenerator::getSource(repeatEvent->getId()));
1575 }
1576}
1577
1578TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
1579 sendAndConsumeKeyDown();
1580
1581 std::unordered_set<int32_t> idSet;
1582 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
1583 InputEvent* repeatEvent = mWindow->consume();
1584 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
1585 int32_t id = repeatEvent->getId();
1586 EXPECT_EQ(idSet.end(), idSet.find(id));
1587 idSet.insert(id);
1588 }
1589}
1590
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001591/* Test InputDispatcher for MultiDisplay */
1592class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
1593public:
1594 static constexpr int32_t SECOND_DISPLAY_ID = 1;
Prabir Pradhan3608aad2019-10-02 17:08:26 -07001595 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001596 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08001597
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001598 application1 = new FakeApplicationHandle();
1599 windowInPrimary = new FakeWindowHandle(application1, mDispatcher, "D_1",
1600 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08001601
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001602 // Set focus window for primary display, but focused display would be second one.
1603 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001604 windowInPrimary->setFocus(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001605 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001606 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08001607
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001608 application2 = new FakeApplicationHandle();
1609 windowInSecondary = new FakeWindowHandle(application2, mDispatcher, "D_2",
1610 SECOND_DISPLAY_ID);
1611 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001612 // Set focus display to second one.
1613 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
1614 // Set focus window for second display.
1615 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001616 windowInSecondary->setFocus(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001617 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001618 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001619 }
1620
Prabir Pradhan3608aad2019-10-02 17:08:26 -07001621 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001622 InputDispatcherTest::TearDown();
1623
1624 application1.clear();
1625 windowInPrimary.clear();
1626 application2.clear();
1627 windowInSecondary.clear();
1628 }
1629
1630protected:
1631 sp<FakeApplicationHandle> application1;
1632 sp<FakeWindowHandle> windowInPrimary;
1633 sp<FakeApplicationHandle> application2;
1634 sp<FakeWindowHandle> windowInSecondary;
1635};
1636
1637TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
1638 // Test touch down on primary display.
1639 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1640 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +08001641 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001642 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001643 windowInSecondary->assertNoEvents();
1644
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001645 // Test touch down on second display.
1646 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1647 AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Arthur Hungb92218b2018-08-14 12:00:21 +08001648 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1649 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001650 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08001651}
1652
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001653TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08001654 // Test inject a key down with display id specified.
1655 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
1656 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001657 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08001658 windowInSecondary->assertNoEvents();
1659
1660 // Test inject a key down without display id specified.
Arthur Hungb92218b2018-08-14 12:00:21 +08001661 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
1662 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1663 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001664 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08001665
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08001666 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00001667 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08001668
1669 // Expect old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001670 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
1671 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08001672
1673 // Test inject a key down, should timeout because of no target window.
1674 ASSERT_EQ(INPUT_EVENT_INJECTION_TIMED_OUT, injectKeyDown(mDispatcher))
1675 << "Inject key event should return INPUT_EVENT_INJECTION_TIMED_OUT";
1676 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001677 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08001678 windowInSecondary->assertNoEvents();
1679}
1680
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001681// Test per-display input monitors for motion event.
1682TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08001683 FakeMonitorReceiver monitorInPrimary =
1684 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
1685 FakeMonitorReceiver monitorInSecondary =
1686 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001687
1688 // Test touch down on primary display.
1689 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1690 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1691 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001692 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001693 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001694 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08001695 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001696
1697 // Test touch down on second display.
1698 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1699 AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
1700 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1701 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08001702 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001703 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08001704 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001705
1706 // Test inject a non-pointer motion event.
1707 // If specific a display, it will dispatch to the focused window of particular display,
1708 // or it will dispatch to the focused window of focused display.
1709 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1710 AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
1711 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1712 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08001713 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001714 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08001715 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001716}
1717
1718// Test per-display input monitors for key event.
1719TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
1720 //Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08001721 FakeMonitorReceiver monitorInPrimary =
1722 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
1723 FakeMonitorReceiver monitorInSecondary =
1724 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001725
1726 // Test inject a key down.
1727 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
1728 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1729 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08001730 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001731 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08001732 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001733}
1734
Jackal Guof9696682018-10-05 12:23:23 +08001735class InputFilterTest : public InputDispatcherTest {
1736protected:
1737 static constexpr int32_t SECOND_DISPLAY_ID = 1;
1738
1739 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) {
1740 NotifyMotionArgs motionArgs;
1741
1742 motionArgs = generateMotionArgs(
1743 AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
1744 mDispatcher->notifyMotion(&motionArgs);
1745 motionArgs = generateMotionArgs(
1746 AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
1747 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001748 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08001749 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08001750 mFakePolicy->assertFilterInputEventWasCalled(motionArgs);
Jackal Guof9696682018-10-05 12:23:23 +08001751 } else {
1752 mFakePolicy->assertFilterInputEventWasNotCalled();
1753 }
1754 }
1755
1756 void testNotifyKey(bool expectToBeFiltered) {
1757 NotifyKeyArgs keyArgs;
1758
1759 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
1760 mDispatcher->notifyKey(&keyArgs);
1761 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
1762 mDispatcher->notifyKey(&keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001763 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08001764
1765 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08001766 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08001767 } else {
1768 mFakePolicy->assertFilterInputEventWasNotCalled();
1769 }
1770 }
1771};
1772
1773// Test InputFilter for MotionEvent
1774TEST_F(InputFilterTest, MotionEvent_InputFilter) {
1775 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
1776 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
1777 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
1778
1779 // Enable InputFilter
1780 mDispatcher->setInputFilterEnabled(true);
1781 // Test touch on both primary and second display, and check if both events are filtered.
1782 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
1783 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
1784
1785 // Disable InputFilter
1786 mDispatcher->setInputFilterEnabled(false);
1787 // Test touch on both primary and second display, and check if both events aren't filtered.
1788 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
1789 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
1790}
1791
1792// Test InputFilter for KeyEvent
1793TEST_F(InputFilterTest, KeyEvent_InputFilter) {
1794 // Since the InputFilter is disabled by default, check if key event aren't filtered.
1795 testNotifyKey(/*expectToBeFiltered*/ false);
1796
1797 // Enable InputFilter
1798 mDispatcher->setInputFilterEnabled(true);
1799 // Send a key event, and check if it is filtered.
1800 testNotifyKey(/*expectToBeFiltered*/ true);
1801
1802 // Disable InputFilter
1803 mDispatcher->setInputFilterEnabled(false);
1804 // Send a key event, and check if it isn't filtered.
1805 testNotifyKey(/*expectToBeFiltered*/ false);
1806}
1807
chaviwfd6d3512019-03-25 13:23:49 -07001808class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07001809 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07001810 InputDispatcherTest::SetUp();
1811
1812 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1813 mUnfocusedWindow = new FakeWindowHandle(application, mDispatcher, "Top",
1814 ADISPLAY_ID_DEFAULT);
1815 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
1816 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
1817 // window.
1818 mUnfocusedWindow->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL);
1819
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08001820 mFocusedWindow =
1821 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
1822 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
1823 mFocusedWindow->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL);
1824 mFocusedWindowTouchPoint = 60;
chaviwfd6d3512019-03-25 13:23:49 -07001825
1826 // Set focused application.
1827 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001828 mFocusedWindow->setFocus(true);
chaviwfd6d3512019-03-25 13:23:49 -07001829
1830 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00001831 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001832 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07001833 }
1834
Prabir Pradhan3608aad2019-10-02 17:08:26 -07001835 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07001836 InputDispatcherTest::TearDown();
1837
1838 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08001839 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07001840 }
1841
1842protected:
1843 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08001844 sp<FakeWindowHandle> mFocusedWindow;
1845 int32_t mFocusedWindowTouchPoint;
chaviwfd6d3512019-03-25 13:23:49 -07001846};
1847
1848// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
1849// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
1850// the onPointerDownOutsideFocus callback.
1851TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
1852 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1853 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, 20, 20))
1854 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
chaviwfd6d3512019-03-25 13:23:49 -07001855
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001856 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07001857 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
1858}
1859
1860// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
1861// DOWN on the window that doesn't have focus. Ensure no window received the
1862// onPointerDownOutsideFocus callback.
1863TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
1864 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1865 AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, 20, 20))
1866 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
chaviwfd6d3512019-03-25 13:23:49 -07001867
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001868 ASSERT_TRUE(mDispatcher->waitForIdle());
1869 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07001870}
1871
1872// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
1873// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
1874TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
1875 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
1876 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
chaviwfd6d3512019-03-25 13:23:49 -07001877
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001878 ASSERT_TRUE(mDispatcher->waitForIdle());
1879 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07001880}
1881
1882// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
1883// DOWN on the window that already has focus. Ensure no window received the
1884// onPointerDownOutsideFocus callback.
1885TEST_F(InputDispatcherOnPointerDownOutsideFocus,
1886 OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08001887 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1888 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1889 mFocusedWindowTouchPoint, mFocusedWindowTouchPoint))
chaviwfd6d3512019-03-25 13:23:49 -07001890 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
chaviwfd6d3512019-03-25 13:23:49 -07001891
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001892 ASSERT_TRUE(mDispatcher->waitForIdle());
1893 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07001894}
1895
chaviwaf87b3e2019-10-01 16:59:28 -07001896// These tests ensures we can send touch events to a single client when there are multiple input
1897// windows that point to the same client token.
1898class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
1899 virtual void SetUp() override {
1900 InputDispatcherTest::SetUp();
1901
1902 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1903 mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1",
1904 ADISPLAY_ID_DEFAULT);
1905 // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window.
1906 // We also need FLAG_SPLIT_TOUCH or we won't be able to get touches for both windows.
1907 mWindow1->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL |
1908 InputWindowInfo::FLAG_SPLIT_TOUCH);
1909 mWindow1->setId(0);
1910 mWindow1->setFrame(Rect(0, 0, 100, 100));
1911
1912 mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2",
1913 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
1914 mWindow2->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL |
1915 InputWindowInfo::FLAG_SPLIT_TOUCH);
1916 mWindow2->setId(1);
1917 mWindow2->setFrame(Rect(100, 100, 200, 200));
1918
Arthur Hung72d8dc32020-03-28 00:48:39 +00001919 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07001920 }
1921
1922protected:
1923 sp<FakeWindowHandle> mWindow1;
1924 sp<FakeWindowHandle> mWindow2;
1925
1926 // Helper function to convert the point from screen coordinates into the window's space
1927 static PointF getPointInWindow(const InputWindowInfo* windowInfo, const PointF& point) {
1928 float x = windowInfo->windowXScale * (point.x - windowInfo->frameLeft);
1929 float y = windowInfo->windowYScale * (point.y - windowInfo->frameTop);
1930 return {x, y};
1931 }
1932
1933 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
1934 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001935 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07001936 InputEvent* event = window->consume();
1937
1938 ASSERT_NE(nullptr, event) << name.c_str()
1939 << ": consumer should have returned non-NULL event.";
1940
1941 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
1942 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
1943 << " event, got " << inputEventTypeToString(event->getType()) << " event";
1944
1945 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
1946 EXPECT_EQ(expectedAction, motionEvent.getAction());
1947
1948 for (size_t i = 0; i < points.size(); i++) {
1949 float expectedX = points[i].x;
1950 float expectedY = points[i].y;
1951
1952 EXPECT_EQ(expectedX, motionEvent.getX(i))
1953 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
1954 << ", got " << motionEvent.getX(i);
1955 EXPECT_EQ(expectedY, motionEvent.getY(i))
1956 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
1957 << ", got " << motionEvent.getY(i);
1958 }
1959 }
1960};
1961
1962TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
1963 // Touch Window 1
1964 PointF touchedPoint = {10, 10};
1965 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
1966
1967 NotifyMotionArgs motionArgs =
1968 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1969 ADISPLAY_ID_DEFAULT, {touchedPoint});
1970 mDispatcher->notifyMotion(&motionArgs);
1971 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, {expectedPoint});
1972
1973 // Release touch on Window 1
1974 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1975 ADISPLAY_ID_DEFAULT, {touchedPoint});
1976 mDispatcher->notifyMotion(&motionArgs);
1977 // consume the UP event
1978 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_UP, {expectedPoint});
1979
1980 // Touch Window 2
1981 touchedPoint = {150, 150};
1982 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
1983
1984 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1985 ADISPLAY_ID_DEFAULT, {touchedPoint});
1986 mDispatcher->notifyMotion(&motionArgs);
1987
1988 // Consuming from window1 since it's the window that has the InputReceiver
1989 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, {expectedPoint});
1990}
1991
1992TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentScale) {
1993 mWindow2->setWindowScale(0.5f, 0.5f);
1994
1995 // Touch Window 1
1996 PointF touchedPoint = {10, 10};
1997 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
1998
1999 NotifyMotionArgs motionArgs =
2000 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2001 ADISPLAY_ID_DEFAULT, {touchedPoint});
2002 mDispatcher->notifyMotion(&motionArgs);
2003 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, {expectedPoint});
2004
2005 // Release touch on Window 1
2006 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2007 ADISPLAY_ID_DEFAULT, {touchedPoint});
2008 mDispatcher->notifyMotion(&motionArgs);
2009 // consume the UP event
2010 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_UP, {expectedPoint});
2011
2012 // Touch Window 2
2013 touchedPoint = {150, 150};
2014 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
2015
2016 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2017 ADISPLAY_ID_DEFAULT, {touchedPoint});
2018 mDispatcher->notifyMotion(&motionArgs);
2019
2020 // Consuming from window1 since it's the window that has the InputReceiver
2021 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, {expectedPoint});
2022}
2023
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002024TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentScale) {
2025 mWindow2->setWindowScale(0.5f, 0.5f);
2026
2027 // Touch Window 1
2028 std::vector<PointF> touchedPoints = {PointF{10, 10}};
2029 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
2030
2031 NotifyMotionArgs motionArgs =
2032 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2033 ADISPLAY_ID_DEFAULT, touchedPoints);
2034 mDispatcher->notifyMotion(&motionArgs);
2035 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, expectedPoints);
2036
2037 // Touch Window 2
2038 int32_t actionPointerDown =
2039 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
2040 touchedPoints.emplace_back(PointF{150, 150});
2041 expectedPoints.emplace_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
2042
2043 motionArgs = generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN,
2044 ADISPLAY_ID_DEFAULT, touchedPoints);
2045 mDispatcher->notifyMotion(&motionArgs);
2046
2047 // Consuming from window1 since it's the window that has the InputReceiver
2048 consumeMotionEvent(mWindow1, actionPointerDown, expectedPoints);
2049}
2050
2051TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentScale) {
2052 mWindow2->setWindowScale(0.5f, 0.5f);
2053
2054 // Touch Window 1
2055 std::vector<PointF> touchedPoints = {PointF{10, 10}};
2056 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
2057
2058 NotifyMotionArgs motionArgs =
2059 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2060 ADISPLAY_ID_DEFAULT, touchedPoints);
2061 mDispatcher->notifyMotion(&motionArgs);
2062 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, expectedPoints);
2063
2064 // Touch Window 2
2065 int32_t actionPointerDown =
2066 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
2067 touchedPoints.emplace_back(PointF{150, 150});
2068 expectedPoints.emplace_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
2069
2070 motionArgs = generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN,
2071 ADISPLAY_ID_DEFAULT, touchedPoints);
2072 mDispatcher->notifyMotion(&motionArgs);
2073
2074 // Consuming from window1 since it's the window that has the InputReceiver
2075 consumeMotionEvent(mWindow1, actionPointerDown, expectedPoints);
2076
2077 // Move both windows
2078 touchedPoints = {{20, 20}, {175, 175}};
2079 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
2080 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
2081
2082 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2083 ADISPLAY_ID_DEFAULT, touchedPoints);
2084 mDispatcher->notifyMotion(&motionArgs);
2085
2086 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_MOVE, expectedPoints);
2087}
2088
2089TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
2090 mWindow1->setWindowScale(0.5f, 0.5f);
2091
2092 // Touch Window 1
2093 std::vector<PointF> touchedPoints = {PointF{10, 10}};
2094 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
2095
2096 NotifyMotionArgs motionArgs =
2097 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2098 ADISPLAY_ID_DEFAULT, touchedPoints);
2099 mDispatcher->notifyMotion(&motionArgs);
2100 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, expectedPoints);
2101
2102 // Touch Window 2
2103 int32_t actionPointerDown =
2104 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
2105 touchedPoints.emplace_back(PointF{150, 150});
2106 expectedPoints.emplace_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
2107
2108 motionArgs = generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN,
2109 ADISPLAY_ID_DEFAULT, touchedPoints);
2110 mDispatcher->notifyMotion(&motionArgs);
2111
2112 // Consuming from window1 since it's the window that has the InputReceiver
2113 consumeMotionEvent(mWindow1, actionPointerDown, expectedPoints);
2114
2115 // Move both windows
2116 touchedPoints = {{20, 20}, {175, 175}};
2117 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
2118 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
2119
2120 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2121 ADISPLAY_ID_DEFAULT, touchedPoints);
2122 mDispatcher->notifyMotion(&motionArgs);
2123
2124 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_MOVE, expectedPoints);
2125}
2126
Garfield Tane84e6f92019-08-29 17:28:41 -07002127} // namespace android::inputdispatcher