blob: 2b74868645dd435d810a6514427f1f8b990f542f [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
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700130 virtual nsecs_t notifyAnr(const sp<InputApplicationHandle>&, const sp<IBinder>&,
131 const std::string&) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800132 return 0;
133 }
134
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700135 virtual void notifyInputChannelBroken(const sp<IBinder>&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800136
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700137 virtual void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700138
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700139 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800140 *outConfig = mConfig;
141 }
142
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800143 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Jackal Guof9696682018-10-05 12:23:23 +0800144 switch (inputEvent->getType()) {
145 case AINPUT_EVENT_TYPE_KEY: {
146 const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800147 mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800148 break;
149 }
150
151 case AINPUT_EVENT_TYPE_MOTION: {
152 const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800153 mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800154 break;
155 }
156 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800157 return true;
158 }
159
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700160 virtual void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800161
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700162 virtual void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800163
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700164 virtual nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*,
165 uint32_t) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800166 return 0;
167 }
168
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700169 virtual bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t,
170 KeyEvent*) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800171 return false;
172 }
173
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800174 virtual void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
175 uint32_t policyFlags) override {
176 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
177 * essentially a passthrough for notifySwitch.
178 */
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800179 mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800180 }
181
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700182 virtual void pokeUserActivity(nsecs_t, int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800183
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700184 virtual bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800185 return false;
186 }
Jackal Guof9696682018-10-05 12:23:23 +0800187
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700188 virtual void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
chaviwfd6d3512019-03-25 13:23:49 -0700189 mOnPointerDownToken = newToken;
190 }
191
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800192 void assertFilterInputEventWasCalled(int type, nsecs_t eventTime, int32_t action,
193 int32_t displayId) {
194 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
195 ASSERT_EQ(mFilteredEvent->getType(), type);
196
197 if (type == AINPUT_EVENT_TYPE_KEY) {
198 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*mFilteredEvent);
199 EXPECT_EQ(keyEvent.getEventTime(), eventTime);
200 EXPECT_EQ(keyEvent.getAction(), action);
201 EXPECT_EQ(keyEvent.getDisplayId(), displayId);
202 } else if (type == AINPUT_EVENT_TYPE_MOTION) {
203 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*mFilteredEvent);
204 EXPECT_EQ(motionEvent.getEventTime(), eventTime);
205 EXPECT_EQ(motionEvent.getAction(), action);
206 EXPECT_EQ(motionEvent.getDisplayId(), displayId);
207 } else {
208 FAIL() << "Unknown type: " << type;
209 }
210
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800211 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800212 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800213};
214
Gang Wang342c9272020-01-13 13:15:04 -0500215// --- HmacKeyManagerTest ---
216
217class HmacKeyManagerTest : public testing::Test {
218protected:
219 HmacKeyManager mHmacKeyManager;
220};
221
222/**
223 * Ensure that separate calls to sign the same data are generating the same key.
224 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
225 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
226 * tests.
227 */
228TEST_F(HmacKeyManagerTest, GeneratedHmac_IsConsistent) {
229 KeyEvent event = getTestKeyEvent();
230 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
231
232 std::array<uint8_t, 32> hmac1 = mHmacKeyManager.sign(verifiedEvent);
233 std::array<uint8_t, 32> hmac2 = mHmacKeyManager.sign(verifiedEvent);
234 ASSERT_EQ(hmac1, hmac2);
235}
236
237/**
238 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
239 */
240TEST_F(HmacKeyManagerTest, GeneratedHmac_ChangesWhenFieldsChange) {
241 KeyEvent event = getTestKeyEvent();
242 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
243 std::array<uint8_t, 32> initialHmac = mHmacKeyManager.sign(verifiedEvent);
244
245 verifiedEvent.deviceId += 1;
246 ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent));
247
248 verifiedEvent.source += 1;
249 ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent));
250
251 verifiedEvent.eventTimeNanos += 1;
252 ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent));
253
254 verifiedEvent.displayId += 1;
255 ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent));
256
257 verifiedEvent.action += 1;
258 ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent));
259
260 verifiedEvent.downTimeNanos += 1;
261 ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent));
262
263 verifiedEvent.flags += 1;
264 ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent));
265
266 verifiedEvent.keyCode += 1;
267 ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent));
268
269 verifiedEvent.scanCode += 1;
270 ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent));
271
272 verifiedEvent.metaState += 1;
273 ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent));
274
275 verifiedEvent.repeatCount += 1;
276 ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent));
277}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800278
279// --- InputDispatcherTest ---
280
281class InputDispatcherTest : public testing::Test {
282protected:
283 sp<FakeInputDispatcherPolicy> mFakePolicy;
284 sp<InputDispatcher> mDispatcher;
285
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700286 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800287 mFakePolicy = new FakeInputDispatcherPolicy();
288 mDispatcher = new InputDispatcher(mFakePolicy);
Arthur Hungb92218b2018-08-14 12:00:21 +0800289 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
290 //Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700291 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800292 }
293
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700294 virtual void TearDown() override {
295 ASSERT_EQ(OK, mDispatcher->stop());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800296 mFakePolicy.clear();
297 mDispatcher.clear();
298 }
299};
300
301
302TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
303 KeyEvent event;
304
305 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800306 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
307 INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600308 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
309 ARBITRARY_TIME);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800310 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800311 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800312 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
313 << "Should reject key events with undefined action.";
314
315 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800316 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
317 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600318 ARBITRARY_TIME, ARBITRARY_TIME);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800319 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800320 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800321 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
322 << "Should reject key events with ACTION_MULTIPLE.";
323}
324
325TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
326 MotionEvent event;
327 PointerProperties pointerProperties[MAX_POINTERS + 1];
328 PointerCoords pointerCoords[MAX_POINTERS + 1];
329 for (int i = 0; i <= MAX_POINTERS; i++) {
330 pointerProperties[i].clear();
331 pointerProperties[i].id = i;
332 pointerCoords[i].clear();
333 }
334
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800335 // Some constants commonly used below
336 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
337 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
338 constexpr int32_t metaState = AMETA_NONE;
339 constexpr MotionClassification classification = MotionClassification::NONE;
340
Michael Wrightd02c5b62014-02-10 15:10:22 -0800341 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800342 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600343 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification, 1 /* xScale */,
344 1 /* yScale */, 0, 0, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
345 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700346 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800347 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800348 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800349 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
350 << "Should reject motion events with undefined action.";
351
352 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800353 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700354 AMOTION_EVENT_ACTION_POINTER_DOWN |
355 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600356 0, 0, edgeFlags, metaState, 0, classification, 1 /* xScale */, 1 /* yScale */,
357 0, 0, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
358 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700359 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800360 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800361 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800362 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
363 << "Should reject motion events with pointer down index too large.";
364
Garfield Tanfbe732e2020-01-24 11:26:14 -0800365 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700366 AMOTION_EVENT_ACTION_POINTER_DOWN |
367 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600368 0, 0, edgeFlags, metaState, 0, classification, 1 /* xScale */, 1 /* yScale */,
369 0, 0, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
370 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700371 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800372 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800373 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800374 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
375 << "Should reject motion events with pointer down index too small.";
376
377 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800378 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700379 AMOTION_EVENT_ACTION_POINTER_UP |
380 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600381 0, 0, edgeFlags, metaState, 0, classification, 1 /* xScale */, 1 /* yScale */,
382 0, 0, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
383 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700384 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800385 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800386 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800387 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
388 << "Should reject motion events with pointer up index too large.";
389
Garfield Tanfbe732e2020-01-24 11:26:14 -0800390 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700391 AMOTION_EVENT_ACTION_POINTER_UP |
392 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600393 0, 0, edgeFlags, metaState, 0, classification, 1 /* xScale */, 1 /* yScale */,
394 0, 0, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
395 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700396 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800397 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800398 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800399 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
400 << "Should reject motion events with pointer up index too small.";
401
402 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800403 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
404 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
405 1 /* xScale */, 1 /* yScale */, 0, 0, 0, 0,
406 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
407 ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700408 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800409 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800410 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800411 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
412 << "Should reject motion events with 0 pointers.";
413
Garfield Tanfbe732e2020-01-24 11:26:14 -0800414 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
415 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
416 1 /* xScale */, 1 /* yScale */, 0, 0, 0, 0,
417 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
418 ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700419 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800420 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800421 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800422 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
423 << "Should reject motion events with more than MAX_POINTERS pointers.";
424
425 // Rejects motion events with invalid pointer ids.
426 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800427 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
428 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
429 1 /* xScale */, 1 /* yScale */, 0, 0, 0, 0,
430 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
431 ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700432 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800433 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800434 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800435 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
436 << "Should reject motion events with pointer ids less than 0.";
437
438 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800439 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
440 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
441 1 /* xScale */, 1 /* yScale */, 0, 0, 0, 0,
442 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
443 ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700444 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800445 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800446 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800447 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
448 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
449
450 // Rejects motion events with duplicate pointer ids.
451 pointerProperties[0].id = 1;
452 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800453 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
454 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
455 1 /* xScale */, 1 /* yScale */, 0, 0, 0, 0,
456 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
457 ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700458 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800459 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800460 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800461 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
462 << "Should reject motion events with duplicate pointer ids.";
463}
464
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800465/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
466
467TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
468 constexpr nsecs_t eventTime = 20;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800469 NotifyConfigurationChangedArgs args(10 /*id*/, eventTime);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800470 mDispatcher->notifyConfigurationChanged(&args);
471 ASSERT_TRUE(mDispatcher->waitForIdle());
472
473 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
474}
475
476TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800477 NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/,
478 2 /*switchMask*/);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800479 mDispatcher->notifySwitch(&args);
480
481 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
482 args.policyFlags |= POLICY_FLAG_TRUSTED;
483 mFakePolicy->assertNotifySwitchWasCalled(args);
484}
485
Arthur Hungb92218b2018-08-14 12:00:21 +0800486// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800487static constexpr int32_t INJECT_EVENT_TIMEOUT = 500;
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -0700488static constexpr nsecs_t DISPATCHING_TIMEOUT = seconds_to_nanoseconds(5);
Arthur Hungb92218b2018-08-14 12:00:21 +0800489
490class FakeApplicationHandle : public InputApplicationHandle {
491public:
492 FakeApplicationHandle() {}
493 virtual ~FakeApplicationHandle() {}
494
495 virtual bool updateInfo() {
Arthur Hung7a0c39a2019-03-20 16:52:24 +0800496 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Arthur Hungb92218b2018-08-14 12:00:21 +0800497 return true;
498 }
499};
500
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800501class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800502public:
chaviwd1c23182019-12-20 18:44:56 -0800503 explicit FakeInputReceiver(const sp<InputChannel>& clientChannel, const std::string name)
504 : mName(name) {
505 mConsumer = std::make_unique<InputConsumer>(clientChannel);
506 }
507
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800508 InputEvent* consume() {
Arthur Hungb92218b2018-08-14 12:00:21 +0800509 uint32_t consumeSeq;
510 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800511
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800512 std::chrono::time_point start = std::chrono::steady_clock::now();
513 status_t status = WOULD_BLOCK;
514 while (status == WOULD_BLOCK) {
chaviw81e2bb92019-12-18 15:03:51 -0800515 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800516 &event);
517 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
518 if (elapsed > 100ms) {
519 break;
520 }
521 }
522
523 if (status == WOULD_BLOCK) {
524 // Just means there's no event available.
525 return nullptr;
526 }
527
528 if (status != OK) {
529 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
530 return nullptr;
531 }
532 if (event == nullptr) {
533 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
534 return nullptr;
535 }
536
chaviwd1c23182019-12-20 18:44:56 -0800537 status = mConsumer->sendFinishedSignal(consumeSeq, true);
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800538 if (status != OK) {
539 ADD_FAILURE() << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
540 }
541 return event;
542 }
543
544 void consumeEvent(int32_t expectedEventType, int32_t expectedAction, int32_t expectedDisplayId,
545 int32_t expectedFlags) {
546 InputEvent* event = consume();
547
548 ASSERT_NE(nullptr, event) << mName.c_str()
549 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800550 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800551 << mName.c_str() << "expected " << inputEventTypeToString(expectedEventType)
552 << " event, got " << inputEventTypeToString(event->getType()) << " event";
Arthur Hungb92218b2018-08-14 12:00:21 +0800553
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800554 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
Tiger Huang8664f8c2018-10-11 19:14:35 +0800555
Tiger Huang8664f8c2018-10-11 19:14:35 +0800556 switch (expectedEventType) {
557 case AINPUT_EVENT_TYPE_KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800558 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
559 EXPECT_EQ(expectedAction, keyEvent.getAction());
560 EXPECT_EQ(expectedFlags, keyEvent.getFlags());
Tiger Huang8664f8c2018-10-11 19:14:35 +0800561 break;
562 }
563 case AINPUT_EVENT_TYPE_MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800564 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
565 EXPECT_EQ(expectedAction, motionEvent.getAction());
566 EXPECT_EQ(expectedFlags, motionEvent.getFlags());
Tiger Huang8664f8c2018-10-11 19:14:35 +0800567 break;
568 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100569 case AINPUT_EVENT_TYPE_FOCUS: {
570 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
571 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800572 default: {
573 FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType;
574 }
575 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800576 }
577
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100578 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
579 InputEvent* event = consume();
580 ASSERT_NE(nullptr, event) << mName.c_str()
581 << ": consumer should have returned non-NULL event.";
582 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
583 << "Got " << inputEventTypeToString(event->getType())
584 << " event instead of FOCUS event";
585
586 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
587 << mName.c_str() << ": event displayId should always be NONE.";
588
589 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
590 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
591 EXPECT_EQ(inTouchMode, focusEvent->getInTouchMode());
592 }
593
chaviwd1c23182019-12-20 18:44:56 -0800594 void assertNoEvents() {
595 InputEvent* event = consume();
596 ASSERT_EQ(nullptr, event)
597 << mName.c_str()
598 << ": should not have received any events, so consume() should return NULL";
599 }
600
601 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
602
603protected:
604 std::unique_ptr<InputConsumer> mConsumer;
605 PreallocatedInputEventFactory mEventFactory;
606
607 std::string mName;
608};
609
610class FakeWindowHandle : public InputWindowHandle {
611public:
612 static const int32_t WIDTH = 600;
613 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -0800614
615 FakeWindowHandle(const sp<InputApplicationHandle>& inputApplicationHandle,
616 const sp<InputDispatcher>& dispatcher, const std::string name,
617 int32_t displayId, sp<IBinder> token = nullptr)
618 : mName(name) {
619 if (token == nullptr) {
620 sp<InputChannel> serverChannel, clientChannel;
621 InputChannel::openInputChannelPair(name, serverChannel, clientChannel);
622 mInputReceiver = std::make_unique<FakeInputReceiver>(clientChannel, name);
623 dispatcher->registerInputChannel(serverChannel);
624 token = serverChannel->getConnectionToken();
625 }
626
627 inputApplicationHandle->updateInfo();
628 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
629
630 mInfo.token = token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700631 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -0800632 mInfo.name = name;
633 mInfo.layoutParamsFlags = 0;
634 mInfo.layoutParamsType = InputWindowInfo::TYPE_APPLICATION;
635 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
636 mInfo.frameLeft = 0;
637 mInfo.frameTop = 0;
638 mInfo.frameRight = WIDTH;
639 mInfo.frameBottom = HEIGHT;
640 mInfo.globalScaleFactor = 1.0;
641 mInfo.touchableRegion.clear();
642 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
643 mInfo.visible = true;
644 mInfo.canReceiveKeys = true;
645 mInfo.hasFocus = false;
646 mInfo.hasWallpaper = false;
647 mInfo.paused = false;
chaviwd1c23182019-12-20 18:44:56 -0800648 mInfo.ownerPid = INJECTOR_PID;
649 mInfo.ownerUid = INJECTOR_UID;
650 mInfo.inputFeatures = 0;
651 mInfo.displayId = displayId;
652 }
653
654 virtual bool updateInfo() { return true; }
655
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100656 void setFocus(bool hasFocus) { mInfo.hasFocus = hasFocus; }
chaviwd1c23182019-12-20 18:44:56 -0800657
658 void setFrame(const Rect& frame) {
659 mInfo.frameLeft = frame.left;
660 mInfo.frameTop = frame.top;
661 mInfo.frameRight = frame.right;
662 mInfo.frameBottom = frame.bottom;
663 mInfo.touchableRegion.clear();
664 mInfo.addTouchableRegion(frame);
665 }
666
667 void setLayoutParamFlags(int32_t flags) { mInfo.layoutParamsFlags = flags; }
668
chaviwaf87b3e2019-10-01 16:59:28 -0700669 void setWindowScale(float xScale, float yScale) {
670 mInfo.windowXScale = xScale;
671 mInfo.windowYScale = yScale;
672 }
673
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800674 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
675 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
676 expectedFlags);
677 }
678
Svet Ganov5d3bc372020-01-26 23:11:07 -0800679 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
680 int32_t expectedFlags = 0) {
681 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId,
682 expectedFlags);
683 }
684
685 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
686 int32_t expectedFlags = 0) {
687 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId,
688 expectedFlags);
689 }
690
691 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
692 int32_t expectedFlags = 0) {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800693 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
694 expectedFlags);
695 }
696
Svet Ganov5d3bc372020-01-26 23:11:07 -0800697 void consumeMotionPointerDown(int32_t pointerIdx,
698 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, int32_t expectedFlags = 0) {
699 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN
700 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
701 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
702 }
703
704 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
705 int32_t expectedFlags = 0) {
706 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP
707 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
708 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
709 }
710
711 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
712 int32_t expectedFlags = 0) {
Michael Wright3a240c42019-12-10 20:53:41 +0000713 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
714 expectedFlags);
715 }
716
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100717 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
718 ASSERT_NE(mInputReceiver, nullptr)
719 << "Cannot consume events from a window with no receiver";
720 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
721 }
722
chaviwd1c23182019-12-20 18:44:56 -0800723 void consumeEvent(int32_t expectedEventType, int32_t expectedAction, int32_t expectedDisplayId,
724 int32_t expectedFlags) {
725 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
726 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
727 expectedFlags);
728 }
729
chaviwaf87b3e2019-10-01 16:59:28 -0700730 InputEvent* consume() {
731 if (mInputReceiver == nullptr) {
732 return nullptr;
733 }
734 return mInputReceiver->consume();
735 }
736
Arthur Hungb92218b2018-08-14 12:00:21 +0800737 void assertNoEvents() {
chaviwd1c23182019-12-20 18:44:56 -0800738 ASSERT_NE(mInputReceiver, nullptr)
739 << "Call 'assertNoEvents' on a window with an InputReceiver";
740 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +0800741 }
742
chaviwaf87b3e2019-10-01 16:59:28 -0700743 sp<IBinder> getToken() { return mInfo.token; }
744
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100745 const std::string& getName() { return mName; }
746
chaviwd1c23182019-12-20 18:44:56 -0800747private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100748 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -0800749 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700750 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800751};
752
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700753std::atomic<int32_t> FakeWindowHandle::sId{1};
754
Tiger Huang721e26f2018-07-24 22:26:19 +0800755static int32_t injectKeyDown(const sp<InputDispatcher>& dispatcher,
756 int32_t displayId = ADISPLAY_ID_NONE) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800757 KeyEvent event;
758 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
759
760 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800761 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
762 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A,
763 AMETA_NONE,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600764 /* repeatCount */ 0, currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +0800765
766 // Inject event until dispatch out.
767 return dispatcher->injectInputEvent(
768 &event,
769 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT,
770 INJECT_EVENT_TIMEOUT, POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
771}
772
Garfield Tan00f511d2019-06-12 16:55:40 -0700773static int32_t injectMotionEvent(const sp<InputDispatcher>& dispatcher, int32_t action,
774 int32_t source, int32_t displayId, int32_t x, int32_t y,
775 int32_t xCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION,
776 int32_t yCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800777 MotionEvent event;
778 PointerProperties pointerProperties[1];
779 PointerCoords pointerCoords[1];
780
781 pointerProperties[0].clear();
782 pointerProperties[0].id = 0;
783 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
784
785 pointerCoords[0].clear();
chaviwfd6d3512019-03-25 13:23:49 -0700786 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
787 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Arthur Hungb92218b2018-08-14 12:00:21 +0800788
789 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
790 // Define a valid motion down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800791 event.initialize(InputEvent::nextId(), DEVICE_ID, source, displayId, INVALID_HMAC, action,
792 /* actionButton */ 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600793 /* flags */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -0700794 /* edgeFlags */ 0, AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600795 /* xScale */ 1, /* yScale */ 1, /* xOffset */ 0, /* yOffset */ 0,
796 /* xPrecision */ 0, /* yPrecision */ 0, xCursorPosition, yCursorPosition,
797 currentTime, currentTime,
Garfield Tan00f511d2019-06-12 16:55:40 -0700798 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Arthur Hungb92218b2018-08-14 12:00:21 +0800799
800 // Inject event until dispatch out.
801 return dispatcher->injectInputEvent(
802 &event,
803 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT,
804 INJECT_EVENT_TIMEOUT, POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
805}
806
Garfield Tan00f511d2019-06-12 16:55:40 -0700807static int32_t injectMotionDown(const sp<InputDispatcher>& dispatcher, int32_t source,
808 int32_t displayId, int32_t x = 100, int32_t y = 200) {
809 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, x, y);
810}
811
Michael Wright3a240c42019-12-10 20:53:41 +0000812static int32_t injectMotionUp(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_UP, source, displayId, x, y);
815}
816
Jackal Guof9696682018-10-05 12:23:23 +0800817static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
818 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
819 // Define a valid key event.
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800820 NotifyKeyArgs args(/* id */ 0, currentTime, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
821 POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, KEY_A,
822 AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +0800823
824 return args;
825}
826
chaviwd1c23182019-12-20 18:44:56 -0800827static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId,
828 const std::vector<PointF>& points) {
829 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -0700830 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
831 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
832 }
833
chaviwd1c23182019-12-20 18:44:56 -0800834 PointerProperties pointerProperties[pointerCount];
835 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +0800836
chaviwd1c23182019-12-20 18:44:56 -0800837 for (size_t i = 0; i < pointerCount; i++) {
838 pointerProperties[i].clear();
839 pointerProperties[i].id = i;
840 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jackal Guof9696682018-10-05 12:23:23 +0800841
chaviwd1c23182019-12-20 18:44:56 -0800842 pointerCoords[i].clear();
843 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
844 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
845 }
Jackal Guof9696682018-10-05 12:23:23 +0800846
847 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
848 // Define a valid motion event.
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800849 NotifyMotionArgs args(/* id */ 0, currentTime, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -0700850 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
851 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -0800852 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
853 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -0700854 AMOTION_EVENT_INVALID_CURSOR_POSITION,
855 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +0800856
857 return args;
858}
859
chaviwd1c23182019-12-20 18:44:56 -0800860static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
861 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
862}
863
Arthur Hungb92218b2018-08-14 12:00:21 +0800864TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
865 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800866 sp<FakeWindowHandle> window = new FakeWindowHandle(application, mDispatcher, "Fake Window",
867 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800868
Arthur Hung72d8dc32020-03-28 00:48:39 +0000869 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800870 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
871 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +0800872 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
873
874 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800875 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800876}
877
878// The foreground window should receive the first touch down event.
879TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
880 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800881 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
882 ADISPLAY_ID_DEFAULT);
883 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
884 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800885
Arthur Hung72d8dc32020-03-28 00:48:39 +0000886 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800887 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
888 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +0800889 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
890
891 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800892 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800893 windowSecond->assertNoEvents();
894}
895
896TEST_F(InputDispatcherTest, SetInputWindow_FocusedWindow) {
897 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800898 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
899 ADISPLAY_ID_DEFAULT);
900 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
901 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800902
Arthur Hung7ab76b12019-01-09 19:17:20 +0800903 // Set focused application.
Tiger Huang721e26f2018-07-24 22:26:19 +0800904 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Arthur Hungb92218b2018-08-14 12:00:21 +0800905
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100906 // Display should have only one focused window
907 windowSecond->setFocus(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +0000908 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100909
910 windowSecond->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +0800911 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
912 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
913
914 // Focused window should receive event.
915 windowTop->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800916 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +0800917}
918
Arthur Hung7ab76b12019-01-09 19:17:20 +0800919TEST_F(InputDispatcherTest, SetInputWindow_FocusPriority) {
920 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
921 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
922 ADISPLAY_ID_DEFAULT);
923 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
924 ADISPLAY_ID_DEFAULT);
925
926 // Set focused application.
927 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
928
929 // Display has two focused windows. Add them to inputWindowsHandles in z-order (top most first)
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100930 windowTop->setFocus(true);
931 windowSecond->setFocus(true);
Arthur Hung7ab76b12019-01-09 19:17:20 +0800932
Arthur Hung72d8dc32020-03-28 00:48:39 +0000933 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100934 windowTop->consumeFocusEvent(true);
Arthur Hung7ab76b12019-01-09 19:17:20 +0800935 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
936 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
937
938 // Top focused window should receive event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800939 windowTop->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung7ab76b12019-01-09 19:17:20 +0800940 windowSecond->assertNoEvents();
941}
942
Arthur Hung3b413f22018-10-26 18:05:34 +0800943TEST_F(InputDispatcherTest, SetInputWindow_InputWindowInfo) {
944 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
945
946 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
947 ADISPLAY_ID_DEFAULT);
948 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
949 ADISPLAY_ID_DEFAULT);
950
Arthur Hung832bc4a2019-01-28 11:43:17 +0800951 // Set focused application.
952 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Arthur Hung3b413f22018-10-26 18:05:34 +0800953
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100954 windowTop->setFocus(true);
955 windowSecond->setFocus(true);
Arthur Hung3b413f22018-10-26 18:05:34 +0800956 // Release channel for window is no longer valid.
957 windowTop->releaseChannel();
Arthur Hung72d8dc32020-03-28 00:48:39 +0000958 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100959 windowSecond->consumeFocusEvent(true);
Arthur Hung3b413f22018-10-26 18:05:34 +0800960
Arthur Hung832bc4a2019-01-28 11:43:17 +0800961 // Test inject a key down, should dispatch to a valid window.
962 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
963 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Arthur Hung3b413f22018-10-26 18:05:34 +0800964
965 // Top window is invalid, so it should not receive any input event.
966 windowTop->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800967 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung3b413f22018-10-26 18:05:34 +0800968}
969
Garfield Tan00f511d2019-06-12 16:55:40 -0700970TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
971 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
972
973 sp<FakeWindowHandle> windowLeft =
974 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
975 windowLeft->setFrame(Rect(0, 0, 600, 800));
976 windowLeft->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL);
977 sp<FakeWindowHandle> windowRight =
978 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
979 windowRight->setFrame(Rect(600, 0, 1200, 800));
980 windowRight->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL);
981
982 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
983
Arthur Hung72d8dc32020-03-28 00:48:39 +0000984 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -0700985
986 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
987 // left window. This event should be dispatched to the left window.
988 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
989 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
990 ADISPLAY_ID_DEFAULT, 610, 400, 599, 400));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800991 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -0700992 windowRight->assertNoEvents();
993}
994
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800995TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
996 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
997 sp<FakeWindowHandle> window =
998 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100999 window->setFocus(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001000
Arthur Hung72d8dc32020-03-28 00:48:39 +00001001 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001002 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001003
1004 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1005 mDispatcher->notifyKey(&keyArgs);
1006
1007 // Window should receive key down event.
1008 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1009
1010 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
1011 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001012 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001013 mDispatcher->notifyDeviceReset(&args);
1014 window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
1015 AKEY_EVENT_FLAG_CANCELED);
1016}
1017
1018TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
1019 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1020 sp<FakeWindowHandle> window =
1021 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1022
Arthur Hung72d8dc32020-03-28 00:48:39 +00001023 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001024
1025 NotifyMotionArgs motionArgs =
1026 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1027 ADISPLAY_ID_DEFAULT);
1028 mDispatcher->notifyMotion(&motionArgs);
1029
1030 // Window should receive motion down event.
1031 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1032
1033 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
1034 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001035 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001036 mDispatcher->notifyDeviceReset(&args);
1037 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT,
1038 0 /*expectedFlags*/);
1039}
1040
Svet Ganov5d3bc372020-01-26 23:11:07 -08001041TEST_F(InputDispatcherTest, TransferTouchFocus_OnePointer) {
1042 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1043
1044 // Create a couple of windows
1045 sp<FakeWindowHandle> firstWindow = new FakeWindowHandle(application, mDispatcher,
1046 "First Window", ADISPLAY_ID_DEFAULT);
1047 sp<FakeWindowHandle> secondWindow = new FakeWindowHandle(application, mDispatcher,
1048 "Second Window", ADISPLAY_ID_DEFAULT);
1049
1050 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001051 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001052
1053 // Send down to the first window
1054 NotifyMotionArgs downMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
1055 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT);
1056 mDispatcher->notifyMotion(&downMotionArgs);
1057 // Only the first window should get the down event
1058 firstWindow->consumeMotionDown();
1059 secondWindow->assertNoEvents();
1060
1061 // Transfer touch focus to the second window
1062 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1063 // The first window gets cancel and the second gets down
1064 firstWindow->consumeMotionCancel();
1065 secondWindow->consumeMotionDown();
1066
1067 // Send up event to the second window
1068 NotifyMotionArgs upMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP,
1069 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT);
1070 mDispatcher->notifyMotion(&upMotionArgs);
1071 // The first window gets no events and the second gets up
1072 firstWindow->assertNoEvents();
1073 secondWindow->consumeMotionUp();
1074}
1075
1076TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointerNoSplitTouch) {
1077 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1078
1079 PointF touchPoint = {10, 10};
1080
1081 // Create a couple of windows
1082 sp<FakeWindowHandle> firstWindow = new FakeWindowHandle(application, mDispatcher,
1083 "First Window", ADISPLAY_ID_DEFAULT);
1084 sp<FakeWindowHandle> secondWindow = new FakeWindowHandle(application, mDispatcher,
1085 "Second Window", ADISPLAY_ID_DEFAULT);
1086
1087 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001088 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001089
1090 // Send down to the first window
1091 NotifyMotionArgs downMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
1092 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {touchPoint});
1093 mDispatcher->notifyMotion(&downMotionArgs);
1094 // Only the first window should get the down event
1095 firstWindow->consumeMotionDown();
1096 secondWindow->assertNoEvents();
1097
1098 // Send pointer down to the first window
1099 NotifyMotionArgs pointerDownMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN
1100 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1101 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint});
1102 mDispatcher->notifyMotion(&pointerDownMotionArgs);
1103 // Only the first window should get the pointer down event
1104 firstWindow->consumeMotionPointerDown(1);
1105 secondWindow->assertNoEvents();
1106
1107 // Transfer touch focus to the second window
1108 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1109 // The first window gets cancel and the second gets down and pointer down
1110 firstWindow->consumeMotionCancel();
1111 secondWindow->consumeMotionDown();
1112 secondWindow->consumeMotionPointerDown(1);
1113
1114 // Send pointer up to the second window
1115 NotifyMotionArgs pointerUpMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP
1116 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1117 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint});
1118 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1119 // The first window gets nothing and the second gets pointer up
1120 firstWindow->assertNoEvents();
1121 secondWindow->consumeMotionPointerUp(1);
1122
1123 // Send up event to the second window
1124 NotifyMotionArgs upMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP,
1125 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT);
1126 mDispatcher->notifyMotion(&upMotionArgs);
1127 // The first window gets nothing and the second gets up
1128 firstWindow->assertNoEvents();
1129 secondWindow->consumeMotionUp();
1130}
1131
1132TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
1133 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1134
1135 // Create a non touch modal window that supports split touch
1136 sp<FakeWindowHandle> firstWindow = new FakeWindowHandle(application, mDispatcher,
1137 "First Window", ADISPLAY_ID_DEFAULT);
1138 firstWindow->setFrame(Rect(0, 0, 600, 400));
1139 firstWindow->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL
1140 | InputWindowInfo::FLAG_SPLIT_TOUCH);
1141
1142 // Create a non touch modal window that supports split touch
1143 sp<FakeWindowHandle> secondWindow = new FakeWindowHandle(application, mDispatcher,
1144 "Second Window", ADISPLAY_ID_DEFAULT);
1145 secondWindow->setFrame(Rect(0, 400, 600, 800));
1146 secondWindow->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL
1147 | InputWindowInfo::FLAG_SPLIT_TOUCH);
1148
1149 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001150 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001151
1152 PointF pointInFirst = {300, 200};
1153 PointF pointInSecond = {300, 600};
1154
1155 // Send down to the first window
1156 NotifyMotionArgs firstDownMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
1157 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {pointInFirst});
1158 mDispatcher->notifyMotion(&firstDownMotionArgs);
1159 // Only the first window should get the down event
1160 firstWindow->consumeMotionDown();
1161 secondWindow->assertNoEvents();
1162
1163 // Send down to the second window
1164 NotifyMotionArgs secondDownMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN
1165 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1166 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {pointInFirst, pointInSecond});
1167 mDispatcher->notifyMotion(&secondDownMotionArgs);
1168 // The first window gets a move and the second a down
1169 firstWindow->consumeMotionMove();
1170 secondWindow->consumeMotionDown();
1171
1172 // Transfer touch focus to the second window
1173 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1174 // The first window gets cancel and the new gets pointer down (it already saw down)
1175 firstWindow->consumeMotionCancel();
1176 secondWindow->consumeMotionPointerDown(1);
1177
1178 // Send pointer up to the second window
1179 NotifyMotionArgs pointerUpMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP
1180 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1181 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {pointInFirst, pointInSecond});
1182 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1183 // The first window gets nothing and the second gets pointer up
1184 firstWindow->assertNoEvents();
1185 secondWindow->consumeMotionPointerUp(1);
1186
1187 // Send up event to the second window
1188 NotifyMotionArgs upMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP,
1189 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT);
1190 mDispatcher->notifyMotion(&upMotionArgs);
1191 // The first window gets nothing and the second gets up
1192 firstWindow->assertNoEvents();
1193 secondWindow->consumeMotionUp();
1194}
1195
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001196TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
1197 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1198 sp<FakeWindowHandle> window =
1199 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1200
1201 window->setFocus(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001202 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001203
1204 window->consumeFocusEvent(true);
1205
1206 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1207 mDispatcher->notifyKey(&keyArgs);
1208
1209 // Window should receive key down event.
1210 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1211}
1212
1213TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
1214 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1215 sp<FakeWindowHandle> window =
1216 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1217
Arthur Hung72d8dc32020-03-28 00:48:39 +00001218 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001219
1220 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1221 mDispatcher->notifyKey(&keyArgs);
1222 mDispatcher->waitForIdle();
1223
1224 window->assertNoEvents();
1225}
1226
1227// If a window is touchable, but does not have focus, it should receive motion events, but not keys
1228TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
1229 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1230 sp<FakeWindowHandle> window =
1231 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1232
Arthur Hung72d8dc32020-03-28 00:48:39 +00001233 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001234
1235 // Send key
1236 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1237 mDispatcher->notifyKey(&keyArgs);
1238 // Send motion
1239 NotifyMotionArgs motionArgs =
1240 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1241 ADISPLAY_ID_DEFAULT);
1242 mDispatcher->notifyMotion(&motionArgs);
1243
1244 // Window should receive only the motion event
1245 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1246 window->assertNoEvents(); // Key event or focus event will not be received
1247}
1248
chaviwd1c23182019-12-20 18:44:56 -08001249class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00001250public:
1251 FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name,
chaviwd1c23182019-12-20 18:44:56 -08001252 int32_t displayId, bool isGestureMonitor = false) {
1253 sp<InputChannel> serverChannel, clientChannel;
1254 InputChannel::openInputChannelPair(name, serverChannel, clientChannel);
1255 mInputReceiver = std::make_unique<FakeInputReceiver>(clientChannel, name);
1256 dispatcher->registerInputMonitor(serverChannel, displayId, isGestureMonitor);
Michael Wright3a240c42019-12-10 20:53:41 +00001257 }
1258
chaviwd1c23182019-12-20 18:44:56 -08001259 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
1260
1261 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1262 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN,
1263 expectedDisplayId, expectedFlags);
1264 }
1265
1266 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1267 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
1268 expectedDisplayId, expectedFlags);
1269 }
1270
1271 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1272 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP,
1273 expectedDisplayId, expectedFlags);
1274 }
1275
1276 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
1277
1278private:
1279 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00001280};
1281
1282// Tests for gesture monitors
1283TEST_F(InputDispatcherTest, GestureMonitor_ReceivesMotionEvents) {
1284 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1285 sp<FakeWindowHandle> window =
1286 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001287 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00001288
chaviwd1c23182019-12-20 18:44:56 -08001289 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
1290 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00001291
1292 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1293 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1294 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1295 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001296 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00001297}
1298
1299TEST_F(InputDispatcherTest, GestureMonitor_DoesNotReceiveKeyEvents) {
1300 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1301 sp<FakeWindowHandle> window =
1302 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1303
1304 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001305 window->setFocus(true);
Michael Wright3a240c42019-12-10 20:53:41 +00001306
Arthur Hung72d8dc32020-03-28 00:48:39 +00001307 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001308 window->consumeFocusEvent(true);
Michael Wright3a240c42019-12-10 20:53:41 +00001309
chaviwd1c23182019-12-20 18:44:56 -08001310 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
1311 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00001312
1313 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
1314 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1315 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001316 monitor.assertNoEvents();
Michael Wright3a240c42019-12-10 20:53:41 +00001317}
1318
1319TEST_F(InputDispatcherTest, GestureMonitor_CanPilferAfterWindowIsRemovedMidStream) {
1320 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1321 sp<FakeWindowHandle> window =
1322 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001323 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00001324
chaviwd1c23182019-12-20 18:44:56 -08001325 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
1326 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00001327
1328 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1329 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1330 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1331 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001332 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00001333
1334 window->releaseChannel();
1335
chaviwd1c23182019-12-20 18:44:56 -08001336 mDispatcher->pilferPointers(monitor.getToken());
Michael Wright3a240c42019-12-10 20:53:41 +00001337
1338 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1339 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1340 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08001341 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00001342}
1343
chaviw81e2bb92019-12-18 15:03:51 -08001344TEST_F(InputDispatcherTest, TestMoveEvent) {
1345 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1346 sp<FakeWindowHandle> window =
1347 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1348
Arthur Hung72d8dc32020-03-28 00:48:39 +00001349 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08001350
1351 NotifyMotionArgs motionArgs =
1352 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1353 ADISPLAY_ID_DEFAULT);
1354
1355 mDispatcher->notifyMotion(&motionArgs);
1356 // Window should receive motion down event.
1357 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1358
1359 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001360 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08001361 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1362 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
1363 motionArgs.pointerCoords[0].getX() - 10);
1364
1365 mDispatcher->notifyMotion(&motionArgs);
1366 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
1367 0 /*expectedFlags*/);
1368}
1369
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001370/**
1371 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
1372 * the device default right away. In the test scenario, we check both the default value,
1373 * and the action of enabling / disabling.
1374 */
1375TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
1376 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1377 sp<FakeWindowHandle> window =
1378 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
1379
1380 // Set focused application.
1381 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1382 window->setFocus(true);
1383
1384 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00001385 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001386 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
1387
1388 SCOPED_TRACE("Remove the window to trigger focus loss");
1389 window->setFocus(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001390 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001391 window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/);
1392
1393 SCOPED_TRACE("Disable touch mode");
1394 mDispatcher->setInTouchMode(false);
1395 window->setFocus(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001396 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001397 window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/);
1398
1399 SCOPED_TRACE("Remove the window to trigger focus loss");
1400 window->setFocus(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001401 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001402 window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/);
1403
1404 SCOPED_TRACE("Enable touch mode again");
1405 mDispatcher->setInTouchMode(true);
1406 window->setFocus(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001407 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001408 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
1409
1410 window->assertNoEvents();
1411}
1412
Gang Wange9087892020-01-07 12:17:14 -05001413TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
1414 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1415 sp<FakeWindowHandle> window =
1416 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
1417
1418 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1419 window->setFocus(true);
1420
Arthur Hung72d8dc32020-03-28 00:48:39 +00001421 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Gang Wange9087892020-01-07 12:17:14 -05001422 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
1423
1424 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
1425 mDispatcher->notifyKey(&keyArgs);
1426
1427 InputEvent* event = window->consume();
1428 ASSERT_NE(event, nullptr);
1429
1430 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
1431 ASSERT_NE(verified, nullptr);
1432 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
1433
1434 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
1435 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
1436 ASSERT_EQ(keyArgs.source, verified->source);
1437 ASSERT_EQ(keyArgs.displayId, verified->displayId);
1438
1439 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
1440
1441 ASSERT_EQ(keyArgs.action, verifiedKey.action);
1442 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05001443 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
1444 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
1445 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
1446 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
1447 ASSERT_EQ(0, verifiedKey.repeatCount);
1448}
1449
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08001450TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
1451 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1452 sp<FakeWindowHandle> window =
1453 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
1454
1455 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1456
Arthur Hung72d8dc32020-03-28 00:48:39 +00001457 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08001458
1459 NotifyMotionArgs motionArgs =
1460 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1461 ADISPLAY_ID_DEFAULT);
1462 mDispatcher->notifyMotion(&motionArgs);
1463
1464 InputEvent* event = window->consume();
1465 ASSERT_NE(event, nullptr);
1466
1467 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
1468 ASSERT_NE(verified, nullptr);
1469 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
1470
1471 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
1472 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
1473 EXPECT_EQ(motionArgs.source, verified->source);
1474 EXPECT_EQ(motionArgs.displayId, verified->displayId);
1475
1476 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
1477
1478 EXPECT_EQ(motionArgs.pointerCoords[0].getX(), verifiedMotion.rawX);
1479 EXPECT_EQ(motionArgs.pointerCoords[0].getY(), verifiedMotion.rawY);
1480 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
1481 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
1482 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
1483 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
1484 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
1485}
1486
Garfield Tan1c7bc862020-01-28 13:24:04 -08001487class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
1488protected:
1489 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
1490 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
1491
1492 sp<FakeApplicationHandle> mApp;
1493 sp<FakeWindowHandle> mWindow;
1494
1495 virtual void SetUp() override {
1496 mFakePolicy = new FakeInputDispatcherPolicy();
1497 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
1498 mDispatcher = new InputDispatcher(mFakePolicy);
1499 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
1500 ASSERT_EQ(OK, mDispatcher->start());
1501
1502 setUpWindow();
1503 }
1504
1505 void setUpWindow() {
1506 mApp = new FakeApplicationHandle();
1507 mWindow = new FakeWindowHandle(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1508
1509 mWindow->setFocus(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001510 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Garfield Tan1c7bc862020-01-28 13:24:04 -08001511
1512 mWindow->consumeFocusEvent(true);
1513 }
1514
1515 void sendAndConsumeKeyDown() {
1516 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1517 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
1518 mDispatcher->notifyKey(&keyArgs);
1519
1520 // Window should receive key down event.
1521 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1522 }
1523
1524 void expectKeyRepeatOnce(int32_t repeatCount) {
1525 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
1526 InputEvent* repeatEvent = mWindow->consume();
1527 ASSERT_NE(nullptr, repeatEvent);
1528
1529 uint32_t eventType = repeatEvent->getType();
1530 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType);
1531
1532 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
1533 uint32_t eventAction = repeatKeyEvent->getAction();
1534 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
1535 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
1536 }
1537
1538 void sendAndConsumeKeyUp() {
1539 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
1540 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
1541 mDispatcher->notifyKey(&keyArgs);
1542
1543 // Window should receive key down event.
1544 mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
1545 0 /*expectedFlags*/);
1546 }
1547};
1548
1549TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
1550 sendAndConsumeKeyDown();
1551 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
1552 expectKeyRepeatOnce(repeatCount);
1553 }
1554}
1555
1556TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
1557 sendAndConsumeKeyDown();
1558 expectKeyRepeatOnce(1 /*repeatCount*/);
1559 sendAndConsumeKeyUp();
1560 mWindow->assertNoEvents();
1561}
1562
1563TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
1564 sendAndConsumeKeyDown();
1565 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
1566 InputEvent* repeatEvent = mWindow->consume();
1567 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
1568 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
1569 IdGenerator::getSource(repeatEvent->getId()));
1570 }
1571}
1572
1573TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
1574 sendAndConsumeKeyDown();
1575
1576 std::unordered_set<int32_t> idSet;
1577 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
1578 InputEvent* repeatEvent = mWindow->consume();
1579 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
1580 int32_t id = repeatEvent->getId();
1581 EXPECT_EQ(idSet.end(), idSet.find(id));
1582 idSet.insert(id);
1583 }
1584}
1585
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001586/* Test InputDispatcher for MultiDisplay */
1587class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
1588public:
1589 static constexpr int32_t SECOND_DISPLAY_ID = 1;
Prabir Pradhan3608aad2019-10-02 17:08:26 -07001590 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001591 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08001592
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001593 application1 = new FakeApplicationHandle();
1594 windowInPrimary = new FakeWindowHandle(application1, mDispatcher, "D_1",
1595 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08001596
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001597 // Set focus window for primary display, but focused display would be second one.
1598 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001599 windowInPrimary->setFocus(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001600 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001601 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08001602
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001603 application2 = new FakeApplicationHandle();
1604 windowInSecondary = new FakeWindowHandle(application2, mDispatcher, "D_2",
1605 SECOND_DISPLAY_ID);
1606 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001607 // Set focus display to second one.
1608 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
1609 // Set focus window for second display.
1610 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001611 windowInSecondary->setFocus(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001612 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001613 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001614 }
1615
Prabir Pradhan3608aad2019-10-02 17:08:26 -07001616 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001617 InputDispatcherTest::TearDown();
1618
1619 application1.clear();
1620 windowInPrimary.clear();
1621 application2.clear();
1622 windowInSecondary.clear();
1623 }
1624
1625protected:
1626 sp<FakeApplicationHandle> application1;
1627 sp<FakeWindowHandle> windowInPrimary;
1628 sp<FakeApplicationHandle> application2;
1629 sp<FakeWindowHandle> windowInSecondary;
1630};
1631
1632TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
1633 // Test touch down on primary display.
1634 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1635 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +08001636 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001637 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001638 windowInSecondary->assertNoEvents();
1639
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001640 // Test touch down on second display.
1641 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1642 AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Arthur Hungb92218b2018-08-14 12:00:21 +08001643 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1644 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001645 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08001646}
1647
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001648TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08001649 // Test inject a key down with display id specified.
1650 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
1651 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001652 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08001653 windowInSecondary->assertNoEvents();
1654
1655 // Test inject a key down without display id specified.
Arthur Hungb92218b2018-08-14 12:00:21 +08001656 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
1657 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1658 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001659 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08001660
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08001661 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00001662 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08001663
1664 // Expect old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001665 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
1666 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08001667
1668 // Test inject a key down, should timeout because of no target window.
1669 ASSERT_EQ(INPUT_EVENT_INJECTION_TIMED_OUT, injectKeyDown(mDispatcher))
1670 << "Inject key event should return INPUT_EVENT_INJECTION_TIMED_OUT";
1671 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001672 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08001673 windowInSecondary->assertNoEvents();
1674}
1675
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001676// Test per-display input monitors for motion event.
1677TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08001678 FakeMonitorReceiver monitorInPrimary =
1679 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
1680 FakeMonitorReceiver monitorInSecondary =
1681 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001682
1683 // Test touch down on primary display.
1684 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1685 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1686 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001687 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001688 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001689 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08001690 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001691
1692 // Test touch down on second display.
1693 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1694 AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
1695 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1696 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08001697 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001698 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08001699 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001700
1701 // Test inject a non-pointer motion event.
1702 // If specific a display, it will dispatch to the focused window of particular display,
1703 // or it will dispatch to the focused window of focused display.
1704 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1705 AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
1706 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1707 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08001708 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001709 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08001710 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001711}
1712
1713// Test per-display input monitors for key event.
1714TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
1715 //Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08001716 FakeMonitorReceiver monitorInPrimary =
1717 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
1718 FakeMonitorReceiver monitorInSecondary =
1719 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001720
1721 // Test inject a key down.
1722 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
1723 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1724 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08001725 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001726 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08001727 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001728}
1729
Jackal Guof9696682018-10-05 12:23:23 +08001730class InputFilterTest : public InputDispatcherTest {
1731protected:
1732 static constexpr int32_t SECOND_DISPLAY_ID = 1;
1733
1734 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) {
1735 NotifyMotionArgs motionArgs;
1736
1737 motionArgs = generateMotionArgs(
1738 AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
1739 mDispatcher->notifyMotion(&motionArgs);
1740 motionArgs = generateMotionArgs(
1741 AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
1742 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001743 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08001744 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08001745 mFakePolicy->assertFilterInputEventWasCalled(motionArgs);
Jackal Guof9696682018-10-05 12:23:23 +08001746 } else {
1747 mFakePolicy->assertFilterInputEventWasNotCalled();
1748 }
1749 }
1750
1751 void testNotifyKey(bool expectToBeFiltered) {
1752 NotifyKeyArgs keyArgs;
1753
1754 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
1755 mDispatcher->notifyKey(&keyArgs);
1756 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
1757 mDispatcher->notifyKey(&keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001758 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08001759
1760 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08001761 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08001762 } else {
1763 mFakePolicy->assertFilterInputEventWasNotCalled();
1764 }
1765 }
1766};
1767
1768// Test InputFilter for MotionEvent
1769TEST_F(InputFilterTest, MotionEvent_InputFilter) {
1770 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
1771 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
1772 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
1773
1774 // Enable InputFilter
1775 mDispatcher->setInputFilterEnabled(true);
1776 // Test touch on both primary and second display, and check if both events are filtered.
1777 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
1778 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
1779
1780 // Disable InputFilter
1781 mDispatcher->setInputFilterEnabled(false);
1782 // Test touch on both primary and second display, and check if both events aren't filtered.
1783 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
1784 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
1785}
1786
1787// Test InputFilter for KeyEvent
1788TEST_F(InputFilterTest, KeyEvent_InputFilter) {
1789 // Since the InputFilter is disabled by default, check if key event aren't filtered.
1790 testNotifyKey(/*expectToBeFiltered*/ false);
1791
1792 // Enable InputFilter
1793 mDispatcher->setInputFilterEnabled(true);
1794 // Send a key event, and check if it is filtered.
1795 testNotifyKey(/*expectToBeFiltered*/ true);
1796
1797 // Disable InputFilter
1798 mDispatcher->setInputFilterEnabled(false);
1799 // Send a key event, and check if it isn't filtered.
1800 testNotifyKey(/*expectToBeFiltered*/ false);
1801}
1802
chaviwfd6d3512019-03-25 13:23:49 -07001803class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07001804 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07001805 InputDispatcherTest::SetUp();
1806
1807 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1808 mUnfocusedWindow = new FakeWindowHandle(application, mDispatcher, "Top",
1809 ADISPLAY_ID_DEFAULT);
1810 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
1811 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
1812 // window.
1813 mUnfocusedWindow->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL);
1814
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08001815 mFocusedWindow =
1816 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
1817 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
1818 mFocusedWindow->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL);
1819 mFocusedWindowTouchPoint = 60;
chaviwfd6d3512019-03-25 13:23:49 -07001820
1821 // Set focused application.
1822 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001823 mFocusedWindow->setFocus(true);
chaviwfd6d3512019-03-25 13:23:49 -07001824
1825 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00001826 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001827 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07001828 }
1829
Prabir Pradhan3608aad2019-10-02 17:08:26 -07001830 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07001831 InputDispatcherTest::TearDown();
1832
1833 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08001834 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07001835 }
1836
1837protected:
1838 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08001839 sp<FakeWindowHandle> mFocusedWindow;
1840 int32_t mFocusedWindowTouchPoint;
chaviwfd6d3512019-03-25 13:23:49 -07001841};
1842
1843// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
1844// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
1845// the onPointerDownOutsideFocus callback.
1846TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
1847 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1848 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, 20, 20))
1849 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07001850 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07001851
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001852 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07001853 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
1854}
1855
1856// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
1857// DOWN on the window that doesn't have focus. Ensure no window received the
1858// onPointerDownOutsideFocus callback.
1859TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
1860 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1861 AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, 20, 20))
1862 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07001863 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07001864
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001865 ASSERT_TRUE(mDispatcher->waitForIdle());
1866 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07001867}
1868
1869// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
1870// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
1871TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
1872 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
1873 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07001874 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07001875
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001876 ASSERT_TRUE(mDispatcher->waitForIdle());
1877 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07001878}
1879
1880// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
1881// DOWN on the window that already has focus. Ensure no window received the
1882// onPointerDownOutsideFocus callback.
1883TEST_F(InputDispatcherOnPointerDownOutsideFocus,
1884 OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08001885 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1886 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1887 mFocusedWindowTouchPoint, mFocusedWindowTouchPoint))
chaviwfd6d3512019-03-25 13:23:49 -07001888 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07001889 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07001890
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001891 ASSERT_TRUE(mDispatcher->waitForIdle());
1892 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07001893}
1894
chaviwaf87b3e2019-10-01 16:59:28 -07001895// These tests ensures we can send touch events to a single client when there are multiple input
1896// windows that point to the same client token.
1897class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
1898 virtual void SetUp() override {
1899 InputDispatcherTest::SetUp();
1900
1901 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
1902 mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1",
1903 ADISPLAY_ID_DEFAULT);
1904 // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window.
1905 // We also need FLAG_SPLIT_TOUCH or we won't be able to get touches for both windows.
1906 mWindow1->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL |
1907 InputWindowInfo::FLAG_SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07001908 mWindow1->setFrame(Rect(0, 0, 100, 100));
1909
1910 mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2",
1911 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
1912 mWindow2->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL |
1913 InputWindowInfo::FLAG_SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07001914 mWindow2->setFrame(Rect(100, 100, 200, 200));
1915
Arthur Hung72d8dc32020-03-28 00:48:39 +00001916 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07001917 }
1918
1919protected:
1920 sp<FakeWindowHandle> mWindow1;
1921 sp<FakeWindowHandle> mWindow2;
1922
1923 // Helper function to convert the point from screen coordinates into the window's space
1924 static PointF getPointInWindow(const InputWindowInfo* windowInfo, const PointF& point) {
1925 float x = windowInfo->windowXScale * (point.x - windowInfo->frameLeft);
1926 float y = windowInfo->windowYScale * (point.y - windowInfo->frameTop);
1927 return {x, y};
1928 }
1929
1930 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
1931 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001932 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07001933 InputEvent* event = window->consume();
1934
1935 ASSERT_NE(nullptr, event) << name.c_str()
1936 << ": consumer should have returned non-NULL event.";
1937
1938 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
1939 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
1940 << " event, got " << inputEventTypeToString(event->getType()) << " event";
1941
1942 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
1943 EXPECT_EQ(expectedAction, motionEvent.getAction());
1944
1945 for (size_t i = 0; i < points.size(); i++) {
1946 float expectedX = points[i].x;
1947 float expectedY = points[i].y;
1948
1949 EXPECT_EQ(expectedX, motionEvent.getX(i))
1950 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
1951 << ", got " << motionEvent.getX(i);
1952 EXPECT_EQ(expectedY, motionEvent.getY(i))
1953 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
1954 << ", got " << motionEvent.getY(i);
1955 }
1956 }
1957};
1958
1959TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
1960 // Touch Window 1
1961 PointF touchedPoint = {10, 10};
1962 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
1963
1964 NotifyMotionArgs motionArgs =
1965 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1966 ADISPLAY_ID_DEFAULT, {touchedPoint});
1967 mDispatcher->notifyMotion(&motionArgs);
1968 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, {expectedPoint});
1969
1970 // Release touch on Window 1
1971 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
1972 ADISPLAY_ID_DEFAULT, {touchedPoint});
1973 mDispatcher->notifyMotion(&motionArgs);
1974 // consume the UP event
1975 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_UP, {expectedPoint});
1976
1977 // Touch Window 2
1978 touchedPoint = {150, 150};
1979 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
1980
1981 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1982 ADISPLAY_ID_DEFAULT, {touchedPoint});
1983 mDispatcher->notifyMotion(&motionArgs);
1984
1985 // Consuming from window1 since it's the window that has the InputReceiver
1986 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, {expectedPoint});
1987}
1988
1989TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentScale) {
1990 mWindow2->setWindowScale(0.5f, 0.5f);
1991
1992 // Touch Window 1
1993 PointF touchedPoint = {10, 10};
1994 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
1995
1996 NotifyMotionArgs motionArgs =
1997 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1998 ADISPLAY_ID_DEFAULT, {touchedPoint});
1999 mDispatcher->notifyMotion(&motionArgs);
2000 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, {expectedPoint});
2001
2002 // Release touch on Window 1
2003 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2004 ADISPLAY_ID_DEFAULT, {touchedPoint});
2005 mDispatcher->notifyMotion(&motionArgs);
2006 // consume the UP event
2007 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_UP, {expectedPoint});
2008
2009 // Touch Window 2
2010 touchedPoint = {150, 150};
2011 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
2012
2013 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2014 ADISPLAY_ID_DEFAULT, {touchedPoint});
2015 mDispatcher->notifyMotion(&motionArgs);
2016
2017 // Consuming from window1 since it's the window that has the InputReceiver
2018 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, {expectedPoint});
2019}
2020
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002021TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentScale) {
2022 mWindow2->setWindowScale(0.5f, 0.5f);
2023
2024 // Touch Window 1
2025 std::vector<PointF> touchedPoints = {PointF{10, 10}};
2026 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
2027
2028 NotifyMotionArgs motionArgs =
2029 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2030 ADISPLAY_ID_DEFAULT, touchedPoints);
2031 mDispatcher->notifyMotion(&motionArgs);
2032 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, expectedPoints);
2033
2034 // Touch Window 2
2035 int32_t actionPointerDown =
2036 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
2037 touchedPoints.emplace_back(PointF{150, 150});
2038 expectedPoints.emplace_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
2039
2040 motionArgs = generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN,
2041 ADISPLAY_ID_DEFAULT, touchedPoints);
2042 mDispatcher->notifyMotion(&motionArgs);
2043
2044 // Consuming from window1 since it's the window that has the InputReceiver
2045 consumeMotionEvent(mWindow1, actionPointerDown, expectedPoints);
2046}
2047
2048TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentScale) {
2049 mWindow2->setWindowScale(0.5f, 0.5f);
2050
2051 // Touch Window 1
2052 std::vector<PointF> touchedPoints = {PointF{10, 10}};
2053 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
2054
2055 NotifyMotionArgs motionArgs =
2056 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2057 ADISPLAY_ID_DEFAULT, touchedPoints);
2058 mDispatcher->notifyMotion(&motionArgs);
2059 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, expectedPoints);
2060
2061 // Touch Window 2
2062 int32_t actionPointerDown =
2063 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
2064 touchedPoints.emplace_back(PointF{150, 150});
2065 expectedPoints.emplace_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
2066
2067 motionArgs = generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN,
2068 ADISPLAY_ID_DEFAULT, touchedPoints);
2069 mDispatcher->notifyMotion(&motionArgs);
2070
2071 // Consuming from window1 since it's the window that has the InputReceiver
2072 consumeMotionEvent(mWindow1, actionPointerDown, expectedPoints);
2073
2074 // Move both windows
2075 touchedPoints = {{20, 20}, {175, 175}};
2076 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
2077 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
2078
2079 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2080 ADISPLAY_ID_DEFAULT, touchedPoints);
2081 mDispatcher->notifyMotion(&motionArgs);
2082
2083 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_MOVE, expectedPoints);
2084}
2085
2086TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
2087 mWindow1->setWindowScale(0.5f, 0.5f);
2088
2089 // Touch Window 1
2090 std::vector<PointF> touchedPoints = {PointF{10, 10}};
2091 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
2092
2093 NotifyMotionArgs motionArgs =
2094 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2095 ADISPLAY_ID_DEFAULT, touchedPoints);
2096 mDispatcher->notifyMotion(&motionArgs);
2097 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, expectedPoints);
2098
2099 // Touch Window 2
2100 int32_t actionPointerDown =
2101 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
2102 touchedPoints.emplace_back(PointF{150, 150});
2103 expectedPoints.emplace_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
2104
2105 motionArgs = generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN,
2106 ADISPLAY_ID_DEFAULT, touchedPoints);
2107 mDispatcher->notifyMotion(&motionArgs);
2108
2109 // Consuming from window1 since it's the window that has the InputReceiver
2110 consumeMotionEvent(mWindow1, actionPointerDown, expectedPoints);
2111
2112 // Move both windows
2113 touchedPoints = {{20, 20}, {175, 175}};
2114 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
2115 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
2116
2117 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2118 ADISPLAY_ID_DEFAULT, touchedPoints);
2119 mDispatcher->notifyMotion(&motionArgs);
2120
2121 consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_MOVE, expectedPoints);
2122}
2123
Garfield Tane84e6f92019-08-29 17:28:41 -07002124} // namespace android::inputdispatcher