blob: 6ef0028660c6dccdbcdd2fa59ac7d8c2cd25510a [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>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070020#include <android-base/thread_annotations.h>
Robert Carr803535b2018-08-02 16:38:15 -070021#include <binder/Binder.h>
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080022#include <input/Input.h>
Robert Carr803535b2018-08-02 16:38:15 -070023
Michael Wrightd02c5b62014-02-10 15:10:22 -080024#include <gtest/gtest.h>
25#include <linux/input.h>
Garfield Tan1c7bc862020-01-28 13:24:04 -080026#include <cinttypes>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070027#include <thread>
Garfield Tan1c7bc862020-01-28 13:24:04 -080028#include <unordered_set>
chaviwd1c23182019-12-20 18:44:56 -080029#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080030
Garfield Tan1c7bc862020-01-28 13:24:04 -080031using android::base::StringPrintf;
Michael Wright44753b12020-07-08 13:48:11 +010032using namespace android::flag_operators;
Garfield Tan1c7bc862020-01-28 13:24:04 -080033
Garfield Tane84e6f92019-08-29 17:28:41 -070034namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080035
36// An arbitrary time value.
37static const nsecs_t ARBITRARY_TIME = 1234;
38
39// An arbitrary device id.
40static const int32_t DEVICE_ID = 1;
41
Jeff Brownf086ddb2014-02-11 14:28:48 -080042// An arbitrary display id.
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -080043static const int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
Jeff Brownf086ddb2014-02-11 14:28:48 -080044
Michael Wrightd02c5b62014-02-10 15:10:22 -080045// An arbitrary injector pid / uid pair that has permission to inject events.
46static const int32_t INJECTOR_PID = 999;
47static const int32_t INJECTOR_UID = 1001;
48
chaviwd1c23182019-12-20 18:44:56 -080049struct PointF {
50 float x;
51 float y;
52};
Michael Wrightd02c5b62014-02-10 15:10:22 -080053
Gang Wang342c9272020-01-13 13:15:04 -050054/**
55 * Return a DOWN key event with KEYCODE_A.
56 */
57static KeyEvent getTestKeyEvent() {
58 KeyEvent event;
59
Garfield Tanfbe732e2020-01-24 11:26:14 -080060 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
61 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
62 ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -050063 return event;
64}
65
Michael Wrightd02c5b62014-02-10 15:10:22 -080066// --- FakeInputDispatcherPolicy ---
67
68class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
69 InputDispatcherConfiguration mConfig;
70
71protected:
72 virtual ~FakeInputDispatcherPolicy() {
73 }
74
75public:
76 FakeInputDispatcherPolicy() {
Jackal Guof9696682018-10-05 12:23:23 +080077 }
78
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080079 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080080 assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_KEY, args.eventTime, args.action,
81 args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080082 }
83
Siarhei Vishniakou8935a802019-11-15 16:41:44 -080084 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args) {
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -080085 assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_MOTION, args.eventTime, args.action,
86 args.displayId);
Jackal Guof9696682018-10-05 12:23:23 +080087 }
88
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -070089 void assertFilterInputEventWasNotCalled() {
90 std::scoped_lock lock(mLock);
91 ASSERT_EQ(nullptr, mFilteredEvent);
92 }
Michael Wrightd02c5b62014-02-10 15:10:22 -080093
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -080094 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -070095 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -080096 ASSERT_TRUE(mConfigurationChangedTime)
97 << "Timed out waiting for configuration changed call";
98 ASSERT_EQ(*mConfigurationChangedTime, when);
99 mConfigurationChangedTime = std::nullopt;
100 }
101
102 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700103 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800104 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800105 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800106 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
107 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
108 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
109 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
110 mLastNotifySwitch = std::nullopt;
111 }
112
chaviwfd6d3512019-03-25 13:23:49 -0700113 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700114 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800115 ASSERT_EQ(touchedToken, mOnPointerDownToken);
116 mOnPointerDownToken.clear();
117 }
118
119 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700120 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800121 ASSERT_TRUE(mOnPointerDownToken == nullptr)
122 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700123 }
124
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700125 // This function must be called soon after the expected ANR timer starts,
126 // because we are also checking how much time has passed.
Chris Yea209fde2020-07-22 13:54:51 -0700127 void assertNotifyAnrWasCalled(
128 std::chrono::nanoseconds timeout,
129 const std::shared_ptr<InputApplicationHandle>& expectedApplication,
130 const sp<IBinder>& expectedToken) {
131 std::pair<std::shared_ptr<InputApplicationHandle>, sp<IBinder>> anrData;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700132 ASSERT_NO_FATAL_FAILURE(anrData = getNotifyAnrData(timeout));
133 ASSERT_EQ(expectedApplication, anrData.first);
134 ASSERT_EQ(expectedToken, anrData.second);
135 }
136
Chris Yea209fde2020-07-22 13:54:51 -0700137 std::pair<std::shared_ptr<InputApplicationHandle>, sp<IBinder>> getNotifyAnrData(
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700138 std::chrono::nanoseconds timeout) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700139 const std::chrono::time_point start = std::chrono::steady_clock::now();
140 std::unique_lock lock(mLock);
141 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
142 android::base::ScopedLockAssertion assumeLocked(mLock);
143
144 // If there is an ANR, Dispatcher won't be idle because there are still events
145 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
146 // before checking if ANR was called.
147 // Since dispatcher is not guaranteed to call notifyAnr right away, we need to provide
148 // it some time to act. 100ms seems reasonable.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700149 mNotifyAnr.wait_for(lock, timeToWait, [this]() REQUIRES(mLock) {
150 return !mAnrApplications.empty() && !mAnrWindowTokens.empty();
151 });
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700152 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700153 if (mAnrApplications.empty() || mAnrWindowTokens.empty()) {
154 ADD_FAILURE() << "Did not receive ANR callback";
155 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700156 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
157 // the dispatcher started counting before this function was called
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700158 if (std::chrono::abs(timeout - waited) > 100ms) {
159 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
160 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
161 << "ms, but waited "
162 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
163 << "ms instead";
164 }
Chris Yea209fde2020-07-22 13:54:51 -0700165 std::pair<std::shared_ptr<InputApplicationHandle>, sp<IBinder>> result =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700166 std::make_pair(mAnrApplications.front(), mAnrWindowTokens.front());
167 mAnrApplications.pop();
168 mAnrWindowTokens.pop();
169 return result;
170 }
171
172 void assertNotifyAnrWasNotCalled() {
173 std::scoped_lock lock(mLock);
174 ASSERT_TRUE(mAnrApplications.empty());
175 ASSERT_TRUE(mAnrWindowTokens.empty());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700176 }
177
Garfield Tan1c7bc862020-01-28 13:24:04 -0800178 void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) {
179 mConfig.keyRepeatTimeout = timeout;
180 mConfig.keyRepeatDelay = delay;
181 }
182
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700183 void setAnrTimeout(std::chrono::nanoseconds timeout) { mAnrTimeout = timeout; }
184
Michael Wrightd02c5b62014-02-10 15:10:22 -0800185private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700186 std::mutex mLock;
187 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
188 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
189 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
190 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800191
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700192 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700193 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700194 std::queue<sp<IBinder>> mAnrWindowTokens GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700195 std::condition_variable mNotifyAnr;
196 std::chrono::nanoseconds mAnrTimeout = 0ms;
197
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800198 virtual void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700199 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800200 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800201 }
202
Chris Yea209fde2020-07-22 13:54:51 -0700203 std::chrono::nanoseconds notifyAnr(const std::shared_ptr<InputApplicationHandle>& application,
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500204 const sp<IBinder>& windowToken,
205 const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700206 std::scoped_lock lock(mLock);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700207 mAnrApplications.push(application);
208 mAnrWindowTokens.push(windowToken);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700209 mNotifyAnr.notify_all();
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500210 return mAnrTimeout;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800211 }
212
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700213 virtual void notifyInputChannelBroken(const sp<IBinder>&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800214
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700215 virtual void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700216
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700217 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800218 *outConfig = mConfig;
219 }
220
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800221 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700222 std::scoped_lock lock(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800223 switch (inputEvent->getType()) {
224 case AINPUT_EVENT_TYPE_KEY: {
225 const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800226 mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800227 break;
228 }
229
230 case AINPUT_EVENT_TYPE_MOTION: {
231 const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800232 mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800233 break;
234 }
235 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800236 return true;
237 }
238
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700239 virtual void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800240
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700241 virtual void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800242
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700243 virtual nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*,
244 uint32_t) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800245 return 0;
246 }
247
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700248 virtual bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t,
249 KeyEvent*) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800250 return false;
251 }
252
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800253 virtual void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
254 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700255 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800256 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
257 * essentially a passthrough for notifySwitch.
258 */
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800259 mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800260 }
261
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700262 virtual void pokeUserActivity(nsecs_t, int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800263
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700264 virtual bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800265 return false;
266 }
Jackal Guof9696682018-10-05 12:23:23 +0800267
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700268 virtual void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700269 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700270 mOnPointerDownToken = newToken;
271 }
272
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800273 void assertFilterInputEventWasCalled(int type, nsecs_t eventTime, int32_t action,
274 int32_t displayId) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700275 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800276 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
277 ASSERT_EQ(mFilteredEvent->getType(), type);
278
279 if (type == AINPUT_EVENT_TYPE_KEY) {
280 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*mFilteredEvent);
281 EXPECT_EQ(keyEvent.getEventTime(), eventTime);
282 EXPECT_EQ(keyEvent.getAction(), action);
283 EXPECT_EQ(keyEvent.getDisplayId(), displayId);
284 } else if (type == AINPUT_EVENT_TYPE_MOTION) {
285 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*mFilteredEvent);
286 EXPECT_EQ(motionEvent.getEventTime(), eventTime);
287 EXPECT_EQ(motionEvent.getAction(), action);
288 EXPECT_EQ(motionEvent.getDisplayId(), displayId);
289 } else {
290 FAIL() << "Unknown type: " << type;
291 }
292
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800293 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800294 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800295};
296
Michael Wrightd02c5b62014-02-10 15:10:22 -0800297// --- InputDispatcherTest ---
298
299class InputDispatcherTest : public testing::Test {
300protected:
301 sp<FakeInputDispatcherPolicy> mFakePolicy;
302 sp<InputDispatcher> mDispatcher;
303
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700304 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800305 mFakePolicy = new FakeInputDispatcherPolicy();
306 mDispatcher = new InputDispatcher(mFakePolicy);
Arthur Hungb92218b2018-08-14 12:00:21 +0800307 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
308 //Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700309 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800310 }
311
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700312 virtual void TearDown() override {
313 ASSERT_EQ(OK, mDispatcher->stop());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800314 mFakePolicy.clear();
315 mDispatcher.clear();
316 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700317
318 /**
319 * Used for debugging when writing the test
320 */
321 void dumpDispatcherState() {
322 std::string dump;
323 mDispatcher->dump(dump);
324 std::stringstream ss(dump);
325 std::string to;
326
327 while (std::getline(ss, to, '\n')) {
328 ALOGE("%s", to.c_str());
329 }
330 }
Vishnu Nair958da932020-08-21 17:12:37 -0700331
332 void setFocusedWindow(const sp<InputWindowHandle>& window,
333 const sp<InputWindowHandle>& focusedWindow = nullptr) {
334 FocusRequest request;
335 request.token = window->getToken();
336 if (focusedWindow) {
337 request.focusedToken = focusedWindow->getToken();
338 }
339 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
340 request.displayId = window->getInfo()->displayId;
341 mDispatcher->setFocusedWindow(request);
342 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800343};
344
345
346TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
347 KeyEvent event;
348
349 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800350 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
351 INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600352 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
353 ARBITRARY_TIME);
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700354 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED,
355 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
356 INPUT_EVENT_INJECTION_SYNC_NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800357 << "Should reject key events with undefined action.";
358
359 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800360 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
361 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600362 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700363 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED,
364 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
365 INPUT_EVENT_INJECTION_SYNC_NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800366 << "Should reject key events with ACTION_MULTIPLE.";
367}
368
369TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
370 MotionEvent event;
371 PointerProperties pointerProperties[MAX_POINTERS + 1];
372 PointerCoords pointerCoords[MAX_POINTERS + 1];
373 for (int i = 0; i <= MAX_POINTERS; i++) {
374 pointerProperties[i].clear();
375 pointerProperties[i].id = i;
376 pointerCoords[i].clear();
377 }
378
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800379 // Some constants commonly used below
380 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
381 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
382 constexpr int32_t metaState = AMETA_NONE;
383 constexpr MotionClassification classification = MotionClassification::NONE;
384
chaviw9eaa22c2020-07-01 16:21:27 -0700385 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800386 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800387 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
chaviw9eaa22c2020-07-01 16:21:27 -0700388 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification,
389 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600390 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700391 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700392 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED,
393 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
394 INPUT_EVENT_INJECTION_SYNC_NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800395 << "Should reject motion events with undefined action.";
396
397 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800398 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700399 AMOTION_EVENT_ACTION_POINTER_DOWN |
400 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700401 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
402 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
403 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
404 pointerCoords);
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700405 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED,
406 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
407 INPUT_EVENT_INJECTION_SYNC_NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800408 << "Should reject motion events with pointer down index too large.";
409
Garfield Tanfbe732e2020-01-24 11:26:14 -0800410 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700411 AMOTION_EVENT_ACTION_POINTER_DOWN |
412 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700413 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
414 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
415 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
416 pointerCoords);
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700417 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED,
418 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
419 INPUT_EVENT_INJECTION_SYNC_NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800420 << "Should reject motion events with pointer down index too small.";
421
422 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800423 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700424 AMOTION_EVENT_ACTION_POINTER_UP |
425 (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700426 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
427 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
428 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
429 pointerCoords);
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700430 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED,
431 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
432 INPUT_EVENT_INJECTION_SYNC_NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800433 << "Should reject motion events with pointer up index too large.";
434
Garfield Tanfbe732e2020-01-24 11:26:14 -0800435 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700436 AMOTION_EVENT_ACTION_POINTER_UP |
437 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700438 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
439 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
440 ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties,
441 pointerCoords);
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700442 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED,
443 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
444 INPUT_EVENT_INJECTION_SYNC_NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800445 << "Should reject motion events with pointer up index too small.";
446
447 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800448 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
449 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700450 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
451 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700452 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700453 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED,
454 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
455 INPUT_EVENT_INJECTION_SYNC_NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800456 << "Should reject motion events with 0 pointers.";
457
Garfield Tanfbe732e2020-01-24 11:26:14 -0800458 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
459 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700460 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
461 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700462 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700463 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED,
464 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
465 INPUT_EVENT_INJECTION_SYNC_NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800466 << "Should reject motion events with more than MAX_POINTERS pointers.";
467
468 // Rejects motion events with invalid pointer ids.
469 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800470 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
471 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700472 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
473 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700474 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700475 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED,
476 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
477 INPUT_EVENT_INJECTION_SYNC_NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800478 << "Should reject motion events with pointer ids less than 0.";
479
480 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800481 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
482 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700483 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
484 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700485 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700486 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED,
487 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
488 INPUT_EVENT_INJECTION_SYNC_NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800489 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
490
491 // Rejects motion events with duplicate pointer ids.
492 pointerProperties[0].id = 1;
493 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800494 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
495 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700496 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
497 AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700498 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700499 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED,
500 mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
501 INPUT_EVENT_INJECTION_SYNC_NONE, 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800502 << "Should reject motion events with duplicate pointer ids.";
503}
504
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800505/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
506
507TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
508 constexpr nsecs_t eventTime = 20;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800509 NotifyConfigurationChangedArgs args(10 /*id*/, eventTime);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800510 mDispatcher->notifyConfigurationChanged(&args);
511 ASSERT_TRUE(mDispatcher->waitForIdle());
512
513 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
514}
515
516TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800517 NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/,
518 2 /*switchMask*/);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800519 mDispatcher->notifySwitch(&args);
520
521 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
522 args.policyFlags |= POLICY_FLAG_TRUSTED;
523 mFakePolicy->assertNotifySwitchWasCalled(args);
524}
525
Arthur Hungb92218b2018-08-14 12:00:21 +0800526// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700527static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700528static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s;
Arthur Hungb92218b2018-08-14 12:00:21 +0800529
530class FakeApplicationHandle : public InputApplicationHandle {
531public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700532 FakeApplicationHandle() {
533 mInfo.name = "Fake Application";
534 mInfo.token = new BBinder();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500535 mInfo.dispatchingTimeoutMillis =
536 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700537 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800538 virtual ~FakeApplicationHandle() {}
539
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700540 virtual bool updateInfo() override {
Arthur Hungb92218b2018-08-14 12:00:21 +0800541 return true;
542 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700543
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500544 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
545 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700546 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800547};
548
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800549class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800550public:
Garfield Tan62b523e2020-09-21 16:07:29 -0700551 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800552 : mName(name) {
Garfield Tan62b523e2020-09-21 16:07:29 -0700553 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800554 }
555
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800556 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700557 InputEvent* event;
558 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
559 if (!consumeSeq) {
560 return nullptr;
561 }
562 finishEvent(*consumeSeq);
563 return event;
564 }
565
566 /**
567 * Receive an event without acknowledging it.
568 * Return the sequence number that could later be used to send finished signal.
569 */
570 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800571 uint32_t consumeSeq;
572 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800573
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800574 std::chrono::time_point start = std::chrono::steady_clock::now();
575 status_t status = WOULD_BLOCK;
576 while (status == WOULD_BLOCK) {
chaviw81e2bb92019-12-18 15:03:51 -0800577 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800578 &event);
579 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
580 if (elapsed > 100ms) {
581 break;
582 }
583 }
584
585 if (status == WOULD_BLOCK) {
586 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700587 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800588 }
589
590 if (status != OK) {
591 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700592 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800593 }
594 if (event == nullptr) {
595 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700596 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800597 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700598 if (outEvent != nullptr) {
599 *outEvent = event;
600 }
601 return consumeSeq;
602 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800603
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700604 /**
605 * To be used together with "receiveEvent" to complete the consumption of an event.
606 */
607 void finishEvent(uint32_t consumeSeq) {
608 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
609 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800610 }
611
612 void consumeEvent(int32_t expectedEventType, int32_t expectedAction, int32_t expectedDisplayId,
613 int32_t expectedFlags) {
614 InputEvent* event = consume();
615
616 ASSERT_NE(nullptr, event) << mName.c_str()
617 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800618 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700619 << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType)
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800620 << " event, got " << inputEventTypeToString(event->getType()) << " event";
Arthur Hungb92218b2018-08-14 12:00:21 +0800621
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800622 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
Tiger Huang8664f8c2018-10-11 19:14:35 +0800623
Tiger Huang8664f8c2018-10-11 19:14:35 +0800624 switch (expectedEventType) {
625 case AINPUT_EVENT_TYPE_KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800626 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
627 EXPECT_EQ(expectedAction, keyEvent.getAction());
628 EXPECT_EQ(expectedFlags, keyEvent.getFlags());
Tiger Huang8664f8c2018-10-11 19:14:35 +0800629 break;
630 }
631 case AINPUT_EVENT_TYPE_MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800632 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
633 EXPECT_EQ(expectedAction, motionEvent.getAction());
634 EXPECT_EQ(expectedFlags, motionEvent.getFlags());
Tiger Huang8664f8c2018-10-11 19:14:35 +0800635 break;
636 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100637 case AINPUT_EVENT_TYPE_FOCUS: {
638 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
639 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800640 default: {
641 FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType;
642 }
643 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800644 }
645
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100646 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
647 InputEvent* event = consume();
648 ASSERT_NE(nullptr, event) << mName.c_str()
649 << ": consumer should have returned non-NULL event.";
650 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
651 << "Got " << inputEventTypeToString(event->getType())
652 << " event instead of FOCUS event";
653
654 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
655 << mName.c_str() << ": event displayId should always be NONE.";
656
657 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
658 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
659 EXPECT_EQ(inTouchMode, focusEvent->getInTouchMode());
660 }
661
chaviwd1c23182019-12-20 18:44:56 -0800662 void assertNoEvents() {
663 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700664 if (event == nullptr) {
665 return;
666 }
667 if (event->getType() == AINPUT_EVENT_TYPE_KEY) {
668 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
669 ADD_FAILURE() << "Received key event "
670 << KeyEvent::actionToString(keyEvent.getAction());
671 } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) {
672 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
673 ADD_FAILURE() << "Received motion event "
674 << MotionEvent::actionToString(motionEvent.getAction());
675 } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) {
676 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
677 ADD_FAILURE() << "Received focus event, hasFocus = "
678 << (focusEvent.getHasFocus() ? "true" : "false");
679 }
680 FAIL() << mName.c_str()
681 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -0800682 }
683
684 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
685
686protected:
687 std::unique_ptr<InputConsumer> mConsumer;
688 PreallocatedInputEventFactory mEventFactory;
689
690 std::string mName;
691};
692
693class FakeWindowHandle : public InputWindowHandle {
694public:
695 static const int32_t WIDTH = 600;
696 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -0800697
Chris Yea209fde2020-07-22 13:54:51 -0700698 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
chaviwd1c23182019-12-20 18:44:56 -0800699 const sp<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500700 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -0800701 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500702 if (token == std::nullopt) {
Garfield Tan62b523e2020-09-21 16:07:29 -0700703 base::Result<std::unique_ptr<InputChannel>> channel =
704 dispatcher->createInputChannel(name);
705 token = (*channel)->getConnectionToken();
706 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -0800707 }
708
709 inputApplicationHandle->updateInfo();
710 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
711
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500712 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700713 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -0800714 mInfo.name = name;
Michael Wright44753b12020-07-08 13:48:11 +0100715 mInfo.type = InputWindowInfo::Type::APPLICATION;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500716 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
chaviwd1c23182019-12-20 18:44:56 -0800717 mInfo.frameLeft = 0;
718 mInfo.frameTop = 0;
719 mInfo.frameRight = WIDTH;
720 mInfo.frameBottom = HEIGHT;
chaviw1ff3d1e2020-07-01 15:53:47 -0700721 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -0800722 mInfo.globalScaleFactor = 1.0;
723 mInfo.touchableRegion.clear();
724 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
725 mInfo.visible = true;
Vishnu Nair47074b82020-08-14 11:54:47 -0700726 mInfo.focusable = false;
chaviwd1c23182019-12-20 18:44:56 -0800727 mInfo.hasWallpaper = false;
728 mInfo.paused = false;
chaviwd1c23182019-12-20 18:44:56 -0800729 mInfo.ownerPid = INJECTOR_PID;
730 mInfo.ownerUid = INJECTOR_UID;
chaviwd1c23182019-12-20 18:44:56 -0800731 mInfo.displayId = displayId;
732 }
733
734 virtual bool updateInfo() { return true; }
735
Vishnu Nair47074b82020-08-14 11:54:47 -0700736 void setFocusable(bool focusable) { mInfo.focusable = focusable; }
chaviwd1c23182019-12-20 18:44:56 -0800737
Vishnu Nair958da932020-08-21 17:12:37 -0700738 void setVisible(bool visible) { mInfo.visible = visible; }
739
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700740 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500741 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700742 }
743
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700744 void setPaused(bool paused) { mInfo.paused = paused; }
745
chaviwd1c23182019-12-20 18:44:56 -0800746 void setFrame(const Rect& frame) {
747 mInfo.frameLeft = frame.left;
748 mInfo.frameTop = frame.top;
749 mInfo.frameRight = frame.right;
750 mInfo.frameBottom = frame.bottom;
chaviw1ff3d1e2020-07-01 15:53:47 -0700751 mInfo.transform.set(frame.left, frame.top);
chaviwd1c23182019-12-20 18:44:56 -0800752 mInfo.touchableRegion.clear();
753 mInfo.addTouchableRegion(frame);
754 }
755
Michael Wright44753b12020-07-08 13:48:11 +0100756 void setFlags(Flags<InputWindowInfo::Flag> flags) { mInfo.flags = flags; }
chaviwd1c23182019-12-20 18:44:56 -0800757
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500758 void setInputFeatures(InputWindowInfo::Feature features) { mInfo.inputFeatures = features; }
759
chaviw9eaa22c2020-07-01 16:21:27 -0700760 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
761 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
762 }
763
764 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -0700765
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800766 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
767 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
768 expectedFlags);
769 }
770
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700771 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
772 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
773 }
774
Svet Ganov5d3bc372020-01-26 23:11:07 -0800775 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
776 int32_t expectedFlags = 0) {
777 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId,
778 expectedFlags);
779 }
780
781 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
782 int32_t expectedFlags = 0) {
783 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId,
784 expectedFlags);
785 }
786
787 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
788 int32_t expectedFlags = 0) {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800789 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
790 expectedFlags);
791 }
792
Svet Ganov5d3bc372020-01-26 23:11:07 -0800793 void consumeMotionPointerDown(int32_t pointerIdx,
794 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, int32_t expectedFlags = 0) {
795 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN
796 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
797 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
798 }
799
800 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
801 int32_t expectedFlags = 0) {
802 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP
803 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
804 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
805 }
806
807 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
808 int32_t expectedFlags = 0) {
Michael Wright3a240c42019-12-10 20:53:41 +0000809 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
810 expectedFlags);
811 }
812
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100813 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
814 ASSERT_NE(mInputReceiver, nullptr)
815 << "Cannot consume events from a window with no receiver";
816 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
817 }
818
chaviwd1c23182019-12-20 18:44:56 -0800819 void consumeEvent(int32_t expectedEventType, int32_t expectedAction, int32_t expectedDisplayId,
820 int32_t expectedFlags) {
821 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
822 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
823 expectedFlags);
824 }
825
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700826 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700827 if (mInputReceiver == nullptr) {
828 ADD_FAILURE() << "Invalid receive event on window with no receiver";
829 return std::nullopt;
830 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700831 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700832 }
833
834 void finishEvent(uint32_t sequenceNum) {
835 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
836 mInputReceiver->finishEvent(sequenceNum);
837 }
838
chaviwaf87b3e2019-10-01 16:59:28 -0700839 InputEvent* consume() {
840 if (mInputReceiver == nullptr) {
841 return nullptr;
842 }
843 return mInputReceiver->consume();
844 }
845
Arthur Hungb92218b2018-08-14 12:00:21 +0800846 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500847 if (mInputReceiver == nullptr &&
848 mInfo.inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL)) {
849 return; // Can't receive events if the window does not have input channel
850 }
851 ASSERT_NE(nullptr, mInputReceiver)
852 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -0800853 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +0800854 }
855
chaviwaf87b3e2019-10-01 16:59:28 -0700856 sp<IBinder> getToken() { return mInfo.token; }
857
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100858 const std::string& getName() { return mName; }
859
chaviwd1c23182019-12-20 18:44:56 -0800860private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100861 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -0800862 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700863 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800864};
865
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700866std::atomic<int32_t> FakeWindowHandle::sId{1};
867
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700868static int32_t injectKey(const sp<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700869 int32_t displayId = ADISPLAY_ID_NONE,
870 int32_t syncMode = INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT,
871 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800872 KeyEvent event;
873 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
874
875 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800876 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700877 INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE,
878 repeatCount, currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +0800879
880 // Inject event until dispatch out.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700881 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, syncMode,
882 injectionTimeout,
883 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
Arthur Hungb92218b2018-08-14 12:00:21 +0800884}
885
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700886static int32_t injectKeyDown(const sp<InputDispatcher>& dispatcher,
887 int32_t displayId = ADISPLAY_ID_NONE) {
888 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId);
889}
890
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700891static int32_t injectKeyUp(const sp<InputDispatcher>& dispatcher,
892 int32_t displayId = ADISPLAY_ID_NONE) {
893 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId);
894}
895
Garfield Tandf26e862020-07-01 20:18:19 -0700896class PointerBuilder {
897public:
898 PointerBuilder(int32_t id, int32_t toolType) {
899 mProperties.clear();
900 mProperties.id = id;
901 mProperties.toolType = toolType;
902 mCoords.clear();
903 }
904
905 PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); }
906
907 PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); }
908
909 PointerBuilder& axis(int32_t axis, float value) {
910 mCoords.setAxisValue(axis, value);
911 return *this;
912 }
913
914 PointerProperties buildProperties() const { return mProperties; }
915
916 PointerCoords buildCoords() const { return mCoords; }
917
918private:
919 PointerProperties mProperties;
920 PointerCoords mCoords;
921};
922
923class MotionEventBuilder {
924public:
925 MotionEventBuilder(int32_t action, int32_t source) {
926 mAction = action;
927 mSource = source;
928 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
929 }
930
931 MotionEventBuilder& eventTime(nsecs_t eventTime) {
932 mEventTime = eventTime;
933 return *this;
934 }
935
936 MotionEventBuilder& displayId(int32_t displayId) {
937 mDisplayId = displayId;
938 return *this;
939 }
940
941 MotionEventBuilder& actionButton(int32_t actionButton) {
942 mActionButton = actionButton;
943 return *this;
944 }
945
946 MotionEventBuilder& buttonState(int32_t actionButton) {
947 mActionButton = actionButton;
948 return *this;
949 }
950
951 MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) {
952 mRawXCursorPosition = rawXCursorPosition;
953 return *this;
954 }
955
956 MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) {
957 mRawYCursorPosition = rawYCursorPosition;
958 return *this;
959 }
960
961 MotionEventBuilder& pointer(PointerBuilder pointer) {
962 mPointers.push_back(pointer);
963 return *this;
964 }
965
966 MotionEvent build() {
967 std::vector<PointerProperties> pointerProperties;
968 std::vector<PointerCoords> pointerCoords;
969 for (const PointerBuilder& pointer : mPointers) {
970 pointerProperties.push_back(pointer.buildProperties());
971 pointerCoords.push_back(pointer.buildCoords());
972 }
973
974 // Set mouse cursor position for the most common cases to avoid boilerplate.
975 if (mSource == AINPUT_SOURCE_MOUSE &&
976 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) &&
977 mPointers.size() == 1) {
978 mRawXCursorPosition = pointerCoords[0].getX();
979 mRawYCursorPosition = pointerCoords[0].getY();
980 }
981
982 MotionEvent event;
chaviw9eaa22c2020-07-01 16:21:27 -0700983 ui::Transform identityTransform;
Garfield Tandf26e862020-07-01 20:18:19 -0700984 event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC,
985 mAction, mActionButton, /* flags */ 0, /* edgeFlags */ 0, AMETA_NONE,
chaviw9eaa22c2020-07-01 16:21:27 -0700986 mButtonState, MotionClassification::NONE, identityTransform,
987 /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition,
988 mRawYCursorPosition, mEventTime, mEventTime, mPointers.size(),
989 pointerProperties.data(), pointerCoords.data());
Garfield Tandf26e862020-07-01 20:18:19 -0700990
991 return event;
992 }
993
994private:
995 int32_t mAction;
996 int32_t mSource;
997 nsecs_t mEventTime;
998 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
999 int32_t mActionButton{0};
1000 int32_t mButtonState{0};
1001 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1002 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1003
1004 std::vector<PointerBuilder> mPointers;
1005};
1006
1007static int32_t injectMotionEvent(
1008 const sp<InputDispatcher>& dispatcher, const MotionEvent& event,
1009 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
1010 int32_t injectionMode = INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT) {
1011 return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, injectionMode,
1012 injectionTimeout,
1013 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
1014}
1015
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001016static int32_t injectMotionEvent(
1017 const sp<InputDispatcher>& dispatcher, int32_t action, int32_t source, int32_t displayId,
1018 const PointF& position,
1019 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001020 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1021 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
1022 int32_t injectionMode = INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT,
1023 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC)) {
Garfield Tandf26e862020-07-01 20:18:19 -07001024 MotionEvent event = MotionEventBuilder(action, source)
1025 .displayId(displayId)
1026 .eventTime(eventTime)
1027 .rawXCursorPosition(cursorPosition.x)
1028 .rawYCursorPosition(cursorPosition.y)
1029 .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER)
1030 .x(position.x)
1031 .y(position.y))
1032 .build();
Arthur Hungb92218b2018-08-14 12:00:21 +08001033
1034 // Inject event until dispatch out.
Garfield Tandf26e862020-07-01 20:18:19 -07001035 return injectMotionEvent(dispatcher, event);
Arthur Hungb92218b2018-08-14 12:00:21 +08001036}
1037
Garfield Tan00f511d2019-06-12 16:55:40 -07001038static int32_t injectMotionDown(const sp<InputDispatcher>& dispatcher, int32_t source,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001039 int32_t displayId, const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001040 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001041}
1042
Michael Wright3a240c42019-12-10 20:53:41 +00001043static int32_t injectMotionUp(const sp<InputDispatcher>& dispatcher, int32_t source,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001044 int32_t displayId, const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001045 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001046}
1047
Jackal Guof9696682018-10-05 12:23:23 +08001048static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1049 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1050 // Define a valid key event.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001051 NotifyKeyArgs args(/* id */ 0, currentTime, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
1052 POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, KEY_A,
1053 AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001054
1055 return args;
1056}
1057
chaviwd1c23182019-12-20 18:44:56 -08001058static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId,
1059 const std::vector<PointF>& points) {
1060 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001061 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1062 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1063 }
1064
chaviwd1c23182019-12-20 18:44:56 -08001065 PointerProperties pointerProperties[pointerCount];
1066 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001067
chaviwd1c23182019-12-20 18:44:56 -08001068 for (size_t i = 0; i < pointerCount; i++) {
1069 pointerProperties[i].clear();
1070 pointerProperties[i].id = i;
1071 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001072
chaviwd1c23182019-12-20 18:44:56 -08001073 pointerCoords[i].clear();
1074 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1075 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1076 }
Jackal Guof9696682018-10-05 12:23:23 +08001077
1078 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1079 // Define a valid motion event.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001080 NotifyMotionArgs args(/* id */ 0, currentTime, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001081 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1082 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001083 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1084 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001085 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1086 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001087
1088 return args;
1089}
1090
chaviwd1c23182019-12-20 18:44:56 -08001091static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1092 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1093}
1094
Arthur Hungb92218b2018-08-14 12:00:21 +08001095TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001096 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001097 sp<FakeWindowHandle> window = new FakeWindowHandle(application, mDispatcher, "Fake Window",
1098 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001099
Arthur Hung72d8dc32020-03-28 00:48:39 +00001100 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001101 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1102 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +08001103 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1104
1105 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001106 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001107}
1108
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001109/**
1110 * Calling setInputWindows once with FLAG_NOT_TOUCH_MODAL should not cause any issues.
1111 * To ensure that window receives only events that were directly inside of it, add
1112 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1113 * when finding touched windows.
1114 * This test serves as a sanity check for the next test, where setInputWindows is
1115 * called twice.
1116 */
1117TEST_F(InputDispatcherTest, SetInputWindowOnce_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001118 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001119 sp<FakeWindowHandle> window =
1120 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1121 window->setFrame(Rect(0, 0, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01001122 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001123
1124 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1125 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1126 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1127 {50, 50}))
1128 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1129
1130 // Window should receive motion event.
1131 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1132}
1133
1134/**
1135 * Calling setInputWindows twice, with the same info, should not cause any issues.
1136 * To ensure that window receives only events that were directly inside of it, add
1137 * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input
1138 * when finding touched windows.
1139 */
1140TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001141 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001142 sp<FakeWindowHandle> window =
1143 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1144 window->setFrame(Rect(0, 0, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01001145 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001146
1147 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1148 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1149 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1150 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1151 {50, 50}))
1152 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1153
1154 // Window should receive motion event.
1155 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1156}
1157
Arthur Hungb92218b2018-08-14 12:00:21 +08001158// The foreground window should receive the first touch down event.
1159TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001160 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001161 sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top",
1162 ADISPLAY_ID_DEFAULT);
1163 sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second",
1164 ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001165
Arthur Hung72d8dc32020-03-28 00:48:39 +00001166 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001167 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
1168 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +08001169 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1170
1171 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001172 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001173 windowSecond->assertNoEvents();
1174}
1175
Garfield Tandf26e862020-07-01 20:18:19 -07001176TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07001177 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07001178 sp<FakeWindowHandle> windowLeft =
1179 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1180 windowLeft->setFrame(Rect(0, 0, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001181 windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001182 sp<FakeWindowHandle> windowRight =
1183 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1184 windowRight->setFrame(Rect(600, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001185 windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001186
1187 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1188
1189 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
1190
1191 // Start cursor position in right window so that we can move the cursor to left window.
1192 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1193 injectMotionEvent(mDispatcher,
1194 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1195 AINPUT_SOURCE_MOUSE)
1196 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1197 .x(900)
1198 .y(400))
1199 .build()));
1200 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1201 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1202 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1203 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1204
1205 // Move cursor into left window
1206 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1207 injectMotionEvent(mDispatcher,
1208 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1209 AINPUT_SOURCE_MOUSE)
1210 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1211 .x(300)
1212 .y(400))
1213 .build()));
1214 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1215 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1216 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1217 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1218 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1219 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1220
1221 // Inject a series of mouse events for a mouse click
1222 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1223 injectMotionEvent(mDispatcher,
1224 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1225 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1226 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1227 .x(300)
1228 .y(400))
1229 .build()));
1230 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1231
1232 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1233 injectMotionEvent(mDispatcher,
1234 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
1235 AINPUT_SOURCE_MOUSE)
1236 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1237 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1238 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1239 .x(300)
1240 .y(400))
1241 .build()));
1242 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1243 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1244
1245 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1246 injectMotionEvent(mDispatcher,
1247 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1248 AINPUT_SOURCE_MOUSE)
1249 .buttonState(0)
1250 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1251 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1252 .x(300)
1253 .y(400))
1254 .build()));
1255 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1256 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1257
1258 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1259 injectMotionEvent(mDispatcher,
1260 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
1261 .buttonState(0)
1262 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1263 .x(300)
1264 .y(400))
1265 .build()));
1266 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1267
1268 // Move mouse cursor back to right window
1269 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1270 injectMotionEvent(mDispatcher,
1271 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1272 AINPUT_SOURCE_MOUSE)
1273 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1274 .x(900)
1275 .y(400))
1276 .build()));
1277 windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1278 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1279 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1280 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1281 windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE,
1282 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1283}
1284
1285// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
1286// directly in this test.
1287TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07001288 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07001289 sp<FakeWindowHandle> window =
1290 new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1291 window->setFrame(Rect(0, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001292 window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tandf26e862020-07-01 20:18:19 -07001293
1294 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1295
1296 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1297
1298 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1299 injectMotionEvent(mDispatcher,
1300 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
1301 AINPUT_SOURCE_MOUSE)
1302 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1303 .x(300)
1304 .y(400))
1305 .build()));
1306 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER,
1307 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1308
1309 // Inject a series of mouse events for a mouse click
1310 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1311 injectMotionEvent(mDispatcher,
1312 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
1313 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1314 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1315 .x(300)
1316 .y(400))
1317 .build()));
1318 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1319
1320 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1321 injectMotionEvent(mDispatcher,
1322 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
1323 AINPUT_SOURCE_MOUSE)
1324 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
1325 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1326 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1327 .x(300)
1328 .y(400))
1329 .build()));
1330 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS,
1331 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1332
1333 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1334 injectMotionEvent(mDispatcher,
1335 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1336 AINPUT_SOURCE_MOUSE)
1337 .buttonState(0)
1338 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
1339 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1340 .x(300)
1341 .y(400))
1342 .build()));
1343 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
1344 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1345
1346 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1347 injectMotionEvent(mDispatcher,
1348 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
1349 .buttonState(0)
1350 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1351 .x(300)
1352 .y(400))
1353 .build()));
1354 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1355
1356 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1357 injectMotionEvent(mDispatcher,
1358 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
1359 AINPUT_SOURCE_MOUSE)
1360 .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
1361 .x(300)
1362 .y(400))
1363 .build()));
1364 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT,
1365 ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */);
1366}
1367
Garfield Tan00f511d2019-06-12 16:55:40 -07001368TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07001369 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07001370
1371 sp<FakeWindowHandle> windowLeft =
1372 new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1373 windowLeft->setFrame(Rect(0, 0, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001374 windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07001375 sp<FakeWindowHandle> windowRight =
1376 new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1377 windowRight->setFrame(Rect(600, 0, 1200, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001378 windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Garfield Tan00f511d2019-06-12 16:55:40 -07001379
1380 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1381
Arthur Hung72d8dc32020-03-28 00:48:39 +00001382 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07001383
1384 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
1385 // left window. This event should be dispatched to the left window.
1386 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1387 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001388 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001389 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07001390 windowRight->assertNoEvents();
1391}
1392
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001393TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001394 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001395 sp<FakeWindowHandle> window =
1396 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07001397 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001398
Arthur Hung72d8dc32020-03-28 00:48:39 +00001399 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001400 setFocusedWindow(window);
1401
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001402 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001403
1404 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1405 mDispatcher->notifyKey(&keyArgs);
1406
1407 // Window should receive key down event.
1408 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1409
1410 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
1411 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001412 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001413 mDispatcher->notifyDeviceReset(&args);
1414 window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
1415 AKEY_EVENT_FLAG_CANCELED);
1416}
1417
1418TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001419 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001420 sp<FakeWindowHandle> window =
1421 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1422
Arthur Hung72d8dc32020-03-28 00:48:39 +00001423 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001424
1425 NotifyMotionArgs motionArgs =
1426 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1427 ADISPLAY_ID_DEFAULT);
1428 mDispatcher->notifyMotion(&motionArgs);
1429
1430 // Window should receive motion down event.
1431 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1432
1433 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
1434 // on the app side.
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001435 NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001436 mDispatcher->notifyDeviceReset(&args);
1437 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT,
1438 0 /*expectedFlags*/);
1439}
1440
Svet Ganov5d3bc372020-01-26 23:11:07 -08001441TEST_F(InputDispatcherTest, TransferTouchFocus_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07001442 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001443
1444 // Create a couple of windows
1445 sp<FakeWindowHandle> firstWindow = new FakeWindowHandle(application, mDispatcher,
1446 "First Window", ADISPLAY_ID_DEFAULT);
1447 sp<FakeWindowHandle> secondWindow = new FakeWindowHandle(application, mDispatcher,
1448 "Second Window", ADISPLAY_ID_DEFAULT);
1449
1450 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001451 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001452
1453 // Send down to the first window
1454 NotifyMotionArgs downMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
1455 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT);
1456 mDispatcher->notifyMotion(&downMotionArgs);
1457 // Only the first window should get the down event
1458 firstWindow->consumeMotionDown();
1459 secondWindow->assertNoEvents();
1460
1461 // Transfer touch focus to the second window
1462 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1463 // The first window gets cancel and the second gets down
1464 firstWindow->consumeMotionCancel();
1465 secondWindow->consumeMotionDown();
1466
1467 // Send up event to the second window
1468 NotifyMotionArgs upMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP,
1469 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT);
1470 mDispatcher->notifyMotion(&upMotionArgs);
1471 // The first window gets no events and the second gets up
1472 firstWindow->assertNoEvents();
1473 secondWindow->consumeMotionUp();
1474}
1475
1476TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointerNoSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001477 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001478
1479 PointF touchPoint = {10, 10};
1480
1481 // Create a couple of windows
1482 sp<FakeWindowHandle> firstWindow = new FakeWindowHandle(application, mDispatcher,
1483 "First Window", ADISPLAY_ID_DEFAULT);
1484 sp<FakeWindowHandle> secondWindow = new FakeWindowHandle(application, mDispatcher,
1485 "Second Window", ADISPLAY_ID_DEFAULT);
1486
1487 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001488 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001489
1490 // Send down to the first window
1491 NotifyMotionArgs downMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
1492 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {touchPoint});
1493 mDispatcher->notifyMotion(&downMotionArgs);
1494 // Only the first window should get the down event
1495 firstWindow->consumeMotionDown();
1496 secondWindow->assertNoEvents();
1497
1498 // Send pointer down to the first window
1499 NotifyMotionArgs pointerDownMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN
1500 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1501 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint});
1502 mDispatcher->notifyMotion(&pointerDownMotionArgs);
1503 // Only the first window should get the pointer down event
1504 firstWindow->consumeMotionPointerDown(1);
1505 secondWindow->assertNoEvents();
1506
1507 // Transfer touch focus to the second window
1508 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1509 // The first window gets cancel and the second gets down and pointer down
1510 firstWindow->consumeMotionCancel();
1511 secondWindow->consumeMotionDown();
1512 secondWindow->consumeMotionPointerDown(1);
1513
1514 // Send pointer up to the second window
1515 NotifyMotionArgs pointerUpMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP
1516 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1517 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint});
1518 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1519 // The first window gets nothing and the second gets pointer up
1520 firstWindow->assertNoEvents();
1521 secondWindow->consumeMotionPointerUp(1);
1522
1523 // Send up event to the second window
1524 NotifyMotionArgs upMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP,
1525 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT);
1526 mDispatcher->notifyMotion(&upMotionArgs);
1527 // The first window gets nothing and the second gets up
1528 firstWindow->assertNoEvents();
1529 secondWindow->consumeMotionUp();
1530}
1531
1532TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001533 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08001534
1535 // Create a non touch modal window that supports split touch
1536 sp<FakeWindowHandle> firstWindow = new FakeWindowHandle(application, mDispatcher,
1537 "First Window", ADISPLAY_ID_DEFAULT);
1538 firstWindow->setFrame(Rect(0, 0, 600, 400));
Michael Wright44753b12020-07-08 13:48:11 +01001539 firstWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1540 InputWindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001541
1542 // Create a non touch modal window that supports split touch
1543 sp<FakeWindowHandle> secondWindow = new FakeWindowHandle(application, mDispatcher,
1544 "Second Window", ADISPLAY_ID_DEFAULT);
1545 secondWindow->setFrame(Rect(0, 400, 600, 800));
Michael Wright44753b12020-07-08 13:48:11 +01001546 secondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
1547 InputWindowInfo::Flag::SPLIT_TOUCH);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001548
1549 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00001550 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08001551
1552 PointF pointInFirst = {300, 200};
1553 PointF pointInSecond = {300, 600};
1554
1555 // Send down to the first window
1556 NotifyMotionArgs firstDownMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
1557 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {pointInFirst});
1558 mDispatcher->notifyMotion(&firstDownMotionArgs);
1559 // Only the first window should get the down event
1560 firstWindow->consumeMotionDown();
1561 secondWindow->assertNoEvents();
1562
1563 // Send down to the second window
1564 NotifyMotionArgs secondDownMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN
1565 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1566 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {pointInFirst, pointInSecond});
1567 mDispatcher->notifyMotion(&secondDownMotionArgs);
1568 // The first window gets a move and the second a down
1569 firstWindow->consumeMotionMove();
1570 secondWindow->consumeMotionDown();
1571
1572 // Transfer touch focus to the second window
1573 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
1574 // The first window gets cancel and the new gets pointer down (it already saw down)
1575 firstWindow->consumeMotionCancel();
1576 secondWindow->consumeMotionPointerDown(1);
1577
1578 // Send pointer up to the second window
1579 NotifyMotionArgs pointerUpMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP
1580 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1581 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {pointInFirst, pointInSecond});
1582 mDispatcher->notifyMotion(&pointerUpMotionArgs);
1583 // The first window gets nothing and the second gets pointer up
1584 firstWindow->assertNoEvents();
1585 secondWindow->consumeMotionPointerUp(1);
1586
1587 // Send up event to the second window
1588 NotifyMotionArgs upMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP,
1589 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT);
1590 mDispatcher->notifyMotion(&upMotionArgs);
1591 // The first window gets nothing and the second gets up
1592 firstWindow->assertNoEvents();
1593 secondWindow->consumeMotionUp();
1594}
1595
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001596TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07001597 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001598 sp<FakeWindowHandle> window =
1599 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1600
Vishnu Nair47074b82020-08-14 11:54:47 -07001601 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001602 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001603 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001604
1605 window->consumeFocusEvent(true);
1606
1607 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1608 mDispatcher->notifyKey(&keyArgs);
1609
1610 // Window should receive key down event.
1611 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
1612}
1613
1614TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07001615 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001616 sp<FakeWindowHandle> window =
1617 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1618
Arthur Hung72d8dc32020-03-28 00:48:39 +00001619 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001620
1621 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1622 mDispatcher->notifyKey(&keyArgs);
1623 mDispatcher->waitForIdle();
1624
1625 window->assertNoEvents();
1626}
1627
1628// If a window is touchable, but does not have focus, it should receive motion events, but not keys
1629TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07001630 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001631 sp<FakeWindowHandle> window =
1632 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1633
Arthur Hung72d8dc32020-03-28 00:48:39 +00001634 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001635
1636 // Send key
1637 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
1638 mDispatcher->notifyKey(&keyArgs);
1639 // Send motion
1640 NotifyMotionArgs motionArgs =
1641 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1642 ADISPLAY_ID_DEFAULT);
1643 mDispatcher->notifyMotion(&motionArgs);
1644
1645 // Window should receive only the motion event
1646 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1647 window->assertNoEvents(); // Key event or focus event will not be received
1648}
1649
chaviwd1c23182019-12-20 18:44:56 -08001650class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00001651public:
1652 FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name,
chaviwd1c23182019-12-20 18:44:56 -08001653 int32_t displayId, bool isGestureMonitor = false) {
Garfield Tan62b523e2020-09-21 16:07:29 -07001654 base::Result<std::unique_ptr<InputChannel>> channel =
1655 dispatcher->createInputMonitor(displayId, isGestureMonitor, name);
1656 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00001657 }
1658
chaviwd1c23182019-12-20 18:44:56 -08001659 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
1660
1661 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1662 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN,
1663 expectedDisplayId, expectedFlags);
1664 }
1665
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001666 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
1667
1668 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
1669
chaviwd1c23182019-12-20 18:44:56 -08001670 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1671 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
1672 expectedDisplayId, expectedFlags);
1673 }
1674
1675 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1676 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP,
1677 expectedDisplayId, expectedFlags);
1678 }
1679
1680 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
1681
1682private:
1683 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00001684};
1685
1686// Tests for gesture monitors
1687TEST_F(InputDispatcherTest, GestureMonitor_ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07001688 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00001689 sp<FakeWindowHandle> window =
1690 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001691 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00001692
chaviwd1c23182019-12-20 18:44:56 -08001693 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
1694 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00001695
1696 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1697 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1698 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1699 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001700 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00001701}
1702
1703TEST_F(InputDispatcherTest, GestureMonitor_DoesNotReceiveKeyEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07001704 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00001705 sp<FakeWindowHandle> window =
1706 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1707
1708 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07001709 window->setFocusable(true);
Michael Wright3a240c42019-12-10 20:53:41 +00001710
Arthur Hung72d8dc32020-03-28 00:48:39 +00001711 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001712 setFocusedWindow(window);
1713
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001714 window->consumeFocusEvent(true);
Michael Wright3a240c42019-12-10 20:53:41 +00001715
chaviwd1c23182019-12-20 18:44:56 -08001716 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
1717 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00001718
1719 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
1720 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1721 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001722 monitor.assertNoEvents();
Michael Wright3a240c42019-12-10 20:53:41 +00001723}
1724
1725TEST_F(InputDispatcherTest, GestureMonitor_CanPilferAfterWindowIsRemovedMidStream) {
Chris Yea209fde2020-07-22 13:54:51 -07001726 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Michael Wright3a240c42019-12-10 20:53:41 +00001727 sp<FakeWindowHandle> window =
1728 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001729 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00001730
chaviwd1c23182019-12-20 18:44:56 -08001731 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT,
1732 true /*isGestureMonitor*/);
Michael Wright3a240c42019-12-10 20:53:41 +00001733
1734 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1735 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1736 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1737 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08001738 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00001739
1740 window->releaseChannel();
1741
chaviwd1c23182019-12-20 18:44:56 -08001742 mDispatcher->pilferPointers(monitor.getToken());
Michael Wright3a240c42019-12-10 20:53:41 +00001743
1744 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1745 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1746 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08001747 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00001748}
1749
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001750TEST_F(InputDispatcherTest, UnresponsiveGestureMonitor_GetsAnr) {
1751 FakeMonitorReceiver monitor =
1752 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
1753 true /*isGestureMonitor*/);
1754
1755 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
1756 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
1757 std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
1758 ASSERT_TRUE(consumeSeq);
1759
1760 mFakePolicy->assertNotifyAnrWasCalled(DISPATCHING_TIMEOUT, nullptr, monitor.getToken());
1761 monitor.finishEvent(*consumeSeq);
1762 ASSERT_TRUE(mDispatcher->waitForIdle());
1763}
1764
chaviw81e2bb92019-12-18 15:03:51 -08001765TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07001766 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
chaviw81e2bb92019-12-18 15:03:51 -08001767 sp<FakeWindowHandle> window =
1768 new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
1769
Arthur Hung72d8dc32020-03-28 00:48:39 +00001770 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08001771
1772 NotifyMotionArgs motionArgs =
1773 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1774 ADISPLAY_ID_DEFAULT);
1775
1776 mDispatcher->notifyMotion(&motionArgs);
1777 // Window should receive motion down event.
1778 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1779
1780 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001781 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08001782 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1783 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
1784 motionArgs.pointerCoords[0].getX() - 10);
1785
1786 mDispatcher->notifyMotion(&motionArgs);
1787 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
1788 0 /*expectedFlags*/);
1789}
1790
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001791/**
1792 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
1793 * the device default right away. In the test scenario, we check both the default value,
1794 * and the action of enabling / disabling.
1795 */
1796TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07001797 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001798 sp<FakeWindowHandle> window =
1799 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
1800
1801 // Set focused application.
1802 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07001803 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001804
1805 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00001806 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001807 setFocusedWindow(window);
1808
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001809 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
1810
1811 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07001812 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001813 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001814 window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/);
1815
1816 SCOPED_TRACE("Disable touch mode");
1817 mDispatcher->setInTouchMode(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07001818 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001819 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001820 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001821 window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/);
1822
1823 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07001824 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001825 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001826 window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/);
1827
1828 SCOPED_TRACE("Enable touch mode again");
1829 mDispatcher->setInTouchMode(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07001830 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00001831 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001832 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001833 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
1834
1835 window->assertNoEvents();
1836}
1837
Gang Wange9087892020-01-07 12:17:14 -05001838TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07001839 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Gang Wange9087892020-01-07 12:17:14 -05001840 sp<FakeWindowHandle> window =
1841 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
1842
1843 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07001844 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05001845
Arthur Hung72d8dc32020-03-28 00:48:39 +00001846 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07001847 setFocusedWindow(window);
1848
Gang Wange9087892020-01-07 12:17:14 -05001849 window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/);
1850
1851 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
1852 mDispatcher->notifyKey(&keyArgs);
1853
1854 InputEvent* event = window->consume();
1855 ASSERT_NE(event, nullptr);
1856
1857 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
1858 ASSERT_NE(verified, nullptr);
1859 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
1860
1861 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
1862 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
1863 ASSERT_EQ(keyArgs.source, verified->source);
1864 ASSERT_EQ(keyArgs.displayId, verified->displayId);
1865
1866 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
1867
1868 ASSERT_EQ(keyArgs.action, verifiedKey.action);
1869 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05001870 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
1871 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
1872 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
1873 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
1874 ASSERT_EQ(0, verifiedKey.repeatCount);
1875}
1876
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08001877TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07001878 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08001879 sp<FakeWindowHandle> window =
1880 new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT);
1881
1882 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1883
Arthur Hung72d8dc32020-03-28 00:48:39 +00001884 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08001885
1886 NotifyMotionArgs motionArgs =
1887 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
1888 ADISPLAY_ID_DEFAULT);
1889 mDispatcher->notifyMotion(&motionArgs);
1890
1891 InputEvent* event = window->consume();
1892 ASSERT_NE(event, nullptr);
1893
1894 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
1895 ASSERT_NE(verified, nullptr);
1896 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
1897
1898 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
1899 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
1900 EXPECT_EQ(motionArgs.source, verified->source);
1901 EXPECT_EQ(motionArgs.displayId, verified->displayId);
1902
1903 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
1904
1905 EXPECT_EQ(motionArgs.pointerCoords[0].getX(), verifiedMotion.rawX);
1906 EXPECT_EQ(motionArgs.pointerCoords[0].getY(), verifiedMotion.rawY);
1907 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
1908 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
1909 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
1910 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
1911 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
1912}
1913
chaviw09c8d2d2020-08-24 15:48:26 -07001914/**
1915 * Ensure that separate calls to sign the same data are generating the same key.
1916 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
1917 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
1918 * tests.
1919 */
1920TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
1921 KeyEvent event = getTestKeyEvent();
1922 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
1923
1924 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
1925 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
1926 ASSERT_EQ(hmac1, hmac2);
1927}
1928
1929/**
1930 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
1931 */
1932TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
1933 KeyEvent event = getTestKeyEvent();
1934 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
1935 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
1936
1937 verifiedEvent.deviceId += 1;
1938 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
1939
1940 verifiedEvent.source += 1;
1941 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
1942
1943 verifiedEvent.eventTimeNanos += 1;
1944 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
1945
1946 verifiedEvent.displayId += 1;
1947 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
1948
1949 verifiedEvent.action += 1;
1950 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
1951
1952 verifiedEvent.downTimeNanos += 1;
1953 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
1954
1955 verifiedEvent.flags += 1;
1956 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
1957
1958 verifiedEvent.keyCode += 1;
1959 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
1960
1961 verifiedEvent.scanCode += 1;
1962 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
1963
1964 verifiedEvent.metaState += 1;
1965 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
1966
1967 verifiedEvent.repeatCount += 1;
1968 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
1969}
1970
Vishnu Nair958da932020-08-21 17:12:37 -07001971TEST_F(InputDispatcherTest, SetFocusedWindow) {
1972 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1973 sp<FakeWindowHandle> windowTop =
1974 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
1975 sp<FakeWindowHandle> windowSecond =
1976 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
1977 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1978
1979 // Top window is also focusable but is not granted focus.
1980 windowTop->setFocusable(true);
1981 windowSecond->setFocusable(true);
1982 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
1983 setFocusedWindow(windowSecond);
1984
1985 windowSecond->consumeFocusEvent(true);
1986 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
1987 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
1988
1989 // Focused window should receive event.
1990 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
1991 windowTop->assertNoEvents();
1992}
1993
1994TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
1995 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1996 sp<FakeWindowHandle> window =
1997 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
1998 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
1999
2000 window->setFocusable(true);
2001 // Release channel for window is no longer valid.
2002 window->releaseChannel();
2003 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2004 setFocusedWindow(window);
2005
2006 // Test inject a key down, should timeout.
2007 ASSERT_EQ(INPUT_EVENT_INJECTION_TIMED_OUT, injectKeyDown(mDispatcher))
2008 << "Inject key event should return INPUT_EVENT_INJECTION_TIMED_OUT";
2009
2010 // window channel is invalid, so it should not receive any input event.
2011 window->assertNoEvents();
2012}
2013
2014TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
2015 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2016 sp<FakeWindowHandle> window =
2017 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2018 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2019
2020 // Window is not focusable.
2021 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2022 setFocusedWindow(window);
2023
2024 // Test inject a key down, should timeout.
2025 ASSERT_EQ(INPUT_EVENT_INJECTION_TIMED_OUT, injectKeyDown(mDispatcher))
2026 << "Inject key event should return INPUT_EVENT_INJECTION_TIMED_OUT";
2027
2028 // window is invalid, so it should not receive any input event.
2029 window->assertNoEvents();
2030}
2031
2032TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
2033 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2034 sp<FakeWindowHandle> windowTop =
2035 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2036 sp<FakeWindowHandle> windowSecond =
2037 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2038 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2039
2040 windowTop->setFocusable(true);
2041 windowSecond->setFocusable(true);
2042 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2043 setFocusedWindow(windowTop);
2044 windowTop->consumeFocusEvent(true);
2045
2046 setFocusedWindow(windowSecond, windowTop);
2047 windowSecond->consumeFocusEvent(true);
2048 windowTop->consumeFocusEvent(false);
2049
2050 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
2051 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
2052
2053 // Focused window should receive event.
2054 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
2055}
2056
2057TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) {
2058 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2059 sp<FakeWindowHandle> windowTop =
2060 new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
2061 sp<FakeWindowHandle> windowSecond =
2062 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2063 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2064
2065 windowTop->setFocusable(true);
2066 windowSecond->setFocusable(true);
2067 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
2068 setFocusedWindow(windowSecond, windowTop);
2069
2070 ASSERT_EQ(INPUT_EVENT_INJECTION_TIMED_OUT, injectKeyDown(mDispatcher))
2071 << "Inject key event should return INPUT_EVENT_INJECTION_TIMED_OUT";
2072
2073 // Event should be dropped.
2074 windowTop->assertNoEvents();
2075 windowSecond->assertNoEvents();
2076}
2077
2078TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
2079 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2080 sp<FakeWindowHandle> window =
2081 new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2082 sp<FakeWindowHandle> previousFocusedWindow =
2083 new FakeWindowHandle(application, mDispatcher, "previousFocusedWindow",
2084 ADISPLAY_ID_DEFAULT);
2085 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2086
2087 window->setFocusable(true);
2088 previousFocusedWindow->setFocusable(true);
2089 window->setVisible(false);
2090 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
2091 setFocusedWindow(previousFocusedWindow);
2092 previousFocusedWindow->consumeFocusEvent(true);
2093
2094 // Requesting focus on invisible window takes focus from currently focused window.
2095 setFocusedWindow(window);
2096 previousFocusedWindow->consumeFocusEvent(false);
2097
2098 // Injected key goes to pending queue.
2099 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
2100 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
2101 ADISPLAY_ID_DEFAULT, INPUT_EVENT_INJECTION_SYNC_NONE));
2102
2103 // Window does not get focus event or key down.
2104 window->assertNoEvents();
2105
2106 // Window becomes visible.
2107 window->setVisible(true);
2108 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2109
2110 // Window receives focus event.
2111 window->consumeFocusEvent(true);
2112 // Focused window receives key down.
2113 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2114}
2115
Garfield Tan1c7bc862020-01-28 13:24:04 -08002116class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
2117protected:
2118 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
2119 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
2120
Chris Yea209fde2020-07-22 13:54:51 -07002121 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002122 sp<FakeWindowHandle> mWindow;
2123
2124 virtual void SetUp() override {
2125 mFakePolicy = new FakeInputDispatcherPolicy();
2126 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
2127 mDispatcher = new InputDispatcher(mFakePolicy);
2128 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
2129 ASSERT_EQ(OK, mDispatcher->start());
2130
2131 setUpWindow();
2132 }
2133
2134 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07002135 mApp = std::make_shared<FakeApplicationHandle>();
Garfield Tan1c7bc862020-01-28 13:24:04 -08002136 mWindow = new FakeWindowHandle(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
2137
Vishnu Nair47074b82020-08-14 11:54:47 -07002138 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002139 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002140 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002141 mWindow->consumeFocusEvent(true);
2142 }
2143
2144 void sendAndConsumeKeyDown() {
2145 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
2146 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
2147 mDispatcher->notifyKey(&keyArgs);
2148
2149 // Window should receive key down event.
2150 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2151 }
2152
2153 void expectKeyRepeatOnce(int32_t repeatCount) {
2154 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
2155 InputEvent* repeatEvent = mWindow->consume();
2156 ASSERT_NE(nullptr, repeatEvent);
2157
2158 uint32_t eventType = repeatEvent->getType();
2159 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType);
2160
2161 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
2162 uint32_t eventAction = repeatKeyEvent->getAction();
2163 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
2164 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
2165 }
2166
2167 void sendAndConsumeKeyUp() {
2168 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
2169 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
2170 mDispatcher->notifyKey(&keyArgs);
2171
2172 // Window should receive key down event.
2173 mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
2174 0 /*expectedFlags*/);
2175 }
2176};
2177
2178TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
2179 sendAndConsumeKeyDown();
2180 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2181 expectKeyRepeatOnce(repeatCount);
2182 }
2183}
2184
2185TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
2186 sendAndConsumeKeyDown();
2187 expectKeyRepeatOnce(1 /*repeatCount*/);
2188 sendAndConsumeKeyUp();
2189 mWindow->assertNoEvents();
2190}
2191
2192TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
2193 sendAndConsumeKeyDown();
2194 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2195 InputEvent* repeatEvent = mWindow->consume();
2196 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2197 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
2198 IdGenerator::getSource(repeatEvent->getId()));
2199 }
2200}
2201
2202TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
2203 sendAndConsumeKeyDown();
2204
2205 std::unordered_set<int32_t> idSet;
2206 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
2207 InputEvent* repeatEvent = mWindow->consume();
2208 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
2209 int32_t id = repeatEvent->getId();
2210 EXPECT_EQ(idSet.end(), idSet.find(id));
2211 idSet.insert(id);
2212 }
2213}
2214
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002215/* Test InputDispatcher for MultiDisplay */
2216class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
2217public:
2218 static constexpr int32_t SECOND_DISPLAY_ID = 1;
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002219 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002220 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08002221
Chris Yea209fde2020-07-22 13:54:51 -07002222 application1 = std::make_shared<FakeApplicationHandle>();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002223 windowInPrimary = new FakeWindowHandle(application1, mDispatcher, "D_1",
2224 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002225
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002226 // Set focus window for primary display, but focused display would be second one.
2227 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07002228 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002229 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002230 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002231 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08002232
Chris Yea209fde2020-07-22 13:54:51 -07002233 application2 = std::make_shared<FakeApplicationHandle>();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002234 windowInSecondary = new FakeWindowHandle(application2, mDispatcher, "D_2",
2235 SECOND_DISPLAY_ID);
2236 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002237 // Set focus display to second one.
2238 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
2239 // Set focus window for second display.
2240 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07002241 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00002242 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002243 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002244 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002245 }
2246
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002247 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002248 InputDispatcherTest::TearDown();
2249
Chris Yea209fde2020-07-22 13:54:51 -07002250 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002251 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07002252 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002253 windowInSecondary.clear();
2254 }
2255
2256protected:
Chris Yea209fde2020-07-22 13:54:51 -07002257 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002258 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07002259 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002260 sp<FakeWindowHandle> windowInSecondary;
2261};
2262
2263TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
2264 // Test touch down on primary display.
2265 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
2266 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungb92218b2018-08-14 12:00:21 +08002267 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002268 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08002269 windowInSecondary->assertNoEvents();
2270
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002271 // Test touch down on second display.
2272 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
2273 AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Arthur Hungb92218b2018-08-14 12:00:21 +08002274 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
2275 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002276 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08002277}
2278
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002279TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08002280 // Test inject a key down with display id specified.
2281 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2282 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002283 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08002284 windowInSecondary->assertNoEvents();
2285
2286 // Test inject a key down without display id specified.
Arthur Hungb92218b2018-08-14 12:00:21 +08002287 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
2288 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
2289 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002290 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08002291
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002292 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00002293 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08002294
2295 // Expect old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002296 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
2297 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08002298
2299 // Test inject a key down, should timeout because of no target window.
2300 ASSERT_EQ(INPUT_EVENT_INJECTION_TIMED_OUT, injectKeyDown(mDispatcher))
2301 << "Inject key event should return INPUT_EVENT_INJECTION_TIMED_OUT";
2302 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002303 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08002304 windowInSecondary->assertNoEvents();
2305}
2306
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002307// Test per-display input monitors for motion event.
2308TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08002309 FakeMonitorReceiver monitorInPrimary =
2310 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
2311 FakeMonitorReceiver monitorInSecondary =
2312 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002313
2314 // Test touch down on primary display.
2315 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
2316 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
2317 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002318 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08002319 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002320 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002321 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002322
2323 // Test touch down on second display.
2324 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
2325 AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
2326 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
2327 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002328 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002329 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08002330 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002331
2332 // Test inject a non-pointer motion event.
2333 // If specific a display, it will dispatch to the focused window of particular display,
2334 // or it will dispatch to the focused window of focused display.
2335 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher,
2336 AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
2337 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
2338 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002339 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002340 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08002341 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002342}
2343
2344// Test per-display input monitors for key event.
2345TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
2346 //Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08002347 FakeMonitorReceiver monitorInPrimary =
2348 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
2349 FakeMonitorReceiver monitorInSecondary =
2350 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002351
2352 // Test inject a key down.
2353 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
2354 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
2355 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08002356 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08002357 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08002358 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002359}
2360
Vishnu Nair958da932020-08-21 17:12:37 -07002361TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
2362 sp<FakeWindowHandle> secondWindowInPrimary =
2363 new FakeWindowHandle(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
2364 secondWindowInPrimary->setFocusable(true);
2365 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
2366 setFocusedWindow(secondWindowInPrimary);
2367 windowInPrimary->consumeFocusEvent(false);
2368 secondWindowInPrimary->consumeFocusEvent(true);
2369
2370 // Test inject a key down.
2371 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2372 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
2373 windowInPrimary->assertNoEvents();
2374 windowInSecondary->assertNoEvents();
2375 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2376}
2377
Jackal Guof9696682018-10-05 12:23:23 +08002378class InputFilterTest : public InputDispatcherTest {
2379protected:
2380 static constexpr int32_t SECOND_DISPLAY_ID = 1;
2381
2382 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) {
2383 NotifyMotionArgs motionArgs;
2384
2385 motionArgs = generateMotionArgs(
2386 AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
2387 mDispatcher->notifyMotion(&motionArgs);
2388 motionArgs = generateMotionArgs(
2389 AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
2390 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002391 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08002392 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08002393 mFakePolicy->assertFilterInputEventWasCalled(motionArgs);
Jackal Guof9696682018-10-05 12:23:23 +08002394 } else {
2395 mFakePolicy->assertFilterInputEventWasNotCalled();
2396 }
2397 }
2398
2399 void testNotifyKey(bool expectToBeFiltered) {
2400 NotifyKeyArgs keyArgs;
2401
2402 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
2403 mDispatcher->notifyKey(&keyArgs);
2404 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
2405 mDispatcher->notifyKey(&keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002406 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08002407
2408 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08002409 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08002410 } else {
2411 mFakePolicy->assertFilterInputEventWasNotCalled();
2412 }
2413 }
2414};
2415
2416// Test InputFilter for MotionEvent
2417TEST_F(InputFilterTest, MotionEvent_InputFilter) {
2418 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
2419 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
2420 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
2421
2422 // Enable InputFilter
2423 mDispatcher->setInputFilterEnabled(true);
2424 // Test touch on both primary and second display, and check if both events are filtered.
2425 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
2426 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
2427
2428 // Disable InputFilter
2429 mDispatcher->setInputFilterEnabled(false);
2430 // Test touch on both primary and second display, and check if both events aren't filtered.
2431 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
2432 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
2433}
2434
2435// Test InputFilter for KeyEvent
2436TEST_F(InputFilterTest, KeyEvent_InputFilter) {
2437 // Since the InputFilter is disabled by default, check if key event aren't filtered.
2438 testNotifyKey(/*expectToBeFiltered*/ false);
2439
2440 // Enable InputFilter
2441 mDispatcher->setInputFilterEnabled(true);
2442 // Send a key event, and check if it is filtered.
2443 testNotifyKey(/*expectToBeFiltered*/ true);
2444
2445 // Disable InputFilter
2446 mDispatcher->setInputFilterEnabled(false);
2447 // Send a key event, and check if it isn't filtered.
2448 testNotifyKey(/*expectToBeFiltered*/ false);
2449}
2450
chaviwfd6d3512019-03-25 13:23:49 -07002451class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002452 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07002453 InputDispatcherTest::SetUp();
2454
Chris Yea209fde2020-07-22 13:54:51 -07002455 std::shared_ptr<FakeApplicationHandle> application =
2456 std::make_shared<FakeApplicationHandle>();
chaviwfd6d3512019-03-25 13:23:49 -07002457 mUnfocusedWindow = new FakeWindowHandle(application, mDispatcher, "Top",
2458 ADISPLAY_ID_DEFAULT);
2459 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
2460 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
2461 // window.
Michael Wright44753b12020-07-08 13:48:11 +01002462 mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07002463
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002464 mFocusedWindow =
2465 new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
2466 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01002467 mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
chaviwfd6d3512019-03-25 13:23:49 -07002468
2469 // Set focused application.
2470 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07002471 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07002472
2473 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00002474 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002475 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002476 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07002477 }
2478
Prabir Pradhan3608aad2019-10-02 17:08:26 -07002479 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07002480 InputDispatcherTest::TearDown();
2481
2482 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002483 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07002484 }
2485
2486protected:
2487 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002488 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002489 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07002490};
2491
2492// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
2493// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
2494// the onPointerDownOutsideFocus callback.
2495TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002496 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
2497 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2498 {20, 20}))
chaviwfd6d3512019-03-25 13:23:49 -07002499 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002500 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07002501
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002502 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07002503 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
2504}
2505
2506// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
2507// DOWN on the window that doesn't have focus. Ensure no window received the
2508// onPointerDownOutsideFocus callback.
2509TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002510 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
2511 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
chaviwfd6d3512019-03-25 13:23:49 -07002512 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002513 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07002514
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002515 ASSERT_TRUE(mDispatcher->waitForIdle());
2516 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07002517}
2518
2519// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
2520// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
2521TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
2522 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
2523 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002524 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07002525
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002526 ASSERT_TRUE(mDispatcher->waitForIdle());
2527 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07002528}
2529
2530// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
2531// DOWN on the window that already has focus. Ensure no window received the
2532// onPointerDownOutsideFocus callback.
2533TEST_F(InputDispatcherOnPointerDownOutsideFocus,
2534 OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08002535 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
2536 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07002537 FOCUSED_WINDOW_TOUCH_POINT))
chaviwfd6d3512019-03-25 13:23:49 -07002538 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07002539 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07002540
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08002541 ASSERT_TRUE(mDispatcher->waitForIdle());
2542 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07002543}
2544
chaviwaf87b3e2019-10-01 16:59:28 -07002545// These tests ensures we can send touch events to a single client when there are multiple input
2546// windows that point to the same client token.
2547class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
2548 virtual void SetUp() override {
2549 InputDispatcherTest::SetUp();
2550
Chris Yea209fde2020-07-22 13:54:51 -07002551 std::shared_ptr<FakeApplicationHandle> application =
2552 std::make_shared<FakeApplicationHandle>();
chaviwaf87b3e2019-10-01 16:59:28 -07002553 mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1",
2554 ADISPLAY_ID_DEFAULT);
2555 // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window.
2556 // We also need FLAG_SPLIT_TOUCH or we won't be able to get touches for both windows.
Michael Wright44753b12020-07-08 13:48:11 +01002557 mWindow1->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
2558 InputWindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07002559 mWindow1->setFrame(Rect(0, 0, 100, 100));
2560
2561 mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2",
2562 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
Michael Wright44753b12020-07-08 13:48:11 +01002563 mWindow2->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
2564 InputWindowInfo::Flag::SPLIT_TOUCH);
chaviwaf87b3e2019-10-01 16:59:28 -07002565 mWindow2->setFrame(Rect(100, 100, 200, 200));
2566
Arthur Hung72d8dc32020-03-28 00:48:39 +00002567 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07002568 }
2569
2570protected:
2571 sp<FakeWindowHandle> mWindow1;
2572 sp<FakeWindowHandle> mWindow2;
2573
2574 // Helper function to convert the point from screen coordinates into the window's space
2575 static PointF getPointInWindow(const InputWindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07002576 vec2 vals = windowInfo->transform.transform(point.x, point.y);
2577 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07002578 }
2579
2580 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
2581 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002582 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07002583 InputEvent* event = window->consume();
2584
2585 ASSERT_NE(nullptr, event) << name.c_str()
2586 << ": consumer should have returned non-NULL event.";
2587
2588 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
2589 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
2590 << " event, got " << inputEventTypeToString(event->getType()) << " event";
2591
2592 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
2593 EXPECT_EQ(expectedAction, motionEvent.getAction());
2594
2595 for (size_t i = 0; i < points.size(); i++) {
2596 float expectedX = points[i].x;
2597 float expectedY = points[i].y;
2598
2599 EXPECT_EQ(expectedX, motionEvent.getX(i))
2600 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
2601 << ", got " << motionEvent.getX(i);
2602 EXPECT_EQ(expectedY, motionEvent.getY(i))
2603 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
2604 << ", got " << motionEvent.getY(i);
2605 }
2606 }
chaviw9eaa22c2020-07-01 16:21:27 -07002607
2608 void touchAndAssertPositions(int32_t action, std::vector<PointF> touchedPoints,
2609 std::vector<PointF> expectedPoints) {
2610 NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
2611 ADISPLAY_ID_DEFAULT, touchedPoints);
2612 mDispatcher->notifyMotion(&motionArgs);
2613
2614 // Always consume from window1 since it's the window that has the InputReceiver
2615 consumeMotionEvent(mWindow1, action, expectedPoints);
2616 }
chaviwaf87b3e2019-10-01 16:59:28 -07002617};
2618
2619TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
2620 // Touch Window 1
2621 PointF touchedPoint = {10, 10};
2622 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07002623 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07002624
2625 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07002626 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07002627
2628 // Touch Window 2
2629 touchedPoint = {150, 150};
2630 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07002631 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07002632}
2633
chaviw9eaa22c2020-07-01 16:21:27 -07002634TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
2635 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07002636 mWindow2->setWindowScale(0.5f, 0.5f);
2637
2638 // Touch Window 1
2639 PointF touchedPoint = {10, 10};
2640 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07002641 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07002642 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07002643 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07002644
2645 // Touch Window 2
2646 touchedPoint = {150, 150};
2647 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07002648 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
2649 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07002650
chaviw9eaa22c2020-07-01 16:21:27 -07002651 // Update the transform so rotation is set
2652 mWindow2->setWindowTransform(0, -1, 1, 0);
2653 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
2654 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07002655}
2656
chaviw9eaa22c2020-07-01 16:21:27 -07002657TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002658 mWindow2->setWindowScale(0.5f, 0.5f);
2659
2660 // Touch Window 1
2661 std::vector<PointF> touchedPoints = {PointF{10, 10}};
2662 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07002663 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002664
2665 // Touch Window 2
2666 int32_t actionPointerDown =
2667 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07002668 touchedPoints.push_back(PointF{150, 150});
2669 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
2670 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002671
chaviw9eaa22c2020-07-01 16:21:27 -07002672 // Release Window 2
2673 int32_t actionPointerUp =
2674 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
2675 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
2676 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002677
chaviw9eaa22c2020-07-01 16:21:27 -07002678 // Update the transform so rotation is set for Window 2
2679 mWindow2->setWindowTransform(0, -1, 1, 0);
2680 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
2681 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002682}
2683
chaviw9eaa22c2020-07-01 16:21:27 -07002684TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002685 mWindow2->setWindowScale(0.5f, 0.5f);
2686
2687 // Touch Window 1
2688 std::vector<PointF> touchedPoints = {PointF{10, 10}};
2689 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07002690 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002691
2692 // Touch Window 2
2693 int32_t actionPointerDown =
2694 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07002695 touchedPoints.push_back(PointF{150, 150});
2696 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002697
chaviw9eaa22c2020-07-01 16:21:27 -07002698 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002699
2700 // Move both windows
2701 touchedPoints = {{20, 20}, {175, 175}};
2702 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
2703 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
2704
chaviw9eaa22c2020-07-01 16:21:27 -07002705 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002706
chaviw9eaa22c2020-07-01 16:21:27 -07002707 // Release Window 2
2708 int32_t actionPointerUp =
2709 AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
2710 touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints);
2711 expectedPoints.pop_back();
2712
2713 // Touch Window 2
2714 mWindow2->setWindowTransform(0, -1, 1, 0);
2715 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
2716 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
2717
2718 // Move both windows
2719 touchedPoints = {{20, 20}, {175, 175}};
2720 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
2721 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
2722
2723 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002724}
2725
2726TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
2727 mWindow1->setWindowScale(0.5f, 0.5f);
2728
2729 // Touch Window 1
2730 std::vector<PointF> touchedPoints = {PointF{10, 10}};
2731 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07002732 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002733
2734 // Touch Window 2
2735 int32_t actionPointerDown =
2736 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
chaviw9eaa22c2020-07-01 16:21:27 -07002737 touchedPoints.push_back(PointF{150, 150});
2738 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002739
chaviw9eaa22c2020-07-01 16:21:27 -07002740 touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002741
2742 // Move both windows
2743 touchedPoints = {{20, 20}, {175, 175}};
2744 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
2745 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
2746
chaviw9eaa22c2020-07-01 16:21:27 -07002747 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002748}
2749
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002750class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
2751 virtual void SetUp() override {
2752 InputDispatcherTest::SetUp();
2753
Chris Yea209fde2020-07-22 13:54:51 -07002754 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002755 mApplication->setDispatchingTimeout(20ms);
2756 mWindow =
2757 new FakeWindowHandle(mApplication, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
2758 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05002759 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07002760 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002761 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
2762 // window.
Michael Wright44753b12020-07-08 13:48:11 +01002763 mWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002764
2765 // Set focused application.
2766 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
2767
2768 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07002769 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002770 mWindow->consumeFocusEvent(true);
2771 }
2772
2773 virtual void TearDown() override {
2774 InputDispatcherTest::TearDown();
2775 mWindow.clear();
2776 }
2777
2778protected:
Chris Yea209fde2020-07-22 13:54:51 -07002779 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002780 sp<FakeWindowHandle> mWindow;
2781 static constexpr PointF WINDOW_LOCATION = {20, 20};
2782
2783 void tapOnWindow() {
2784 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
2785 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2786 WINDOW_LOCATION));
2787 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
2788 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2789 WINDOW_LOCATION));
2790 }
2791};
2792
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002793// Send a tap and respond, which should not cause an ANR.
2794TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
2795 tapOnWindow();
2796 mWindow->consumeMotionDown();
2797 mWindow->consumeMotionUp();
2798 ASSERT_TRUE(mDispatcher->waitForIdle());
2799 mFakePolicy->assertNotifyAnrWasNotCalled();
2800}
2801
2802// Send a regular key and respond, which should not cause an ANR.
2803TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
2804 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher));
2805 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
2806 ASSERT_TRUE(mDispatcher->waitForIdle());
2807 mFakePolicy->assertNotifyAnrWasNotCalled();
2808}
2809
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05002810TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
2811 mWindow->setFocusable(false);
2812 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
2813 mWindow->consumeFocusEvent(false);
2814
2815 int32_t result =
2816 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
2817 INPUT_EVENT_INJECTION_SYNC_NONE, 10ms /*injectionTimeout*/);
2818 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, result);
2819 // Key will not go to window because we have no focused window.
2820 // The 'no focused window' ANR timer should start instead.
2821
2822 // Now, the focused application goes away.
2823 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
2824 // The key should get dropped and there should be no ANR.
2825
2826 ASSERT_TRUE(mDispatcher->waitForIdle());
2827 mFakePolicy->assertNotifyAnrWasNotCalled();
2828}
2829
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002830// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002831// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
2832// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002833TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
2834 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
2835 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2836 WINDOW_LOCATION));
2837
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002838 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
2839 ASSERT_TRUE(sequenceNum);
2840 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
2841 mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/, mWindow->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002842
2843 // The remaining lines are not really needed for the test, but kept as a sanity check
2844 mWindow->finishEvent(*sequenceNum);
2845 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
2846 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002847 ASSERT_TRUE(mDispatcher->waitForIdle());
2848}
2849
2850// Send a key to the app and have the app not respond right away.
2851TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
2852 // Inject a key, and don't respond - expect that ANR is called.
2853 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher));
2854 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
2855 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002856 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002857 mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/, mWindow->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07002858 ASSERT_TRUE(mDispatcher->waitForIdle());
2859}
2860
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002861// We have a focused application, but no focused window
2862TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07002863 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002864 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
2865 mWindow->consumeFocusEvent(false);
2866
2867 // taps on the window work as normal
2868 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
2869 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2870 WINDOW_LOCATION));
2871 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
2872 mDispatcher->waitForIdle();
2873 mFakePolicy->assertNotifyAnrWasNotCalled();
2874
2875 // Once a focused event arrives, we get an ANR for this application
2876 // We specify the injection timeout to be smaller than the application timeout, to ensure that
2877 // injection times out (instead of failing).
2878 const int32_t result =
2879 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
2880 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT, 10ms);
2881 ASSERT_EQ(INPUT_EVENT_INJECTION_TIMED_OUT, result);
2882 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
2883 mFakePolicy->assertNotifyAnrWasCalled(timeout, mApplication, nullptr /*windowToken*/);
2884 ASSERT_TRUE(mDispatcher->waitForIdle());
2885}
2886
2887// We have a focused application, but no focused window
2888// If the policy wants to keep waiting on the focused window to be added, make sure
2889// that this timeout extension is honored and ANR is raised again.
2890TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_ExtendsAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07002891 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002892 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
2893 mWindow->consumeFocusEvent(false);
2894 const std::chrono::duration timeout = 5ms;
2895 mFakePolicy->setAnrTimeout(timeout);
2896
2897 // Once a focused event arrives, we get an ANR for this application
2898 // We specify the injection timeout to be smaller than the application timeout, to ensure that
2899 // injection times out (instead of failing).
2900 const int32_t result =
2901 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
2902 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT, 10ms);
2903 ASSERT_EQ(INPUT_EVENT_INJECTION_TIMED_OUT, result);
2904 const std::chrono::duration appTimeout =
2905 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
2906 mFakePolicy->assertNotifyAnrWasCalled(appTimeout, mApplication, nullptr /*windowToken*/);
2907
2908 // After the extended time has passed, ANR should be raised again
2909 mFakePolicy->assertNotifyAnrWasCalled(timeout, mApplication, nullptr /*windowToken*/);
2910
2911 // If we stop extending the timeout, dispatcher should go to idle.
2912 // Another ANR may be raised during this time
2913 mFakePolicy->setAnrTimeout(0ms);
2914 ASSERT_TRUE(mDispatcher->waitForIdle());
2915}
2916
2917// We have a focused application, but no focused window
2918TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07002919 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002920 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
2921 mWindow->consumeFocusEvent(false);
2922
2923 // Once a focused event arrives, we get an ANR for this application
2924 const int32_t result =
2925 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
2926 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT, 10ms);
2927 ASSERT_EQ(INPUT_EVENT_INJECTION_TIMED_OUT, result);
2928
2929 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
2930 mFakePolicy->assertNotifyAnrWasCalled(timeout, mApplication, nullptr /*windowToken*/);
2931
2932 // Future focused events get dropped right away
2933 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, injectKeyDown(mDispatcher));
2934 ASSERT_TRUE(mDispatcher->waitForIdle());
2935 mWindow->assertNoEvents();
2936}
2937
2938/**
2939 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
2940 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
2941 * If we process 1 of the events, but ANR on the second event with the same timestamp,
2942 * the ANR mechanism should still work.
2943 *
2944 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
2945 * DOWN event, while not responding on the second one.
2946 */
2947TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
2948 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
2949 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
2950 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
2951 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
2952 AMOTION_EVENT_INVALID_CURSOR_POSITION},
2953 500ms, INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT, currentTime);
2954
2955 // Now send ACTION_UP, with identical timestamp
2956 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
2957 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
2958 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
2959 AMOTION_EVENT_INVALID_CURSOR_POSITION},
2960 500ms, INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT, currentTime);
2961
2962 // We have now sent down and up. Let's consume first event and then ANR on the second.
2963 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2964 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
2965 mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/, mWindow->getToken());
2966}
2967
2968// If an app is not responding to a key event, gesture monitors should continue to receive
2969// new motion events
2970TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnKey) {
2971 FakeMonitorReceiver monitor =
2972 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
2973 true /*isGestureMonitor*/);
2974
2975 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
2976 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
2977 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
2978
2979 // Stuck on the ACTION_UP
2980 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
2981 mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr, mWindow->getToken());
2982
2983 // New tap will go to the gesture monitor, but not to the window
2984 tapOnWindow();
2985 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
2986 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
2987
2988 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
2989 mDispatcher->waitForIdle();
2990 mWindow->assertNoEvents();
2991 monitor.assertNoEvents();
2992}
2993
2994// If an app is not responding to a motion event, gesture monitors should continue to receive
2995// new motion events
2996TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnMotion) {
2997 FakeMonitorReceiver monitor =
2998 FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT,
2999 true /*isGestureMonitor*/);
3000
3001 tapOnWindow();
3002 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3003 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3004
3005 mWindow->consumeMotionDown();
3006 // Stuck on the ACTION_UP
3007 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
3008 mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr, mWindow->getToken());
3009
3010 // New tap will go to the gesture monitor, but not to the window
3011 tapOnWindow();
3012 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
3013 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
3014
3015 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
3016 mDispatcher->waitForIdle();
3017 mWindow->assertNoEvents();
3018 monitor.assertNoEvents();
3019}
3020
3021// If a window is unresponsive, then you get anr. if the window later catches up and starts to
3022// process events, you don't get an anr. When the window later becomes unresponsive again, you
3023// get an ANR again.
3024// 1. tap -> block on ACTION_UP -> receive ANR
3025// 2. consume all pending events (= queue becomes healthy again)
3026// 3. tap again -> block on ACTION_UP again -> receive ANR second time
3027TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
3028 tapOnWindow();
3029
3030 mWindow->consumeMotionDown();
3031 // Block on ACTION_UP
3032 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
3033 mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/, mWindow->getToken());
3034 mWindow->consumeMotionUp(); // Now the connection should be healthy again
3035 mDispatcher->waitForIdle();
3036 mWindow->assertNoEvents();
3037
3038 tapOnWindow();
3039 mWindow->consumeMotionDown();
3040 mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/, mWindow->getToken());
3041 mWindow->consumeMotionUp();
3042
3043 mDispatcher->waitForIdle();
3044 mWindow->assertNoEvents();
3045}
3046
3047// If the policy tells us to raise ANR again after some time, ensure that the timeout extension
3048// is honored
3049TEST_F(InputDispatcherSingleWindowAnr, Policy_CanExtendTimeout) {
3050 const std::chrono::duration timeout = 5ms;
3051 mFakePolicy->setAnrTimeout(timeout);
3052
3053 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
3054 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3055 WINDOW_LOCATION));
3056
3057 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
3058 mFakePolicy->assertNotifyAnrWasCalled(windowTimeout, nullptr /*application*/,
3059 mWindow->getToken());
3060
3061 // Since the policy wanted to extend ANR, make sure it is called again after the extension
3062 mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/, mWindow->getToken());
3063 mFakePolicy->setAnrTimeout(0ms);
3064 std::this_thread::sleep_for(windowTimeout);
3065 // We are not checking if ANR has been called, because it may have been called again by the
3066 // time we set the timeout to 0
3067
3068 // When the policy finally says stop, we should get ACTION_CANCEL
3069 mWindow->consumeMotionDown();
3070 mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL,
3071 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3072 mWindow->assertNoEvents();
3073}
3074
3075/**
3076 * If a window is processing a motion event, and then a key event comes in, the key event should
3077 * not to to the focused window until the motion is processed.
3078 *
3079 * Warning!!!
3080 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
3081 * and the injection timeout that we specify when injecting the key.
3082 * We must have the injection timeout (10ms) be smaller than
3083 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
3084 *
3085 * If that value changes, this test should also change.
3086 */
3087TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
3088 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3089 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3090
3091 tapOnWindow();
3092 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3093 ASSERT_TRUE(downSequenceNum);
3094 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3095 ASSERT_TRUE(upSequenceNum);
3096 // Don't finish the events yet, and send a key
3097 // Injection will "succeed" because we will eventually give up and send the key to the focused
3098 // window even if motions are still being processed. But because the injection timeout is short,
3099 // we will receive INJECTION_TIMED_OUT as the result.
3100
3101 int32_t result =
3102 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT,
3103 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT, 10ms);
3104 ASSERT_EQ(INPUT_EVENT_INJECTION_TIMED_OUT, result);
3105 // Key will not be sent to the window, yet, because the window is still processing events
3106 // and the key remains pending, waiting for the touch events to be processed
3107 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3108 ASSERT_FALSE(keySequenceNum);
3109
3110 std::this_thread::sleep_for(500ms);
3111 // if we wait long enough though, dispatcher will give up, and still send the key
3112 // to the focused window, even though we have not yet finished the motion event
3113 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3114 mWindow->finishEvent(*downSequenceNum);
3115 mWindow->finishEvent(*upSequenceNum);
3116}
3117
3118/**
3119 * If a window is processing a motion event, and then a key event comes in, the key event should
3120 * not go to the focused window until the motion is processed.
3121 * If then a new motion comes in, then the pending key event should be going to the currently
3122 * focused window right away.
3123 */
3124TEST_F(InputDispatcherSingleWindowAnr,
3125 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
3126 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
3127 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
3128
3129 tapOnWindow();
3130 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
3131 ASSERT_TRUE(downSequenceNum);
3132 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
3133 ASSERT_TRUE(upSequenceNum);
3134 // Don't finish the events yet, and send a key
3135 // Injection is async, so it will succeed
3136 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
3137 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
3138 ADISPLAY_ID_DEFAULT, INPUT_EVENT_INJECTION_SYNC_NONE));
3139 // At this point, key is still pending, and should not be sent to the application yet.
3140 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
3141 ASSERT_FALSE(keySequenceNum);
3142
3143 // Now tap down again. It should cause the pending key to go to the focused window right away.
3144 tapOnWindow();
3145 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
3146 // the other events yet. We can finish events in any order.
3147 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
3148 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
3149 mWindow->consumeMotionDown();
3150 mWindow->consumeMotionUp();
3151 mWindow->assertNoEvents();
3152}
3153
3154class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
3155 virtual void SetUp() override {
3156 InputDispatcherTest::SetUp();
3157
Chris Yea209fde2020-07-22 13:54:51 -07003158 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003159 mApplication->setDispatchingTimeout(10ms);
3160 mUnfocusedWindow =
3161 new FakeWindowHandle(mApplication, mDispatcher, "Unfocused", ADISPLAY_ID_DEFAULT);
3162 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
3163 // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this
3164 // window.
3165 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Michael Wright44753b12020-07-08 13:48:11 +01003166 mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3167 InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH |
3168 InputWindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003169
3170 mFocusedWindow =
3171 new FakeWindowHandle(mApplication, mDispatcher, "Focused", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05003172 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003173 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Michael Wright44753b12020-07-08 13:48:11 +01003174 mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL |
3175 InputWindowInfo::Flag::SPLIT_TOUCH);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003176
3177 // Set focused application.
3178 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07003179 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003180
3181 // Expect one focus window exist in display.
3182 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003183 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003184 mFocusedWindow->consumeFocusEvent(true);
3185 }
3186
3187 virtual void TearDown() override {
3188 InputDispatcherTest::TearDown();
3189
3190 mUnfocusedWindow.clear();
3191 mFocusedWindow.clear();
3192 }
3193
3194protected:
Chris Yea209fde2020-07-22 13:54:51 -07003195 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003196 sp<FakeWindowHandle> mUnfocusedWindow;
3197 sp<FakeWindowHandle> mFocusedWindow;
3198 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
3199 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
3200 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
3201
3202 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
3203
3204 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
3205
3206private:
3207 void tap(const PointF& location) {
3208 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
3209 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3210 location));
3211 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
3212 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3213 location));
3214 }
3215};
3216
3217// If we have 2 windows that are both unresponsive, the one with the shortest timeout
3218// should be ANR'd first.
3219TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
3220 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
3221 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3222 FOCUSED_WINDOW_LOCATION))
3223 << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED";
3224 mFocusedWindow->consumeMotionDown();
3225 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3226 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3227 // We consumed all events, so no ANR
3228 ASSERT_TRUE(mDispatcher->waitForIdle());
3229 mFakePolicy->assertNotifyAnrWasNotCalled();
3230
3231 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
3232 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3233 FOCUSED_WINDOW_LOCATION));
3234 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
3235 ASSERT_TRUE(unfocusedSequenceNum);
3236 std::optional<uint32_t> focusedSequenceNum = mFocusedWindow->receiveEvent();
3237 ASSERT_TRUE(focusedSequenceNum);
3238
3239 const std::chrono::duration timeout =
3240 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
3241 mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/,
3242 mFocusedWindow->getToken());
3243
3244 mFocusedWindow->finishEvent(*focusedSequenceNum);
3245 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
3246 ASSERT_TRUE(mDispatcher->waitForIdle());
3247}
3248
3249// If we have 2 windows with identical timeouts that are both unresponsive,
3250// it doesn't matter which order they should have ANR.
3251// But we should receive ANR for both.
3252TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
3253 // Set the timeout for unfocused window to match the focused window
3254 mUnfocusedWindow->setDispatchingTimeout(10ms);
3255 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
3256
3257 tapOnFocusedWindow();
3258 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Chris Yea209fde2020-07-22 13:54:51 -07003259 std::pair<std::shared_ptr<InputApplicationHandle>, sp<IBinder>> anrData1 =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003260 mFakePolicy->getNotifyAnrData(10ms);
Chris Yea209fde2020-07-22 13:54:51 -07003261 std::pair<std::shared_ptr<InputApplicationHandle>, sp<IBinder>> anrData2 =
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003262 mFakePolicy->getNotifyAnrData(0ms);
3263
3264 // We don't know which window will ANR first. But both of them should happen eventually.
3265 ASSERT_TRUE(mFocusedWindow->getToken() == anrData1.second ||
3266 mFocusedWindow->getToken() == anrData2.second);
3267 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrData1.second ||
3268 mUnfocusedWindow->getToken() == anrData2.second);
3269
3270 ASSERT_TRUE(mDispatcher->waitForIdle());
3271 mFakePolicy->assertNotifyAnrWasNotCalled();
3272}
3273
3274// If a window is already not responding, the second tap on the same window should be ignored.
3275// We should also log an error to account for the dropped event (not tested here).
3276// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
3277TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
3278 tapOnFocusedWindow();
3279 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3280 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3281 // Receive the events, but don't respond
3282 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
3283 ASSERT_TRUE(downEventSequenceNum);
3284 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
3285 ASSERT_TRUE(upEventSequenceNum);
3286 const std::chrono::duration timeout =
3287 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
3288 mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/,
3289 mFocusedWindow->getToken());
3290
3291 // Tap once again
3292 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
3293 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED,
3294 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3295 FOCUSED_WINDOW_LOCATION));
3296 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED,
3297 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3298 FOCUSED_WINDOW_LOCATION));
3299 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
3300 // valid touch target
3301 mUnfocusedWindow->assertNoEvents();
3302
3303 // Consume the first tap
3304 mFocusedWindow->finishEvent(*downEventSequenceNum);
3305 mFocusedWindow->finishEvent(*upEventSequenceNum);
3306 ASSERT_TRUE(mDispatcher->waitForIdle());
3307 // The second tap did not go to the focused window
3308 mFocusedWindow->assertNoEvents();
3309 // should not have another ANR after the window just became healthy again
3310 mFakePolicy->assertNotifyAnrWasNotCalled();
3311}
3312
3313// If you tap outside of all windows, there will not be ANR
3314TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
3315 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED,
3316 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3317 LOCATION_OUTSIDE_ALL_WINDOWS));
3318 ASSERT_TRUE(mDispatcher->waitForIdle());
3319 mFakePolicy->assertNotifyAnrWasNotCalled();
3320}
3321
3322// Since the focused window is paused, tapping on it should not produce any events
3323TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
3324 mFocusedWindow->setPaused(true);
3325 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
3326
3327 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED,
3328 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3329 FOCUSED_WINDOW_LOCATION));
3330
3331 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
3332 ASSERT_TRUE(mDispatcher->waitForIdle());
3333 // Should not ANR because the window is paused, and touches shouldn't go to it
3334 mFakePolicy->assertNotifyAnrWasNotCalled();
3335
3336 mFocusedWindow->assertNoEvents();
3337 mUnfocusedWindow->assertNoEvents();
3338}
3339
3340/**
3341 * If a window is processing a motion event, and then a key event comes in, the key event should
3342 * not to to the focused window until the motion is processed.
3343 * If a different window becomes focused at this time, the key should go to that window instead.
3344 *
3345 * Warning!!!
3346 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
3347 * and the injection timeout that we specify when injecting the key.
3348 * We must have the injection timeout (10ms) be smaller than
3349 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
3350 *
3351 * If that value changes, this test should also change.
3352 */
3353TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
3354 // Set a long ANR timeout to prevent it from triggering
3355 mFocusedWindow->setDispatchingTimeout(2s);
3356 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
3357
3358 tapOnUnfocusedWindow();
3359 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
3360 ASSERT_TRUE(downSequenceNum);
3361 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
3362 ASSERT_TRUE(upSequenceNum);
3363 // Don't finish the events yet, and send a key
3364 // Injection will succeed because we will eventually give up and send the key to the focused
3365 // window even if motions are still being processed.
3366
3367 int32_t result =
3368 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT,
3369 INPUT_EVENT_INJECTION_SYNC_NONE, 10ms /*injectionTimeout*/);
3370 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, result);
3371 // Key will not be sent to the window, yet, because the window is still processing events
3372 // and the key remains pending, waiting for the touch events to be processed
3373 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
3374 ASSERT_FALSE(keySequenceNum);
3375
3376 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07003377 mFocusedWindow->setFocusable(false);
3378 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003379 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003380 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003381
3382 // Focus events should precede the key events
3383 mUnfocusedWindow->consumeFocusEvent(true);
3384 mFocusedWindow->consumeFocusEvent(false);
3385
3386 // Finish the tap events, which should unblock dispatcher
3387 mUnfocusedWindow->finishEvent(*downSequenceNum);
3388 mUnfocusedWindow->finishEvent(*upSequenceNum);
3389
3390 // Now that all queues are cleared and no backlog in the connections, the key event
3391 // can finally go to the newly focused "mUnfocusedWindow".
3392 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3393 mFocusedWindow->assertNoEvents();
3394 mUnfocusedWindow->assertNoEvents();
3395}
3396
3397// When the touch stream is split across 2 windows, and one of them does not respond,
3398// then ANR should be raised and the touch should be canceled for the unresponsive window.
3399// The other window should not be affected by that.
3400TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
3401 // Touch Window 1
3402 NotifyMotionArgs motionArgs =
3403 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3404 ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION});
3405 mDispatcher->notifyMotion(&motionArgs);
3406 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
3407 ADISPLAY_ID_DEFAULT, 0 /*flags*/);
3408
3409 // Touch Window 2
3410 int32_t actionPointerDown =
3411 AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
3412
3413 motionArgs =
3414 generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3415 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION});
3416 mDispatcher->notifyMotion(&motionArgs);
3417
3418 const std::chrono::duration timeout =
3419 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
3420 mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/,
3421 mFocusedWindow->getToken());
3422
3423 mUnfocusedWindow->consumeMotionDown();
3424 mFocusedWindow->consumeMotionDown();
3425 // Focused window may or may not receive ACTION_MOVE
3426 // But it should definitely receive ACTION_CANCEL due to the ANR
3427 InputEvent* event;
3428 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
3429 ASSERT_TRUE(moveOrCancelSequenceNum);
3430 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
3431 ASSERT_NE(nullptr, event);
3432 ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION);
3433 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
3434 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
3435 mFocusedWindow->consumeMotionCancel();
3436 } else {
3437 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
3438 }
3439
3440 ASSERT_TRUE(mDispatcher->waitForIdle());
3441 mUnfocusedWindow->assertNoEvents();
3442 mFocusedWindow->assertNoEvents();
3443}
3444
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05003445// These tests ensure we cannot send touch events to a window that's positioned behind a window
3446// that has feature NO_INPUT_CHANNEL.
3447// Layout:
3448// Top (closest to user)
3449// mNoInputWindow (above all windows)
3450// mBottomWindow
3451// Bottom (furthest from user)
3452class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
3453 virtual void SetUp() override {
3454 InputDispatcherTest::SetUp();
3455
3456 mApplication = std::make_shared<FakeApplicationHandle>();
3457 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
3458 "Window without input channel", ADISPLAY_ID_DEFAULT,
3459 std::make_optional<sp<IBinder>>(nullptr) /*token*/);
3460
3461 mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL);
3462 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
3463 // It's perfectly valid for this window to not have an associated input channel
3464
3465 mBottomWindow = new FakeWindowHandle(mApplication, mDispatcher, "Bottom window",
3466 ADISPLAY_ID_DEFAULT);
3467 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
3468
3469 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
3470 }
3471
3472protected:
3473 std::shared_ptr<FakeApplicationHandle> mApplication;
3474 sp<FakeWindowHandle> mNoInputWindow;
3475 sp<FakeWindowHandle> mBottomWindow;
3476};
3477
3478TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
3479 PointF touchedPoint = {10, 10};
3480
3481 NotifyMotionArgs motionArgs =
3482 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3483 ADISPLAY_ID_DEFAULT, {touchedPoint});
3484 mDispatcher->notifyMotion(&motionArgs);
3485
3486 mNoInputWindow->assertNoEvents();
3487 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
3488 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
3489 // and therefore should prevent mBottomWindow from receiving touches
3490 mBottomWindow->assertNoEvents();
3491}
3492
3493/**
3494 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
3495 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
3496 */
3497TEST_F(InputDispatcherMultiWindowOcclusionTests,
3498 NoInputChannelFeature_DropsTouchesWithValidChannel) {
3499 mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher,
3500 "Window with input channel and NO_INPUT_CHANNEL",
3501 ADISPLAY_ID_DEFAULT);
3502
3503 mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL);
3504 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
3505 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
3506
3507 PointF touchedPoint = {10, 10};
3508
3509 NotifyMotionArgs motionArgs =
3510 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3511 ADISPLAY_ID_DEFAULT, {touchedPoint});
3512 mDispatcher->notifyMotion(&motionArgs);
3513
3514 mNoInputWindow->assertNoEvents();
3515 mBottomWindow->assertNoEvents();
3516}
3517
Vishnu Nair958da932020-08-21 17:12:37 -07003518class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
3519protected:
3520 std::shared_ptr<FakeApplicationHandle> mApp;
3521 sp<FakeWindowHandle> mWindow;
3522 sp<FakeWindowHandle> mMirror;
3523
3524 virtual void SetUp() override {
3525 InputDispatcherTest::SetUp();
3526 mApp = std::make_shared<FakeApplicationHandle>();
3527 mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
3528 mMirror = new FakeWindowHandle(mApp, mDispatcher, "TestWindowMirror", ADISPLAY_ID_DEFAULT,
3529 mWindow->getToken());
3530 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
3531 mWindow->setFocusable(true);
3532 mMirror->setFocusable(true);
3533 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
3534 }
3535};
3536
3537TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
3538 // Request focus on a mirrored window
3539 setFocusedWindow(mMirror);
3540
3541 // window gets focused
3542 mWindow->consumeFocusEvent(true);
3543 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
3544 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
3545 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
3546}
3547
3548// A focused & mirrored window remains focused only if the window and its mirror are both
3549// focusable.
3550TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
3551 setFocusedWindow(mMirror);
3552
3553 // window gets focused
3554 mWindow->consumeFocusEvent(true);
3555 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
3556 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
3557 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
3558 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyUp(mDispatcher))
3559 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
3560 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
3561
3562 mMirror->setFocusable(false);
3563 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
3564
3565 // window loses focus since one of the windows associated with the token in not focusable
3566 mWindow->consumeFocusEvent(false);
3567
3568 ASSERT_EQ(INPUT_EVENT_INJECTION_TIMED_OUT, injectKeyDown(mDispatcher))
3569 << "Inject key event should return INPUT_EVENT_INJECTION_TIMED_OUT";
3570 mWindow->assertNoEvents();
3571}
3572
3573// A focused & mirrored window remains focused until the window and its mirror both become
3574// invisible.
3575TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
3576 setFocusedWindow(mMirror);
3577
3578 // window gets focused
3579 mWindow->consumeFocusEvent(true);
3580 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
3581 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
3582 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
3583 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyUp(mDispatcher))
3584 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
3585 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
3586
3587 mMirror->setVisible(false);
3588 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
3589
3590 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
3591 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
3592 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
3593 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyUp(mDispatcher))
3594 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
3595 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
3596
3597 mWindow->setVisible(false);
3598 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
3599
3600 // window loses focus only after all windows associated with the token become invisible.
3601 mWindow->consumeFocusEvent(false);
3602
3603 ASSERT_EQ(INPUT_EVENT_INJECTION_TIMED_OUT, injectKeyDown(mDispatcher))
3604 << "Inject key event should return INPUT_EVENT_INJECTION_TIMED_OUT";
3605 mWindow->assertNoEvents();
3606}
3607
3608// A focused & mirrored window remains focused until both windows are removed.
3609TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
3610 setFocusedWindow(mMirror);
3611
3612 // window gets focused
3613 mWindow->consumeFocusEvent(true);
3614 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
3615 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
3616 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
3617 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyUp(mDispatcher))
3618 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
3619 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
3620
3621 // single window is removed but the window token remains focused
3622 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
3623
3624 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher))
3625 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
3626 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
3627 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyUp(mDispatcher))
3628 << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED";
3629 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
3630
3631 // Both windows are removed
3632 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
3633 mWindow->consumeFocusEvent(false);
3634
3635 ASSERT_EQ(INPUT_EVENT_INJECTION_TIMED_OUT, injectKeyDown(mDispatcher))
3636 << "Inject key event should return INPUT_EVENT_INJECTION_TIMED_OUT";
3637 mWindow->assertNoEvents();
3638}
3639
3640// Focus request can be pending until one window becomes visible.
3641TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
3642 // Request focus on an invisible mirror.
3643 mWindow->setVisible(false);
3644 mMirror->setVisible(false);
3645 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
3646 setFocusedWindow(mMirror);
3647
3648 // Injected key goes to pending queue.
3649 ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED,
3650 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */,
3651 ADISPLAY_ID_DEFAULT, INPUT_EVENT_INJECTION_SYNC_NONE));
3652
3653 mMirror->setVisible(true);
3654 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
3655
3656 // window gets focused
3657 mWindow->consumeFocusEvent(true);
3658 // window gets the pending key event
3659 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3660}
Garfield Tane84e6f92019-08-29 17:28:41 -07003661} // namespace android::inputdispatcher