blob: 8b74b2524d04be253a6a44f490f30bda1e389cc9 [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"
Prabir Pradhan8ede1d12023-05-08 19:37:44 +000018#include "../BlockingQueue.h"
Prabir Pradhan9205e422023-05-16 20:06:13 +000019#include "EventBuilders.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080020
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -070021#include <android-base/properties.h>
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080022#include <android-base/silent_death_test.h>
Garfield Tan1c7bc862020-01-28 13:24:04 -080023#include <android-base/stringprintf.h>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070024#include <android-base/thread_annotations.h>
Robert Carr803535b2018-08-02 16:38:15 -070025#include <binder/Binder.h>
Michael Wright8e9a8562022-02-09 13:44:29 +000026#include <fcntl.h>
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -080027#include <gmock/gmock.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080028#include <gtest/gtest.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100029#include <input/Input.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080030#include <linux/input.h>
Prabir Pradhan07e05b62021-11-19 03:57:24 -080031#include <sys/epoll.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100032
Garfield Tan1c7bc862020-01-28 13:24:04 -080033#include <cinttypes>
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080034#include <compare>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070035#include <thread>
Garfield Tan1c7bc862020-01-28 13:24:04 -080036#include <unordered_set>
chaviwd1c23182019-12-20 18:44:56 -080037#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080038
Garfield Tan1c7bc862020-01-28 13:24:04 -080039using android::base::StringPrintf;
chaviw3277faf2021-05-19 16:45:23 -050040using android::gui::FocusRequest;
41using android::gui::TouchOcclusionMode;
42using android::gui::WindowInfo;
43using android::gui::WindowInfoHandle;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080044using android::os::InputEventInjectionResult;
45using android::os::InputEventInjectionSync;
Garfield Tan1c7bc862020-01-28 13:24:04 -080046
Garfield Tane84e6f92019-08-29 17:28:41 -070047namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080048
Dominik Laskowski2f01d772022-03-23 16:01:29 -070049using namespace ftl::flag_operators;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -080050using testing::AllOf;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070051
Michael Wrightd02c5b62014-02-10 15:10:22 -080052// An arbitrary time value.
Prabir Pradhan5735a322022-04-11 17:23:34 +000053static constexpr nsecs_t ARBITRARY_TIME = 1234;
Michael Wrightd02c5b62014-02-10 15:10:22 -080054
55// An arbitrary device id.
Prabir Pradhan9205e422023-05-16 20:06:13 +000056static constexpr int32_t DEVICE_ID = DEFAULT_DEVICE_ID;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -080057static constexpr int32_t SECOND_DEVICE_ID = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -080058
Jeff Brownf086ddb2014-02-11 14:28:48 -080059// An arbitrary display id.
Arthur Hungabbb9d82021-09-01 14:52:30 +000060static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
61static constexpr int32_t SECOND_DISPLAY_ID = 1;
Jeff Brownf086ddb2014-02-11 14:28:48 -080062
Prabir Pradhan8ede1d12023-05-08 19:37:44 +000063// Ensure common actions are interchangeable between keys and motions for convenience.
64static_assert(AMOTION_EVENT_ACTION_DOWN == AKEY_EVENT_ACTION_DOWN);
65static_assert(AMOTION_EVENT_ACTION_UP == AKEY_EVENT_ACTION_UP);
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080066static constexpr int32_t ACTION_DOWN = AMOTION_EVENT_ACTION_DOWN;
67static constexpr int32_t ACTION_MOVE = AMOTION_EVENT_ACTION_MOVE;
68static constexpr int32_t ACTION_UP = AMOTION_EVENT_ACTION_UP;
69static constexpr int32_t ACTION_HOVER_ENTER = AMOTION_EVENT_ACTION_HOVER_ENTER;
Siarhei Vishniakoua235c042023-05-02 09:59:09 -070070static constexpr int32_t ACTION_HOVER_MOVE = AMOTION_EVENT_ACTION_HOVER_MOVE;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080071static constexpr int32_t ACTION_HOVER_EXIT = AMOTION_EVENT_ACTION_HOVER_EXIT;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080072static constexpr int32_t ACTION_OUTSIDE = AMOTION_EVENT_ACTION_OUTSIDE;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -080073static constexpr int32_t ACTION_CANCEL = AMOTION_EVENT_ACTION_CANCEL;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080074/**
75 * The POINTER_DOWN(0) is an unusual, but valid, action. It just means that the new pointer in the
76 * MotionEvent is at the index 0 rather than 1 (or later). That is, the pointer id=0 (which is at
77 * index 0) is the new pointer going down. The same pointer could have been placed at a different
78 * index, and the action would become POINTER_1_DOWN, 2, etc..; these would all be valid. In
79 * general, we try to place pointer id = 0 at the index 0. Of course, this is not possible if
80 * pointer id=0 leaves but the pointer id=1 remains.
81 */
82static constexpr int32_t POINTER_0_DOWN =
83 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080084static constexpr int32_t POINTER_1_DOWN =
85 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +000086static constexpr int32_t POINTER_2_DOWN =
87 AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +000088static constexpr int32_t POINTER_3_DOWN =
89 AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Arthur Hungc539dbb2022-12-08 07:45:36 +000090static constexpr int32_t POINTER_0_UP =
91 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080092static constexpr int32_t POINTER_1_UP =
93 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Harry Cuttsb166c002023-05-09 13:06:05 +000094static constexpr int32_t POINTER_2_UP =
95 AMOTION_EVENT_ACTION_POINTER_UP | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080096
Antonio Kantek15beb512022-06-13 22:35:41 +000097// The default pid and uid for windows created on the primary display by the test.
Prabir Pradhan5735a322022-04-11 17:23:34 +000098static constexpr int32_t WINDOW_PID = 999;
99static constexpr int32_t WINDOW_UID = 1001;
100
Antonio Kantek15beb512022-06-13 22:35:41 +0000101// The default pid and uid for the windows created on the secondary display by the test.
102static constexpr int32_t SECONDARY_WINDOW_PID = 1010;
103static constexpr int32_t SECONDARY_WINDOW_UID = 1012;
104
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +0000105// An arbitrary pid of the gesture monitor window
106static constexpr int32_t MONITOR_PID = 2001;
107
Siarhei Vishniakou289e9242022-02-15 14:50:16 -0800108static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms;
109
Arthur Hungc539dbb2022-12-08 07:45:36 +0000110static constexpr int expectedWallpaperFlags =
111 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
112
Siarhei Vishniakou56e79092023-02-21 19:13:16 -0800113using ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID;
114
chaviwd1c23182019-12-20 18:44:56 -0800115struct PointF {
116 float x;
117 float y;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800118 auto operator<=>(const PointF&) const = default;
chaviwd1c23182019-12-20 18:44:56 -0800119};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800120
Gang Wang342c9272020-01-13 13:15:04 -0500121/**
122 * Return a DOWN key event with KEYCODE_A.
123 */
124static KeyEvent getTestKeyEvent() {
125 KeyEvent event;
126
Garfield Tanfbe732e2020-01-24 11:26:14 -0800127 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
128 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
129 ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -0500130 return event;
131}
132
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000133static void assertMotionAction(int32_t expectedAction, int32_t receivedAction) {
134 ASSERT_EQ(expectedAction, receivedAction)
135 << "expected " << MotionEvent::actionToString(expectedAction) << ", got "
136 << MotionEvent::actionToString(receivedAction);
137}
138
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800139MATCHER_P(WithMotionAction, action, "MotionEvent with specified action") {
140 bool matches = action == arg.getAction();
141 if (!matches) {
142 *result_listener << "expected action " << MotionEvent::actionToString(action)
143 << ", but got " << MotionEvent::actionToString(arg.getAction());
144 }
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -0800145 if (action == AMOTION_EVENT_ACTION_DOWN) {
146 if (!matches) {
147 *result_listener << "; ";
148 }
149 *result_listener << "downTime should match eventTime for ACTION_DOWN events";
150 matches &= arg.getDownTime() == arg.getEventTime();
151 }
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800152 if (action == AMOTION_EVENT_ACTION_CANCEL) {
153 if (!matches) {
154 *result_listener << "; ";
155 }
156 *result_listener << "expected FLAG_CANCELED to be set with ACTION_CANCEL, but was not set";
157 matches &= (arg.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0;
158 }
159 return matches;
160}
161
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -0800162MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") {
163 return arg.getDownTime() == downTime;
164}
165
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800166MATCHER_P(WithDisplayId, displayId, "InputEvent with specified displayId") {
167 return arg.getDisplayId() == displayId;
168}
169
Siarhei Vishniakouf372b812023-02-14 18:06:51 -0800170MATCHER_P(WithDeviceId, deviceId, "InputEvent with specified deviceId") {
171 return arg.getDeviceId() == deviceId;
172}
173
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800174MATCHER_P(WithSource, source, "InputEvent with specified source") {
175 *result_listener << "expected source " << inputEventSourceToString(source) << ", but got "
176 << inputEventSourceToString(arg.getSource());
177 return arg.getSource() == source;
178}
179
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800180MATCHER_P(WithFlags, flags, "InputEvent with specified flags") {
181 return arg.getFlags() == flags;
182}
183
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800184MATCHER_P2(WithCoords, x, y, "MotionEvent with specified coordinates") {
185 if (arg.getPointerCount() != 1) {
186 *result_listener << "Expected 1 pointer, got " << arg.getPointerCount();
187 return false;
188 }
Harry Cutts33476232023-01-30 19:57:29 +0000189 return arg.getX(/*pointerIndex=*/0) == x && arg.getY(/*pointerIndex=*/0) == y;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800190}
191
Siarhei Vishniakouf372b812023-02-14 18:06:51 -0800192MATCHER_P(WithPointerCount, pointerCount, "MotionEvent with specified number of pointers") {
193 return arg.getPointerCount() == pointerCount;
194}
195
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800196MATCHER_P(WithPointers, pointers, "MotionEvent with specified pointers") {
197 // Build a map for the received pointers, by pointer id
198 std::map<int32_t /*pointerId*/, PointF> actualPointers;
199 for (size_t pointerIndex = 0; pointerIndex < arg.getPointerCount(); pointerIndex++) {
200 const int32_t pointerId = arg.getPointerId(pointerIndex);
201 actualPointers[pointerId] = {arg.getX(pointerIndex), arg.getY(pointerIndex)};
202 }
203 return pointers == actualPointers;
204}
205
Michael Wrightd02c5b62014-02-10 15:10:22 -0800206// --- FakeInputDispatcherPolicy ---
207
208class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
209 InputDispatcherConfiguration mConfig;
210
Prabir Pradhanedd96402022-02-15 01:46:16 -0800211 using AnrResult = std::pair<sp<IBinder>, int32_t /*pid*/>;
212
Michael Wrightd02c5b62014-02-10 15:10:22 -0800213public:
Prabir Pradhana41d2442023-04-20 21:30:40 +0000214 FakeInputDispatcherPolicy() = default;
215 virtual ~FakeInputDispatcherPolicy() = default;
Jackal Guof9696682018-10-05 12:23:23 +0800216
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800217 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700218 assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700219 ASSERT_EQ(event.getType(), InputEventType::KEY);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700220 EXPECT_EQ(event.getDisplayId(), args.displayId);
221
222 const auto& keyEvent = static_cast<const KeyEvent&>(event);
223 EXPECT_EQ(keyEvent.getEventTime(), args.eventTime);
224 EXPECT_EQ(keyEvent.getAction(), args.action);
225 });
Jackal Guof9696682018-10-05 12:23:23 +0800226 }
227
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700228 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) {
229 assertFilterInputEventWasCalledInternal([&](const InputEvent& event) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700230 ASSERT_EQ(event.getType(), InputEventType::MOTION);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700231 EXPECT_EQ(event.getDisplayId(), args.displayId);
232
233 const auto& motionEvent = static_cast<const MotionEvent&>(event);
234 EXPECT_EQ(motionEvent.getEventTime(), args.eventTime);
235 EXPECT_EQ(motionEvent.getAction(), args.action);
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000236 EXPECT_NEAR(motionEvent.getX(0), point.x, MotionEvent::ROUNDING_PRECISION);
237 EXPECT_NEAR(motionEvent.getY(0), point.y, MotionEvent::ROUNDING_PRECISION);
238 EXPECT_NEAR(motionEvent.getRawX(0), point.x, MotionEvent::ROUNDING_PRECISION);
239 EXPECT_NEAR(motionEvent.getRawY(0), point.y, MotionEvent::ROUNDING_PRECISION);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700240 });
Jackal Guof9696682018-10-05 12:23:23 +0800241 }
242
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700243 void assertFilterInputEventWasNotCalled() {
244 std::scoped_lock lock(mLock);
245 ASSERT_EQ(nullptr, mFilteredEvent);
246 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800247
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800248 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700249 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800250 ASSERT_TRUE(mConfigurationChangedTime)
251 << "Timed out waiting for configuration changed call";
252 ASSERT_EQ(*mConfigurationChangedTime, when);
253 mConfigurationChangedTime = std::nullopt;
254 }
255
256 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700257 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800258 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800259 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800260 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
261 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
262 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
263 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
264 mLastNotifySwitch = std::nullopt;
265 }
266
chaviwfd6d3512019-03-25 13:23:49 -0700267 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700268 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800269 ASSERT_EQ(touchedToken, mOnPointerDownToken);
270 mOnPointerDownToken.clear();
271 }
272
273 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700274 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800275 ASSERT_TRUE(mOnPointerDownToken == nullptr)
276 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700277 }
278
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700279 // This function must be called soon after the expected ANR timer starts,
280 // because we are also checking how much time has passed.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500281 void assertNotifyNoFocusedWindowAnrWasCalled(
Chris Yea209fde2020-07-22 13:54:51 -0700282 std::chrono::nanoseconds timeout,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500283 const std::shared_ptr<InputApplicationHandle>& expectedApplication) {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800284 std::unique_lock lock(mLock);
285 android::base::ScopedLockAssertion assumeLocked(mLock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500286 std::shared_ptr<InputApplicationHandle> application;
Prabir Pradhanedd96402022-02-15 01:46:16 -0800287 ASSERT_NO_FATAL_FAILURE(
288 application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock));
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500289 ASSERT_EQ(expectedApplication, application);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700290 }
291
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000292 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
Prabir Pradhanedd96402022-02-15 01:46:16 -0800293 const sp<WindowInfoHandle>& window) {
294 LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null");
295 assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(),
296 window->getInfo()->ownerPid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500297 }
298
Prabir Pradhanedd96402022-02-15 01:46:16 -0800299 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
300 const sp<IBinder>& expectedToken,
301 int32_t expectedPid) {
302 std::unique_lock lock(mLock);
303 android::base::ScopedLockAssertion assumeLocked(mLock);
304 AnrResult result;
305 ASSERT_NO_FATAL_FAILURE(result =
306 getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock));
307 const auto& [token, pid] = result;
308 ASSERT_EQ(expectedToken, token);
309 ASSERT_EQ(expectedPid, pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500310 }
311
Prabir Pradhanedd96402022-02-15 01:46:16 -0800312 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000313 sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500314 std::unique_lock lock(mLock);
315 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800316 AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock);
317 const auto& [token, _] = result;
318 return token;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000319 }
320
Prabir Pradhanedd96402022-02-15 01:46:16 -0800321 void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken,
322 int32_t expectedPid) {
323 std::unique_lock lock(mLock);
324 android::base::ScopedLockAssertion assumeLocked(mLock);
325 AnrResult result;
326 ASSERT_NO_FATAL_FAILURE(
327 result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock));
328 const auto& [token, pid] = result;
329 ASSERT_EQ(expectedToken, token);
330 ASSERT_EQ(expectedPid, pid);
331 }
332
333 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000334 sp<IBinder> getResponsiveWindowToken() {
335 std::unique_lock lock(mLock);
336 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800337 AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock);
338 const auto& [token, _] = result;
339 return token;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700340 }
341
342 void assertNotifyAnrWasNotCalled() {
343 std::scoped_lock lock(mLock);
344 ASSERT_TRUE(mAnrApplications.empty());
Prabir Pradhanedd96402022-02-15 01:46:16 -0800345 ASSERT_TRUE(mAnrWindows.empty());
346 ASSERT_TRUE(mResponsiveWindows.empty())
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500347 << "ANR was not called, but please also consume the 'connection is responsive' "
348 "signal";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700349 }
350
Garfield Tan1c7bc862020-01-28 13:24:04 -0800351 void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) {
352 mConfig.keyRepeatTimeout = timeout;
353 mConfig.keyRepeatDelay = delay;
354 }
355
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000356 PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -0800357 std::unique_lock lock(mLock);
358 base::ScopedLockAssertion assumeLocked(mLock);
359
360 if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms,
361 [this, enabled]() REQUIRES(mLock) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000362 return mPointerCaptureRequest->enable ==
Prabir Pradhan99987712020-11-10 18:43:05 -0800363 enabled;
364 })) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000365 ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled
366 << ") to be called.";
367 return {};
Prabir Pradhan99987712020-11-10 18:43:05 -0800368 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000369 auto request = *mPointerCaptureRequest;
370 mPointerCaptureRequest.reset();
371 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -0800372 }
373
374 void assertSetPointerCaptureNotCalled() {
375 std::unique_lock lock(mLock);
376 base::ScopedLockAssertion assumeLocked(mLock);
377
378 if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000379 FAIL() << "Expected setPointerCapture(request) to not be called, but was called. "
Prabir Pradhan99987712020-11-10 18:43:05 -0800380 "enabled = "
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000381 << std::to_string(mPointerCaptureRequest->enable);
Prabir Pradhan99987712020-11-10 18:43:05 -0800382 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000383 mPointerCaptureRequest.reset();
Prabir Pradhan99987712020-11-10 18:43:05 -0800384 }
385
arthurhungf452d0b2021-01-06 00:19:52 +0800386 void assertDropTargetEquals(const sp<IBinder>& targetToken) {
387 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800388 ASSERT_TRUE(mNotifyDropWindowWasCalled);
arthurhungf452d0b2021-01-06 00:19:52 +0800389 ASSERT_EQ(targetToken, mDropTargetWindowToken);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800390 mNotifyDropWindowWasCalled = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800391 }
392
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800393 void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) {
394 std::unique_lock lock(mLock);
395 base::ScopedLockAssertion assumeLocked(mLock);
396 std::optional<sp<IBinder>> receivedToken =
397 getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock,
398 mNotifyInputChannelBroken);
399 ASSERT_TRUE(receivedToken.has_value());
400 ASSERT_EQ(token, *receivedToken);
401 }
402
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800403 /**
404 * Set policy timeout. A value of zero means next key will not be intercepted.
405 */
406 void setInterceptKeyTimeout(std::chrono::milliseconds timeout) {
407 mInterceptKeyTimeout = timeout;
408 }
409
Josep del Riob3981622023-04-18 15:49:45 +0000410 void assertUserActivityPoked() {
411 std::scoped_lock lock(mLock);
412 ASSERT_TRUE(mPokedUserActivity) << "Expected user activity to have been poked";
413 }
414
415 void assertUserActivityNotPoked() {
416 std::scoped_lock lock(mLock);
417 ASSERT_FALSE(mPokedUserActivity) << "Expected user activity not to have been poked";
418 }
419
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000420 void assertNotifyDeviceInteractionWasCalled(int32_t deviceId, std::set<int32_t> uids) {
421 ASSERT_EQ(std::make_pair(deviceId, uids), mNotifiedInteractions.popWithTimeout(100ms));
422 }
423
424 void assertNotifyDeviceInteractionWasNotCalled() {
425 ASSERT_FALSE(mNotifiedInteractions.popWithTimeout(10ms));
426 }
427
Michael Wrightd02c5b62014-02-10 15:10:22 -0800428private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700429 std::mutex mLock;
430 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
431 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
432 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
433 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800434
Prabir Pradhan99987712020-11-10 18:43:05 -0800435 std::condition_variable mPointerCaptureChangedCondition;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000436
437 std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock);
Prabir Pradhan99987712020-11-10 18:43:05 -0800438
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700439 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700440 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800441 std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock);
442 std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700443 std::condition_variable mNotifyAnr;
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800444 std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock);
445 std::condition_variable mNotifyInputChannelBroken;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700446
arthurhungf452d0b2021-01-06 00:19:52 +0800447 sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800448 bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false;
Josep del Riob3981622023-04-18 15:49:45 +0000449 bool mPokedUserActivity GUARDED_BY(mLock) = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800450
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800451 std::chrono::milliseconds mInterceptKeyTimeout = 0ms;
452
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000453 BlockingQueue<std::pair<int32_t /*deviceId*/, std::set<int32_t /*uid*/>>> mNotifiedInteractions;
454
Prabir Pradhanedd96402022-02-15 01:46:16 -0800455 // All three ANR-related callbacks behave the same way, so we use this generic function to wait
456 // for a specific container to become non-empty. When the container is non-empty, return the
457 // first entry from the container and erase it.
458 template <class T>
459 T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage,
460 std::unique_lock<std::mutex>& lock) REQUIRES(mLock) {
461 // If there is an ANR, Dispatcher won't be idle because there are still events
462 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
463 // before checking if ANR was called.
464 // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need
465 // to provide it some time to act. 100ms seems reasonable.
466 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
467 const std::chrono::time_point start = std::chrono::steady_clock::now();
468 std::optional<T> token =
469 getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr);
470 if (!token.has_value()) {
471 ADD_FAILURE() << "Did not receive the ANR callback";
472 return {};
473 }
474
475 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
476 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
477 // the dispatcher started counting before this function was called
478 if (std::chrono::abs(timeout - waited) > 100ms) {
479 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
480 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
481 << "ms, but waited "
482 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
483 << "ms instead";
484 }
485 return *token;
486 }
487
488 template <class T>
489 std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout,
490 std::queue<T>& storage,
491 std::unique_lock<std::mutex>& lock,
492 std::condition_variable& condition)
493 REQUIRES(mLock) {
494 condition.wait_for(lock, timeout,
495 [&storage]() REQUIRES(mLock) { return !storage.empty(); });
496 if (storage.empty()) {
497 ADD_FAILURE() << "Did not receive the expected callback";
498 return std::nullopt;
499 }
500 T item = storage.front();
501 storage.pop();
502 return std::make_optional(item);
503 }
504
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600505 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700506 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800507 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800508 }
509
Prabir Pradhanedd96402022-02-15 01:46:16 -0800510 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<int32_t> pid,
511 const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700512 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800513 ASSERT_TRUE(pid.has_value());
514 mAnrWindows.push({connectionToken, *pid});
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700515 mNotifyAnr.notify_all();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500516 }
517
Prabir Pradhanedd96402022-02-15 01:46:16 -0800518 void notifyWindowResponsive(const sp<IBinder>& connectionToken,
519 std::optional<int32_t> pid) override {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500520 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800521 ASSERT_TRUE(pid.has_value());
522 mResponsiveWindows.push({connectionToken, *pid});
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500523 mNotifyAnr.notify_all();
524 }
525
526 void notifyNoFocusedWindowAnr(
527 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
528 std::scoped_lock lock(mLock);
529 mAnrApplications.push(applicationHandle);
530 mNotifyAnr.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800531 }
532
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800533 void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override {
534 std::scoped_lock lock(mLock);
535 mBrokenInputChannels.push(connectionToken);
536 mNotifyInputChannelBroken.notify_all();
537 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800538
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600539 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700540
Chris Yef59a2f42020-10-16 12:55:26 -0700541 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
542 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
543 const std::vector<float>& values) override {}
544
545 void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType,
546 InputDeviceSensorAccuracy accuracy) override {}
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000547
Chris Yefb552902021-02-03 17:18:37 -0800548 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
549
Prabir Pradhana41d2442023-04-20 21:30:40 +0000550 InputDispatcherConfiguration getDispatcherConfiguration() override { return mConfig; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800551
Prabir Pradhana41d2442023-04-20 21:30:40 +0000552 bool filterInputEvent(const InputEvent& inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700553 std::scoped_lock lock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +0000554 switch (inputEvent.getType()) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700555 case InputEventType::KEY: {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000556 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(inputEvent);
557 mFilteredEvent = std::make_unique<KeyEvent>(keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800558 break;
559 }
560
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700561 case InputEventType::MOTION: {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000562 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(inputEvent);
563 mFilteredEvent = std::make_unique<MotionEvent>(motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800564 break;
565 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700566 default: {
567 ADD_FAILURE() << "Should only filter keys or motions";
568 break;
569 }
Jackal Guof9696682018-10-05 12:23:23 +0800570 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800571 return true;
572 }
573
Prabir Pradhana41d2442023-04-20 21:30:40 +0000574 void interceptKeyBeforeQueueing(const KeyEvent& inputEvent, uint32_t&) override {
575 if (inputEvent.getAction() == AKEY_EVENT_ACTION_UP) {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800576 // Clear intercept state when we handled the event.
577 mInterceptKeyTimeout = 0ms;
578 }
579 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800580
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600581 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800582
Prabir Pradhana41d2442023-04-20 21:30:40 +0000583 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&, uint32_t) override {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800584 nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count();
585 // Clear intercept state so we could dispatch the event in next wake.
586 mInterceptKeyTimeout = 0ms;
587 return delay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800588 }
589
Prabir Pradhana41d2442023-04-20 21:30:40 +0000590 std::optional<KeyEvent> dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent&,
591 uint32_t) override {
592 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800593 }
594
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600595 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
596 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700597 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800598 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
599 * essentially a passthrough for notifySwitch.
600 */
Harry Cutts33476232023-01-30 19:57:29 +0000601 mLastNotifySwitch = NotifySwitchArgs(/*id=*/1, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800602 }
603
Josep del Riob3981622023-04-18 15:49:45 +0000604 void pokeUserActivity(nsecs_t, int32_t, int32_t) override {
605 std::scoped_lock lock(mLock);
606 mPokedUserActivity = true;
607 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800608
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600609 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700610 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700611 mOnPointerDownToken = newToken;
612 }
613
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000614 void setPointerCapture(const PointerCaptureRequest& request) override {
Prabir Pradhan99987712020-11-10 18:43:05 -0800615 std::scoped_lock lock(mLock);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000616 mPointerCaptureRequest = {request};
Prabir Pradhan99987712020-11-10 18:43:05 -0800617 mPointerCaptureChangedCondition.notify_all();
618 }
619
arthurhungf452d0b2021-01-06 00:19:52 +0800620 void notifyDropWindow(const sp<IBinder>& token, float x, float y) override {
621 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800622 mNotifyDropWindowWasCalled = true;
arthurhungf452d0b2021-01-06 00:19:52 +0800623 mDropTargetWindowToken = token;
624 }
625
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000626 void notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp,
627 const std::set<int32_t>& uids) override {
628 ASSERT_TRUE(mNotifiedInteractions.emplace(deviceId, uids));
629 }
630
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700631 void assertFilterInputEventWasCalledInternal(
632 const std::function<void(const InputEvent&)>& verify) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700633 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800634 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700635 verify(*mFilteredEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800636 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800637 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800638};
639
Michael Wrightd02c5b62014-02-10 15:10:22 -0800640// --- InputDispatcherTest ---
641
642class InputDispatcherTest : public testing::Test {
643protected:
Prabir Pradhana41d2442023-04-20 21:30:40 +0000644 std::unique_ptr<FakeInputDispatcherPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700645 std::unique_ptr<InputDispatcher> mDispatcher;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800646
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000647 void SetUp() override {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000648 mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>();
649 mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy, STALE_EVENT_TIMEOUT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800650 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000651 // Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700652 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800653 }
654
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000655 void TearDown() override {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700656 ASSERT_EQ(OK, mDispatcher->stop());
Prabir Pradhana41d2442023-04-20 21:30:40 +0000657 mFakePolicy.reset();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700658 mDispatcher.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800659 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700660
661 /**
662 * Used for debugging when writing the test
663 */
664 void dumpDispatcherState() {
665 std::string dump;
666 mDispatcher->dump(dump);
667 std::stringstream ss(dump);
668 std::string to;
669
670 while (std::getline(ss, to, '\n')) {
671 ALOGE("%s", to.c_str());
672 }
673 }
Vishnu Nair958da932020-08-21 17:12:37 -0700674
Chavi Weingarten847e8512023-03-29 00:26:09 +0000675 void setFocusedWindow(const sp<WindowInfoHandle>& window) {
Vishnu Nair958da932020-08-21 17:12:37 -0700676 FocusRequest request;
677 request.token = window->getToken();
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +0000678 request.windowName = window->getName();
Vishnu Nair958da932020-08-21 17:12:37 -0700679 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
680 request.displayId = window->getInfo()->displayId;
681 mDispatcher->setFocusedWindow(request);
682 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800683};
684
Michael Wrightd02c5b62014-02-10 15:10:22 -0800685TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
686 KeyEvent event;
687
688 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800689 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
690 INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600691 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
692 ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800693 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000694 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000695 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800696 << "Should reject key events with undefined action.";
697
698 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800699 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
700 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600701 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800702 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000703 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000704 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800705 << "Should reject key events with ACTION_MULTIPLE.";
706}
707
708TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
709 MotionEvent event;
710 PointerProperties pointerProperties[MAX_POINTERS + 1];
711 PointerCoords pointerCoords[MAX_POINTERS + 1];
Siarhei Vishniakou01747382022-01-20 13:23:27 -0800712 for (size_t i = 0; i <= MAX_POINTERS; i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800713 pointerProperties[i].clear();
714 pointerProperties[i].id = i;
715 pointerCoords[i].clear();
716 }
717
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800718 // Some constants commonly used below
719 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
720 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
721 constexpr int32_t metaState = AMETA_NONE;
722 constexpr MotionClassification classification = MotionClassification::NONE;
723
chaviw9eaa22c2020-07-01 16:21:27 -0700724 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800725 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800726 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
chaviw9eaa22c2020-07-01 16:21:27 -0700727 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification,
728 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700729 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
730 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700731 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800732 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000733 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000734 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800735 << "Should reject motion events with undefined action.";
736
737 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800738 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800739 POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
740 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
741 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
742 ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500743 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800744 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000745 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000746 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800747 << "Should reject motion events with pointer down index too large.";
748
Garfield Tanfbe732e2020-01-24 11:26:14 -0800749 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700750 AMOTION_EVENT_ACTION_POINTER_DOWN |
751 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700752 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
753 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700754 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500755 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800756 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000757 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000758 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800759 << "Should reject motion events with pointer down index too small.";
760
761 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800762 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800763 POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform,
764 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
765 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
766 ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500767 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800768 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000769 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000770 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800771 << "Should reject motion events with pointer up index too large.";
772
Garfield Tanfbe732e2020-01-24 11:26:14 -0800773 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700774 AMOTION_EVENT_ACTION_POINTER_UP |
775 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700776 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
777 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700778 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500779 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800780 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000781 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000782 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800783 << "Should reject motion events with pointer up index too small.";
784
785 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800786 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
787 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700788 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700789 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
790 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700791 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800792 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000793 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000794 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800795 << "Should reject motion events with 0 pointers.";
796
Garfield Tanfbe732e2020-01-24 11:26:14 -0800797 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
798 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700799 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700800 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
801 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700802 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800803 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000804 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000805 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800806 << "Should reject motion events with more than MAX_POINTERS pointers.";
807
808 // Rejects motion events with invalid pointer ids.
809 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800810 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
811 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700812 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700813 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
814 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700815 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800816 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000817 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000818 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800819 << "Should reject motion events with pointer ids less than 0.";
820
821 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800822 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
823 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700824 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700825 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
826 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700827 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800828 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000829 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000830 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800831 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
832
833 // Rejects motion events with duplicate pointer ids.
834 pointerProperties[0].id = 1;
835 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800836 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
837 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700838 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700839 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
840 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700841 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800842 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000843 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000844 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800845 << "Should reject motion events with duplicate pointer ids.";
846}
847
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800848/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
849
850TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
851 constexpr nsecs_t eventTime = 20;
Prabir Pradhan678438e2023-04-13 19:32:51 +0000852 mDispatcher->notifyConfigurationChanged({/*id=*/10, eventTime});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800853 ASSERT_TRUE(mDispatcher->waitForIdle());
854
855 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
856}
857
858TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Harry Cutts33476232023-01-30 19:57:29 +0000859 NotifySwitchArgs args(/*id=*/10, /*eventTime=*/20, /*policyFlags=*/0, /*switchValues=*/1,
860 /*switchMask=*/2);
Prabir Pradhan678438e2023-04-13 19:32:51 +0000861 mDispatcher->notifySwitch(args);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800862
863 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
864 args.policyFlags |= POLICY_FLAG_TRUSTED;
865 mFakePolicy->assertNotifySwitchWasCalled(args);
866}
867
Siarhei Vishniakouadeb6fa2023-05-26 09:11:06 -0700868namespace {
869
Arthur Hungb92218b2018-08-14 12:00:21 +0800870// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700871static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -0700872// Default input dispatching timeout if there is no focused application or paused window
873// from which to determine an appropriate dispatching timeout.
874static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds(
875 android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS *
876 android::base::HwTimeoutMultiplier());
Arthur Hungb92218b2018-08-14 12:00:21 +0800877
878class FakeApplicationHandle : public InputApplicationHandle {
879public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700880 FakeApplicationHandle() {
881 mInfo.name = "Fake Application";
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700882 mInfo.token = sp<BBinder>::make();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500883 mInfo.dispatchingTimeoutMillis =
884 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700885 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800886 virtual ~FakeApplicationHandle() {}
887
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000888 virtual bool updateInfo() override { return true; }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700889
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500890 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
891 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700892 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800893};
894
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800895class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800896public:
Garfield Tan15601662020-09-22 15:32:38 -0700897 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800898 : mName(name) {
Garfield Tan15601662020-09-22 15:32:38 -0700899 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800900 }
901
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800902 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700903 InputEvent* event;
904 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
905 if (!consumeSeq) {
906 return nullptr;
907 }
908 finishEvent(*consumeSeq);
909 return event;
910 }
911
912 /**
913 * Receive an event without acknowledging it.
914 * Return the sequence number that could later be used to send finished signal.
915 */
916 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800917 uint32_t consumeSeq;
918 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800919
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800920 std::chrono::time_point start = std::chrono::steady_clock::now();
921 status_t status = WOULD_BLOCK;
922 while (status == WOULD_BLOCK) {
Harry Cutts33476232023-01-30 19:57:29 +0000923 status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800924 &event);
925 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
926 if (elapsed > 100ms) {
927 break;
928 }
929 }
930
931 if (status == WOULD_BLOCK) {
932 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700933 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800934 }
935
936 if (status != OK) {
937 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700938 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800939 }
940 if (event == nullptr) {
941 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700942 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800943 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700944 if (outEvent != nullptr) {
945 *outEvent = event;
946 }
947 return consumeSeq;
948 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800949
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700950 /**
951 * To be used together with "receiveEvent" to complete the consumption of an event.
952 */
953 void finishEvent(uint32_t consumeSeq) {
954 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
955 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800956 }
957
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000958 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
959 const status_t status = mConsumer->sendTimeline(inputEventId, timeline);
960 ASSERT_EQ(OK, status);
961 }
962
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700963 void consumeEvent(InputEventType expectedEventType, int32_t expectedAction,
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000964 std::optional<int32_t> expectedDisplayId,
965 std::optional<int32_t> expectedFlags) {
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800966 InputEvent* event = consume();
967
968 ASSERT_NE(nullptr, event) << mName.c_str()
969 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800970 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700971 << mName.c_str() << " expected " << ftl::enum_string(expectedEventType)
972 << " event, got " << *event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800973
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000974 if (expectedDisplayId.has_value()) {
975 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
976 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800977
Tiger Huang8664f8c2018-10-11 19:14:35 +0800978 switch (expectedEventType) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700979 case InputEventType::KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800980 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
981 EXPECT_EQ(expectedAction, keyEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000982 if (expectedFlags.has_value()) {
983 EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags());
984 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800985 break;
986 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700987 case InputEventType::MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800988 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000989 assertMotionAction(expectedAction, motionEvent.getAction());
990
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000991 if (expectedFlags.has_value()) {
992 EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags());
993 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800994 break;
995 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700996 case InputEventType::FOCUS: {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100997 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
998 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700999 case InputEventType::CAPTURE: {
Prabir Pradhan99987712020-11-10 18:43:05 -08001000 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
1001 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001002 case InputEventType::TOUCH_MODE: {
Antonio Kantekf16f2832021-09-28 04:39:20 +00001003 FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events";
1004 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001005 case InputEventType::DRAG: {
arthurhungb89ccb02020-12-30 16:19:01 +08001006 FAIL() << "Use 'consumeDragEvent' for DRAG events";
1007 }
Tiger Huang8664f8c2018-10-11 19:14:35 +08001008 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001009 }
1010
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001011 MotionEvent* consumeMotion() {
1012 InputEvent* event = consume();
1013
1014 if (event == nullptr) {
1015 ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one.";
1016 return nullptr;
1017 }
1018
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001019 if (event->getType() != InputEventType::MOTION) {
1020 ADD_FAILURE() << mName << " expected a MotionEvent, got " << *event;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001021 return nullptr;
1022 }
1023 return static_cast<MotionEvent*>(event);
1024 }
1025
1026 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
1027 MotionEvent* motionEvent = consumeMotion();
1028 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
1029 ASSERT_THAT(*motionEvent, matcher);
1030 }
1031
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001032 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
1033 InputEvent* event = consume();
1034 ASSERT_NE(nullptr, event) << mName.c_str()
1035 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001036 ASSERT_EQ(InputEventType::FOCUS, event->getType())
1037 << "Instead of FocusEvent, got " << *event;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001038
1039 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1040 << mName.c_str() << ": event displayId should always be NONE.";
1041
1042 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
1043 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001044 }
1045
Prabir Pradhan99987712020-11-10 18:43:05 -08001046 void consumeCaptureEvent(bool hasCapture) {
1047 const InputEvent* event = consume();
1048 ASSERT_NE(nullptr, event) << mName.c_str()
1049 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001050 ASSERT_EQ(InputEventType::CAPTURE, event->getType())
1051 << "Instead of CaptureEvent, got " << *event;
Prabir Pradhan99987712020-11-10 18:43:05 -08001052
1053 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1054 << mName.c_str() << ": event displayId should always be NONE.";
1055
1056 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
1057 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
1058 }
1059
arthurhungb89ccb02020-12-30 16:19:01 +08001060 void consumeDragEvent(bool isExiting, float x, float y) {
1061 const InputEvent* event = consume();
1062 ASSERT_NE(nullptr, event) << mName.c_str()
1063 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001064 ASSERT_EQ(InputEventType::DRAG, event->getType()) << "Instead of DragEvent, got " << *event;
arthurhungb89ccb02020-12-30 16:19:01 +08001065
1066 EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1067 << mName.c_str() << ": event displayId should always be NONE.";
1068
1069 const auto& dragEvent = static_cast<const DragEvent&>(*event);
1070 EXPECT_EQ(isExiting, dragEvent.isExiting());
1071 EXPECT_EQ(x, dragEvent.getX());
1072 EXPECT_EQ(y, dragEvent.getY());
1073 }
1074
Antonio Kantekf16f2832021-09-28 04:39:20 +00001075 void consumeTouchModeEvent(bool inTouchMode) {
1076 const InputEvent* event = consume();
1077 ASSERT_NE(nullptr, event) << mName.c_str()
1078 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001079 ASSERT_EQ(InputEventType::TOUCH_MODE, event->getType())
1080 << "Instead of TouchModeEvent, got " << *event;
Antonio Kantekf16f2832021-09-28 04:39:20 +00001081
1082 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1083 << mName.c_str() << ": event displayId should always be NONE.";
1084 const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event);
1085 EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode());
1086 }
1087
chaviwd1c23182019-12-20 18:44:56 -08001088 void assertNoEvents() {
1089 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001090 if (event == nullptr) {
1091 return;
1092 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001093 if (event->getType() == InputEventType::KEY) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001094 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
1095 ADD_FAILURE() << "Received key event "
1096 << KeyEvent::actionToString(keyEvent.getAction());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001097 } else if (event->getType() == InputEventType::MOTION) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001098 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1099 ADD_FAILURE() << "Received motion event "
1100 << MotionEvent::actionToString(motionEvent.getAction());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001101 } else if (event->getType() == InputEventType::FOCUS) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001102 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
1103 ADD_FAILURE() << "Received focus event, hasFocus = "
1104 << (focusEvent.getHasFocus() ? "true" : "false");
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001105 } else if (event->getType() == InputEventType::CAPTURE) {
Prabir Pradhan99987712020-11-10 18:43:05 -08001106 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
1107 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
1108 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001109 } else if (event->getType() == InputEventType::TOUCH_MODE) {
Antonio Kantekf16f2832021-09-28 04:39:20 +00001110 const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event);
1111 ADD_FAILURE() << "Received touch mode event, inTouchMode = "
1112 << (touchModeEvent.isInTouchMode() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001113 }
1114 FAIL() << mName.c_str()
1115 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -08001116 }
1117
1118 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
1119
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001120 int getChannelFd() { return mConsumer->getChannel()->getFd().get(); }
1121
chaviwd1c23182019-12-20 18:44:56 -08001122protected:
1123 std::unique_ptr<InputConsumer> mConsumer;
1124 PreallocatedInputEventFactory mEventFactory;
1125
1126 std::string mName;
1127};
1128
chaviw3277faf2021-05-19 16:45:23 -05001129class FakeWindowHandle : public WindowInfoHandle {
chaviwd1c23182019-12-20 18:44:56 -08001130public:
1131 static const int32_t WIDTH = 600;
1132 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -08001133
Chris Yea209fde2020-07-22 13:54:51 -07001134 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001135 const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001136 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -08001137 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001138 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -07001139 base::Result<std::unique_ptr<InputChannel>> channel =
1140 dispatcher->createInputChannel(name);
1141 token = (*channel)->getConnectionToken();
1142 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -08001143 }
1144
1145 inputApplicationHandle->updateInfo();
1146 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
1147
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001148 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001149 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -08001150 mInfo.name = name;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001151 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001152 mInfo.alpha = 1.0;
chaviwd1c23182019-12-20 18:44:56 -08001153 mInfo.frameLeft = 0;
1154 mInfo.frameTop = 0;
1155 mInfo.frameRight = WIDTH;
1156 mInfo.frameBottom = HEIGHT;
chaviw1ff3d1e2020-07-01 15:53:47 -07001157 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -08001158 mInfo.globalScaleFactor = 1.0;
1159 mInfo.touchableRegion.clear();
1160 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
Prabir Pradhan5735a322022-04-11 17:23:34 +00001161 mInfo.ownerPid = WINDOW_PID;
1162 mInfo.ownerUid = WINDOW_UID;
chaviwd1c23182019-12-20 18:44:56 -08001163 mInfo.displayId = displayId;
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001164 mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT;
chaviwd1c23182019-12-20 18:44:56 -08001165 }
1166
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07001167 sp<FakeWindowHandle> clone(int32_t displayId) {
1168 sp<FakeWindowHandle> handle = sp<FakeWindowHandle>::make(mInfo.name + "(Mirror)");
1169 handle->mInfo = mInfo;
1170 handle->mInfo.displayId = displayId;
1171 handle->mInfo.id = sId++;
1172 handle->mInputReceiver = mInputReceiver;
1173 return handle;
1174 }
1175
1176 /**
1177 * This is different from clone, because clone will make a "mirror" window - a window with the
1178 * same token, but a different ID. The original window and the clone window are allowed to be
1179 * sent to the dispatcher at the same time - they can coexist inside the dispatcher.
1180 * This function will create a different object of WindowInfoHandle, but with the same
1181 * properties as the original object - including the ID.
1182 * You can use either the old or the new object to consume the events.
1183 * IMPORTANT: The duplicated object is supposed to replace the original object, and not appear
1184 * at the same time inside dispatcher.
1185 */
1186 sp<FakeWindowHandle> duplicate() {
1187 sp<FakeWindowHandle> handle = sp<FakeWindowHandle>::make(mName);
1188 handle->mInfo = mInfo;
1189 handle->mInputReceiver = mInputReceiver;
Arthur Hungabbb9d82021-09-01 14:52:30 +00001190 return handle;
1191 }
1192
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001193 void setTouchable(bool touchable) {
1194 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable);
1195 }
chaviwd1c23182019-12-20 18:44:56 -08001196
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001197 void setFocusable(bool focusable) {
1198 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable);
1199 }
1200
1201 void setVisible(bool visible) {
1202 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible);
1203 }
Vishnu Nair958da932020-08-21 17:12:37 -07001204
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001205 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001206 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001207 }
1208
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001209 void setPaused(bool paused) {
1210 mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused);
1211 }
1212
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001213 void setPreventSplitting(bool preventSplitting) {
1214 mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001215 }
1216
1217 void setSlippery(bool slippery) {
1218 mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery);
1219 }
1220
1221 void setWatchOutsideTouch(bool watchOutside) {
1222 mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside);
1223 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001224
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001225 void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); }
1226
1227 void setInterceptsStylus(bool interceptsStylus) {
1228 mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus);
1229 }
1230
1231 void setDropInput(bool dropInput) {
1232 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput);
1233 }
1234
1235 void setDropInputIfObscured(bool dropInputIfObscured) {
1236 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured);
1237 }
1238
1239 void setNoInputChannel(bool noInputChannel) {
1240 mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel);
1241 }
1242
Josep del Riob3981622023-04-18 15:49:45 +00001243 void setDisableUserActivity(bool disableUserActivity) {
1244 mInfo.setInputConfig(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY, disableUserActivity);
1245 }
1246
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001247 void setAlpha(float alpha) { mInfo.alpha = alpha; }
1248
chaviw3277faf2021-05-19 16:45:23 -05001249 void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; }
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001250
Bernardo Rufino7393d172021-02-26 13:56:11 +00001251 void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
1252
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001253 void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) {
chaviwd1c23182019-12-20 18:44:56 -08001254 mInfo.frameLeft = frame.left;
1255 mInfo.frameTop = frame.top;
1256 mInfo.frameRight = frame.right;
1257 mInfo.frameBottom = frame.bottom;
1258 mInfo.touchableRegion.clear();
1259 mInfo.addTouchableRegion(frame);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001260
1261 const Rect logicalDisplayFrame = displayTransform.transform(frame);
1262 ui::Transform translate;
1263 translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top);
1264 mInfo.transform = translate * displayTransform;
chaviwd1c23182019-12-20 18:44:56 -08001265 }
1266
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001267 void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; }
1268
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001269 void setIsWallpaper(bool isWallpaper) {
1270 mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper);
1271 }
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001272
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001273 void setDupTouchToWallpaper(bool hasWallpaper) {
1274 mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper);
1275 }
chaviwd1c23182019-12-20 18:44:56 -08001276
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001277 void setTrustedOverlay(bool trustedOverlay) {
1278 mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay);
1279 }
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001280
chaviw9eaa22c2020-07-01 16:21:27 -07001281 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
1282 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
1283 }
1284
1285 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -07001286
yunho.shinf4a80b82020-11-16 21:13:57 +09001287 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
1288
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001289 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001290 consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, expectedFlags);
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001291 }
1292
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001293 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001294 consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001295 }
1296
Svet Ganov5d3bc372020-01-26 23:11:07 -08001297 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001298 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001299 consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId),
1300 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001301 }
1302
1303 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001304 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001305 consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
1306 WithDisplayId(expectedDisplayId), WithFlags(expectedFlags)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001307 }
1308
1309 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001310 int32_t expectedFlags = 0) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001311 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
1312 }
1313
1314 void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
1315 std::optional<int32_t> expectedFlags = std::nullopt) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001316 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001317 expectedFlags);
1318 }
1319
Svet Ganov5d3bc372020-01-26 23:11:07 -08001320 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001321 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1322 int32_t expectedFlags = 0) {
1323 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
1324 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001325 consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001326 }
1327
1328 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001329 int32_t expectedFlags = 0) {
1330 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
1331 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001332 consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001333 }
1334
1335 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001336 int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001337 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
Michael Wright3a240c42019-12-10 20:53:41 +00001338 expectedFlags);
1339 }
1340
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001341 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1342 int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001343 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001344 expectedFlags);
1345 }
1346
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001347 void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1348 int32_t expectedFlags = 0) {
1349 InputEvent* event = consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08001350 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001351 ASSERT_EQ(InputEventType::MOTION, event->getType());
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001352 const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1353 EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked());
1354 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getX());
1355 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getY());
1356 }
1357
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001358 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
1359 ASSERT_NE(mInputReceiver, nullptr)
1360 << "Cannot consume events from a window with no receiver";
1361 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
1362 }
1363
Prabir Pradhan99987712020-11-10 18:43:05 -08001364 void consumeCaptureEvent(bool hasCapture) {
1365 ASSERT_NE(mInputReceiver, nullptr)
1366 << "Cannot consume events from a window with no receiver";
1367 mInputReceiver->consumeCaptureEvent(hasCapture);
1368 }
1369
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001370 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
1371 MotionEvent* motionEvent = consumeMotion();
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08001372 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001373 ASSERT_THAT(*motionEvent, matcher);
1374 }
1375
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001376 void consumeEvent(InputEventType expectedEventType, int32_t expectedAction,
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001377 std::optional<int32_t> expectedDisplayId,
1378 std::optional<int32_t> expectedFlags) {
chaviwd1c23182019-12-20 18:44:56 -08001379 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
1380 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
1381 expectedFlags);
1382 }
1383
arthurhungb89ccb02020-12-30 16:19:01 +08001384 void consumeDragEvent(bool isExiting, float x, float y) {
1385 mInputReceiver->consumeDragEvent(isExiting, x, y);
1386 }
1387
Antonio Kantekf16f2832021-09-28 04:39:20 +00001388 void consumeTouchModeEvent(bool inTouchMode) {
1389 ASSERT_NE(mInputReceiver, nullptr)
1390 << "Cannot consume events from a window with no receiver";
1391 mInputReceiver->consumeTouchModeEvent(inTouchMode);
1392 }
1393
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001394 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001395 if (mInputReceiver == nullptr) {
1396 ADD_FAILURE() << "Invalid receive event on window with no receiver";
1397 return std::nullopt;
1398 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001399 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001400 }
1401
1402 void finishEvent(uint32_t sequenceNum) {
1403 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1404 mInputReceiver->finishEvent(sequenceNum);
1405 }
1406
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001407 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
1408 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1409 mInputReceiver->sendTimeline(inputEventId, timeline);
1410 }
1411
chaviwaf87b3e2019-10-01 16:59:28 -07001412 InputEvent* consume() {
1413 if (mInputReceiver == nullptr) {
1414 return nullptr;
1415 }
1416 return mInputReceiver->consume();
1417 }
1418
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001419 MotionEvent* consumeMotion() {
1420 InputEvent* event = consume();
1421 if (event == nullptr) {
1422 ADD_FAILURE() << "Consume failed : no event";
1423 return nullptr;
1424 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001425 if (event->getType() != InputEventType::MOTION) {
1426 ADD_FAILURE() << "Instead of motion event, got " << *event;
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001427 return nullptr;
1428 }
1429 return static_cast<MotionEvent*>(event);
1430 }
1431
Arthur Hungb92218b2018-08-14 12:00:21 +08001432 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001433 if (mInputReceiver == nullptr &&
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001434 mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001435 return; // Can't receive events if the window does not have input channel
1436 }
1437 ASSERT_NE(nullptr, mInputReceiver)
1438 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -08001439 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +08001440 }
1441
chaviwaf87b3e2019-10-01 16:59:28 -07001442 sp<IBinder> getToken() { return mInfo.token; }
1443
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001444 const std::string& getName() { return mName; }
1445
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001446 void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) {
1447 mInfo.ownerPid = ownerPid;
1448 mInfo.ownerUid = ownerUid;
1449 }
1450
Prabir Pradhanedd96402022-02-15 01:46:16 -08001451 int32_t getPid() const { return mInfo.ownerPid; }
1452
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001453 void destroyReceiver() { mInputReceiver = nullptr; }
1454
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001455 int getChannelFd() { return mInputReceiver->getChannelFd(); }
1456
chaviwd1c23182019-12-20 18:44:56 -08001457private:
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07001458 FakeWindowHandle(std::string name) : mName(name){};
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001459 const std::string mName;
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07001460 std::shared_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001461 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07001462 friend class sp<FakeWindowHandle>;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001463};
1464
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001465std::atomic<int32_t> FakeWindowHandle::sId{1};
1466
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001467static InputEventInjectionResult injectKey(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001468 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001469 int32_t displayId = ADISPLAY_ID_NONE,
1470 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001471 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001472 bool allowKeyRepeat = true, std::optional<int32_t> targetUid = {},
1473 uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001474 KeyEvent event;
1475 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1476
1477 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001478 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Harry Cutts33476232023-01-30 19:57:29 +00001479 INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount,
1480 currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001481
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001482 if (!allowKeyRepeat) {
1483 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
1484 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001485 // Inject event until dispatch out.
Prabir Pradhan5735a322022-04-11 17:23:34 +00001486 return dispatcher->injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001487}
1488
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001489static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001490 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001491 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001492}
1493
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001494// Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without
1495// sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it
1496// has to be woken up to process the repeating key.
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001497static InputEventInjectionResult injectKeyDownNoRepeat(
1498 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001499 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001500 InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT,
Harry Cutts33476232023-01-30 19:57:29 +00001501 /*allowKeyRepeat=*/false);
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001502}
1503
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001504static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001505 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001506 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001507}
1508
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001509static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001510 const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event,
Garfield Tandf26e862020-07-01 20:18:19 -07001511 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001512 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
1513 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
1514 return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout,
1515 policyFlags);
Garfield Tandf26e862020-07-01 20:18:19 -07001516}
1517
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001518static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001519 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source,
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001520 int32_t displayId, const PointF& position = {100, 200},
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001521 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001522 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1523 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001524 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001525 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC),
1526 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07001527 MotionEventBuilder motionBuilder =
1528 MotionEventBuilder(action, source)
1529 .displayId(displayId)
1530 .eventTime(eventTime)
1531 .rawXCursorPosition(cursorPosition.x)
1532 .rawYCursorPosition(cursorPosition.y)
1533 .pointer(
1534 PointerBuilder(/*id=*/0, ToolType::FINGER).x(position.x).y(position.y));
1535 if (MotionEvent::getActionMasked(action) == ACTION_DOWN) {
1536 motionBuilder.downTime(eventTime);
1537 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001538
1539 // Inject event until dispatch out.
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07001540 return injectMotionEvent(dispatcher, motionBuilder.build(), injectionTimeout, injectionMode,
1541 targetUid, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001542}
1543
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001544static InputEventInjectionResult injectMotionDown(
1545 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId,
1546 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001547 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001548}
1549
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001550static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001551 int32_t source, int32_t displayId,
1552 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001553 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001554}
1555
Jackal Guof9696682018-10-05 12:23:23 +08001556static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1557 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1558 // Define a valid key event.
Harry Cutts33476232023-01-30 19:57:29 +00001559 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001560 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A,
1561 KEY_A, AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001562
1563 return args;
1564}
1565
Josep del Riob3981622023-04-18 15:49:45 +00001566static NotifyKeyArgs generateSystemShortcutArgs(int32_t action,
1567 int32_t displayId = ADISPLAY_ID_NONE) {
1568 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1569 // Define a valid key event.
1570 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1571 displayId, 0, action, /* flags */ 0, AKEYCODE_C, KEY_C, AMETA_META_ON,
1572 currentTime);
1573
1574 return args;
1575}
1576
1577static NotifyKeyArgs generateAssistantKeyArgs(int32_t action,
1578 int32_t displayId = ADISPLAY_ID_NONE) {
1579 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1580 // Define a valid key event.
1581 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1582 displayId, 0, action, /* flags */ 0, AKEYCODE_ASSIST, KEY_ASSISTANT,
1583 AMETA_NONE, currentTime);
1584
1585 return args;
1586}
1587
Prabir Pradhan678438e2023-04-13 19:32:51 +00001588[[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source,
1589 int32_t displayId,
1590 const std::vector<PointF>& points) {
chaviwd1c23182019-12-20 18:44:56 -08001591 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001592 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1593 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1594 }
1595
chaviwd1c23182019-12-20 18:44:56 -08001596 PointerProperties pointerProperties[pointerCount];
1597 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001598
chaviwd1c23182019-12-20 18:44:56 -08001599 for (size_t i = 0; i < pointerCount; i++) {
1600 pointerProperties[i].clear();
1601 pointerProperties[i].id = i;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001602 pointerProperties[i].toolType = ToolType::FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001603
chaviwd1c23182019-12-20 18:44:56 -08001604 pointerCoords[i].clear();
1605 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1606 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1607 }
Jackal Guof9696682018-10-05 12:23:23 +08001608
1609 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1610 // Define a valid motion event.
Harry Cutts33476232023-01-30 19:57:29 +00001611 NotifyMotionArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001612 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1613 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001614 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1615 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001616 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1617 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001618
1619 return args;
1620}
1621
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001622static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) {
1623 return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points);
1624}
1625
chaviwd1c23182019-12-20 18:44:56 -08001626static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1627 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1628}
1629
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001630static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(
1631 const PointerCaptureRequest& request) {
Harry Cutts33476232023-01-30 19:57:29 +00001632 return NotifyPointerCaptureChangedArgs(/*id=*/0, systemTime(SYSTEM_TIME_MONOTONIC), request);
Prabir Pradhan99987712020-11-10 18:43:05 -08001633}
1634
Siarhei Vishniakouadeb6fa2023-05-26 09:11:06 -07001635} // namespace
1636
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001637/**
1638 * When a window unexpectedly disposes of its input channel, policy should be notified about the
1639 * broken channel.
1640 */
1641TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) {
1642 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1643 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001644 sp<FakeWindowHandle>::make(application, mDispatcher,
1645 "Window that breaks its input channel", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001646
1647 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1648
1649 // Window closes its channel, but the window remains.
1650 window->destroyReceiver();
1651 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token);
1652}
1653
Arthur Hungb92218b2018-08-14 12:00:21 +08001654TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001655 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001656 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1657 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001658
Arthur Hung72d8dc32020-03-28 00:48:39 +00001659 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001660 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1661 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1662 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001663
1664 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001665 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001666}
1667
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001668TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) {
1669 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001670 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1671 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001672
1673 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1674 // Inject a MotionEvent to an unknown display.
1675 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1676 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE))
1677 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1678
1679 // Window should receive motion event.
1680 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1681}
1682
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001683/**
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001684 * Calling setInputWindows once should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001685 * This test serves as a sanity check for the next test, where setInputWindows is
1686 * called twice.
1687 */
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001688TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) {
Chris Yea209fde2020-07-22 13:54:51 -07001689 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001690 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1691 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001692 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001693
1694 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001695 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001696 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1697 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001698 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001699
1700 // Window should receive motion event.
1701 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1702}
1703
1704/**
1705 * Calling setInputWindows twice, with the same info, should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001706 */
1707TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001708 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001709 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1710 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001711 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001712
1713 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1714 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001715 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001716 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1717 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001718 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001719
1720 // Window should receive motion event.
1721 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1722}
1723
Arthur Hungb92218b2018-08-14 12:00:21 +08001724// The foreground window should receive the first touch down event.
1725TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001726 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001727 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001728 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001729 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001730 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001731
Arthur Hung72d8dc32020-03-28 00:48:39 +00001732 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001733 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1734 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1735 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001736
1737 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001738 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001739 windowSecond->assertNoEvents();
1740}
1741
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001742/**
1743 * Two windows: A top window, and a wallpaper behind the window.
1744 * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window
1745 * gets ACTION_CANCEL.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001746 * 1. foregroundWindow <-- dup touch to wallpaper
1747 * 2. wallpaperWindow <-- is wallpaper
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001748 */
1749TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) {
1750 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1751 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001752 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001753 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001754 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001755 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001756 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001757
1758 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1759 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1760 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1761 {100, 200}))
1762 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1763
1764 // Both foreground window and its wallpaper should receive the touch down
1765 foregroundWindow->consumeMotionDown();
1766 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1767
1768 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1769 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1770 ADISPLAY_ID_DEFAULT, {110, 200}))
1771 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1772
1773 foregroundWindow->consumeMotionMove();
1774 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1775
1776 // Now the foreground window goes away, but the wallpaper stays
1777 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
1778 foregroundWindow->consumeMotionCancel();
1779 // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1780 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1781}
1782
1783/**
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001784 * Two fingers down on the window, and lift off the first finger.
1785 * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event
1786 * contains a single pointer.
1787 */
1788TEST_F(InputDispatcherTest, CancelAfterPointer0Up) {
1789 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1790 sp<FakeWindowHandle> window =
1791 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1792
1793 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001794 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00001795 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1796 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1797 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001798 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00001799 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1800 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1801 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
1802 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001803 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00001804 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
1805 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1806 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
1807 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001808 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
1809 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
1810 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
1811
1812 // Remove the window. The gesture should be canceled
1813 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
1814 const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}};
1815 window->consumeMotionEvent(
1816 AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers)));
1817}
1818
1819/**
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001820 * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above,
1821 * with the following differences:
1822 * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to
1823 * clean up the connection.
1824 * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful.
1825 * Ensure that there's no crash in the dispatcher.
1826 */
1827TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) {
1828 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1829 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001830 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001831 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001832 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001833 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001834 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001835
1836 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1837 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1838 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1839 {100, 200}))
1840 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1841
1842 // Both foreground window and its wallpaper should receive the touch down
1843 foregroundWindow->consumeMotionDown();
1844 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1845
1846 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1847 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1848 ADISPLAY_ID_DEFAULT, {110, 200}))
1849 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1850
1851 foregroundWindow->consumeMotionMove();
1852 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1853
1854 // Wallpaper closes its channel, but the window remains.
1855 wallpaperWindow->destroyReceiver();
1856 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token);
1857
1858 // Now the foreground window goes away, but the wallpaper stays, even though its channel
1859 // is no longer valid.
1860 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
1861 foregroundWindow->consumeMotionCancel();
1862}
1863
Arthur Hungc539dbb2022-12-08 07:45:36 +00001864class ShouldSplitTouchFixture : public InputDispatcherTest,
1865 public ::testing::WithParamInterface<bool> {};
1866INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture,
1867 ::testing::Values(true, false));
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001868/**
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001869 * A single window that receives touch (on top), and a wallpaper window underneath it.
1870 * The top window gets a multitouch gesture.
1871 * Ensure that wallpaper gets the same gesture.
1872 */
Arthur Hungc539dbb2022-12-08 07:45:36 +00001873TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001874 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00001875 sp<FakeWindowHandle> foregroundWindow =
1876 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
1877 foregroundWindow->setDupTouchToWallpaper(true);
1878 foregroundWindow->setPreventSplitting(GetParam());
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001879
1880 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001881 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001882 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001883
Arthur Hungc539dbb2022-12-08 07:45:36 +00001884 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001885
1886 // Touch down on top window
1887 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1888 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1889 {100, 100}))
1890 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1891
1892 // Both top window and its wallpaper should receive the touch down
Arthur Hungc539dbb2022-12-08 07:45:36 +00001893 foregroundWindow->consumeMotionDown();
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001894 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1895
1896 // Second finger down on the top window
1897 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001898 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001899 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001900 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1901 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001902 .build();
1903 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1904 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
1905 InputEventInjectionSync::WAIT_FOR_RESULT))
1906 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1907
Harry Cutts33476232023-01-30 19:57:29 +00001908 foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
1909 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001910 expectedWallpaperFlags);
Arthur Hungc539dbb2022-12-08 07:45:36 +00001911
1912 const MotionEvent secondFingerUpEvent =
1913 MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
1914 .displayId(ADISPLAY_ID_DEFAULT)
1915 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001916 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1917 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Arthur Hungc539dbb2022-12-08 07:45:36 +00001918 .build();
1919 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1920 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
1921 InputEventInjectionSync::WAIT_FOR_RESULT))
1922 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1923 foregroundWindow->consumeMotionPointerUp(0);
1924 wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1925
1926 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08001927 injectMotionEvent(mDispatcher,
1928 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
1929 AINPUT_SOURCE_TOUCHSCREEN)
1930 .displayId(ADISPLAY_ID_DEFAULT)
1931 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
1932 .pointer(PointerBuilder(/* id */ 1,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001933 ToolType::FINGER)
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08001934 .x(100)
1935 .y(100))
1936 .build(),
1937 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT))
Arthur Hungc539dbb2022-12-08 07:45:36 +00001938 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1939 foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1940 wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001941}
1942
1943/**
1944 * Two windows: a window on the left and window on the right.
1945 * A third window, wallpaper, is behind both windows, and spans both top windows.
1946 * The first touch down goes to the left window. A second pointer touches down on the right window.
1947 * The touch is split, so both left and right windows should receive ACTION_DOWN.
1948 * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by
1949 * ACTION_POINTER_DOWN(1).
1950 */
1951TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) {
1952 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1953 sp<FakeWindowHandle> leftWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001954 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001955 leftWindow->setFrame(Rect(0, 0, 200, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001956 leftWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001957
1958 sp<FakeWindowHandle> rightWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001959 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001960 rightWindow->setFrame(Rect(200, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001961 rightWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001962
1963 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001964 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001965 wallpaperWindow->setFrame(Rect(0, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001966 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001967
1968 mDispatcher->setInputWindows(
1969 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
1970
1971 // Touch down on left window
1972 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1973 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1974 {100, 100}))
1975 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1976
1977 // Both foreground window and its wallpaper should receive the touch down
1978 leftWindow->consumeMotionDown();
1979 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1980
1981 // Second finger down on the right window
1982 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001983 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001984 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001985 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1986 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001987 .build();
1988 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1989 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
1990 InputEventInjectionSync::WAIT_FOR_RESULT))
1991 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1992
1993 leftWindow->consumeMotionMove();
1994 // Since the touch is split, right window gets ACTION_DOWN
1995 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00001996 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001997 expectedWallpaperFlags);
1998
1999 // Now, leftWindow, which received the first finger, disappears.
2000 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}});
2001 leftWindow->consumeMotionCancel();
2002 // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
2003 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2004
2005 // The pointer that's still down on the right window moves, and goes to the right window only.
2006 // As far as the dispatcher's concerned though, both pointers are still present.
2007 const MotionEvent secondFingerMoveEvent =
2008 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2009 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002010 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2011 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002012 .build();
2013 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2014 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
2015 InputEventInjectionSync::WAIT_FOR_RESULT));
2016 rightWindow->consumeMotionMove();
2017
2018 leftWindow->assertNoEvents();
2019 rightWindow->assertNoEvents();
2020 wallpaperWindow->assertNoEvents();
2021}
2022
Arthur Hungc539dbb2022-12-08 07:45:36 +00002023/**
2024 * Two windows: a window on the left with dup touch to wallpaper and window on the right without it.
2025 * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL
2026 * The right window should receive ACTION_DOWN.
2027 */
2028TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) {
Arthur Hung74c248d2022-11-23 07:09:59 +00002029 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00002030 sp<FakeWindowHandle> leftWindow =
2031 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2032 leftWindow->setFrame(Rect(0, 0, 200, 200));
2033 leftWindow->setDupTouchToWallpaper(true);
2034 leftWindow->setSlippery(true);
2035
2036 sp<FakeWindowHandle> rightWindow =
2037 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2038 rightWindow->setFrame(Rect(200, 0, 400, 200));
Arthur Hung74c248d2022-11-23 07:09:59 +00002039
2040 sp<FakeWindowHandle> wallpaperWindow =
2041 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
2042 wallpaperWindow->setIsWallpaper(true);
Arthur Hung74c248d2022-11-23 07:09:59 +00002043
Arthur Hungc539dbb2022-12-08 07:45:36 +00002044 mDispatcher->setInputWindows(
2045 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
Arthur Hung74c248d2022-11-23 07:09:59 +00002046
Arthur Hungc539dbb2022-12-08 07:45:36 +00002047 // Touch down on left window
Arthur Hung74c248d2022-11-23 07:09:59 +00002048 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2049 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002050 {100, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002051 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungc539dbb2022-12-08 07:45:36 +00002052
2053 // Both foreground window and its wallpaper should receive the touch down
2054 leftWindow->consumeMotionDown();
Arthur Hung74c248d2022-11-23 07:09:59 +00002055 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2056
Arthur Hungc539dbb2022-12-08 07:45:36 +00002057 // Move to right window, the left window should receive cancel.
Arthur Hung74c248d2022-11-23 07:09:59 +00002058 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002059 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2060 ADISPLAY_ID_DEFAULT, {201, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002061 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2062
Arthur Hungc539dbb2022-12-08 07:45:36 +00002063 leftWindow->consumeMotionCancel();
2064 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2065 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Arthur Hung74c248d2022-11-23 07:09:59 +00002066}
2067
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002068/**
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002069 * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not
2070 * interactive, it might stop sending this flag.
2071 * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure
2072 * to have a consistent input stream.
2073 *
2074 * Test procedure:
2075 * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL.
2076 * DOWN (new gesture).
2077 *
2078 * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent
2079 * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app.
2080 *
2081 * We technically just need a single window here, but we are using two windows (spy on top and a
2082 * regular window below) to emulate the actual situation where it happens on the device.
2083 */
2084TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) {
2085 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2086 sp<FakeWindowHandle> spyWindow =
2087 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2088 spyWindow->setFrame(Rect(0, 0, 200, 200));
2089 spyWindow->setTrustedOverlay(true);
2090 spyWindow->setSpy(true);
2091
2092 sp<FakeWindowHandle> window =
2093 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2094 window->setFrame(Rect(0, 0, 200, 200));
2095
2096 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2097 const int32_t touchDeviceId = 4;
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002098
2099 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002100 mDispatcher->notifyMotion(
2101 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2102 .deviceId(touchDeviceId)
2103 .policyFlags(DEFAULT_POLICY_FLAGS)
2104 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2105 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002106
Prabir Pradhan678438e2023-04-13 19:32:51 +00002107 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2108 .deviceId(touchDeviceId)
2109 .policyFlags(DEFAULT_POLICY_FLAGS)
2110 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2111 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2112 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002113 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2114 spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2115 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2116 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2117
2118 // Cancel the current gesture. Send the cancel without the default policy flags.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002119 mDispatcher->notifyMotion(
2120 MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN)
2121 .deviceId(touchDeviceId)
2122 .policyFlags(0)
2123 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2124 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2125 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002126 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2127 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2128
2129 // We don't need to reset the device to reproduce the issue, but the reset event typically
2130 // follows, so we keep it here to model the actual listener behaviour more closely.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002131 mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId});
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002132
2133 // Start new gesture
Prabir Pradhan678438e2023-04-13 19:32:51 +00002134 mDispatcher->notifyMotion(
2135 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2136 .deviceId(touchDeviceId)
2137 .policyFlags(DEFAULT_POLICY_FLAGS)
2138 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2139 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002140 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2141 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2142
2143 // No more events
2144 spyWindow->assertNoEvents();
2145 window->assertNoEvents();
2146}
2147
2148/**
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002149 * Two windows: a window on the left and a window on the right.
2150 * Mouse is hovered from the right window into the left window.
2151 * Next, we tap on the left window, where the cursor was last seen.
2152 * The second tap is done onto the right window.
2153 * The mouse and tap are from two different devices.
2154 * We technically don't need to set the downtime / eventtime for these events, but setting these
2155 * explicitly helps during debugging.
2156 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2157 * In the buggy implementation, a tap on the right window would cause a crash.
2158 */
2159TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) {
2160 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2161 sp<FakeWindowHandle> leftWindow =
2162 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2163 leftWindow->setFrame(Rect(0, 0, 200, 200));
2164
2165 sp<FakeWindowHandle> rightWindow =
2166 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2167 rightWindow->setFrame(Rect(200, 0, 400, 200));
2168
2169 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2170 // All times need to start at the current time, otherwise the dispatcher will drop the events as
2171 // stale.
2172 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
2173 const int32_t mouseDeviceId = 6;
2174 const int32_t touchDeviceId = 4;
2175 // Move the cursor from right
2176 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2177 injectMotionEvent(mDispatcher,
2178 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2179 AINPUT_SOURCE_MOUSE)
2180 .deviceId(mouseDeviceId)
2181 .downTime(baseTime + 10)
2182 .eventTime(baseTime + 20)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002183 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002184 .x(300)
2185 .y(100))
2186 .build()));
2187 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2188
2189 // .. to the left window
2190 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2191 injectMotionEvent(mDispatcher,
2192 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2193 AINPUT_SOURCE_MOUSE)
2194 .deviceId(mouseDeviceId)
2195 .downTime(baseTime + 10)
2196 .eventTime(baseTime + 30)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002197 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002198 .x(110)
2199 .y(100))
2200 .build()));
2201 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2202 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2203 // Now tap the left window
2204 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2205 injectMotionEvent(mDispatcher,
2206 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2207 AINPUT_SOURCE_TOUCHSCREEN)
2208 .deviceId(touchDeviceId)
2209 .downTime(baseTime + 40)
2210 .eventTime(baseTime + 40)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002211 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002212 .x(100)
2213 .y(100))
2214 .build()));
2215 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2216 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2217
2218 // release tap
2219 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2220 injectMotionEvent(mDispatcher,
2221 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2222 AINPUT_SOURCE_TOUCHSCREEN)
2223 .deviceId(touchDeviceId)
2224 .downTime(baseTime + 40)
2225 .eventTime(baseTime + 50)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002226 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002227 .x(100)
2228 .y(100))
2229 .build()));
2230 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2231
2232 // Tap the window on the right
2233 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2234 injectMotionEvent(mDispatcher,
2235 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2236 AINPUT_SOURCE_TOUCHSCREEN)
2237 .deviceId(touchDeviceId)
2238 .downTime(baseTime + 60)
2239 .eventTime(baseTime + 60)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002240 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002241 .x(300)
2242 .y(100))
2243 .build()));
2244 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2245
2246 // release tap
2247 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2248 injectMotionEvent(mDispatcher,
2249 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2250 AINPUT_SOURCE_TOUCHSCREEN)
2251 .deviceId(touchDeviceId)
2252 .downTime(baseTime + 60)
2253 .eventTime(baseTime + 70)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002254 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002255 .x(300)
2256 .y(100))
2257 .build()));
2258 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2259
2260 // No more events
2261 leftWindow->assertNoEvents();
2262 rightWindow->assertNoEvents();
2263}
2264
2265/**
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002266 * Start hovering in a window. While this hover is still active, make another window appear on top.
2267 * The top, obstructing window has no input channel, so it's not supposed to receive input.
2268 * While the top window is present, the hovering is stopped.
2269 * Later, hovering gets resumed again.
2270 * Ensure that new hover gesture is handled correctly.
2271 * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly
2272 * to the window that's currently being hovered over.
2273 */
2274TEST_F(InputDispatcherTest, HoverWhileWindowAppears) {
2275 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2276 sp<FakeWindowHandle> window =
2277 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2278 window->setFrame(Rect(0, 0, 200, 200));
2279
2280 // Only a single window is present at first
2281 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2282
2283 // Start hovering in the window
2284 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2285 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2286 .build());
2287 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2288
2289 // Now, an obscuring window appears!
2290 sp<FakeWindowHandle> obscuringWindow =
2291 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
2292 ADISPLAY_ID_DEFAULT,
2293 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
2294 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
2295 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
2296 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
2297 obscuringWindow->setNoInputChannel(true);
2298 obscuringWindow->setFocusable(false);
2299 obscuringWindow->setAlpha(1.0);
2300 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
2301
2302 // While this new obscuring window is present, the hovering is stopped
2303 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2304 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2305 .build());
2306 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2307
2308 // Now the obscuring window goes away.
2309 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2310
2311 // And a new hover gesture starts.
2312 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2313 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2314 .build());
2315 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2316}
2317
2318/**
2319 * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to
2320 * the obscuring window.
2321 */
2322TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) {
2323 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2324 sp<FakeWindowHandle> window =
2325 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2326 window->setFrame(Rect(0, 0, 200, 200));
2327
2328 // Only a single window is present at first
2329 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2330
2331 // Start hovering in the window
2332 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2333 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2334 .build());
2335 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2336
2337 // Now, an obscuring window appears!
2338 sp<FakeWindowHandle> obscuringWindow =
2339 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
2340 ADISPLAY_ID_DEFAULT,
2341 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
2342 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
2343 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
2344 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
2345 obscuringWindow->setNoInputChannel(true);
2346 obscuringWindow->setFocusable(false);
2347 obscuringWindow->setAlpha(1.0);
2348 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
2349
2350 // While this new obscuring window is present, the hovering continues. The event can't go to the
2351 // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window.
2352 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2353 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2354 .build());
2355 obscuringWindow->assertNoEvents();
2356 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2357
2358 // Now the obscuring window goes away.
2359 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2360
2361 // Hovering continues in the same position. The hovering pointer re-enters the bottom window,
2362 // so it should generate a HOVER_ENTER
2363 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2364 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2365 .build());
2366 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2367
2368 // Now the MOVE should be getting dispatched normally
2369 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2370 .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110))
2371 .build());
2372 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
2373}
2374
2375/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002376 * Two windows: a window on the left and a window on the right.
2377 * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains
2378 * down. Then, on the left window, also place second touch pointer down.
2379 * This test tries to reproduce a crash.
2380 * In the buggy implementation, second pointer down on the left window would cause a crash.
2381 */
2382TEST_F(InputDispatcherTest, MultiDeviceSplitTouch) {
2383 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2384 sp<FakeWindowHandle> leftWindow =
2385 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2386 leftWindow->setFrame(Rect(0, 0, 200, 200));
2387
2388 sp<FakeWindowHandle> rightWindow =
2389 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2390 rightWindow->setFrame(Rect(200, 0, 400, 200));
2391
2392 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2393
2394 const int32_t touchDeviceId = 4;
2395 const int32_t mouseDeviceId = 6;
2396 NotifyMotionArgs args;
2397
2398 // Start hovering over the left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002399 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2400 .deviceId(mouseDeviceId)
2401 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2402 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002403 leftWindow->consumeMotionEvent(
2404 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2405
2406 // Mouse down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002407 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2408 .deviceId(mouseDeviceId)
2409 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2410 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2411 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002412
2413 leftWindow->consumeMotionEvent(
2414 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
2415 leftWindow->consumeMotionEvent(
2416 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2417
Prabir Pradhan678438e2023-04-13 19:32:51 +00002418 mDispatcher->notifyMotion(
2419 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2420 .deviceId(mouseDeviceId)
2421 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2422 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2423 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2424 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002425 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2426
2427 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002428 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2429 .deviceId(touchDeviceId)
2430 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2431 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002432 leftWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
2433
2434 rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2435
2436 // Second touch pointer down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002437 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2438 .deviceId(touchDeviceId)
2439 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2440 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
2441 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002442 leftWindow->consumeMotionEvent(
2443 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2444 // This MOVE event is not necessary (doesn't carry any new information), but it's there in the
2445 // current implementation.
2446 const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}};
2447 rightWindow->consumeMotionEvent(
2448 AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers)));
2449
2450 leftWindow->assertNoEvents();
2451 rightWindow->assertNoEvents();
2452}
2453
2454/**
2455 * On a single window, use two different devices: mouse and touch.
2456 * Touch happens first, with two pointers going down, and then the first pointer leaving.
2457 * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL.
2458 * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored,
2459 * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not
2460 * represent a new gesture.
2461 */
2462TEST_F(InputDispatcherTest, MixedTouchAndMouseWithPointerDown) {
2463 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2464 sp<FakeWindowHandle> window =
2465 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2466 window->setFrame(Rect(0, 0, 400, 400));
2467
2468 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2469
2470 const int32_t touchDeviceId = 4;
2471 const int32_t mouseDeviceId = 6;
2472 NotifyMotionArgs args;
2473
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08002474 // First touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002475 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2476 .deviceId(touchDeviceId)
2477 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2478 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002479 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002480 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2481 .deviceId(touchDeviceId)
2482 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2483 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2484 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002485 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002486 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
2487 .deviceId(touchDeviceId)
2488 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2489 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2490 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002491 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2492 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2493 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
2494
2495 // Mouse down. The touch should be canceled
Prabir Pradhan678438e2023-04-13 19:32:51 +00002496 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2497 .deviceId(mouseDeviceId)
2498 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2499 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
2500 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002501
2502 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08002503 WithPointerCount(1u)));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002504 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2505
Prabir Pradhan678438e2023-04-13 19:32:51 +00002506 mDispatcher->notifyMotion(
2507 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2508 .deviceId(mouseDeviceId)
2509 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2510 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2511 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
2512 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002513 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2514
2515 // Second touch pointer down.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002516 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2517 .deviceId(touchDeviceId)
2518 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2519 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2520 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002521 // The pointer_down event should be ignored
2522 window->assertNoEvents();
2523}
2524
2525/**
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002526 * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels
2527 * the injected event.
2528 */
2529TEST_F(InputDispatcherTest, UnfinishedInjectedEvent) {
2530 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2531 sp<FakeWindowHandle> window =
2532 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2533 window->setFrame(Rect(0, 0, 400, 400));
2534
2535 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2536
2537 const int32_t touchDeviceId = 4;
2538 NotifyMotionArgs args;
2539 // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after
2540 // completion.
2541 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2542 injectMotionEvent(mDispatcher,
2543 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2544 .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002545 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002546 .x(50)
2547 .y(50))
2548 .build()));
2549 window->consumeMotionEvent(
2550 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
2551
2552 // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer
2553 // should be canceled and the new gesture should take over.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002554 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2555 .deviceId(touchDeviceId)
2556 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2557 .build());
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002558
2559 window->consumeMotionEvent(
2560 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
2561 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2562}
2563
2564/**
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002565 * This test is similar to the test above, but the sequence of injected events is different.
2566 *
2567 * Two windows: a window on the left and a window on the right.
2568 * Mouse is hovered over the left window.
2569 * Next, we tap on the left window, where the cursor was last seen.
2570 *
2571 * After that, we inject one finger down onto the right window, and then a second finger down onto
2572 * the left window.
2573 * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right
2574 * window (first), and then another on the left window (second).
2575 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2576 * In the buggy implementation, second finger down on the left window would cause a crash.
2577 */
2578TEST_F(InputDispatcherTest, HoverTapAndSplitTouch) {
2579 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2580 sp<FakeWindowHandle> leftWindow =
2581 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2582 leftWindow->setFrame(Rect(0, 0, 200, 200));
2583
2584 sp<FakeWindowHandle> rightWindow =
2585 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2586 rightWindow->setFrame(Rect(200, 0, 400, 200));
2587
2588 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2589
2590 const int32_t mouseDeviceId = 6;
2591 const int32_t touchDeviceId = 4;
2592 // Hover over the left window. Keep the cursor there.
2593 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2594 injectMotionEvent(mDispatcher,
2595 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2596 AINPUT_SOURCE_MOUSE)
2597 .deviceId(mouseDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002598 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002599 .x(50)
2600 .y(50))
2601 .build()));
2602 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2603
2604 // Tap on left window
2605 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2606 injectMotionEvent(mDispatcher,
2607 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2608 AINPUT_SOURCE_TOUCHSCREEN)
2609 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002610 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002611 .x(100)
2612 .y(100))
2613 .build()));
2614
2615 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2616 injectMotionEvent(mDispatcher,
2617 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2618 AINPUT_SOURCE_TOUCHSCREEN)
2619 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002620 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002621 .x(100)
2622 .y(100))
2623 .build()));
2624 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2625 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2626 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2627
2628 // First finger down on right window
2629 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2630 injectMotionEvent(mDispatcher,
2631 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2632 AINPUT_SOURCE_TOUCHSCREEN)
2633 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002634 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002635 .x(300)
2636 .y(100))
2637 .build()));
2638 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2639
2640 // Second finger down on the left window
2641 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2642 injectMotionEvent(mDispatcher,
2643 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2644 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002645 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002646 .x(300)
2647 .y(100))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002648 .pointer(PointerBuilder(1, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002649 .x(100)
2650 .y(100))
2651 .build()));
2652 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2653 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE));
2654
2655 // No more events
2656 leftWindow->assertNoEvents();
2657 rightWindow->assertNoEvents();
2658}
2659
2660/**
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002661 * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs.
2662 * While the touch is down, new hover events from the stylus device should be ignored. After the
2663 * touch is gone, stylus hovering should start working again.
2664 */
2665TEST_F(InputDispatcherTest, StylusHoverAndTouchTap) {
2666 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2667 sp<FakeWindowHandle> window =
2668 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2669 window->setFrame(Rect(0, 0, 200, 200));
2670
2671 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2672
2673 const int32_t stylusDeviceId = 5;
2674 const int32_t touchDeviceId = 4;
2675 // Start hovering with stylus
2676 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2677 injectMotionEvent(mDispatcher,
2678 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2679 AINPUT_SOURCE_STYLUS)
2680 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002681 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002682 .x(50)
2683 .y(50))
2684 .build()));
2685 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2686
2687 // Finger down on the window
2688 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2689 injectMotionEvent(mDispatcher,
2690 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2691 AINPUT_SOURCE_TOUCHSCREEN)
2692 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002693 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002694 .x(100)
2695 .y(100))
2696 .build()));
2697 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2698 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2699
2700 // Try to continue hovering with stylus. Since we are already down, injection should fail
2701 ASSERT_EQ(InputEventInjectionResult::FAILED,
2702 injectMotionEvent(mDispatcher,
2703 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2704 AINPUT_SOURCE_STYLUS)
2705 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002706 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002707 .x(50)
2708 .y(50))
2709 .build()));
2710 // No event should be sent. This event should be ignored because a pointer from another device
2711 // is already down.
2712
2713 // Lift up the finger
2714 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2715 injectMotionEvent(mDispatcher,
2716 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2717 AINPUT_SOURCE_TOUCHSCREEN)
2718 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002719 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002720 .x(100)
2721 .y(100))
2722 .build()));
2723 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2724
2725 // Now that the touch is gone, stylus hovering should start working again
2726 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2727 injectMotionEvent(mDispatcher,
2728 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2729 AINPUT_SOURCE_STYLUS)
2730 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002731 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002732 .x(50)
2733 .y(50))
2734 .build()));
2735 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2736 // No more events
2737 window->assertNoEvents();
2738}
2739
2740/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002741 * A spy window above a window with no input channel.
2742 * Start hovering with a stylus device, and then tap with it.
2743 * Ensure spy window receives the entire sequence.
2744 */
2745TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) {
2746 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2747 sp<FakeWindowHandle> spyWindow =
2748 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2749 spyWindow->setFrame(Rect(0, 0, 200, 200));
2750 spyWindow->setTrustedOverlay(true);
2751 spyWindow->setSpy(true);
2752 sp<FakeWindowHandle> window =
2753 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2754 window->setNoInputChannel(true);
2755 window->setFrame(Rect(0, 0, 200, 200));
2756
2757 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2758
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002759 // Start hovering with stylus
Prabir Pradhan678438e2023-04-13 19:32:51 +00002760 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2761 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2762 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002763 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2764 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00002765 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2766 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2767 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002768 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2769
2770 // Stylus touches down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002771 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2772 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2773 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002774 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2775
2776 // Stylus goes up
Prabir Pradhan678438e2023-04-13 19:32:51 +00002777 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS)
2778 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2779 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002780 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP));
2781
2782 // Again hover
Prabir Pradhan678438e2023-04-13 19:32:51 +00002783 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2784 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2785 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002786 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2787 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00002788 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2789 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2790 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002791 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2792
2793 // No more events
2794 spyWindow->assertNoEvents();
2795 window->assertNoEvents();
2796}
2797
2798/**
2799 * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream.
2800 * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse
2801 * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active.
2802 * While the mouse is down, new move events from the touch device should be ignored.
2803 */
2804TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) {
2805 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2806 sp<FakeWindowHandle> spyWindow =
2807 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2808 spyWindow->setFrame(Rect(0, 0, 200, 200));
2809 spyWindow->setTrustedOverlay(true);
2810 spyWindow->setSpy(true);
2811 sp<FakeWindowHandle> window =
2812 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2813 window->setFrame(Rect(0, 0, 200, 200));
2814
2815 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2816
2817 const int32_t mouseDeviceId = 7;
2818 const int32_t touchDeviceId = 4;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002819
2820 // Hover a bit with mouse first
Prabir Pradhan678438e2023-04-13 19:32:51 +00002821 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2822 .deviceId(mouseDeviceId)
2823 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2824 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002825 spyWindow->consumeMotionEvent(
2826 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2827 window->consumeMotionEvent(
2828 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2829
2830 // Start touching
Prabir Pradhan678438e2023-04-13 19:32:51 +00002831 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2832 .deviceId(touchDeviceId)
2833 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
2834 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002835 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2836 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2837 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2838 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2839
Prabir Pradhan678438e2023-04-13 19:32:51 +00002840 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2841 .deviceId(touchDeviceId)
2842 .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55))
2843 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002844 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2845 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2846
2847 // Pilfer the stream
2848 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
2849 window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
2850
Prabir Pradhan678438e2023-04-13 19:32:51 +00002851 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2852 .deviceId(touchDeviceId)
2853 .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60))
2854 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002855 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2856
2857 // Mouse down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002858 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2859 .deviceId(mouseDeviceId)
2860 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2861 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2862 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002863
2864 spyWindow->consumeMotionEvent(
2865 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
2866 spyWindow->consumeMotionEvent(
2867 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2868 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2869
Prabir Pradhan678438e2023-04-13 19:32:51 +00002870 mDispatcher->notifyMotion(
2871 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2872 .deviceId(mouseDeviceId)
2873 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2874 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2875 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2876 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002877 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2878 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2879
2880 // Mouse move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00002881 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
2882 .deviceId(mouseDeviceId)
2883 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2884 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110))
2885 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002886 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2887 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2888
2889 // Touch move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00002890 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2891 .deviceId(touchDeviceId)
2892 .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65))
2893 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002894
2895 // No more events
2896 spyWindow->assertNoEvents();
2897 window->assertNoEvents();
2898}
2899
2900/**
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002901 * On the display, have a single window, and also an area where there's no window.
2902 * First pointer touches the "no window" area of the screen. Second pointer touches the window.
2903 * Make sure that the window receives the second pointer, and first pointer is simply ignored.
2904 */
2905TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) {
2906 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2907 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002908 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002909
2910 mDispatcher->setInputWindows({{DISPLAY_ID, {window}}});
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002911
2912 // Touch down on the empty space
Prabir Pradhan678438e2023-04-13 19:32:51 +00002913 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002914
2915 mDispatcher->waitForIdle();
2916 window->assertNoEvents();
2917
2918 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00002919 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002920 mDispatcher->waitForIdle();
2921 window->consumeMotionDown();
2922}
2923
2924/**
2925 * Same test as above, but instead of touching the empty space, the first touch goes to
2926 * non-touchable window.
2927 */
2928TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) {
2929 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2930 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002931 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002932 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
2933 window1->setTouchable(false);
2934 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002935 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002936 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
2937
2938 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
2939
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002940 // Touch down on the non-touchable window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002941 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002942
2943 mDispatcher->waitForIdle();
2944 window1->assertNoEvents();
2945 window2->assertNoEvents();
2946
2947 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00002948 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002949 mDispatcher->waitForIdle();
2950 window2->consumeMotionDown();
2951}
2952
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002953/**
2954 * When splitting touch events the downTime should be adjusted such that the downTime corresponds
2955 * to the event time of the first ACTION_DOWN sent to the particular window.
2956 */
2957TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) {
2958 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2959 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002960 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002961 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
2962 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002963 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002964 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
2965
2966 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
2967
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002968 // Touch down on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002969 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002970
2971 mDispatcher->waitForIdle();
2972 InputEvent* inputEvent1 = window1->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002973 ASSERT_NE(inputEvent1, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002974 window2->assertNoEvents();
2975 MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1);
2976 nsecs_t downTimeForWindow1 = motionEvent1.getDownTime();
2977 ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime());
2978
2979 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00002980 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002981 mDispatcher->waitForIdle();
2982 InputEvent* inputEvent2 = window2->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002983 ASSERT_NE(inputEvent2, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002984 MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2);
2985 nsecs_t downTimeForWindow2 = motionEvent2.getDownTime();
2986 ASSERT_NE(downTimeForWindow1, downTimeForWindow2);
2987 ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime());
2988
2989 // Now move the pointer on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002990 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002991 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002992 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002993
2994 // Now add new touch down on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002995 mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002996 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002997 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002998
2999 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
3000 window1->consumeMotionMove();
3001 window1->assertNoEvents();
3002
3003 // Now move the pointer on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003004 mDispatcher->notifyMotion(
3005 generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003006 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003007 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003008
Prabir Pradhan678438e2023-04-13 19:32:51 +00003009 mDispatcher->notifyMotion(
3010 generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003011 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003012 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003013}
3014
Garfield Tandf26e862020-07-01 20:18:19 -07003015TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07003016 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07003017 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003018 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003019 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003020 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003021 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003022 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003023
3024 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3025
3026 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
3027
3028 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003029 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003030 injectMotionEvent(mDispatcher,
3031 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3032 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003033 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003034 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003035 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003036
3037 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003038 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003039 injectMotionEvent(mDispatcher,
3040 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3041 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003042 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003043 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003044 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3045 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003046
3047 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003048 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003049 injectMotionEvent(mDispatcher,
3050 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3051 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003052 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003053 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003054 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3055 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003056
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003057 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003058 injectMotionEvent(mDispatcher,
3059 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3060 AINPUT_SOURCE_MOUSE)
3061 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3062 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003063 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003064 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003065 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003066
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003067 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003068 injectMotionEvent(mDispatcher,
3069 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3070 AINPUT_SOURCE_MOUSE)
3071 .buttonState(0)
3072 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003073 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003074 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003075 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003076
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003077 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003078 injectMotionEvent(mDispatcher,
3079 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3080 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003081 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003082 .build()));
3083 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3084
3085 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003086 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003087 injectMotionEvent(mDispatcher,
3088 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3089 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003090 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003091 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003092 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003093
3094 // No more events
3095 windowLeft->assertNoEvents();
3096 windowRight->assertNoEvents();
3097}
3098
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003099/**
3100 * Put two fingers down (and don't release them) and click the mouse button.
3101 * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the
3102 * currently active gesture should be canceled, and the new one should proceed.
3103 */
3104TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) {
3105 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3106 sp<FakeWindowHandle> window =
3107 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3108 window->setFrame(Rect(0, 0, 600, 800));
3109
3110 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3111
3112 const int32_t touchDeviceId = 4;
3113 const int32_t mouseDeviceId = 6;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003114
3115 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003116 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3117 .deviceId(touchDeviceId)
3118 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3119 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003120
Prabir Pradhan678438e2023-04-13 19:32:51 +00003121 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3122 .deviceId(touchDeviceId)
3123 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3124 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
3125 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003126 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3127 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
3128
3129 // Inject a series of mouse events for a mouse click
Prabir Pradhan678438e2023-04-13 19:32:51 +00003130 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3131 .deviceId(mouseDeviceId)
3132 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3133 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
3134 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003135 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
3136 WithPointerCount(2u)));
3137 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3138
Prabir Pradhan678438e2023-04-13 19:32:51 +00003139 mDispatcher->notifyMotion(
3140 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
3141 .deviceId(mouseDeviceId)
3142 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3143 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
3144 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
3145 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003146 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3147
3148 // Try to send more touch events while the mouse is down. Since it's a continuation of an
3149 // already canceled gesture, it should be ignored.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003150 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3151 .deviceId(touchDeviceId)
3152 .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101))
3153 .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121))
3154 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003155 window->assertNoEvents();
3156}
3157
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003158TEST_F(InputDispatcherTest, HoverWithSpyWindows) {
3159 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3160
3161 sp<FakeWindowHandle> spyWindow =
3162 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3163 spyWindow->setFrame(Rect(0, 0, 600, 800));
3164 spyWindow->setTrustedOverlay(true);
3165 spyWindow->setSpy(true);
3166 sp<FakeWindowHandle> window =
3167 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3168 window->setFrame(Rect(0, 0, 600, 800));
3169
3170 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3171 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
3172
3173 // Send mouse cursor to the window
3174 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3175 injectMotionEvent(mDispatcher,
3176 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3177 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003178 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003179 .x(100)
3180 .y(100))
3181 .build()));
3182
3183 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3184 WithSource(AINPUT_SOURCE_MOUSE)));
3185 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3186 WithSource(AINPUT_SOURCE_MOUSE)));
3187
3188 window->assertNoEvents();
3189 spyWindow->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07003190}
3191
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003192TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) {
3193 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3194
3195 sp<FakeWindowHandle> spyWindow =
3196 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3197 spyWindow->setFrame(Rect(0, 0, 600, 800));
3198 spyWindow->setTrustedOverlay(true);
3199 spyWindow->setSpy(true);
3200 sp<FakeWindowHandle> window =
3201 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3202 window->setFrame(Rect(0, 0, 600, 800));
3203
3204 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3205 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
3206
3207 // Send mouse cursor to the window
3208 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3209 injectMotionEvent(mDispatcher,
3210 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3211 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003212 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003213 .x(100)
3214 .y(100))
3215 .build()));
3216
3217 // Move mouse cursor
3218 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3219 injectMotionEvent(mDispatcher,
3220 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3221 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003222 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003223 .x(110)
3224 .y(110))
3225 .build()));
3226
3227 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3228 WithSource(AINPUT_SOURCE_MOUSE)));
3229 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3230 WithSource(AINPUT_SOURCE_MOUSE)));
3231 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3232 WithSource(AINPUT_SOURCE_MOUSE)));
3233 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3234 WithSource(AINPUT_SOURCE_MOUSE)));
3235 // Touch down on the window
3236 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3237 injectMotionEvent(mDispatcher,
3238 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3239 AINPUT_SOURCE_TOUCHSCREEN)
3240 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003241 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003242 .x(200)
3243 .y(200))
3244 .build()));
3245 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3246 WithSource(AINPUT_SOURCE_MOUSE)));
3247 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3248 WithSource(AINPUT_SOURCE_MOUSE)));
3249 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3250 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3251 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3252 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3253
3254 // pilfer the motion, retaining the gesture on the spy window.
3255 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
3256 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
3257 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3258
3259 // Touch UP on the window
3260 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3261 injectMotionEvent(mDispatcher,
3262 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3263 AINPUT_SOURCE_TOUCHSCREEN)
3264 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003265 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003266 .x(200)
3267 .y(200))
3268 .build()));
3269 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3270 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3271
3272 // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going
3273 // to send a new gesture. It should again go to both windows (spy and the window below), just
3274 // like the first gesture did, before pilfering. The window configuration has not changed.
3275
3276 // One more tap - DOWN
3277 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3278 injectMotionEvent(mDispatcher,
3279 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3280 AINPUT_SOURCE_TOUCHSCREEN)
3281 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003282 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003283 .x(250)
3284 .y(250))
3285 .build()));
3286 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3287 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3288 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3289 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3290
3291 // Touch UP on the window
3292 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3293 injectMotionEvent(mDispatcher,
3294 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3295 AINPUT_SOURCE_TOUCHSCREEN)
3296 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003297 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003298 .x(250)
3299 .y(250))
3300 .build()));
3301 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3302 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3303 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3304 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3305
3306 window->assertNoEvents();
3307 spyWindow->assertNoEvents();
3308}
3309
Garfield Tandf26e862020-07-01 20:18:19 -07003310// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
3311// directly in this test.
3312TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07003313 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07003314 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003315 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003316 window->setFrame(Rect(0, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003317
3318 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3319
3320 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3321
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003322 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003323 injectMotionEvent(mDispatcher,
3324 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3325 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003326 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003327 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003328 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003329 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003330 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003331 injectMotionEvent(mDispatcher,
3332 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3333 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003334 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003335 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003336 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3337 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003338
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003339 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003340 injectMotionEvent(mDispatcher,
3341 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3342 AINPUT_SOURCE_MOUSE)
3343 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3344 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003345 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003346 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003347 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003348
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003349 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003350 injectMotionEvent(mDispatcher,
3351 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3352 AINPUT_SOURCE_MOUSE)
3353 .buttonState(0)
3354 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003355 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003356 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003357 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003358
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003359 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003360 injectMotionEvent(mDispatcher,
3361 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3362 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003363 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003364 .build()));
3365 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3366
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07003367 // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the
3368 // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail.
3369 ASSERT_EQ(InputEventInjectionResult::FAILED,
Garfield Tandf26e862020-07-01 20:18:19 -07003370 injectMotionEvent(mDispatcher,
3371 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
3372 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003373 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003374 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003375 window->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07003376}
3377
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003378/**
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003379 * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event
3380 * is generated.
3381 */
3382TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) {
3383 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3384 sp<FakeWindowHandle> window =
3385 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3386 window->setFrame(Rect(0, 0, 1200, 800));
3387
3388 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3389
3390 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3391
3392 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3393 injectMotionEvent(mDispatcher,
3394 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3395 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003396 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003397 .x(300)
3398 .y(400))
3399 .build()));
3400 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
3401
3402 // Remove the window, but keep the channel.
3403 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
3404 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3405}
3406
3407/**
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003408 * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT.
3409 */
3410TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) {
3411 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3412 sp<FakeWindowHandle> window =
3413 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3414 window->setFrame(Rect(0, 0, 100, 100));
3415
3416 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3417
3418 const int32_t mouseDeviceId = 7;
3419 const int32_t touchDeviceId = 4;
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003420
3421 // Start hovering with the mouse
Prabir Pradhan678438e2023-04-13 19:32:51 +00003422 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
3423 .deviceId(mouseDeviceId)
3424 .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10))
3425 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003426 window->consumeMotionEvent(
3427 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3428
3429 // Touch goes down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003430 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3431 .deviceId(touchDeviceId)
3432 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
3433 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003434
3435 window->consumeMotionEvent(
3436 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
3437 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3438}
3439
3440/**
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003441 * Inject a mouse hover event followed by a tap from touchscreen.
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003442 * The tap causes a HOVER_EXIT event to be generated because the current event
3443 * stream's source has been switched.
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003444 */
3445TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) {
3446 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3447 sp<FakeWindowHandle> window =
3448 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3449 window->setFrame(Rect(0, 0, 100, 100));
3450
3451 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3452
3453 // Inject a hover_move from mouse.
3454 NotifyMotionArgs motionArgs =
3455 generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE,
3456 ADISPLAY_ID_DEFAULT, {{50, 50}});
3457 motionArgs.xCursorPosition = 50;
3458 motionArgs.yCursorPosition = 50;
Prabir Pradhan678438e2023-04-13 19:32:51 +00003459 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003460 ASSERT_NO_FATAL_FAILURE(
3461 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3462 WithSource(AINPUT_SOURCE_MOUSE))));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003463
3464 // Tap on the window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003465 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3466 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3467 {{10, 10}}));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003468 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003469 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3470 WithSource(AINPUT_SOURCE_MOUSE))));
3471
3472 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003473 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3474 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
3475
Prabir Pradhan678438e2023-04-13 19:32:51 +00003476 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3477 ADISPLAY_ID_DEFAULT, {{10, 10}}));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003478 ASSERT_NO_FATAL_FAILURE(
3479 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3480 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
3481}
3482
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003483TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) {
3484 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3485 sp<FakeWindowHandle> windowDefaultDisplay =
3486 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
3487 ADISPLAY_ID_DEFAULT);
3488 windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800));
3489 sp<FakeWindowHandle> windowSecondDisplay =
3490 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay",
3491 SECOND_DISPLAY_ID);
3492 windowSecondDisplay->setFrame(Rect(0, 0, 600, 800));
3493
3494 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
3495 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
3496
3497 // Set cursor position in window in default display and check that hover enter and move
3498 // events are generated.
3499 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3500 injectMotionEvent(mDispatcher,
3501 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3502 AINPUT_SOURCE_MOUSE)
3503 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003504 .pointer(PointerBuilder(0, ToolType::MOUSE)
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003505 .x(300)
3506 .y(600))
3507 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003508 windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003509
3510 // Remove all windows in secondary display and check that no event happens on window in
3511 // primary display.
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003512 mDispatcher->setInputWindows(
3513 {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003514 windowDefaultDisplay->assertNoEvents();
3515
3516 // Move cursor position in window in default display and check that only hover move
3517 // event is generated and not hover enter event.
3518 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
3519 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
3520 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3521 injectMotionEvent(mDispatcher,
3522 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3523 AINPUT_SOURCE_MOUSE)
3524 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003525 .pointer(PointerBuilder(0, ToolType::MOUSE)
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003526 .x(400)
3527 .y(700))
3528 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003529 windowDefaultDisplay->consumeMotionEvent(
3530 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3531 WithSource(AINPUT_SOURCE_MOUSE)));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003532 windowDefaultDisplay->assertNoEvents();
3533}
3534
Garfield Tan00f511d2019-06-12 16:55:40 -07003535TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07003536 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07003537
3538 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003539 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003540 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07003541 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003542 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003543 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07003544
3545 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3546
Arthur Hung72d8dc32020-03-28 00:48:39 +00003547 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07003548
3549 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
3550 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003551 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07003552 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003553 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003554 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003555 windowRight->assertNoEvents();
3556}
3557
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003558TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07003559 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003560 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3561 "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07003562 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003563
Arthur Hung72d8dc32020-03-28 00:48:39 +00003564 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003565 setFocusedWindow(window);
3566
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003567 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003568
Prabir Pradhan678438e2023-04-13 19:32:51 +00003569 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003570
3571 // Window should receive key down event.
3572 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3573
3574 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
3575 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003576 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07003577 window->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003578 AKEY_EVENT_FLAG_CANCELED);
3579}
3580
3581TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07003582 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003583 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3584 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003585
Arthur Hung72d8dc32020-03-28 00:48:39 +00003586 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003587
Prabir Pradhan678438e2023-04-13 19:32:51 +00003588 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3589 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003590
3591 // Window should receive motion down event.
3592 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3593
3594 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
3595 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003596 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08003597 window->consumeMotionEvent(
3598 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003599}
3600
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -07003601TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) {
3602 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3603 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3604 "Fake Window", ADISPLAY_ID_DEFAULT);
3605
3606 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3607
3608 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3609 .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10))
3610 .build());
3611
3612 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3613
3614 // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT
3615 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
3616 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3617
3618 // After the device has been reset, a new hovering stream can be sent to the window
3619 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3620 .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15))
3621 .build());
3622 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3623}
3624
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003625TEST_F(InputDispatcherTest, InterceptKeyByPolicy) {
3626 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003627 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3628 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003629 window->setFocusable(true);
3630
3631 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3632 setFocusedWindow(window);
3633
3634 window->consumeFocusEvent(true);
3635
Prabir Pradhan678438e2023-04-13 19:32:51 +00003636 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003637 const std::chrono::milliseconds interceptKeyTimeout = 50ms;
3638 const nsecs_t injectTime = keyArgs.eventTime;
3639 mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout);
Prabir Pradhan678438e2023-04-13 19:32:51 +00003640 mDispatcher->notifyKey(keyArgs);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003641 // The dispatching time should be always greater than or equal to intercept key timeout.
3642 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3643 ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >=
3644 std::chrono::nanoseconds(interceptKeyTimeout).count());
3645}
3646
3647TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) {
3648 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003649 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3650 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003651 window->setFocusable(true);
3652
3653 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3654 setFocusedWindow(window);
3655
3656 window->consumeFocusEvent(true);
3657
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003658 mFakePolicy->setInterceptKeyTimeout(150ms);
Prabir Pradhan678438e2023-04-13 19:32:51 +00003659 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
3660 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003661
3662 // Window should receive key event immediately when same key up.
3663 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3664 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3665}
3666
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003667/**
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003668 * Two windows. First is a regular window. Second does not overlap with the first, and has
3669 * WATCH_OUTSIDE_TOUCH.
3670 * Both windows are owned by the same UID.
3671 * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero
3672 * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID.
3673 */
3674TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) {
3675 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3676 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3677 "First Window", ADISPLAY_ID_DEFAULT);
3678 window->setFrame(Rect{0, 0, 100, 100});
3679
3680 sp<FakeWindowHandle> outsideWindow =
3681 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3682 ADISPLAY_ID_DEFAULT);
3683 outsideWindow->setFrame(Rect{100, 100, 200, 200});
3684 outsideWindow->setWatchOutsideTouch(true);
3685 // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped
3686 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {outsideWindow, window}}});
3687
3688 // Tap on first window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003689 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3690 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3691 {PointF{50, 50}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003692 window->consumeMotionDown();
3693 // The coordinates of the tap in 'outsideWindow' are relative to its top left corner.
3694 // Therefore, we should offset them by (100, 100) relative to the screen's top left corner.
3695 outsideWindow->consumeMotionEvent(
3696 AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50)));
3697}
3698
3699/**
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003700 * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when
3701 * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one
3702 * ACTION_OUTSIDE event is sent per gesture.
3703 */
3704TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) {
3705 // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH.
3706 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003707 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3708 "First Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003709 window->setWatchOutsideTouch(true);
3710 window->setFrame(Rect{0, 0, 100, 100});
3711 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003712 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3713 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003714 secondWindow->setFrame(Rect{100, 100, 200, 200});
3715 sp<FakeWindowHandle> thirdWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003716 sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window",
3717 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003718 thirdWindow->setFrame(Rect{200, 200, 300, 300});
3719 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}});
3720
3721 // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003722 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3723 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3724 {PointF{-10, -10}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003725 window->assertNoEvents();
3726 secondWindow->assertNoEvents();
3727
3728 // The second pointer lands inside `secondWindow`, which should receive a DOWN event.
3729 // Now, `window` should get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003730 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3731 ADISPLAY_ID_DEFAULT,
3732 {PointF{-10, -10}, PointF{105, 105}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003733 const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}};
3734 window->consumeMotionEvent(
3735 AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers)));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003736 secondWindow->consumeMotionDown();
3737 thirdWindow->assertNoEvents();
3738
3739 // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is
3740 // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003741 mDispatcher->notifyMotion(
3742 generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3743 {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003744 window->assertNoEvents();
3745 secondWindow->consumeMotionMove();
3746 thirdWindow->consumeMotionDown();
3747}
3748
Prabir Pradhan814fe082022-07-22 20:22:18 +00003749TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) {
3750 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003751 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3752 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003753 window->setFocusable(true);
3754
Patrick Williamsd828f302023-04-28 17:52:08 -05003755 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00003756 setFocusedWindow(window);
3757
3758 window->consumeFocusEvent(true);
3759
Prabir Pradhan678438e2023-04-13 19:32:51 +00003760 const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3761 const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
3762 mDispatcher->notifyKey(keyDown);
3763 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003764
3765 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3766 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3767
3768 // All windows are removed from the display. Ensure that we can no longer dispatch to it.
Patrick Williamsd828f302023-04-28 17:52:08 -05003769 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00003770
3771 window->consumeFocusEvent(false);
3772
Prabir Pradhan678438e2023-04-13 19:32:51 +00003773 mDispatcher->notifyKey(keyDown);
3774 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003775 window->assertNoEvents();
3776}
3777
Arthur Hung96483742022-11-15 03:30:48 +00003778TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) {
3779 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3780 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3781 "Fake Window", ADISPLAY_ID_DEFAULT);
3782 // Ensure window is non-split and have some transform.
3783 window->setPreventSplitting(true);
3784 window->setWindowOffset(20, 40);
Patrick Williamsd828f302023-04-28 17:52:08 -05003785 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung96483742022-11-15 03:30:48 +00003786
3787 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3788 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3789 {50, 50}))
3790 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3791 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3792
3793 const MotionEvent secondFingerDownEvent =
3794 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3795 .displayId(ADISPLAY_ID_DEFAULT)
3796 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003797 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
3798 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50))
Arthur Hung96483742022-11-15 03:30:48 +00003799 .build();
3800 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3801 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
3802 InputEventInjectionSync::WAIT_FOR_RESULT))
3803 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3804
3805 const MotionEvent* event = window->consumeMotion();
3806 EXPECT_EQ(POINTER_1_DOWN, event->getAction());
3807 EXPECT_EQ(70, event->getX(0)); // 50 + 20
3808 EXPECT_EQ(90, event->getY(0)); // 50 + 40
3809 EXPECT_EQ(-10, event->getX(1)); // -30 + 20
3810 EXPECT_EQ(-10, event->getY(1)); // -50 + 40
3811}
3812
Harry Cuttsb166c002023-05-09 13:06:05 +00003813TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) {
3814 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3815 sp<FakeWindowHandle> window =
3816 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3817 window->setFrame(Rect(0, 0, 400, 400));
3818 sp<FakeWindowHandle> trustedOverlay =
3819 sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay",
3820 ADISPLAY_ID_DEFAULT);
3821 trustedOverlay->setSpy(true);
3822 trustedOverlay->setTrustedOverlay(true);
3823
3824 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {trustedOverlay, window}}});
3825
3826 // Start a three-finger touchpad swipe
3827 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3828 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3829 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3830 .build());
3831 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
3832 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3833 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3834 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3835 .build());
3836 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
3837 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3838 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3839 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
3840 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3841 .build());
3842
3843 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3844 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
3845 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN));
3846
3847 // Move the swipe a bit
3848 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
3849 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3850 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3851 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3852 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3853 .build());
3854
3855 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3856
3857 // End the swipe
3858 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
3859 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3860 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3861 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3862 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3863 .build());
3864 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
3865 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3866 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3867 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3868 .build());
3869 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
3870 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3871 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3872 .build());
3873
3874 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP));
3875 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
3876 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP));
3877
3878 window->assertNoEvents();
3879}
3880
3881TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) {
3882 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3883 sp<FakeWindowHandle> window =
3884 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3885 window->setFrame(Rect(0, 0, 400, 400));
3886 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3887
3888 // Start a three-finger touchpad swipe
3889 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3890 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3891 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3892 .build());
3893 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
3894 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3895 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3896 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3897 .build());
3898 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
3899 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3900 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3901 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
3902 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3903 .build());
3904
3905 // Move the swipe a bit
3906 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
3907 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3908 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3909 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3910 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3911 .build());
3912
3913 // End the swipe
3914 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
3915 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3916 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3917 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3918 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3919 .build());
3920 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
3921 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3922 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3923 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3924 .build());
3925 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
3926 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3927 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3928 .build());
3929
3930 window->assertNoEvents();
3931}
3932
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003933/**
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07003934 * Send a two-pointer gesture to a single window. The window's orientation changes in response to
3935 * the first pointer.
3936 * Ensure that the second pointer is not sent to the window.
3937 *
3938 * The subsequent gesture should be correctly delivered to the window.
3939 */
3940TEST_F(InputDispatcherTest, MultiplePointersWithRotatingWindow) {
3941 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3942 sp<FakeWindowHandle> window =
3943 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3944 window->setFrame(Rect(0, 0, 400, 400));
3945 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3946
3947 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
3948 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3949 .downTime(baseTime + 10)
3950 .eventTime(baseTime + 10)
3951 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3952 .build());
3953
3954 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3955
3956 // We need a new window object for the same window, because dispatcher will store objects by
3957 // reference. That means that the testing code and the dispatcher will refer to the same shared
3958 // object. Calling window->setTransform here would affect dispatcher's comparison
3959 // of the old window to the new window, since both the old window and the new window would be
3960 // updated to the same value.
3961 sp<FakeWindowHandle> windowDup = window->duplicate();
3962
3963 // Change the transform so that the orientation is now different from original.
3964 windowDup->setWindowTransform(0, -1, 1, 0);
3965
3966 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDup}}});
3967
3968 window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
3969
3970 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3971 .downTime(baseTime + 10)
3972 .eventTime(baseTime + 30)
3973 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3974 .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200))
3975 .build());
3976
3977 // Finish the gesture and start a new one. Ensure the new gesture is sent to the window
3978 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
3979 .downTime(baseTime + 10)
3980 .eventTime(baseTime + 40)
3981 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3982 .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200))
3983 .build());
3984 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
3985 .downTime(baseTime + 10)
3986 .eventTime(baseTime + 50)
3987 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3988 .build());
3989
3990 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3991 .downTime(baseTime + 60)
3992 .eventTime(baseTime + 60)
3993 .pointer(PointerBuilder(0, ToolType::FINGER).x(40).y(40))
3994 .build());
3995
3996 windowDup->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3997}
3998
3999/**
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004000 * Ensure the correct coordinate spaces are used by InputDispatcher.
4001 *
4002 * InputDispatcher works in the display space, so its coordinate system is relative to the display
4003 * panel. Windows get events in the window space, and get raw coordinates in the logical display
4004 * space.
4005 */
4006class InputDispatcherDisplayProjectionTest : public InputDispatcherTest {
4007public:
4008 void SetUp() override {
4009 InputDispatcherTest::SetUp();
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004010 removeAllWindowsAndDisplays();
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004011 }
4012
4013 void addDisplayInfo(int displayId, const ui::Transform& transform) {
4014 gui::DisplayInfo info;
4015 info.displayId = displayId;
4016 info.transform = transform;
4017 mDisplayInfos.push_back(std::move(info));
Patrick Williamsd828f302023-04-28 17:52:08 -05004018 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004019 }
4020
4021 void addWindow(const sp<WindowInfoHandle>& windowHandle) {
4022 mWindowInfos.push_back(*windowHandle->getInfo());
Patrick Williamsd828f302023-04-28 17:52:08 -05004023 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004024 }
4025
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004026 void removeAllWindowsAndDisplays() {
4027 mDisplayInfos.clear();
4028 mWindowInfos.clear();
4029 }
4030
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004031 // Set up a test scenario where the display has a scaled projection and there are two windows
4032 // on the display.
4033 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() {
4034 // The display has a projection that has a scale factor of 2 and 4 in the x and y directions
4035 // respectively.
4036 ui::Transform displayTransform;
4037 displayTransform.set(2, 0, 0, 4);
4038 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
4039
4040 std::shared_ptr<FakeApplicationHandle> application =
4041 std::make_shared<FakeApplicationHandle>();
4042
4043 // Add two windows to the display. Their frames are represented in the display space.
4044 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004045 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4046 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004047 firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform);
4048 addWindow(firstWindow);
4049
4050 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004051 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4052 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004053 secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform);
4054 addWindow(secondWindow);
4055 return {std::move(firstWindow), std::move(secondWindow)};
4056 }
4057
4058private:
4059 std::vector<gui::DisplayInfo> mDisplayInfos;
4060 std::vector<gui::WindowInfo> mWindowInfos;
4061};
4062
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004063TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) {
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004064 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4065 // Send down to the first window. The point is represented in the display space. The point is
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004066 // selected so that if the hit test was performed with the point and the bounds being in
4067 // different coordinate spaces, the event would end up in the incorrect window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004068 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4069 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4070 {PointF{75, 55}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004071
4072 firstWindow->consumeMotionDown();
4073 secondWindow->assertNoEvents();
4074}
4075
4076// Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API,
4077// the event should be treated as being in the logical display space.
4078TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) {
4079 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4080 // Send down to the first window. The point is represented in the logical display space. The
4081 // point is selected so that if the hit test was done in logical display space, then it would
4082 // end up in the incorrect window.
4083 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4084 PointF{75 * 2, 55 * 4});
4085
4086 firstWindow->consumeMotionDown();
4087 secondWindow->assertNoEvents();
4088}
4089
Prabir Pradhandaa2f142021-12-10 09:30:08 +00004090// Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed
4091// event should be treated as being in the logical display space.
4092TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) {
4093 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4094
4095 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
4096 ui::Transform injectedEventTransform;
4097 injectedEventTransform.set(matrix);
4098 const vec2 expectedPoint{75, 55}; // The injected point in the logical display space.
4099 const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint);
4100
4101 MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4102 .displayId(ADISPLAY_ID_DEFAULT)
4103 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004104 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER)
Prabir Pradhandaa2f142021-12-10 09:30:08 +00004105 .x(untransformedPoint.x)
4106 .y(untransformedPoint.y))
4107 .build();
4108 event.transform(matrix);
4109
4110 injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT,
4111 InputEventInjectionSync::WAIT_FOR_RESULT);
4112
4113 firstWindow->consumeMotionDown();
4114 secondWindow->assertNoEvents();
4115}
4116
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004117TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) {
4118 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4119
4120 // Send down to the second window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004121 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4122 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4123 {PointF{150, 220}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004124
4125 firstWindow->assertNoEvents();
4126 const MotionEvent* event = secondWindow->consumeMotion();
Siarhei Vishniakoub681c202023-05-01 11:22:33 -07004127 ASSERT_NE(nullptr, event);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004128 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction());
4129
4130 // Ensure that the events from the "getRaw" API are in logical display coordinates.
4131 EXPECT_EQ(300, event->getRawX(0));
4132 EXPECT_EQ(880, event->getRawY(0));
4133
4134 // Ensure that the x and y values are in the window's coordinate space.
4135 // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in
4136 // the logical display space. This will be the origin of the window space.
4137 EXPECT_EQ(100, event->getX(0));
4138 EXPECT_EQ(80, event->getY(0));
4139}
4140
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004141/** Ensure consistent behavior of InputDispatcher in all orientations. */
4142class InputDispatcherDisplayOrientationFixture
4143 : public InputDispatcherDisplayProjectionTest,
4144 public ::testing::WithParamInterface<ui::Rotation> {};
4145
4146// This test verifies the touchable region of a window for all rotations of the display by tapping
4147// in different locations on the display, specifically points close to the four corners of a
4148// window.
4149TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) {
4150 constexpr static int32_t displayWidth = 400;
4151 constexpr static int32_t displayHeight = 800;
4152
4153 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4154
4155 const auto rotation = GetParam();
4156
4157 // Set up the display with the specified rotation.
4158 const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270;
4159 const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth;
4160 const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight;
4161 const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation),
4162 logicalDisplayWidth, logicalDisplayHeight);
4163 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
4164
4165 // Create a window with its bounds determined in the logical display.
4166 const Rect frameInLogicalDisplay(100, 100, 200, 300);
4167 const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay);
4168 sp<FakeWindowHandle> window =
4169 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4170 window->setFrame(frameInDisplay, displayTransform);
4171 addWindow(window);
4172
4173 // The following points in logical display space should be inside the window.
4174 static const std::array<vec2, 4> insidePoints{
4175 {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}};
4176 for (const auto pointInsideWindow : insidePoints) {
4177 const vec2 p = displayTransform.inverse().transform(pointInsideWindow);
4178 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004179 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4180 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4181 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004182 window->consumeMotionDown();
4183
Prabir Pradhan678438e2023-04-13 19:32:51 +00004184 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4185 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4186 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004187 window->consumeMotionUp();
4188 }
4189
4190 // The following points in logical display space should be outside the window.
4191 static const std::array<vec2, 5> outsidePoints{
4192 {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}};
4193 for (const auto pointOutsideWindow : outsidePoints) {
4194 const vec2 p = displayTransform.inverse().transform(pointOutsideWindow);
4195 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004196 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4197 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4198 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004199
Prabir Pradhan678438e2023-04-13 19:32:51 +00004200 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4201 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4202 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004203 }
4204 window->assertNoEvents();
4205}
4206
4207// Run the precision tests for all rotations.
4208INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests,
4209 InputDispatcherDisplayOrientationFixture,
4210 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
4211 ui::ROTATION_270),
4212 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
4213 return ftl::enum_string(testParamInfo.param);
4214 });
4215
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004216using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher,
4217 sp<IBinder>, sp<IBinder>)>;
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004218
4219class TransferTouchFixture : public InputDispatcherTest,
4220 public ::testing::WithParamInterface<TransferFunction> {};
4221
4222TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07004223 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004224
4225 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004226 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004227 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4228 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004229 firstWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004230 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004231 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4232 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004233 sp<FakeWindowHandle> wallpaper =
4234 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
4235 wallpaper->setIsWallpaper(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004236 // Add the windows to the dispatcher
Arthur Hungc539dbb2022-12-08 07:45:36 +00004237 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004238
4239 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004240 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4241 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004242
Svet Ganov5d3bc372020-01-26 23:11:07 -08004243 // Only the first window should get the down event
4244 firstWindow->consumeMotionDown();
4245 secondWindow->assertNoEvents();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004246 wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004247
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004248 // Transfer touch to the second window
4249 TransferFunction f = GetParam();
4250 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4251 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004252 // The first window gets cancel and the second gets down
4253 firstWindow->consumeMotionCancel();
4254 secondWindow->consumeMotionDown();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004255 wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004256
4257 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004258 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4259 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004260 // The first window gets no events and the second gets up
4261 firstWindow->assertNoEvents();
4262 secondWindow->consumeMotionUp();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004263 wallpaper->assertNoEvents();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004264}
4265
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004266/**
4267 * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch
4268 * from. When we have spy windows, there are several windows to choose from: either spy, or the
4269 * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most
4270 * natural to the user.
4271 * In this test, we are sending a pointer to both spy window and first window. We then try to
4272 * transfer touch to the second window. The dispatcher should identify the first window as the
4273 * one that should lose the gesture, and therefore the action should be to move the gesture from
4274 * the first window to the second.
4275 * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test
4276 * the other API, as well.
4277 */
4278TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) {
4279 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4280
4281 // Create a couple of windows + a spy window
4282 sp<FakeWindowHandle> spyWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004283 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004284 spyWindow->setTrustedOverlay(true);
4285 spyWindow->setSpy(true);
4286 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004287 sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004288 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004289 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004290
4291 // Add the windows to the dispatcher
4292 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}});
4293
4294 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004295 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4296 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004297 // Only the first window and spy should get the down event
4298 spyWindow->consumeMotionDown();
4299 firstWindow->consumeMotionDown();
4300
4301 // Transfer touch to the second window. Non-spy window should be preferred over the spy window
4302 // if f === 'transferTouch'.
4303 TransferFunction f = GetParam();
4304 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4305 ASSERT_TRUE(success);
4306 // The first window gets cancel and the second gets down
4307 firstWindow->consumeMotionCancel();
4308 secondWindow->consumeMotionDown();
4309
4310 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004311 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4312 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004313 // The first window gets no events and the second+spy get up
4314 firstWindow->assertNoEvents();
4315 spyWindow->consumeMotionUp();
4316 secondWindow->consumeMotionUp();
4317}
4318
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004319TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004320 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004321
4322 PointF touchPoint = {10, 10};
4323
4324 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004325 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004326 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4327 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004328 firstWindow->setPreventSplitting(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004329 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004330 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4331 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004332 secondWindow->setPreventSplitting(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004333
4334 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004335 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004336
4337 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004338 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4339 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4340 {touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004341 // Only the first window should get the down event
4342 firstWindow->consumeMotionDown();
4343 secondWindow->assertNoEvents();
4344
4345 // Send pointer down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004346 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4347 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004348 // Only the first window should get the pointer down event
4349 firstWindow->consumeMotionPointerDown(1);
4350 secondWindow->assertNoEvents();
4351
4352 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004353 TransferFunction f = GetParam();
4354 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4355 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004356 // The first window gets cancel and the second gets down and pointer down
4357 firstWindow->consumeMotionCancel();
4358 secondWindow->consumeMotionDown();
4359 secondWindow->consumeMotionPointerDown(1);
4360
4361 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004362 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4363 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004364 // The first window gets nothing and the second gets pointer up
4365 firstWindow->assertNoEvents();
4366 secondWindow->consumeMotionPointerUp(1);
4367
4368 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004369 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4370 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004371 // The first window gets nothing and the second gets up
4372 firstWindow->assertNoEvents();
4373 secondWindow->consumeMotionUp();
4374}
4375
Arthur Hungc539dbb2022-12-08 07:45:36 +00004376TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) {
4377 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4378
4379 // Create a couple of windows
4380 sp<FakeWindowHandle> firstWindow =
4381 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4382 ADISPLAY_ID_DEFAULT);
4383 firstWindow->setDupTouchToWallpaper(true);
4384 sp<FakeWindowHandle> secondWindow =
4385 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4386 ADISPLAY_ID_DEFAULT);
4387 secondWindow->setDupTouchToWallpaper(true);
4388
4389 sp<FakeWindowHandle> wallpaper1 =
4390 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT);
4391 wallpaper1->setIsWallpaper(true);
4392
4393 sp<FakeWindowHandle> wallpaper2 =
4394 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT);
4395 wallpaper2->setIsWallpaper(true);
4396 // Add the windows to the dispatcher
4397 mDispatcher->setInputWindows(
4398 {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}});
4399
4400 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004401 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4402 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004403
4404 // Only the first window should get the down event
4405 firstWindow->consumeMotionDown();
4406 secondWindow->assertNoEvents();
4407 wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4408 wallpaper2->assertNoEvents();
4409
4410 // Transfer touch focus to the second window
4411 TransferFunction f = GetParam();
4412 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4413 ASSERT_TRUE(success);
4414
4415 // The first window gets cancel and the second gets down
4416 firstWindow->consumeMotionCancel();
4417 secondWindow->consumeMotionDown();
4418 wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4419 wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4420
4421 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004422 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4423 ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004424 // The first window gets no events and the second gets up
4425 firstWindow->assertNoEvents();
4426 secondWindow->consumeMotionUp();
4427 wallpaper1->assertNoEvents();
4428 wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4429}
4430
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004431// For the cases of single pointer touch and two pointers non-split touch, the api's
4432// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
4433// for the case where there are multiple pointers split across several windows.
4434INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
4435 ::testing::Values(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004436 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4437 sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004438 return dispatcher->transferTouch(destChannelToken,
4439 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004440 },
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004441 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4442 sp<IBinder> from, sp<IBinder> to) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004443 return dispatcher->transferTouchFocus(from, to,
Harry Cutts33476232023-01-30 19:57:29 +00004444 /*isDragAndDrop=*/false);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004445 }));
4446
Svet Ganov5d3bc372020-01-26 23:11:07 -08004447TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004448 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004449
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004450 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004451 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4452 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004453 firstWindow->setFrame(Rect(0, 0, 600, 400));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004454
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004455 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004456 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4457 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004458 secondWindow->setFrame(Rect(0, 400, 600, 800));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004459
4460 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004461 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004462
4463 PointF pointInFirst = {300, 200};
4464 PointF pointInSecond = {300, 600};
4465
4466 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004467 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4468 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4469 {pointInFirst}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004470 // Only the first window should get the down event
4471 firstWindow->consumeMotionDown();
4472 secondWindow->assertNoEvents();
4473
4474 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004475 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4476 ADISPLAY_ID_DEFAULT,
4477 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004478 // The first window gets a move and the second a down
4479 firstWindow->consumeMotionMove();
4480 secondWindow->consumeMotionDown();
4481
4482 // Transfer touch focus to the second window
4483 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
4484 // The first window gets cancel and the new gets pointer down (it already saw down)
4485 firstWindow->consumeMotionCancel();
4486 secondWindow->consumeMotionPointerDown(1);
4487
4488 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004489 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4490 ADISPLAY_ID_DEFAULT,
4491 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004492 // The first window gets nothing and the second gets pointer up
4493 firstWindow->assertNoEvents();
4494 secondWindow->consumeMotionPointerUp(1);
4495
4496 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004497 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4498 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004499 // The first window gets nothing and the second gets up
4500 firstWindow->assertNoEvents();
4501 secondWindow->consumeMotionUp();
4502}
4503
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004504// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
4505// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
4506// touch is not supported, so the touch should continue on those windows and the transferred-to
4507// window should get nothing.
4508TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
4509 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4510
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004511 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004512 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4513 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004514 firstWindow->setFrame(Rect(0, 0, 600, 400));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004515
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004516 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004517 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4518 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004519 secondWindow->setFrame(Rect(0, 400, 600, 800));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004520
4521 // Add the windows to the dispatcher
4522 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4523
4524 PointF pointInFirst = {300, 200};
4525 PointF pointInSecond = {300, 600};
4526
4527 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004528 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4529 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4530 {pointInFirst}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004531 // Only the first window should get the down event
4532 firstWindow->consumeMotionDown();
4533 secondWindow->assertNoEvents();
4534
4535 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004536 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4537 ADISPLAY_ID_DEFAULT,
4538 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004539 // The first window gets a move and the second a down
4540 firstWindow->consumeMotionMove();
4541 secondWindow->consumeMotionDown();
4542
4543 // Transfer touch focus to the second window
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004544 const bool transferred =
4545 mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004546 // The 'transferTouch' call should not succeed, because there are 2 touched windows
4547 ASSERT_FALSE(transferred);
4548 firstWindow->assertNoEvents();
4549 secondWindow->assertNoEvents();
4550
4551 // The rest of the dispatch should proceed as normal
4552 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004553 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4554 ADISPLAY_ID_DEFAULT,
4555 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004556 // The first window gets MOVE and the second gets pointer up
4557 firstWindow->consumeMotionMove();
4558 secondWindow->consumeMotionUp();
4559
4560 // Send up event to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004561 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4562 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004563 // The first window gets nothing and the second gets up
4564 firstWindow->consumeMotionUp();
4565 secondWindow->assertNoEvents();
4566}
4567
Arthur Hungabbb9d82021-09-01 14:52:30 +00004568// This case will create two windows and one mirrored window on the default display and mirror
4569// two windows on the second display. It will test if 'transferTouchFocus' works fine if we put
4570// the windows info of second display before default display.
4571TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) {
4572 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4573 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004574 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004575 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004576 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004577 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004578 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004579
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004580 sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004581 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004582
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004583 sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004584 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004585
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004586 sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004587 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004588
4589 // Update window info, let it find window handle of second display first.
4590 mDispatcher->setInputWindows(
4591 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4592 {ADISPLAY_ID_DEFAULT,
4593 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4594
4595 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4596 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4597 {50, 50}))
4598 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4599
4600 // Window should receive motion event.
4601 firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4602
4603 // Transfer touch focus
4604 ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(),
4605 secondWindowInPrimary->getToken()));
4606 // The first window gets cancel.
4607 firstWindowInPrimary->consumeMotionCancel();
4608 secondWindowInPrimary->consumeMotionDown();
4609
4610 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4611 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4612 ADISPLAY_ID_DEFAULT, {150, 50}))
4613 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4614 firstWindowInPrimary->assertNoEvents();
4615 secondWindowInPrimary->consumeMotionMove();
4616
4617 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4618 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4619 {150, 50}))
4620 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4621 firstWindowInPrimary->assertNoEvents();
4622 secondWindowInPrimary->consumeMotionUp();
4623}
4624
4625// Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use
4626// 'transferTouch' api.
4627TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
4628 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4629 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004630 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004631 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004632 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004633 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004634 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004635
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004636 sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004637 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004638
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004639 sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004640 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004641
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004642 sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004643 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004644
4645 // Update window info, let it find window handle of second display first.
4646 mDispatcher->setInputWindows(
4647 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4648 {ADISPLAY_ID_DEFAULT,
4649 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4650
4651 // Touch on second display.
4652 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4653 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50}))
4654 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4655
4656 // Window should receive motion event.
4657 firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4658
4659 // Transfer touch focus
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004660 ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004661
4662 // The first window gets cancel.
4663 firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID);
4664 secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4665
4666 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4667 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4668 SECOND_DISPLAY_ID, {150, 50}))
4669 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4670 firstWindowInPrimary->assertNoEvents();
4671 secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID);
4672
4673 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4674 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
4675 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4676 firstWindowInPrimary->assertNoEvents();
4677 secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID);
4678}
4679
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004680TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004681 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004682 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4683 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004684
Vishnu Nair47074b82020-08-14 11:54:47 -07004685 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004686 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004687 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004688
4689 window->consumeFocusEvent(true);
4690
Prabir Pradhan678438e2023-04-13 19:32:51 +00004691 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004692
4693 // Window should receive key down event.
4694 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Josep del Riob3981622023-04-18 15:49:45 +00004695
4696 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00004697 mDispatcher->waitForIdle();
Josep del Riob3981622023-04-18 15:49:45 +00004698 mFakePolicy->assertUserActivityPoked();
4699}
4700
4701TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) {
4702 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4703 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4704 "Fake Window", ADISPLAY_ID_DEFAULT);
4705
4706 window->setDisableUserActivity(true);
4707 window->setFocusable(true);
4708 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4709 setFocusedWindow(window);
4710
4711 window->consumeFocusEvent(true);
4712
4713 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4714
4715 // Window should receive key down event.
4716 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4717
4718 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00004719 mDispatcher->waitForIdle();
Josep del Riob3981622023-04-18 15:49:45 +00004720 mFakePolicy->assertUserActivityNotPoked();
4721}
4722
4723TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) {
4724 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4725 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4726 "Fake Window", ADISPLAY_ID_DEFAULT);
4727
4728 window->setFocusable(true);
4729 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4730 setFocusedWindow(window);
4731
4732 window->consumeFocusEvent(true);
4733
4734 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4735 mDispatcher->waitForIdle();
4736
4737 // System key is not passed down
4738 window->assertNoEvents();
4739
4740 // Should have poked user activity
4741 mFakePolicy->assertUserActivityPoked();
4742}
4743
4744TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) {
4745 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4746 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4747 "Fake Window", ADISPLAY_ID_DEFAULT);
4748
4749 window->setFocusable(true);
4750 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4751 setFocusedWindow(window);
4752
4753 window->consumeFocusEvent(true);
4754
4755 mDispatcher->notifyKey(generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4756 mDispatcher->waitForIdle();
4757
4758 // System key is not passed down
4759 window->assertNoEvents();
4760
4761 // Should have poked user activity
4762 mFakePolicy->assertUserActivityPoked();
4763}
4764
4765TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) {
4766 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4767 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4768 "Fake Window", ADISPLAY_ID_DEFAULT);
4769
4770 window->setDisableUserActivity(true);
4771 window->setFocusable(true);
4772 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4773 setFocusedWindow(window);
4774
4775 window->consumeFocusEvent(true);
4776
4777 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4778 mDispatcher->waitForIdle();
4779
4780 // System key is not passed down
4781 window->assertNoEvents();
4782
4783 // Should have poked user activity
4784 mFakePolicy->assertUserActivityPoked();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004785}
4786
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07004787TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) {
4788 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4789 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4790 "Fake Window", ADISPLAY_ID_DEFAULT);
4791
4792 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4793
4794 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4795 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4796 ADISPLAY_ID_DEFAULT, {100, 100}))
4797 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4798
4799 window->consumeMotionEvent(
4800 AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT)));
4801
4802 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00004803 mDispatcher->waitForIdle();
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07004804 mFakePolicy->assertUserActivityPoked();
4805}
4806
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004807TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004808 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004809 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4810 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004811
Arthur Hung72d8dc32020-03-28 00:48:39 +00004812 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004813
Prabir Pradhan678438e2023-04-13 19:32:51 +00004814 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004815 mDispatcher->waitForIdle();
4816
4817 window->assertNoEvents();
4818}
4819
4820// If a window is touchable, but does not have focus, it should receive motion events, but not keys
4821TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07004822 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004823 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4824 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004825
Arthur Hung72d8dc32020-03-28 00:48:39 +00004826 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004827
4828 // Send key
Prabir Pradhan678438e2023-04-13 19:32:51 +00004829 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004830 // Send motion
Prabir Pradhan678438e2023-04-13 19:32:51 +00004831 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4832 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004833
4834 // Window should receive only the motion event
4835 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4836 window->assertNoEvents(); // Key event or focus event will not be received
4837}
4838
arthurhungea3f4fc2020-12-21 23:18:53 +08004839TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
4840 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4841
arthurhungea3f4fc2020-12-21 23:18:53 +08004842 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004843 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4844 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004845 firstWindow->setFrame(Rect(0, 0, 600, 400));
arthurhungea3f4fc2020-12-21 23:18:53 +08004846
arthurhungea3f4fc2020-12-21 23:18:53 +08004847 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004848 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4849 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004850 secondWindow->setFrame(Rect(0, 400, 600, 800));
arthurhungea3f4fc2020-12-21 23:18:53 +08004851
4852 // Add the windows to the dispatcher
4853 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4854
4855 PointF pointInFirst = {300, 200};
4856 PointF pointInSecond = {300, 600};
4857
4858 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004859 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4860 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4861 {pointInFirst}));
arthurhungea3f4fc2020-12-21 23:18:53 +08004862 // Only the first window should get the down event
4863 firstWindow->consumeMotionDown();
4864 secondWindow->assertNoEvents();
4865
4866 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004867 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4868 ADISPLAY_ID_DEFAULT,
4869 {pointInFirst, pointInSecond}));
arthurhungea3f4fc2020-12-21 23:18:53 +08004870 // The first window gets a move and the second a down
4871 firstWindow->consumeMotionMove();
4872 secondWindow->consumeMotionDown();
4873
4874 // Send pointer cancel to the second window
4875 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004876 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08004877 {pointInFirst, pointInSecond});
4878 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
Prabir Pradhan678438e2023-04-13 19:32:51 +00004879 mDispatcher->notifyMotion(pointerUpMotionArgs);
arthurhungea3f4fc2020-12-21 23:18:53 +08004880 // The first window gets move and the second gets cancel.
4881 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
4882 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
4883
4884 // Send up event.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004885 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4886 ADISPLAY_ID_DEFAULT));
arthurhungea3f4fc2020-12-21 23:18:53 +08004887 // The first window gets up and the second gets nothing.
4888 firstWindow->consumeMotionUp();
4889 secondWindow->assertNoEvents();
4890}
4891
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004892TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
4893 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4894
4895 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004896 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004897 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4898 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
4899 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
4900 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
4901
Harry Cutts33476232023-01-30 19:57:29 +00004902 window->sendTimeline(/*inputEventId=*/1, graphicsTimeline);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004903 window->assertNoEvents();
4904 mDispatcher->waitForIdle();
4905}
4906
chaviwd1c23182019-12-20 18:44:56 -08004907class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00004908public:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004909 FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004910 int32_t displayId) {
Garfield Tan15601662020-09-22 15:32:38 -07004911 base::Result<std::unique_ptr<InputChannel>> channel =
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08004912 dispatcher->createInputMonitor(displayId, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07004913 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00004914 }
4915
chaviwd1c23182019-12-20 18:44:56 -08004916 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
4917
4918 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004919 mInputReceiver->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
4920 expectedFlags);
chaviwd1c23182019-12-20 18:44:56 -08004921 }
4922
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004923 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
4924
4925 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
4926
chaviwd1c23182019-12-20 18:44:56 -08004927 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004928 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
chaviwd1c23182019-12-20 18:44:56 -08004929 expectedDisplayId, expectedFlags);
4930 }
4931
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004932 void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004933 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004934 expectedDisplayId, expectedFlags);
4935 }
4936
chaviwd1c23182019-12-20 18:44:56 -08004937 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004938 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP,
chaviwd1c23182019-12-20 18:44:56 -08004939 expectedDisplayId, expectedFlags);
4940 }
4941
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004942 void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08004943 mInputReceiver->consumeMotionEvent(
4944 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
4945 WithDisplayId(expectedDisplayId),
4946 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004947 }
4948
Arthur Hungfbfa5722021-11-16 02:45:54 +00004949 void consumeMotionPointerDown(int32_t pointerIdx) {
4950 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
4951 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004952 mInputReceiver->consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00004953 /*expectedFlags=*/0);
Arthur Hungfbfa5722021-11-16 02:45:54 +00004954 }
4955
Evan Rosky84f07f02021-04-16 10:42:42 -07004956 MotionEvent* consumeMotion() {
4957 InputEvent* event = mInputReceiver->consume();
4958 if (!event) {
4959 ADD_FAILURE() << "No event was produced";
4960 return nullptr;
4961 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004962 if (event->getType() != InputEventType::MOTION) {
4963 ADD_FAILURE() << "Expected MotionEvent, got " << *event;
Evan Rosky84f07f02021-04-16 10:42:42 -07004964 return nullptr;
4965 }
4966 return static_cast<MotionEvent*>(event);
4967 }
4968
chaviwd1c23182019-12-20 18:44:56 -08004969 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
4970
4971private:
4972 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00004973};
4974
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004975using InputDispatcherMonitorTest = InputDispatcherTest;
4976
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004977/**
4978 * Two entities that receive touch: A window, and a global monitor.
4979 * The touch goes to the window, and then the window disappears.
4980 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
4981 * for the monitor, as well.
4982 * 1. foregroundWindow
4983 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
4984 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004985TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004986 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4987 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004988 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004989
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004990 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004991
4992 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4993 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4994 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4995 {100, 200}))
4996 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4997
4998 // Both the foreground window and the global monitor should receive the touch down
4999 window->consumeMotionDown();
5000 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
5001
5002 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5003 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5004 ADISPLAY_ID_DEFAULT, {110, 200}))
5005 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5006
5007 window->consumeMotionMove();
5008 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
5009
5010 // Now the foreground window goes away
5011 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
5012 window->consumeMotionCancel();
5013 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
5014
5015 // If more events come in, there will be no more foreground window to send them to. This will
5016 // cause a cancel for the monitor, as well.
5017 ASSERT_EQ(InputEventInjectionResult::FAILED,
5018 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5019 ADISPLAY_ID_DEFAULT, {120, 200}))
5020 << "Injection should fail because the window was removed";
5021 window->assertNoEvents();
5022 // Global monitor now gets the cancel
5023 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5024}
5025
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005026TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07005027 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005028 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5029 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005030 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00005031
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005032 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005033
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005034 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00005035 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005036 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00005037 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08005038 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005039}
5040
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005041TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
5042 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005043
Chris Yea209fde2020-07-22 13:54:51 -07005044 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005045 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5046 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005047 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00005048
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005049 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00005050 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005051 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08005052 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005053 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005054
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005055 // Pilfer pointers from the monitor.
5056 // This should not do anything and the window should continue to receive events.
5057 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00005058
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005059 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005060 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5061 ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005062 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005063
5064 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
5065 window->consumeMotionMove(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005066}
5067
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005068TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07005069 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005070 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5071 "Fake Window", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07005072 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5073 window->setWindowOffset(20, 40);
5074 window->setWindowTransform(0, 1, -1, 0);
5075
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005076 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07005077
5078 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5079 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5080 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5081 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5082 MotionEvent* event = monitor.consumeMotion();
5083 // Even though window has transform, gesture monitor must not.
5084 ASSERT_EQ(ui::Transform(), event->getTransform());
5085}
5086
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005087TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00005088 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005089 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00005090
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005091 ASSERT_EQ(InputEventInjectionResult::FAILED,
Arthur Hungb3307ee2021-10-14 10:57:37 +00005092 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005093 << "Injection should fail if there is a monitor, but no touchable window";
5094 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00005095}
5096
chaviw81e2bb92019-12-18 15:03:51 -08005097TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005098 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005099 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5100 "Fake Window", ADISPLAY_ID_DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08005101
Arthur Hung72d8dc32020-03-28 00:48:39 +00005102 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08005103
5104 NotifyMotionArgs motionArgs =
5105 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5106 ADISPLAY_ID_DEFAULT);
5107
Prabir Pradhan678438e2023-04-13 19:32:51 +00005108 mDispatcher->notifyMotion(motionArgs);
chaviw81e2bb92019-12-18 15:03:51 -08005109 // Window should receive motion down event.
5110 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5111
5112 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08005113 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08005114 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5115 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
5116 motionArgs.pointerCoords[0].getX() - 10);
5117
Prabir Pradhan678438e2023-04-13 19:32:51 +00005118 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005119 window->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005120 /*expectedFlags=*/0);
chaviw81e2bb92019-12-18 15:03:51 -08005121}
5122
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005123/**
5124 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
5125 * the device default right away. In the test scenario, we check both the default value,
5126 * and the action of enabling / disabling.
5127 */
5128TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07005129 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005130 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5131 "Test window", ADISPLAY_ID_DEFAULT);
Antonio Kantekea47acb2021-12-23 12:41:25 -08005132 const WindowInfo& windowInfo = *window->getInfo();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005133
5134 // Set focused application.
5135 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005136 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005137
5138 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00005139 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005140 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005141 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005142
5143 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07005144 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005145 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00005146 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005147
5148 SCOPED_TRACE("Disable touch mode");
Antonio Kantekea47acb2021-12-23 12:41:25 -08005149 mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00005150 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005151 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07005152 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005153 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005154 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005155 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005156
5157 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07005158 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005159 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00005160 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005161
5162 SCOPED_TRACE("Enable touch mode again");
Antonio Kantekea47acb2021-12-23 12:41:25 -08005163 mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00005164 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005165 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07005166 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005167 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005168 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005169 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005170
5171 window->assertNoEvents();
5172}
5173
Gang Wange9087892020-01-07 12:17:14 -05005174TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005175 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005176 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5177 "Test window", ADISPLAY_ID_DEFAULT);
Gang Wange9087892020-01-07 12:17:14 -05005178
5179 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005180 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05005181
Arthur Hung72d8dc32020-03-28 00:48:39 +00005182 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005183 setFocusedWindow(window);
5184
Harry Cutts33476232023-01-30 19:57:29 +00005185 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Gang Wange9087892020-01-07 12:17:14 -05005186
Prabir Pradhan678438e2023-04-13 19:32:51 +00005187 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
5188 mDispatcher->notifyKey(keyArgs);
Gang Wange9087892020-01-07 12:17:14 -05005189
5190 InputEvent* event = window->consume();
5191 ASSERT_NE(event, nullptr);
5192
5193 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
5194 ASSERT_NE(verified, nullptr);
5195 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
5196
5197 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
5198 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
5199 ASSERT_EQ(keyArgs.source, verified->source);
5200 ASSERT_EQ(keyArgs.displayId, verified->displayId);
5201
5202 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
5203
5204 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05005205 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005206 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05005207 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
5208 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
5209 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
5210 ASSERT_EQ(0, verifiedKey.repeatCount);
5211}
5212
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005213TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005214 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005215 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5216 "Test window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005217
5218 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5219
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07005220 ui::Transform transform;
5221 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
5222
5223 gui::DisplayInfo displayInfo;
5224 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
5225 displayInfo.transform = transform;
5226
Patrick Williamsd828f302023-04-28 17:52:08 -05005227 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005228
Prabir Pradhan678438e2023-04-13 19:32:51 +00005229 const NotifyMotionArgs motionArgs =
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005230 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5231 ADISPLAY_ID_DEFAULT);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005232 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005233
5234 InputEvent* event = window->consume();
5235 ASSERT_NE(event, nullptr);
5236
5237 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
5238 ASSERT_NE(verified, nullptr);
5239 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
5240
5241 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
5242 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
5243 EXPECT_EQ(motionArgs.source, verified->source);
5244 EXPECT_EQ(motionArgs.displayId, verified->displayId);
5245
5246 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
5247
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07005248 const vec2 rawXY =
5249 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
5250 motionArgs.pointerCoords[0].getXYValue());
5251 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
5252 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005253 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005254 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005255 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005256 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
5257 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
5258}
5259
chaviw09c8d2d2020-08-24 15:48:26 -07005260/**
5261 * Ensure that separate calls to sign the same data are generating the same key.
5262 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
5263 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
5264 * tests.
5265 */
5266TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
5267 KeyEvent event = getTestKeyEvent();
5268 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5269
5270 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
5271 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
5272 ASSERT_EQ(hmac1, hmac2);
5273}
5274
5275/**
5276 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
5277 */
5278TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
5279 KeyEvent event = getTestKeyEvent();
5280 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5281 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
5282
5283 verifiedEvent.deviceId += 1;
5284 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5285
5286 verifiedEvent.source += 1;
5287 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5288
5289 verifiedEvent.eventTimeNanos += 1;
5290 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5291
5292 verifiedEvent.displayId += 1;
5293 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5294
5295 verifiedEvent.action += 1;
5296 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5297
5298 verifiedEvent.downTimeNanos += 1;
5299 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5300
5301 verifiedEvent.flags += 1;
5302 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5303
5304 verifiedEvent.keyCode += 1;
5305 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5306
5307 verifiedEvent.scanCode += 1;
5308 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5309
5310 verifiedEvent.metaState += 1;
5311 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5312
5313 verifiedEvent.repeatCount += 1;
5314 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5315}
5316
Vishnu Nair958da932020-08-21 17:12:37 -07005317TEST_F(InputDispatcherTest, SetFocusedWindow) {
5318 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5319 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005320 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005321 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005322 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005323 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5324
5325 // Top window is also focusable but is not granted focus.
5326 windowTop->setFocusable(true);
5327 windowSecond->setFocusable(true);
5328 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5329 setFocusedWindow(windowSecond);
5330
5331 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005332 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5333 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005334
5335 // Focused window should receive event.
5336 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5337 windowTop->assertNoEvents();
5338}
5339
5340TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
5341 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5342 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005343 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005344 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5345
5346 window->setFocusable(true);
5347 // Release channel for window is no longer valid.
5348 window->releaseChannel();
5349 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5350 setFocusedWindow(window);
5351
5352 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005353 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5354 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005355
5356 // window channel is invalid, so it should not receive any input event.
5357 window->assertNoEvents();
5358}
5359
5360TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
5361 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5362 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005363 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005364 window->setFocusable(false);
Vishnu Nair958da932020-08-21 17:12:37 -07005365 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5366
Vishnu Nair958da932020-08-21 17:12:37 -07005367 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5368 setFocusedWindow(window);
5369
5370 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005371 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5372 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005373
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005374 // window is not focusable, so it should not receive any input event.
Vishnu Nair958da932020-08-21 17:12:37 -07005375 window->assertNoEvents();
5376}
5377
5378TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
5379 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5380 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005381 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005382 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005383 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005384 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5385
5386 windowTop->setFocusable(true);
5387 windowSecond->setFocusable(true);
5388 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5389 setFocusedWindow(windowTop);
5390 windowTop->consumeFocusEvent(true);
5391
Chavi Weingarten847e8512023-03-29 00:26:09 +00005392 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
5393 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005394 windowSecond->consumeFocusEvent(true);
5395 windowTop->consumeFocusEvent(false);
5396
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005397 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5398 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005399
5400 // Focused window should receive event.
5401 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5402}
5403
Chavi Weingarten847e8512023-03-29 00:26:09 +00005404TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) {
Vishnu Nair958da932020-08-21 17:12:37 -07005405 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5406 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005407 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005408 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005409 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005410 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5411
5412 windowTop->setFocusable(true);
Chavi Weingarten847e8512023-03-29 00:26:09 +00005413 windowSecond->setFocusable(false);
5414 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
Vishnu Nair958da932020-08-21 17:12:37 -07005415 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Chavi Weingarten847e8512023-03-29 00:26:09 +00005416 setFocusedWindow(windowTop);
5417 windowTop->consumeFocusEvent(true);
Vishnu Nair958da932020-08-21 17:12:37 -07005418
Chavi Weingarten847e8512023-03-29 00:26:09 +00005419 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5420 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005421
5422 // Event should be dropped.
Chavi Weingarten847e8512023-03-29 00:26:09 +00005423 windowTop->consumeKeyDown(ADISPLAY_ID_NONE);
Vishnu Nair958da932020-08-21 17:12:37 -07005424 windowSecond->assertNoEvents();
5425}
5426
5427TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
5428 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5429 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005430 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005431 sp<FakeWindowHandle> previousFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005432 sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow",
5433 ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005434 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5435
5436 window->setFocusable(true);
5437 previousFocusedWindow->setFocusable(true);
5438 window->setVisible(false);
5439 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
5440 setFocusedWindow(previousFocusedWindow);
5441 previousFocusedWindow->consumeFocusEvent(true);
5442
5443 // Requesting focus on invisible window takes focus from currently focused window.
5444 setFocusedWindow(window);
5445 previousFocusedWindow->consumeFocusEvent(false);
5446
5447 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005448 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005449 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
5450 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07005451
5452 // Window does not get focus event or key down.
5453 window->assertNoEvents();
5454
5455 // Window becomes visible.
5456 window->setVisible(true);
5457 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5458
5459 // Window receives focus event.
5460 window->consumeFocusEvent(true);
5461 // Focused window receives key down.
5462 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5463}
5464
Vishnu Nair599f1412021-06-21 10:39:58 -07005465TEST_F(InputDispatcherTest, DisplayRemoved) {
5466 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5467 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005468 sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
Vishnu Nair599f1412021-06-21 10:39:58 -07005469 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5470
5471 // window is granted focus.
5472 window->setFocusable(true);
5473 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5474 setFocusedWindow(window);
5475 window->consumeFocusEvent(true);
5476
5477 // When a display is removed window loses focus.
5478 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
5479 window->consumeFocusEvent(false);
5480}
5481
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005482/**
5483 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
5484 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
5485 * of the 'slipperyEnterWindow'.
5486 *
5487 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
5488 * a way so that the touched location is no longer covered by the top window.
5489 *
5490 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
5491 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
5492 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
5493 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
5494 * with ACTION_DOWN).
5495 * Thus, the touch has been transferred from the top window into the bottom window, because the top
5496 * window moved itself away from the touched location and had Flag::SLIPPERY.
5497 *
5498 * Even though the top window moved away from the touched location, it is still obscuring the bottom
5499 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
5500 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
5501 *
5502 * In this test, we ensure that the event received by the bottom window has
5503 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
5504 */
5505TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
Prabir Pradhan5735a322022-04-11 17:23:34 +00005506 constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1;
5507 constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1;
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005508
5509 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5510 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5511
5512 sp<FakeWindowHandle> slipperyExitWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005513 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005514 slipperyExitWindow->setSlippery(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005515 // Make sure this one overlaps the bottom window
5516 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
5517 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
5518 // one. Windows with the same owner are not considered to be occluding each other.
5519 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
5520
5521 sp<FakeWindowHandle> slipperyEnterWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005522 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005523 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
5524
5525 mDispatcher->setInputWindows(
5526 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5527
5528 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
Prabir Pradhan678438e2023-04-13 19:32:51 +00005529 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5530 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5531 {{50, 50}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005532 slipperyExitWindow->consumeMotionDown();
5533 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
5534 mDispatcher->setInputWindows(
5535 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5536
Prabir Pradhan678438e2023-04-13 19:32:51 +00005537 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE,
5538 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5539 {{51, 51}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005540
5541 slipperyExitWindow->consumeMotionCancel();
5542
5543 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
5544 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
5545}
5546
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07005547/**
5548 * Two windows, one on the left and another on the right. The left window is slippery. The right
5549 * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the
5550 * touch moves from the left window into the right window, the gesture should continue to go to the
5551 * left window. Touch shouldn't slip because the right window can't receive touches. This test
5552 * reproduces a crash.
5553 */
5554TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) {
5555 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5556
5557 sp<FakeWindowHandle> leftSlipperyWindow =
5558 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
5559 leftSlipperyWindow->setSlippery(true);
5560 leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100));
5561
5562 sp<FakeWindowHandle> rightDropTouchesWindow =
5563 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
5564 rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100));
5565 rightDropTouchesWindow->setDropInput(true);
5566
5567 mDispatcher->setInputWindows(
5568 {{ADISPLAY_ID_DEFAULT, {leftSlipperyWindow, rightDropTouchesWindow}}});
5569
5570 // Start touch in the left window
5571 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5572 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
5573 .build());
5574 leftSlipperyWindow->consumeMotionDown();
5575
5576 // And move it into the right window
5577 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
5578 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
5579 .build());
5580
5581 // Since the right window isn't eligible to receive input, touch does not slip.
5582 // The left window continues to receive the gesture.
5583 leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
5584 rightDropTouchesWindow->assertNoEvents();
5585}
5586
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005587TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithMotions) {
5588 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5589
5590 sp<FakeWindowHandle> leftWindow =
5591 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
5592 leftWindow->setFrame(Rect(0, 0, 100, 100));
5593 leftWindow->setOwnerInfo(1, 101);
5594
5595 sp<FakeWindowHandle> rightSpy =
5596 sp<FakeWindowHandle>::make(application, mDispatcher, "Right spy", ADISPLAY_ID_DEFAULT);
5597 rightSpy->setFrame(Rect(100, 0, 200, 100));
5598 rightSpy->setOwnerInfo(2, 102);
5599 rightSpy->setSpy(true);
5600 rightSpy->setTrustedOverlay(true);
5601
5602 sp<FakeWindowHandle> rightWindow =
5603 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
5604 rightWindow->setFrame(Rect(100, 0, 200, 100));
5605 rightWindow->setOwnerInfo(3, 103);
5606
5607 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightSpy, rightWindow, leftWindow}}});
5608
5609 // Touch in the left window
5610 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5611 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
5612 .build());
5613 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionDown());
5614 mDispatcher->waitForIdle();
5615 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {101}));
5616
5617 // Touch another finger over the right windows
5618 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5619 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
5620 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
5621 .build());
5622 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionDown());
5623 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionDown());
5624 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionMove());
5625 mDispatcher->waitForIdle();
5626 ASSERT_NO_FATAL_FAILURE(
5627 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {101, 102, 103}));
5628
5629 // Release finger over left window. The UP actions are not treated as device interaction.
5630 // The windows that did not receive the UP pointer will receive MOVE events, but since this
5631 // is part of the UP action, we do not treat this as device interaction.
5632 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
5633 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
5634 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
5635 .build());
5636 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionUp());
5637 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove());
5638 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove());
5639 mDispatcher->waitForIdle();
5640 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
5641
5642 // Move remaining finger
5643 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
5644 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
5645 .build());
5646 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove());
5647 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove());
5648 mDispatcher->waitForIdle();
5649 ASSERT_NO_FATAL_FAILURE(
5650 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {102, 103}));
5651
5652 // Release all fingers
5653 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
5654 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
5655 .build());
5656 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionUp());
5657 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionUp());
5658 mDispatcher->waitForIdle();
5659 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
5660}
5661
5662TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithKeys) {
5663 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5664
5665 sp<FakeWindowHandle> window =
5666 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
5667 window->setFrame(Rect(0, 0, 100, 100));
5668 window->setOwnerInfo(1, 101);
5669
5670 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5671 setFocusedWindow(window);
5672 ASSERT_NO_FATAL_FAILURE(window->consumeFocusEvent(true));
5673
5674 mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).build());
5675 ASSERT_NO_FATAL_FAILURE(window->consumeKeyDown(ADISPLAY_ID_DEFAULT));
5676 mDispatcher->waitForIdle();
5677 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {101}));
5678
5679 // The UP actions are not treated as device interaction.
5680 mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).build());
5681 ASSERT_NO_FATAL_FAILURE(window->consumeKeyUp(ADISPLAY_ID_DEFAULT));
5682 mDispatcher->waitForIdle();
5683 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
5684}
5685
Garfield Tan1c7bc862020-01-28 13:24:04 -08005686class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
5687protected:
5688 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
5689 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
5690
Chris Yea209fde2020-07-22 13:54:51 -07005691 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005692 sp<FakeWindowHandle> mWindow;
5693
5694 virtual void SetUp() override {
Prabir Pradhana41d2442023-04-20 21:30:40 +00005695 mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005696 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Prabir Pradhana41d2442023-04-20 21:30:40 +00005697 mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy);
Prabir Pradhan87112a72023-04-20 19:13:39 +00005698 mDispatcher->requestRefreshConfiguration();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005699 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
5700 ASSERT_EQ(OK, mDispatcher->start());
5701
5702 setUpWindow();
5703 }
5704
5705 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07005706 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005707 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005708
Vishnu Nair47074b82020-08-14 11:54:47 -07005709 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005710 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005711 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005712 mWindow->consumeFocusEvent(true);
5713 }
5714
Chris Ye2ad95392020-09-01 13:44:44 -07005715 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005716 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005717 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005718 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005719 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005720
5721 // Window should receive key down event.
5722 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5723 }
5724
5725 void expectKeyRepeatOnce(int32_t repeatCount) {
5726 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
5727 InputEvent* repeatEvent = mWindow->consume();
5728 ASSERT_NE(nullptr, repeatEvent);
5729
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005730 ASSERT_EQ(InputEventType::KEY, repeatEvent->getType());
Garfield Tan1c7bc862020-01-28 13:24:04 -08005731
5732 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
5733 uint32_t eventAction = repeatKeyEvent->getAction();
5734 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
5735 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
5736 }
5737
Chris Ye2ad95392020-09-01 13:44:44 -07005738 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005739 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005740 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005741 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005742 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005743
5744 // Window should receive key down event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005745 mWindow->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005746 /*expectedFlags=*/0);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005747 }
5748};
5749
5750TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Harry Cutts33476232023-01-30 19:57:29 +00005751 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005752 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5753 expectKeyRepeatOnce(repeatCount);
5754 }
5755}
5756
5757TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
Harry Cutts33476232023-01-30 19:57:29 +00005758 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005759 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5760 expectKeyRepeatOnce(repeatCount);
5761 }
Harry Cutts33476232023-01-30 19:57:29 +00005762 sendAndConsumeKeyDown(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005763 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08005764 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5765 expectKeyRepeatOnce(repeatCount);
5766 }
5767}
5768
5769TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005770 sendAndConsumeKeyDown(/*deviceId=*/1);
5771 expectKeyRepeatOnce(/*repeatCount=*/1);
5772 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005773 mWindow->assertNoEvents();
5774}
5775
5776TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005777 sendAndConsumeKeyDown(/*deviceId=*/1);
5778 expectKeyRepeatOnce(/*repeatCount=*/1);
5779 sendAndConsumeKeyDown(/*deviceId=*/2);
5780 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005781 // Stale key up from device 1.
Harry Cutts33476232023-01-30 19:57:29 +00005782 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005783 // Device 2 is still down, keep repeating
Harry Cutts33476232023-01-30 19:57:29 +00005784 expectKeyRepeatOnce(/*repeatCount=*/2);
5785 expectKeyRepeatOnce(/*repeatCount=*/3);
Chris Ye2ad95392020-09-01 13:44:44 -07005786 // Device 2 key up
Harry Cutts33476232023-01-30 19:57:29 +00005787 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005788 mWindow->assertNoEvents();
5789}
5790
5791TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005792 sendAndConsumeKeyDown(/*deviceId=*/1);
5793 expectKeyRepeatOnce(/*repeatCount=*/1);
5794 sendAndConsumeKeyDown(/*deviceId=*/2);
5795 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005796 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
Harry Cutts33476232023-01-30 19:57:29 +00005797 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005798 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08005799 mWindow->assertNoEvents();
5800}
5801
liushenxiang42232912021-05-21 20:24:09 +08005802TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
5803 sendAndConsumeKeyDown(DEVICE_ID);
Harry Cutts33476232023-01-30 19:57:29 +00005804 expectKeyRepeatOnce(/*repeatCount=*/1);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005805 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
liushenxiang42232912021-05-21 20:24:09 +08005806 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
5807 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
5808 mWindow->assertNoEvents();
5809}
5810
Garfield Tan1c7bc862020-01-28 13:24:04 -08005811TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005812 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005813 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005814 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5815 InputEvent* repeatEvent = mWindow->consume();
5816 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5817 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
5818 IdGenerator::getSource(repeatEvent->getId()));
5819 }
5820}
5821
5822TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005823 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005824 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005825
5826 std::unordered_set<int32_t> idSet;
5827 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5828 InputEvent* repeatEvent = mWindow->consume();
5829 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5830 int32_t id = repeatEvent->getId();
5831 EXPECT_EQ(idSet.end(), idSet.find(id));
5832 idSet.insert(id);
5833 }
5834}
5835
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005836/* Test InputDispatcher for MultiDisplay */
5837class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
5838public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005839 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005840 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08005841
Chris Yea209fde2020-07-22 13:54:51 -07005842 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005843 windowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005844 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005845
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005846 // Set focus window for primary display, but focused display would be second one.
5847 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07005848 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005849 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005850 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005851 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08005852
Chris Yea209fde2020-07-22 13:54:51 -07005853 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005854 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005855 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005856 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005857 // Set focus display to second one.
5858 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
5859 // Set focus window for second display.
5860 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07005861 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005862 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005863 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005864 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005865 }
5866
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005867 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005868 InputDispatcherTest::TearDown();
5869
Chris Yea209fde2020-07-22 13:54:51 -07005870 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005871 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07005872 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005873 windowInSecondary.clear();
5874 }
5875
5876protected:
Chris Yea209fde2020-07-22 13:54:51 -07005877 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005878 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07005879 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005880 sp<FakeWindowHandle> windowInSecondary;
5881};
5882
5883TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
5884 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005885 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5886 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5887 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005888 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08005889 windowInSecondary->assertNoEvents();
5890
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005891 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005892 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5893 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5894 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005895 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005896 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08005897}
5898
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005899TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08005900 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005901 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5902 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005903 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005904 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08005905 windowInSecondary->assertNoEvents();
5906
5907 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005908 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005909 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005910 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005911 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08005912
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005913 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00005914 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08005915
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005916 // Old focus should receive a cancel event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005917 windowInSecondary->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005918 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08005919
5920 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005921 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005922 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08005923 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005924 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08005925 windowInSecondary->assertNoEvents();
5926}
5927
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005928// Test per-display input monitors for motion event.
5929TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08005930 FakeMonitorReceiver monitorInPrimary =
5931 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5932 FakeMonitorReceiver monitorInSecondary =
5933 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005934
5935 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005936 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5937 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5938 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005939 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08005940 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005941 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005942 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005943
5944 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005945 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5946 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5947 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005948 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005949 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005950 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08005951 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005952
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08005953 // Lift up the touch from the second display
5954 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5955 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5956 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5957 windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID);
5958 monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID);
5959
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005960 // Test inject a non-pointer motion event.
5961 // If specific a display, it will dispatch to the focused window of particular display,
5962 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005963 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5964 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
5965 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005966 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005967 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005968 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08005969 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005970}
5971
5972// Test per-display input monitors for key event.
5973TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005974 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08005975 FakeMonitorReceiver monitorInPrimary =
5976 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5977 FakeMonitorReceiver monitorInSecondary =
5978 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005979
5980 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005981 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5982 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005983 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005984 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005985 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08005986 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005987}
5988
Vishnu Nair958da932020-08-21 17:12:37 -07005989TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
5990 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005991 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005992 secondWindowInPrimary->setFocusable(true);
5993 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
5994 setFocusedWindow(secondWindowInPrimary);
5995 windowInPrimary->consumeFocusEvent(false);
5996 secondWindowInPrimary->consumeFocusEvent(true);
5997
5998 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005999 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
6000 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006001 windowInPrimary->assertNoEvents();
6002 windowInSecondary->assertNoEvents();
6003 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6004}
6005
Arthur Hungdfd528e2021-12-08 13:23:04 +00006006TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
6007 FakeMonitorReceiver monitorInPrimary =
6008 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6009 FakeMonitorReceiver monitorInSecondary =
6010 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
6011
6012 // Test touch down on primary display.
6013 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6014 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
6015 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6016 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6017 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
6018
6019 // Test touch down on second display.
6020 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6021 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
6022 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6023 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
6024 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
6025
6026 // Trigger cancel touch.
6027 mDispatcher->cancelCurrentTouch();
6028 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6029 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6030 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
6031 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
6032
6033 // Test inject a move motion event, no window/monitor should receive the event.
6034 ASSERT_EQ(InputEventInjectionResult::FAILED,
6035 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6036 ADISPLAY_ID_DEFAULT, {110, 200}))
6037 << "Inject motion event should return InputEventInjectionResult::FAILED";
6038 windowInPrimary->assertNoEvents();
6039 monitorInPrimary.assertNoEvents();
6040
6041 ASSERT_EQ(InputEventInjectionResult::FAILED,
6042 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6043 SECOND_DISPLAY_ID, {110, 200}))
6044 << "Inject motion event should return InputEventInjectionResult::FAILED";
6045 windowInSecondary->assertNoEvents();
6046 monitorInSecondary.assertNoEvents();
6047}
6048
Jackal Guof9696682018-10-05 12:23:23 +08006049class InputFilterTest : public InputDispatcherTest {
6050protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006051 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
6052 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08006053 NotifyMotionArgs motionArgs;
6054
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006055 motionArgs =
6056 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006057 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006058 motionArgs =
6059 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006060 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006061 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08006062 if (expectToBeFiltered) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006063 const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue());
6064 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08006065 } else {
6066 mFakePolicy->assertFilterInputEventWasNotCalled();
6067 }
6068 }
6069
6070 void testNotifyKey(bool expectToBeFiltered) {
6071 NotifyKeyArgs keyArgs;
6072
6073 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006074 mDispatcher->notifyKey(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08006075 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006076 mDispatcher->notifyKey(keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006077 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08006078
6079 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08006080 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08006081 } else {
6082 mFakePolicy->assertFilterInputEventWasNotCalled();
6083 }
6084 }
6085};
6086
6087// Test InputFilter for MotionEvent
6088TEST_F(InputFilterTest, MotionEvent_InputFilter) {
6089 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
6090 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
6091 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
6092
6093 // Enable InputFilter
6094 mDispatcher->setInputFilterEnabled(true);
6095 // Test touch on both primary and second display, and check if both events are filtered.
6096 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
6097 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
6098
6099 // Disable InputFilter
6100 mDispatcher->setInputFilterEnabled(false);
6101 // Test touch on both primary and second display, and check if both events aren't filtered.
6102 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
6103 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
6104}
6105
6106// Test InputFilter for KeyEvent
6107TEST_F(InputFilterTest, KeyEvent_InputFilter) {
6108 // Since the InputFilter is disabled by default, check if key event aren't filtered.
6109 testNotifyKey(/*expectToBeFiltered*/ false);
6110
6111 // Enable InputFilter
6112 mDispatcher->setInputFilterEnabled(true);
6113 // Send a key event, and check if it is filtered.
6114 testNotifyKey(/*expectToBeFiltered*/ true);
6115
6116 // Disable InputFilter
6117 mDispatcher->setInputFilterEnabled(false);
6118 // Send a key event, and check if it isn't filtered.
6119 testNotifyKey(/*expectToBeFiltered*/ false);
6120}
6121
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006122// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
6123// logical display coordinate space.
6124TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
6125 ui::Transform firstDisplayTransform;
6126 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
6127 ui::Transform secondDisplayTransform;
6128 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
6129
6130 std::vector<gui::DisplayInfo> displayInfos(2);
6131 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
6132 displayInfos[0].transform = firstDisplayTransform;
6133 displayInfos[1].displayId = SECOND_DISPLAY_ID;
6134 displayInfos[1].transform = secondDisplayTransform;
6135
Patrick Williamsd828f302023-04-28 17:52:08 -05006136 mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0});
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006137
6138 // Enable InputFilter
6139 mDispatcher->setInputFilterEnabled(true);
6140
6141 // Ensure the correct transforms are used for the displays.
6142 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform);
6143 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform);
6144}
6145
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006146class InputFilterInjectionPolicyTest : public InputDispatcherTest {
6147protected:
6148 virtual void SetUp() override {
6149 InputDispatcherTest::SetUp();
6150
6151 /**
6152 * We don't need to enable input filter to test the injected event policy, but we enabled it
6153 * here to make the tests more realistic, since this policy only matters when inputfilter is
6154 * on.
6155 */
6156 mDispatcher->setInputFilterEnabled(true);
6157
6158 std::shared_ptr<InputApplicationHandle> application =
6159 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006160 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
6161 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006162
6163 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6164 mWindow->setFocusable(true);
6165 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6166 setFocusedWindow(mWindow);
6167 mWindow->consumeFocusEvent(true);
6168 }
6169
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006170 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
6171 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006172 KeyEvent event;
6173
6174 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
6175 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
6176 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
Harry Cutts33476232023-01-30 19:57:29 +00006177 KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006178 const int32_t additionalPolicyFlags =
6179 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
6180 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006181 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006182 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
6183 policyFlags | additionalPolicyFlags));
6184
6185 InputEvent* received = mWindow->consume();
6186 ASSERT_NE(nullptr, received);
6187 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006188 ASSERT_EQ(received->getType(), InputEventType::KEY);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006189 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
6190 ASSERT_EQ(flags, keyEvent.getFlags());
6191 }
6192
6193 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
6194 int32_t flags) {
6195 MotionEvent event;
6196 PointerProperties pointerProperties[1];
6197 PointerCoords pointerCoords[1];
6198 pointerProperties[0].clear();
6199 pointerProperties[0].id = 0;
6200 pointerCoords[0].clear();
6201 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
6202 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
6203
6204 ui::Transform identityTransform;
6205 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
6206 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
6207 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
6208 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
6209 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07006210 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07006211 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006212 /*pointerCount*/ 1, pointerProperties, pointerCoords);
6213
6214 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
6215 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006216 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006217 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
6218 policyFlags | additionalPolicyFlags));
6219
6220 InputEvent* received = mWindow->consume();
6221 ASSERT_NE(nullptr, received);
6222 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006223 ASSERT_EQ(received->getType(), InputEventType::MOTION);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006224 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
6225 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006226 }
6227
6228private:
6229 sp<FakeWindowHandle> mWindow;
6230};
6231
6232TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006233 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
6234 // filter. Without it, the event will no different from a regularly injected event, and the
6235 // injected device id will be overwritten.
Harry Cutts33476232023-01-30 19:57:29 +00006236 testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
6237 /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006238}
6239
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006240TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006241 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00006242 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006243 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
6244}
6245
6246TEST_F(InputFilterInjectionPolicyTest,
6247 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
6248 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00006249 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006250 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006251}
6252
6253TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
Harry Cutts33476232023-01-30 19:57:29 +00006254 testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3,
6255 /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006256}
6257
chaviwfd6d3512019-03-25 13:23:49 -07006258class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006259 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07006260 InputDispatcherTest::SetUp();
6261
Chris Yea209fde2020-07-22 13:54:51 -07006262 std::shared_ptr<FakeApplicationHandle> application =
6263 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006264 mUnfocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006265 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07006266 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07006267
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006268 mFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006269 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006270 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07006271
6272 // Set focused application.
6273 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07006274 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07006275
6276 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00006277 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006278 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006279 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07006280 }
6281
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006282 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07006283 InputDispatcherTest::TearDown();
6284
6285 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006286 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07006287 }
6288
6289protected:
6290 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006291 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006292 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07006293};
6294
6295// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
6296// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
6297// the onPointerDownOutsideFocus callback.
6298TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006299 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006300 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6301 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006302 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006303 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006304
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006305 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07006306 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
6307}
6308
6309// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
6310// DOWN on the window that doesn't have focus. Ensure no window received the
6311// onPointerDownOutsideFocus callback.
6312TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006313 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006314 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006315 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006316 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006317
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006318 ASSERT_TRUE(mDispatcher->waitForIdle());
6319 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006320}
6321
6322// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
6323// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
6324TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006325 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6326 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006327 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006328 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07006329
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006330 ASSERT_TRUE(mDispatcher->waitForIdle());
6331 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006332}
6333
6334// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
6335// DOWN on the window that already has focus. Ensure no window received the
6336// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006337TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006338 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006339 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006340 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006341 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006342 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006343
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006344 ASSERT_TRUE(mDispatcher->waitForIdle());
6345 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006346}
6347
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006348// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
6349// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
6350TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
6351 const MotionEvent event =
6352 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
6353 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006354 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006355 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
6356 .build();
6357 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
6358 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6359 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
6360
6361 ASSERT_TRUE(mDispatcher->waitForIdle());
6362 mFakePolicy->assertOnPointerDownWasNotCalled();
6363 // Ensure that the unfocused window did not receive any FOCUS events.
6364 mUnfocusedWindow->assertNoEvents();
6365}
6366
chaviwaf87b3e2019-10-01 16:59:28 -07006367// These tests ensures we can send touch events to a single client when there are multiple input
6368// windows that point to the same client token.
6369class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
6370 virtual void SetUp() override {
6371 InputDispatcherTest::SetUp();
6372
Chris Yea209fde2020-07-22 13:54:51 -07006373 std::shared_ptr<FakeApplicationHandle> application =
6374 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006375 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
6376 ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07006377 mWindow1->setFrame(Rect(0, 0, 100, 100));
6378
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006379 mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2",
6380 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviwaf87b3e2019-10-01 16:59:28 -07006381 mWindow2->setFrame(Rect(100, 100, 200, 200));
6382
Arthur Hung72d8dc32020-03-28 00:48:39 +00006383 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07006384 }
6385
6386protected:
6387 sp<FakeWindowHandle> mWindow1;
6388 sp<FakeWindowHandle> mWindow2;
6389
6390 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05006391 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07006392 vec2 vals = windowInfo->transform.transform(point.x, point.y);
6393 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07006394 }
6395
6396 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
6397 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006398 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07006399 InputEvent* event = window->consume();
6400
6401 ASSERT_NE(nullptr, event) << name.c_str()
6402 << ": consumer should have returned non-NULL event.";
6403
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006404 ASSERT_EQ(InputEventType::MOTION, event->getType())
6405 << name.c_str() << ": expected MotionEvent, got " << *event;
chaviwaf87b3e2019-10-01 16:59:28 -07006406
6407 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006408 assertMotionAction(expectedAction, motionEvent.getAction());
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08006409 ASSERT_EQ(points.size(), motionEvent.getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07006410
6411 for (size_t i = 0; i < points.size(); i++) {
6412 float expectedX = points[i].x;
6413 float expectedY = points[i].y;
6414
6415 EXPECT_EQ(expectedX, motionEvent.getX(i))
6416 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
6417 << ", got " << motionEvent.getX(i);
6418 EXPECT_EQ(expectedY, motionEvent.getY(i))
6419 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
6420 << ", got " << motionEvent.getY(i);
6421 }
6422 }
chaviw9eaa22c2020-07-01 16:21:27 -07006423
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08006424 void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07006425 std::vector<PointF> expectedPoints) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00006426 mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
6427 ADISPLAY_ID_DEFAULT, touchedPoints));
chaviw9eaa22c2020-07-01 16:21:27 -07006428
6429 // Always consume from window1 since it's the window that has the InputReceiver
6430 consumeMotionEvent(mWindow1, action, expectedPoints);
6431 }
chaviwaf87b3e2019-10-01 16:59:28 -07006432};
6433
6434TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
6435 // Touch Window 1
6436 PointF touchedPoint = {10, 10};
6437 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006438 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006439
6440 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006441 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006442
6443 // Touch Window 2
6444 touchedPoint = {150, 150};
6445 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006446 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006447}
6448
chaviw9eaa22c2020-07-01 16:21:27 -07006449TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
6450 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07006451 mWindow2->setWindowScale(0.5f, 0.5f);
6452
6453 // Touch Window 1
6454 PointF touchedPoint = {10, 10};
6455 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006456 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006457 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006458 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006459
6460 // Touch Window 2
6461 touchedPoint = {150, 150};
6462 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006463 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
6464 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006465
chaviw9eaa22c2020-07-01 16:21:27 -07006466 // Update the transform so rotation is set
6467 mWindow2->setWindowTransform(0, -1, 1, 0);
6468 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
6469 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006470}
6471
chaviw9eaa22c2020-07-01 16:21:27 -07006472TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006473 mWindow2->setWindowScale(0.5f, 0.5f);
6474
6475 // Touch Window 1
6476 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6477 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006478 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006479
6480 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006481 touchedPoints.push_back(PointF{150, 150});
6482 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006483 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006484
chaviw9eaa22c2020-07-01 16:21:27 -07006485 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006486 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006487 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006488
chaviw9eaa22c2020-07-01 16:21:27 -07006489 // Update the transform so rotation is set for Window 2
6490 mWindow2->setWindowTransform(0, -1, 1, 0);
6491 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006492 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006493}
6494
chaviw9eaa22c2020-07-01 16:21:27 -07006495TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006496 mWindow2->setWindowScale(0.5f, 0.5f);
6497
6498 // Touch Window 1
6499 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6500 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006501 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006502
6503 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006504 touchedPoints.push_back(PointF{150, 150});
6505 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006506
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006507 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006508
6509 // Move both windows
6510 touchedPoints = {{20, 20}, {175, 175}};
6511 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6512 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6513
chaviw9eaa22c2020-07-01 16:21:27 -07006514 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006515
chaviw9eaa22c2020-07-01 16:21:27 -07006516 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006517 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006518 expectedPoints.pop_back();
6519
6520 // Touch Window 2
6521 mWindow2->setWindowTransform(0, -1, 1, 0);
6522 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006523 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006524
6525 // Move both windows
6526 touchedPoints = {{20, 20}, {175, 175}};
6527 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6528 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6529
6530 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006531}
6532
6533TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
6534 mWindow1->setWindowScale(0.5f, 0.5f);
6535
6536 // Touch Window 1
6537 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6538 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006539 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006540
6541 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006542 touchedPoints.push_back(PointF{150, 150});
6543 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006544
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006545 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006546
6547 // Move both windows
6548 touchedPoints = {{20, 20}, {175, 175}};
6549 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6550 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6551
chaviw9eaa22c2020-07-01 16:21:27 -07006552 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006553}
6554
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07006555/**
6556 * When one of the windows is slippery, the touch should not slip into the other window with the
6557 * same input channel.
6558 */
6559TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) {
6560 mWindow1->setSlippery(true);
6561 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
6562
6563 // Touch down in window 1
6564 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6565 ADISPLAY_ID_DEFAULT, {{50, 50}}));
6566 consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}});
6567
6568 // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip.
6569 // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN
6570 // getting generated.
6571 mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6572 ADISPLAY_ID_DEFAULT, {{150, 150}}));
6573
6574 consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}});
6575}
6576
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07006577/**
6578 * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and
6579 * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window
6580 * that the pointer is hovering over may have a different transform.
6581 */
6582TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) {
6583 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
6584
6585 // Start hover in window 1
6586 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN,
6587 ADISPLAY_ID_DEFAULT, {{50, 50}}));
6588 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
6589 {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})});
6590
6591 // Move hover to window 2.
6592 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6593 ADISPLAY_ID_DEFAULT, {{150, 150}}));
6594
6595 consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}});
6596 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
6597 {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})});
6598}
6599
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006600class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
6601 virtual void SetUp() override {
6602 InputDispatcherTest::SetUp();
6603
Chris Yea209fde2020-07-22 13:54:51 -07006604 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006605 mApplication->setDispatchingTimeout(20ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006606 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
6607 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006608 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05006609 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07006610 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006611
6612 // Set focused application.
6613 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
6614
6615 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006616 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006617 mWindow->consumeFocusEvent(true);
6618 }
6619
6620 virtual void TearDown() override {
6621 InputDispatcherTest::TearDown();
6622 mWindow.clear();
6623 }
6624
6625protected:
Chris Yea209fde2020-07-22 13:54:51 -07006626 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006627 sp<FakeWindowHandle> mWindow;
6628 static constexpr PointF WINDOW_LOCATION = {20, 20};
6629
6630 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006631 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006632 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6633 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006634 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006635 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6636 WINDOW_LOCATION));
6637 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006638
6639 sp<FakeWindowHandle> addSpyWindow() {
6640 sp<FakeWindowHandle> spy =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006641 sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006642 spy->setTrustedOverlay(true);
6643 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006644 spy->setSpy(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006645 spy->setDispatchingTimeout(30ms);
6646 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}});
6647 return spy;
6648 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006649};
6650
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006651// Send a tap and respond, which should not cause an ANR.
6652TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
6653 tapOnWindow();
6654 mWindow->consumeMotionDown();
6655 mWindow->consumeMotionUp();
6656 ASSERT_TRUE(mDispatcher->waitForIdle());
6657 mFakePolicy->assertNotifyAnrWasNotCalled();
6658}
6659
6660// Send a regular key and respond, which should not cause an ANR.
6661TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006662 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006663 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
6664 ASSERT_TRUE(mDispatcher->waitForIdle());
6665 mFakePolicy->assertNotifyAnrWasNotCalled();
6666}
6667
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006668TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
6669 mWindow->setFocusable(false);
6670 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6671 mWindow->consumeFocusEvent(false);
6672
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006673 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006674 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6675 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
6676 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006677 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006678 // Key will not go to window because we have no focused window.
6679 // The 'no focused window' ANR timer should start instead.
6680
6681 // Now, the focused application goes away.
6682 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
6683 // The key should get dropped and there should be no ANR.
6684
6685 ASSERT_TRUE(mDispatcher->waitForIdle());
6686 mFakePolicy->assertNotifyAnrWasNotCalled();
6687}
6688
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006689// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006690// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
6691// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006692TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006693 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006694 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6695 WINDOW_LOCATION));
6696
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006697 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
6698 ASSERT_TRUE(sequenceNum);
6699 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006700 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006701
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006702 mWindow->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006703 mWindow->consumeMotionEvent(
6704 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006705 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006706 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006707}
6708
6709// Send a key to the app and have the app not respond right away.
6710TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
6711 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006712 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006713 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
6714 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006715 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006716 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006717 ASSERT_TRUE(mDispatcher->waitForIdle());
6718}
6719
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006720// We have a focused application, but no focused window
6721TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006722 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006723 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6724 mWindow->consumeFocusEvent(false);
6725
6726 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006727 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006728 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6729 WINDOW_LOCATION));
6730 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
6731 mDispatcher->waitForIdle();
6732 mFakePolicy->assertNotifyAnrWasNotCalled();
6733
6734 // Once a focused event arrives, we get an ANR for this application
6735 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6736 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006737 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006738 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6739 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006740 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006741 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07006742 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006743 ASSERT_TRUE(mDispatcher->waitForIdle());
6744}
6745
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006746/**
6747 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
6748 * there will not be an ANR.
6749 */
6750TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
6751 mWindow->setFocusable(false);
6752 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6753 mWindow->consumeFocusEvent(false);
6754
6755 KeyEvent event;
6756 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
6757 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
6758
6759 // Define a valid key down event that is stale (too old).
6760 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
6761 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A,
Harry Cutts33476232023-01-30 19:57:29 +00006762 AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006763
6764 const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
6765
6766 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006767 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006768 InputEventInjectionSync::WAIT_FOR_RESULT,
6769 INJECT_EVENT_TIMEOUT, policyFlags);
6770 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
6771 << "Injection should fail because the event is stale";
6772
6773 ASSERT_TRUE(mDispatcher->waitForIdle());
6774 mFakePolicy->assertNotifyAnrWasNotCalled();
6775 mWindow->assertNoEvents();
6776}
6777
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006778// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006779// Make sure that we don't notify policy twice about the same ANR.
6780TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006781 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006782 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6783 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006784
6785 // Once a focused event arrives, we get an ANR for this application
6786 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6787 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006788 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006789 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6790 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006791 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Vishnu Naire4df8752022-09-08 09:17:55 -07006792 const std::chrono::duration appTimeout =
6793 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6794 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006795
Vishnu Naire4df8752022-09-08 09:17:55 -07006796 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006797 // ANR should not be raised again. It is up to policy to do that if it desires.
6798 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006799
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006800 // If we now get a focused window, the ANR should stop, but the policy handles that via
6801 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006802 ASSERT_TRUE(mDispatcher->waitForIdle());
6803}
6804
6805// We have a focused application, but no focused window
6806TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006807 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006808 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6809 mWindow->consumeFocusEvent(false);
6810
6811 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006812 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006813 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006814 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
6815 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006816
Vishnu Naire4df8752022-09-08 09:17:55 -07006817 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6818 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006819
6820 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006821 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006822 ASSERT_TRUE(mDispatcher->waitForIdle());
6823 mWindow->assertNoEvents();
6824}
6825
6826/**
6827 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
6828 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
6829 * If we process 1 of the events, but ANR on the second event with the same timestamp,
6830 * the ANR mechanism should still work.
6831 *
6832 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
6833 * DOWN event, while not responding on the second one.
6834 */
6835TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
6836 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
6837 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6838 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6839 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6840 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006841 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006842
6843 // Now send ACTION_UP, with identical timestamp
6844 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
6845 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6846 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6847 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006848 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006849
6850 // We have now sent down and up. Let's consume first event and then ANR on the second.
6851 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6852 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006853 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006854}
6855
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006856// A spy window can receive an ANR
6857TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
6858 sp<FakeWindowHandle> spy = addSpyWindow();
6859
6860 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6861 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6862 WINDOW_LOCATION));
6863 mWindow->consumeMotionDown();
6864
6865 std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN
6866 ASSERT_TRUE(sequenceNum);
6867 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006868 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006869
6870 spy->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006871 spy->consumeMotionEvent(
6872 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006873 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006874 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006875}
6876
6877// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006878// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006879TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
6880 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006881
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006882 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6883 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006884 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006885 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006886
6887 // Stuck on the ACTION_UP
6888 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006889 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006890
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006891 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006892 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006893 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6894 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006895
6896 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6897 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006898 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006899 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006900 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006901}
6902
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006903// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006904// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006905TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
6906 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006907
6908 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006909 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6910 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006911
6912 mWindow->consumeMotionDown();
6913 // Stuck on the ACTION_UP
6914 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006915 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006916
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006917 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006918 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006919 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6920 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006921
6922 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6923 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006924 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006925 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006926 spy->assertNoEvents();
6927}
6928
6929TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
6930 mDispatcher->setMonitorDispatchingTimeoutForTest(30ms);
6931
6932 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6933
6934 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6935 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6936 WINDOW_LOCATION));
6937
6938 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6939 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
6940 ASSERT_TRUE(consumeSeq);
6941
Prabir Pradhanedd96402022-02-15 01:46:16 -08006942 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006943
6944 monitor.finishEvent(*consumeSeq);
6945 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6946
6947 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006948 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006949}
6950
6951// If a window is unresponsive, then you get anr. if the window later catches up and starts to
6952// process events, you don't get an anr. When the window later becomes unresponsive again, you
6953// get an ANR again.
6954// 1. tap -> block on ACTION_UP -> receive ANR
6955// 2. consume all pending events (= queue becomes healthy again)
6956// 3. tap again -> block on ACTION_UP again -> receive ANR second time
6957TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
6958 tapOnWindow();
6959
6960 mWindow->consumeMotionDown();
6961 // Block on ACTION_UP
6962 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006963 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006964 mWindow->consumeMotionUp(); // Now the connection should be healthy again
6965 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006966 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006967 mWindow->assertNoEvents();
6968
6969 tapOnWindow();
6970 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006971 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006972 mWindow->consumeMotionUp();
6973
6974 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006975 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006976 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006977 mWindow->assertNoEvents();
6978}
6979
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006980// If a connection remains unresponsive for a while, make sure policy is only notified once about
6981// it.
6982TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006983 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006984 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6985 WINDOW_LOCATION));
6986
6987 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006988 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006989 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006990 // 'notifyConnectionUnresponsive' should only be called once per connection
6991 mFakePolicy->assertNotifyAnrWasNotCalled();
6992 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006993 mWindow->consumeMotionDown();
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006994 mWindow->consumeMotionEvent(
6995 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006996 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006997 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006998 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006999 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007000}
7001
7002/**
7003 * If a window is processing a motion event, and then a key event comes in, the key event should
7004 * not to to the focused window until the motion is processed.
7005 *
7006 * Warning!!!
7007 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
7008 * and the injection timeout that we specify when injecting the key.
7009 * We must have the injection timeout (10ms) be smaller than
7010 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
7011 *
7012 * If that value changes, this test should also change.
7013 */
7014TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
7015 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
7016 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
7017
7018 tapOnWindow();
7019 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
7020 ASSERT_TRUE(downSequenceNum);
7021 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
7022 ASSERT_TRUE(upSequenceNum);
7023 // Don't finish the events yet, and send a key
7024 // Injection will "succeed" because we will eventually give up and send the key to the focused
7025 // window even if motions are still being processed. But because the injection timeout is short,
7026 // we will receive INJECTION_TIMED_OUT as the result.
7027
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007028 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007029 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007030 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
7031 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007032 // Key will not be sent to the window, yet, because the window is still processing events
7033 // and the key remains pending, waiting for the touch events to be processed
7034 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
7035 ASSERT_FALSE(keySequenceNum);
7036
7037 std::this_thread::sleep_for(500ms);
7038 // if we wait long enough though, dispatcher will give up, and still send the key
7039 // to the focused window, even though we have not yet finished the motion event
7040 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7041 mWindow->finishEvent(*downSequenceNum);
7042 mWindow->finishEvent(*upSequenceNum);
7043}
7044
7045/**
7046 * If a window is processing a motion event, and then a key event comes in, the key event should
7047 * not go to the focused window until the motion is processed.
7048 * If then a new motion comes in, then the pending key event should be going to the currently
7049 * focused window right away.
7050 */
7051TEST_F(InputDispatcherSingleWindowAnr,
7052 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
7053 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
7054 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
7055
7056 tapOnWindow();
7057 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
7058 ASSERT_TRUE(downSequenceNum);
7059 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
7060 ASSERT_TRUE(upSequenceNum);
7061 // Don't finish the events yet, and send a key
7062 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007063 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007064 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7065 InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007066 // At this point, key is still pending, and should not be sent to the application yet.
7067 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
7068 ASSERT_FALSE(keySequenceNum);
7069
7070 // Now tap down again. It should cause the pending key to go to the focused window right away.
7071 tapOnWindow();
7072 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
7073 // the other events yet. We can finish events in any order.
7074 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
7075 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
7076 mWindow->consumeMotionDown();
7077 mWindow->consumeMotionUp();
7078 mWindow->assertNoEvents();
7079}
7080
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07007081/**
7082 * Send an event to the app and have the app not respond right away.
7083 * When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
7084 * So InputDispatcher will enqueue ACTION_CANCEL event as well.
7085 * At some point, the window becomes responsive again.
7086 * Ensure that subsequent events get dropped, and the next gesture is delivered.
7087 */
7088TEST_F(InputDispatcherSingleWindowAnr, TwoGesturesWithAnr) {
7089 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7090 .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10))
7091 .build());
7092
7093 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
7094 ASSERT_TRUE(sequenceNum);
7095 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
7096 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
7097
7098 mWindow->finishEvent(*sequenceNum);
7099 mWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
7100 ASSERT_TRUE(mDispatcher->waitForIdle());
7101 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
7102
7103 // Now that the window is responsive, let's continue the gesture.
7104 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
7105 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
7106 .build());
7107
7108 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7109 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
7110 .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3))
7111 .build());
7112
7113 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
7114 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
7115 .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3))
7116 .build());
7117 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
7118 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
7119 .build());
7120 // We already canceled this pointer, so the window shouldn't get any new events.
7121 mWindow->assertNoEvents();
7122
7123 // Start another one.
7124 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7125 .pointer(PointerBuilder(0, ToolType::FINGER).x(15).y(15))
7126 .build());
7127 mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
7128}
7129
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007130class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
7131 virtual void SetUp() override {
7132 InputDispatcherTest::SetUp();
7133
Chris Yea209fde2020-07-22 13:54:51 -07007134 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007135 mApplication->setDispatchingTimeout(10ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007136 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
7137 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007138 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007139 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007140 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007141
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007142 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
7143 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05007144 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007145 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007146
7147 // Set focused application.
7148 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07007149 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007150
7151 // Expect one focus window exist in display.
7152 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07007153 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007154 mFocusedWindow->consumeFocusEvent(true);
7155 }
7156
7157 virtual void TearDown() override {
7158 InputDispatcherTest::TearDown();
7159
7160 mUnfocusedWindow.clear();
7161 mFocusedWindow.clear();
7162 }
7163
7164protected:
Chris Yea209fde2020-07-22 13:54:51 -07007165 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007166 sp<FakeWindowHandle> mUnfocusedWindow;
7167 sp<FakeWindowHandle> mFocusedWindow;
7168 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
7169 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
7170 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
7171
7172 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
7173
7174 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
7175
7176private:
7177 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007178 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007179 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7180 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007181 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007182 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7183 location));
7184 }
7185};
7186
7187// If we have 2 windows that are both unresponsive, the one with the shortest timeout
7188// should be ANR'd first.
7189TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007190 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007191 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7192 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007193 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007194 mFocusedWindow->consumeMotionDown();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007195 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007196 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007197 // We consumed all events, so no ANR
7198 ASSERT_TRUE(mDispatcher->waitForIdle());
7199 mFakePolicy->assertNotifyAnrWasNotCalled();
7200
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007201 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007202 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7203 FOCUSED_WINDOW_LOCATION));
7204 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
7205 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007206
7207 const std::chrono::duration timeout =
7208 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007209 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007210 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
7211 // sequence to make it consistent
7212 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007213 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007214 mFocusedWindow->consumeMotionDown();
7215 // This cancel is generated because the connection was unresponsive
7216 mFocusedWindow->consumeMotionCancel();
7217 mFocusedWindow->assertNoEvents();
7218 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007219 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08007220 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7221 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007222 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007223}
7224
7225// If we have 2 windows with identical timeouts that are both unresponsive,
7226// it doesn't matter which order they should have ANR.
7227// But we should receive ANR for both.
7228TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
7229 // Set the timeout for unfocused window to match the focused window
7230 mUnfocusedWindow->setDispatchingTimeout(10ms);
7231 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
7232
7233 tapOnFocusedWindow();
7234 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Prabir Pradhanedd96402022-02-15 01:46:16 -08007235 sp<IBinder> anrConnectionToken1, anrConnectionToken2;
7236 ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms));
7237 ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007238
7239 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007240 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
7241 mFocusedWindow->getToken() == anrConnectionToken2);
7242 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
7243 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007244
7245 ASSERT_TRUE(mDispatcher->waitForIdle());
7246 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007247
7248 mFocusedWindow->consumeMotionDown();
7249 mFocusedWindow->consumeMotionUp();
7250 mUnfocusedWindow->consumeMotionOutside();
7251
Prabir Pradhanedd96402022-02-15 01:46:16 -08007252 sp<IBinder> responsiveToken1, responsiveToken2;
7253 ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken());
7254 ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007255
7256 // Both applications should be marked as responsive, in any order
7257 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
7258 mFocusedWindow->getToken() == responsiveToken2);
7259 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
7260 mUnfocusedWindow->getToken() == responsiveToken2);
7261 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007262}
7263
7264// If a window is already not responding, the second tap on the same window should be ignored.
7265// We should also log an error to account for the dropped event (not tested here).
7266// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
7267TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
7268 tapOnFocusedWindow();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007269 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007270 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007271 // Receive the events, but don't respond
7272 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
7273 ASSERT_TRUE(downEventSequenceNum);
7274 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
7275 ASSERT_TRUE(upEventSequenceNum);
7276 const std::chrono::duration timeout =
7277 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007278 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007279
7280 // Tap once again
7281 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007282 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007283 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7284 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007285 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007286 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7287 FOCUSED_WINDOW_LOCATION));
7288 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
7289 // valid touch target
7290 mUnfocusedWindow->assertNoEvents();
7291
7292 // Consume the first tap
7293 mFocusedWindow->finishEvent(*downEventSequenceNum);
7294 mFocusedWindow->finishEvent(*upEventSequenceNum);
7295 ASSERT_TRUE(mDispatcher->waitForIdle());
7296 // The second tap did not go to the focused window
7297 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007298 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -08007299 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7300 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007301 mFakePolicy->assertNotifyAnrWasNotCalled();
7302}
7303
7304// If you tap outside of all windows, there will not be ANR
7305TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007306 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007307 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7308 LOCATION_OUTSIDE_ALL_WINDOWS));
7309 ASSERT_TRUE(mDispatcher->waitForIdle());
7310 mFakePolicy->assertNotifyAnrWasNotCalled();
7311}
7312
7313// Since the focused window is paused, tapping on it should not produce any events
7314TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
7315 mFocusedWindow->setPaused(true);
7316 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
7317
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007318 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007319 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7320 FOCUSED_WINDOW_LOCATION));
7321
7322 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
7323 ASSERT_TRUE(mDispatcher->waitForIdle());
7324 // Should not ANR because the window is paused, and touches shouldn't go to it
7325 mFakePolicy->assertNotifyAnrWasNotCalled();
7326
7327 mFocusedWindow->assertNoEvents();
7328 mUnfocusedWindow->assertNoEvents();
7329}
7330
7331/**
7332 * If a window is processing a motion event, and then a key event comes in, the key event should
7333 * not to to the focused window until the motion is processed.
7334 * If a different window becomes focused at this time, the key should go to that window instead.
7335 *
7336 * Warning!!!
7337 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
7338 * and the injection timeout that we specify when injecting the key.
7339 * We must have the injection timeout (10ms) be smaller than
7340 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
7341 *
7342 * If that value changes, this test should also change.
7343 */
7344TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
7345 // Set a long ANR timeout to prevent it from triggering
7346 mFocusedWindow->setDispatchingTimeout(2s);
7347 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7348
7349 tapOnUnfocusedWindow();
7350 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
7351 ASSERT_TRUE(downSequenceNum);
7352 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
7353 ASSERT_TRUE(upSequenceNum);
7354 // Don't finish the events yet, and send a key
7355 // Injection will succeed because we will eventually give up and send the key to the focused
7356 // window even if motions are still being processed.
7357
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007358 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007359 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7360 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007361 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007362 // Key will not be sent to the window, yet, because the window is still processing events
7363 // and the key remains pending, waiting for the touch events to be processed
7364 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
7365 ASSERT_FALSE(keySequenceNum);
7366
7367 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07007368 mFocusedWindow->setFocusable(false);
7369 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007370 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07007371 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007372
7373 // Focus events should precede the key events
7374 mUnfocusedWindow->consumeFocusEvent(true);
7375 mFocusedWindow->consumeFocusEvent(false);
7376
7377 // Finish the tap events, which should unblock dispatcher
7378 mUnfocusedWindow->finishEvent(*downSequenceNum);
7379 mUnfocusedWindow->finishEvent(*upSequenceNum);
7380
7381 // Now that all queues are cleared and no backlog in the connections, the key event
7382 // can finally go to the newly focused "mUnfocusedWindow".
7383 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7384 mFocusedWindow->assertNoEvents();
7385 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007386 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007387}
7388
7389// When the touch stream is split across 2 windows, and one of them does not respond,
7390// then ANR should be raised and the touch should be canceled for the unresponsive window.
7391// The other window should not be affected by that.
7392TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
7393 // Touch Window 1
Prabir Pradhan678438e2023-04-13 19:32:51 +00007394 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7395 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7396 {FOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007397 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007398 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007399
7400 // Touch Window 2
Prabir Pradhan678438e2023-04-13 19:32:51 +00007401 mDispatcher->notifyMotion(
7402 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7403 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007404
7405 const std::chrono::duration timeout =
7406 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007407 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007408
7409 mUnfocusedWindow->consumeMotionDown();
7410 mFocusedWindow->consumeMotionDown();
7411 // Focused window may or may not receive ACTION_MOVE
7412 // But it should definitely receive ACTION_CANCEL due to the ANR
7413 InputEvent* event;
7414 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
7415 ASSERT_TRUE(moveOrCancelSequenceNum);
7416 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
7417 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007418 ASSERT_EQ(event->getType(), InputEventType::MOTION);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007419 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
7420 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
7421 mFocusedWindow->consumeMotionCancel();
7422 } else {
7423 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
7424 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007425 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08007426 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7427 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007428
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007429 mUnfocusedWindow->assertNoEvents();
7430 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007431 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007432}
7433
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007434/**
7435 * If we have no focused window, and a key comes in, we start the ANR timer.
7436 * The focused application should add a focused window before the timer runs out to prevent ANR.
7437 *
7438 * If the user touches another application during this time, the key should be dropped.
7439 * Next, if a new focused window comes in, without toggling the focused application,
7440 * then no ANR should occur.
7441 *
7442 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
7443 * but in some cases the policy may not update the focused application.
7444 */
7445TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
7446 std::shared_ptr<FakeApplicationHandle> focusedApplication =
7447 std::make_shared<FakeApplicationHandle>();
7448 focusedApplication->setDispatchingTimeout(60ms);
7449 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
7450 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
7451 mFocusedWindow->setFocusable(false);
7452
7453 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7454 mFocusedWindow->consumeFocusEvent(false);
7455
7456 // Send a key. The ANR timer should start because there is no focused window.
7457 // 'focusedApplication' will get blamed if this timer completes.
7458 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007459 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007460 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7461 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
7462 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007463 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007464
7465 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
7466 // then the injected touches won't cause the focused event to get dropped.
7467 // The dispatcher only checks for whether the queue should be pruned upon queueing.
7468 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
7469 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
7470 // For this test, it means that the key would get delivered to the window once it becomes
7471 // focused.
7472 std::this_thread::sleep_for(10ms);
7473
7474 // Touch unfocused window. This should force the pending key to get dropped.
Prabir Pradhan678438e2023-04-13 19:32:51 +00007475 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7476 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7477 {UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007478
7479 // We do not consume the motion right away, because that would require dispatcher to first
7480 // process (== drop) the key event, and by that time, ANR will be raised.
7481 // Set the focused window first.
7482 mFocusedWindow->setFocusable(true);
7483 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7484 setFocusedWindow(mFocusedWindow);
7485 mFocusedWindow->consumeFocusEvent(true);
7486 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
7487 // to another application. This could be a bug / behaviour in the policy.
7488
7489 mUnfocusedWindow->consumeMotionDown();
7490
7491 ASSERT_TRUE(mDispatcher->waitForIdle());
7492 // Should not ANR because we actually have a focused window. It was just added too slowly.
7493 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
7494}
7495
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007496// These tests ensure we cannot send touch events to a window that's positioned behind a window
7497// that has feature NO_INPUT_CHANNEL.
7498// Layout:
7499// Top (closest to user)
7500// mNoInputWindow (above all windows)
7501// mBottomWindow
7502// Bottom (furthest from user)
7503class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
7504 virtual void SetUp() override {
7505 InputDispatcherTest::SetUp();
7506
7507 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007508 mNoInputWindow =
7509 sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7510 "Window without input channel", ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00007511 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007512 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007513 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7514 // It's perfectly valid for this window to not have an associated input channel
7515
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007516 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
7517 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007518 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
7519
7520 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7521 }
7522
7523protected:
7524 std::shared_ptr<FakeApplicationHandle> mApplication;
7525 sp<FakeWindowHandle> mNoInputWindow;
7526 sp<FakeWindowHandle> mBottomWindow;
7527};
7528
7529TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
7530 PointF touchedPoint = {10, 10};
7531
Prabir Pradhan678438e2023-04-13 19:32:51 +00007532 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7533 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7534 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007535
7536 mNoInputWindow->assertNoEvents();
7537 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
7538 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
7539 // and therefore should prevent mBottomWindow from receiving touches
7540 mBottomWindow->assertNoEvents();
7541}
7542
7543/**
7544 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
7545 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
7546 */
7547TEST_F(InputDispatcherMultiWindowOcclusionTests,
7548 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007549 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7550 "Window with input channel and NO_INPUT_CHANNEL",
7551 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007552
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007553 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007554 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7555 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7556
7557 PointF touchedPoint = {10, 10};
7558
Prabir Pradhan678438e2023-04-13 19:32:51 +00007559 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7560 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7561 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007562
7563 mNoInputWindow->assertNoEvents();
7564 mBottomWindow->assertNoEvents();
7565}
7566
Vishnu Nair958da932020-08-21 17:12:37 -07007567class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
7568protected:
7569 std::shared_ptr<FakeApplicationHandle> mApp;
7570 sp<FakeWindowHandle> mWindow;
7571 sp<FakeWindowHandle> mMirror;
7572
7573 virtual void SetUp() override {
7574 InputDispatcherTest::SetUp();
7575 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007576 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
7577 mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror",
7578 ADISPLAY_ID_DEFAULT, mWindow->getToken());
Vishnu Nair958da932020-08-21 17:12:37 -07007579 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7580 mWindow->setFocusable(true);
7581 mMirror->setFocusable(true);
7582 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7583 }
7584};
7585
7586TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
7587 // Request focus on a mirrored window
7588 setFocusedWindow(mMirror);
7589
7590 // window gets focused
7591 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007592 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7593 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007594 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
7595}
7596
7597// A focused & mirrored window remains focused only if the window and its mirror are both
7598// focusable.
7599TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
7600 setFocusedWindow(mMirror);
7601
7602 // window gets focused
7603 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007604 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7605 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007606 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007607 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7608 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007609 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7610
7611 mMirror->setFocusable(false);
7612 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7613
7614 // window loses focus since one of the windows associated with the token in not focusable
7615 mWindow->consumeFocusEvent(false);
7616
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007617 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7618 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007619 mWindow->assertNoEvents();
7620}
7621
7622// A focused & mirrored window remains focused until the window and its mirror both become
7623// invisible.
7624TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
7625 setFocusedWindow(mMirror);
7626
7627 // window gets focused
7628 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007629 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7630 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007631 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007632 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7633 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007634 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7635
7636 mMirror->setVisible(false);
7637 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7638
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007639 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7640 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007641 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007642 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7643 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007644 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7645
7646 mWindow->setVisible(false);
7647 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7648
7649 // window loses focus only after all windows associated with the token become invisible.
7650 mWindow->consumeFocusEvent(false);
7651
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007652 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7653 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007654 mWindow->assertNoEvents();
7655}
7656
7657// A focused & mirrored window remains focused until both windows are removed.
7658TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
7659 setFocusedWindow(mMirror);
7660
7661 // window gets focused
7662 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007663 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7664 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007665 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007666 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7667 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007668 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7669
7670 // single window is removed but the window token remains focused
7671 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
7672
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007673 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7674 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007675 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007676 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7677 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007678 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7679
7680 // Both windows are removed
7681 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
7682 mWindow->consumeFocusEvent(false);
7683
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007684 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7685 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007686 mWindow->assertNoEvents();
7687}
7688
7689// Focus request can be pending until one window becomes visible.
7690TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
7691 // Request focus on an invisible mirror.
7692 mWindow->setVisible(false);
7693 mMirror->setVisible(false);
7694 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7695 setFocusedWindow(mMirror);
7696
7697 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007698 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007699 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7700 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07007701
7702 mMirror->setVisible(true);
7703 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7704
7705 // window gets focused
7706 mWindow->consumeFocusEvent(true);
7707 // window gets the pending key event
7708 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7709}
Prabir Pradhan99987712020-11-10 18:43:05 -08007710
7711class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
7712protected:
7713 std::shared_ptr<FakeApplicationHandle> mApp;
7714 sp<FakeWindowHandle> mWindow;
7715 sp<FakeWindowHandle> mSecondWindow;
7716
7717 void SetUp() override {
7718 InputDispatcherTest::SetUp();
7719 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007720 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007721 mWindow->setFocusable(true);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007722 mSecondWindow =
7723 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007724 mSecondWindow->setFocusable(true);
7725
7726 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7727 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
7728
7729 setFocusedWindow(mWindow);
7730 mWindow->consumeFocusEvent(true);
7731 }
7732
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007733 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007734 mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request));
Prabir Pradhan99987712020-11-10 18:43:05 -08007735 }
7736
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007737 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
7738 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08007739 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007740 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
7741 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007742 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007743 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08007744 }
7745};
7746
7747TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
7748 // Ensure that capture cannot be obtained for unfocused windows.
7749 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
7750 mFakePolicy->assertSetPointerCaptureNotCalled();
7751 mSecondWindow->assertNoEvents();
7752
7753 // Ensure that capture can be enabled from the focus window.
7754 requestAndVerifyPointerCapture(mWindow, true);
7755
7756 // Ensure that capture cannot be disabled from a window that does not have capture.
7757 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
7758 mFakePolicy->assertSetPointerCaptureNotCalled();
7759
7760 // Ensure that capture can be disabled from the window with capture.
7761 requestAndVerifyPointerCapture(mWindow, false);
7762}
7763
7764TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007765 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007766
7767 setFocusedWindow(mSecondWindow);
7768
7769 // Ensure that the capture disabled event was sent first.
7770 mWindow->consumeCaptureEvent(false);
7771 mWindow->consumeFocusEvent(false);
7772 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007773 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007774
7775 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007776 notifyPointerCaptureChanged({});
7777 notifyPointerCaptureChanged(request);
7778 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08007779 mWindow->assertNoEvents();
7780 mSecondWindow->assertNoEvents();
7781 mFakePolicy->assertSetPointerCaptureNotCalled();
7782}
7783
7784TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007785 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007786
7787 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007788 notifyPointerCaptureChanged({});
7789 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007790
7791 // Ensure that Pointer Capture is disabled.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007792 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007793 mWindow->consumeCaptureEvent(false);
7794 mWindow->assertNoEvents();
7795}
7796
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007797TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
7798 requestAndVerifyPointerCapture(mWindow, true);
7799
7800 // The first window loses focus.
7801 setFocusedWindow(mSecondWindow);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007802 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007803 mWindow->consumeCaptureEvent(false);
7804
7805 // Request Pointer Capture from the second window before the notification from InputReader
7806 // arrives.
7807 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007808 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007809
7810 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007811 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007812
7813 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007814 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007815
7816 mSecondWindow->consumeFocusEvent(true);
7817 mSecondWindow->consumeCaptureEvent(true);
7818}
7819
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007820TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
7821 // App repeatedly enables and disables capture.
7822 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7823 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7824 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7825 mFakePolicy->assertSetPointerCaptureCalled(false);
7826 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7827 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7828
7829 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
7830 // first request is now stale, this should do nothing.
7831 notifyPointerCaptureChanged(firstRequest);
7832 mWindow->assertNoEvents();
7833
7834 // InputReader notifies that the second request was enabled.
7835 notifyPointerCaptureChanged(secondRequest);
7836 mWindow->consumeCaptureEvent(true);
7837}
7838
Prabir Pradhan7092e262022-05-03 16:51:09 +00007839TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
7840 requestAndVerifyPointerCapture(mWindow, true);
7841
7842 // App toggles pointer capture off and on.
7843 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7844 mFakePolicy->assertSetPointerCaptureCalled(false);
7845
7846 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7847 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7848
7849 // InputReader notifies that the latest "enable" request was processed, while skipping over the
7850 // preceding "disable" request.
7851 notifyPointerCaptureChanged(enableRequest);
7852
7853 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
7854 // any notifications.
7855 mWindow->assertNoEvents();
7856}
7857
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007858class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
7859protected:
7860 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00007861
7862 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
7863 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
7864
7865 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
7866 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7867
7868 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
7869 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
7870 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7871 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
7872 MAXIMUM_OBSCURING_OPACITY);
7873
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007874 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007875 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007876 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007877
7878 sp<FakeWindowHandle> mTouchWindow;
7879
7880 virtual void SetUp() override {
7881 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007882 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007883 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
7884 }
7885
7886 virtual void TearDown() override {
7887 InputDispatcherTest::TearDown();
7888 mTouchWindow.clear();
7889 }
7890
chaviw3277faf2021-05-19 16:45:23 -05007891 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode,
7892 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007893 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007894 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007895 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007896 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007897 return window;
7898 }
7899
7900 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
7901 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
7902 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007903 sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007904 // Generate an arbitrary PID based on the UID
7905 window->setOwnerInfo(1777 + (uid % 10000), uid);
7906 return window;
7907 }
7908
7909 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007910 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7911 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7912 points));
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007913 }
7914};
7915
7916TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007917 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007918 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007919 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007920
7921 touch();
7922
7923 mTouchWindow->assertNoEvents();
7924}
7925
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007926TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00007927 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
7928 const sp<FakeWindowHandle>& w =
7929 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
7930 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7931
7932 touch();
7933
7934 mTouchWindow->assertNoEvents();
7935}
7936
7937TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007938 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
7939 const sp<FakeWindowHandle>& w =
7940 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
7941 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7942
7943 touch();
7944
7945 w->assertNoEvents();
7946}
7947
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007948TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007949 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
7950 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007951
7952 touch();
7953
7954 mTouchWindow->consumeAnyMotionDown();
7955}
7956
7957TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007958 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007959 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007960 w->setFrame(Rect(0, 0, 50, 50));
7961 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007962
7963 touch({PointF{100, 100}});
7964
7965 mTouchWindow->consumeAnyMotionDown();
7966}
7967
7968TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007969 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007970 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007971 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7972
7973 touch();
7974
7975 mTouchWindow->consumeAnyMotionDown();
7976}
7977
7978TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
7979 const sp<FakeWindowHandle>& w =
7980 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
7981 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007982
7983 touch();
7984
7985 mTouchWindow->consumeAnyMotionDown();
7986}
7987
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007988TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
7989 const sp<FakeWindowHandle>& w =
7990 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
7991 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7992
7993 touch();
7994
7995 w->assertNoEvents();
7996}
7997
7998/**
7999 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
8000 * inside) while letting them pass-through. Note that even though touch passes through the occluding
8001 * window, the occluding window will still receive ACTION_OUTSIDE event.
8002 */
8003TEST_F(InputDispatcherUntrustedTouchesTest,
8004 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
8005 const sp<FakeWindowHandle>& w =
8006 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008007 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00008008 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8009
8010 touch();
8011
8012 w->consumeMotionOutside();
8013}
8014
8015TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
8016 const sp<FakeWindowHandle>& w =
8017 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008018 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00008019 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8020
8021 touch();
8022
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008023 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00008024}
8025
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00008026TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008027 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008028 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8029 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008030 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8031
8032 touch();
8033
8034 mTouchWindow->consumeAnyMotionDown();
8035}
8036
8037TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
8038 const sp<FakeWindowHandle>& w =
8039 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8040 MAXIMUM_OBSCURING_OPACITY);
8041 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00008042
8043 touch();
8044
8045 mTouchWindow->consumeAnyMotionDown();
8046}
8047
8048TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008049 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008050 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8051 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008052 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8053
8054 touch();
8055
8056 mTouchWindow->assertNoEvents();
8057}
8058
8059TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
8060 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
8061 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008062 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
8063 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008064 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008065 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
8066 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008067 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
8068
8069 touch();
8070
8071 mTouchWindow->assertNoEvents();
8072}
8073
8074TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
8075 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
8076 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008077 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
8078 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008079 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008080 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
8081 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008082 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
8083
8084 touch();
8085
8086 mTouchWindow->consumeAnyMotionDown();
8087}
8088
8089TEST_F(InputDispatcherUntrustedTouchesTest,
8090 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
8091 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008092 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8093 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008094 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008095 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
8096 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008097 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
8098
8099 touch();
8100
8101 mTouchWindow->consumeAnyMotionDown();
8102}
8103
8104TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
8105 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008106 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8107 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008108 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008109 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
8110 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008111 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00008112
8113 touch();
8114
8115 mTouchWindow->assertNoEvents();
8116}
8117
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008118TEST_F(InputDispatcherUntrustedTouchesTest,
8119 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
8120 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008121 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
8122 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008123 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008124 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8125 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008126 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
8127
8128 touch();
8129
8130 mTouchWindow->assertNoEvents();
8131}
8132
8133TEST_F(InputDispatcherUntrustedTouchesTest,
8134 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
8135 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008136 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
8137 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008138 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008139 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8140 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008141 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
8142
8143 touch();
8144
8145 mTouchWindow->consumeAnyMotionDown();
8146}
8147
8148TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
8149 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008150 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
8151 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008152 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8153
8154 touch();
8155
8156 mTouchWindow->consumeAnyMotionDown();
8157}
8158
8159TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
8160 const sp<FakeWindowHandle>& w =
8161 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
8162 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8163
8164 touch();
8165
8166 mTouchWindow->consumeAnyMotionDown();
8167}
8168
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00008169TEST_F(InputDispatcherUntrustedTouchesTest,
8170 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
8171 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
8172 const sp<FakeWindowHandle>& w =
8173 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
8174 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8175
8176 touch();
8177
8178 mTouchWindow->assertNoEvents();
8179}
8180
8181TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
8182 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
8183 const sp<FakeWindowHandle>& w =
8184 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
8185 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8186
8187 touch();
8188
8189 mTouchWindow->consumeAnyMotionDown();
8190}
8191
8192TEST_F(InputDispatcherUntrustedTouchesTest,
8193 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
8194 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
8195 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008196 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8197 OPACITY_ABOVE_THRESHOLD);
8198 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8199
8200 touch();
8201
8202 mTouchWindow->consumeAnyMotionDown();
8203}
8204
8205TEST_F(InputDispatcherUntrustedTouchesTest,
8206 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
8207 const sp<FakeWindowHandle>& w1 =
8208 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
8209 OPACITY_BELOW_THRESHOLD);
8210 const sp<FakeWindowHandle>& w2 =
8211 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8212 OPACITY_BELOW_THRESHOLD);
8213 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
8214
8215 touch();
8216
8217 mTouchWindow->assertNoEvents();
8218}
8219
8220/**
8221 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
8222 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
8223 * (which alone would result in allowing touches) does not affect the blocking behavior.
8224 */
8225TEST_F(InputDispatcherUntrustedTouchesTest,
8226 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
8227 const sp<FakeWindowHandle>& wB =
8228 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
8229 OPACITY_BELOW_THRESHOLD);
8230 const sp<FakeWindowHandle>& wC =
8231 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
8232 OPACITY_BELOW_THRESHOLD);
8233 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
8234
8235 touch();
8236
8237 mTouchWindow->assertNoEvents();
8238}
8239
8240/**
8241 * This test is testing that a window from a different UID but with same application token doesn't
8242 * block the touch. Apps can share the application token for close UI collaboration for example.
8243 */
8244TEST_F(InputDispatcherUntrustedTouchesTest,
8245 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
8246 const sp<FakeWindowHandle>& w =
8247 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
8248 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00008249 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8250
8251 touch();
8252
8253 mTouchWindow->consumeAnyMotionDown();
8254}
8255
arthurhungb89ccb02020-12-30 16:19:01 +08008256class InputDispatcherDragTests : public InputDispatcherTest {
8257protected:
8258 std::shared_ptr<FakeApplicationHandle> mApp;
8259 sp<FakeWindowHandle> mWindow;
8260 sp<FakeWindowHandle> mSecondWindow;
8261 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008262 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008263 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
8264 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +08008265
8266 void SetUp() override {
8267 InputDispatcherTest::SetUp();
8268 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008269 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08008270 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08008271
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008272 mSecondWindow =
8273 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08008274 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08008275
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008276 mSpyWindow =
8277 sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008278 mSpyWindow->setSpy(true);
8279 mSpyWindow->setTrustedOverlay(true);
8280 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
8281
arthurhungb89ccb02020-12-30 16:19:01 +08008282 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008283 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08008284 }
8285
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008286 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
8287 switch (fromSource) {
8288 case AINPUT_SOURCE_TOUCHSCREEN:
8289 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8290 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
8291 ADISPLAY_ID_DEFAULT, {50, 50}))
8292 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8293 break;
8294 case AINPUT_SOURCE_STYLUS:
8295 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8296 injectMotionEvent(
8297 mDispatcher,
8298 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8299 AINPUT_SOURCE_STYLUS)
8300 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008301 .pointer(PointerBuilder(0, ToolType::STYLUS)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008302 .x(50)
8303 .y(50))
8304 .build()));
8305 break;
8306 case AINPUT_SOURCE_MOUSE:
8307 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8308 injectMotionEvent(
8309 mDispatcher,
8310 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
8311 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8312 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008313 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008314 .x(50)
8315 .y(50))
8316 .build()));
8317 break;
8318 default:
8319 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
8320 }
arthurhungb89ccb02020-12-30 16:19:01 +08008321
8322 // Window should receive motion event.
8323 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008324 // Spy window should also receive motion event
8325 mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +00008326 }
8327
8328 // Start performing drag, we will create a drag window and transfer touch to it.
8329 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
8330 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008331 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +00008332 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008333 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +00008334 }
arthurhungb89ccb02020-12-30 16:19:01 +08008335
8336 // The drag window covers the entire display
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008337 mDragWindow =
8338 sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008339 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
arthurhungb89ccb02020-12-30 16:19:01 +08008340 mDispatcher->setInputWindows(
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008341 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08008342
8343 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +00008344 bool transferred =
8345 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
Harry Cutts33476232023-01-30 19:57:29 +00008346 /*isDragDrop=*/true);
Arthur Hung54745652022-04-20 07:17:41 +00008347 if (transferred) {
8348 mWindow->consumeMotionCancel();
8349 mDragWindow->consumeMotionDown();
8350 }
8351 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +08008352 }
8353};
8354
8355TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008356 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +08008357
8358 // Move on window.
8359 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8360 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8361 ADISPLAY_ID_DEFAULT, {50, 50}))
8362 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8363 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8364 mWindow->consumeDragEvent(false, 50, 50);
8365 mSecondWindow->assertNoEvents();
8366
8367 // Move to another window.
8368 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8369 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8370 ADISPLAY_ID_DEFAULT, {150, 50}))
8371 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8372 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8373 mWindow->consumeDragEvent(true, 150, 50);
8374 mSecondWindow->consumeDragEvent(false, 50, 50);
8375
8376 // Move back to original window.
8377 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8378 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8379 ADISPLAY_ID_DEFAULT, {50, 50}))
8380 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8381 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8382 mWindow->consumeDragEvent(false, 50, 50);
8383 mSecondWindow->consumeDragEvent(true, -50, 50);
8384
8385 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8386 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
8387 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8388 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8389 mWindow->assertNoEvents();
8390 mSecondWindow->assertNoEvents();
8391}
8392
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008393TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008394 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008395
8396 // No cancel event after drag start
8397 mSpyWindow->assertNoEvents();
8398
8399 const MotionEvent secondFingerDownEvent =
8400 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8401 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008402 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8403 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008404 .build();
8405 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8406 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8407 InputEventInjectionSync::WAIT_FOR_RESULT))
8408 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8409
8410 // Receives cancel for first pointer after next pointer down
8411 mSpyWindow->consumeMotionCancel();
8412 mSpyWindow->consumeMotionDown();
8413
8414 mSpyWindow->assertNoEvents();
8415}
8416
arthurhungf452d0b2021-01-06 00:19:52 +08008417TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008418 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +08008419
8420 // Move on window.
8421 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8422 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8423 ADISPLAY_ID_DEFAULT, {50, 50}))
8424 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8425 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8426 mWindow->consumeDragEvent(false, 50, 50);
8427 mSecondWindow->assertNoEvents();
8428
8429 // Move to another window.
8430 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8431 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8432 ADISPLAY_ID_DEFAULT, {150, 50}))
8433 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8434 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8435 mWindow->consumeDragEvent(true, 150, 50);
8436 mSecondWindow->consumeDragEvent(false, 50, 50);
8437
8438 // drop to another window.
8439 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8440 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8441 {150, 50}))
8442 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8443 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8444 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8445 mWindow->assertNoEvents();
8446 mSecondWindow->assertNoEvents();
8447}
8448
arthurhung6d4bed92021-03-17 11:59:33 +08008449TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008450 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +08008451
8452 // Move on window and keep button pressed.
8453 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8454 injectMotionEvent(mDispatcher,
8455 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
8456 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008457 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008458 .build()))
8459 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8460 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8461 mWindow->consumeDragEvent(false, 50, 50);
8462 mSecondWindow->assertNoEvents();
8463
8464 // Move to another window and release button, expect to drop item.
8465 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8466 injectMotionEvent(mDispatcher,
8467 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
8468 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008469 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008470 .build()))
8471 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8472 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8473 mWindow->assertNoEvents();
8474 mSecondWindow->assertNoEvents();
8475 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8476
8477 // nothing to the window.
8478 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8479 injectMotionEvent(mDispatcher,
8480 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
8481 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008482 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008483 .build()))
8484 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8485 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8486 mWindow->assertNoEvents();
8487 mSecondWindow->assertNoEvents();
8488}
8489
Arthur Hung54745652022-04-20 07:17:41 +00008490TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008491 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +08008492
8493 // Set second window invisible.
8494 mSecondWindow->setVisible(false);
8495 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
8496
8497 // Move on window.
8498 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8499 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8500 ADISPLAY_ID_DEFAULT, {50, 50}))
8501 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8502 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8503 mWindow->consumeDragEvent(false, 50, 50);
8504 mSecondWindow->assertNoEvents();
8505
8506 // Move to another window.
8507 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8508 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8509 ADISPLAY_ID_DEFAULT, {150, 50}))
8510 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8511 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8512 mWindow->consumeDragEvent(true, 150, 50);
8513 mSecondWindow->assertNoEvents();
8514
8515 // drop to another window.
8516 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8517 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8518 {150, 50}))
8519 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8520 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8521 mFakePolicy->assertDropTargetEquals(nullptr);
8522 mWindow->assertNoEvents();
8523 mSecondWindow->assertNoEvents();
8524}
8525
Arthur Hung54745652022-04-20 07:17:41 +00008526TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008527 // Ensure window could track pointerIds if it didn't support split touch.
8528 mWindow->setPreventSplitting(true);
8529
Arthur Hung54745652022-04-20 07:17:41 +00008530 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8531 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8532 {50, 50}))
8533 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8534 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8535
8536 const MotionEvent secondFingerDownEvent =
8537 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8538 .displayId(ADISPLAY_ID_DEFAULT)
8539 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008540 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8541 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008542 .build();
8543 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8544 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8545 InputEventInjectionSync::WAIT_FOR_RESULT))
8546 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00008547 mWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
Arthur Hung54745652022-04-20 07:17:41 +00008548
8549 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008550 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008551}
8552
8553TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
8554 // First down on second window.
8555 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8556 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8557 {150, 50}))
8558 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8559
8560 mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8561
8562 // Second down on first window.
8563 const MotionEvent secondFingerDownEvent =
8564 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8565 .displayId(ADISPLAY_ID_DEFAULT)
8566 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008567 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8568 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008569 .build();
8570 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8571 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8572 InputEventInjectionSync::WAIT_FOR_RESULT))
8573 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8574 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8575
8576 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008577 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008578
8579 // Move on window.
8580 const MotionEvent secondFingerMoveEvent =
8581 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
8582 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008583 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8584 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008585 .build();
8586 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8587 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
8588 InputEventInjectionSync::WAIT_FOR_RESULT));
8589 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8590 mWindow->consumeDragEvent(false, 50, 50);
8591 mSecondWindow->consumeMotionMove();
8592
8593 // Release the drag pointer should perform drop.
8594 const MotionEvent secondFingerUpEvent =
8595 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
8596 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008597 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8598 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008599 .build();
8600 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8601 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
8602 InputEventInjectionSync::WAIT_FOR_RESULT));
8603 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8604 mFakePolicy->assertDropTargetEquals(mWindow->getToken());
8605 mWindow->assertNoEvents();
8606 mSecondWindow->consumeMotionMove();
8607}
8608
Arthur Hung3915c1f2022-05-31 07:17:17 +00008609TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008610 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +00008611
8612 // Update window of second display.
8613 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008614 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008615 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8616
8617 // Let second display has a touch state.
8618 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8619 injectMotionEvent(mDispatcher,
8620 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8621 AINPUT_SOURCE_TOUCHSCREEN)
8622 .displayId(SECOND_DISPLAY_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008623 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Arthur Hung3915c1f2022-05-31 07:17:17 +00008624 .build()));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07008625 windowInSecondary->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
Harry Cutts33476232023-01-30 19:57:29 +00008626 SECOND_DISPLAY_ID, /*expectedFlag=*/0);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008627 // Update window again.
8628 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8629
8630 // Move on window.
8631 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8632 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8633 ADISPLAY_ID_DEFAULT, {50, 50}))
8634 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8635 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8636 mWindow->consumeDragEvent(false, 50, 50);
8637 mSecondWindow->assertNoEvents();
8638
8639 // Move to another window.
8640 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8641 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8642 ADISPLAY_ID_DEFAULT, {150, 50}))
8643 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8644 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8645 mWindow->consumeDragEvent(true, 150, 50);
8646 mSecondWindow->consumeDragEvent(false, 50, 50);
8647
8648 // drop to another window.
8649 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8650 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8651 {150, 50}))
8652 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8653 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8654 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8655 mWindow->assertNoEvents();
8656 mSecondWindow->assertNoEvents();
8657}
8658
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008659TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
8660 startDrag(true, AINPUT_SOURCE_MOUSE);
8661 // Move on window.
8662 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8663 injectMotionEvent(mDispatcher,
8664 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8665 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8666 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008667 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008668 .x(50)
8669 .y(50))
8670 .build()))
8671 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8672 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8673 mWindow->consumeDragEvent(false, 50, 50);
8674 mSecondWindow->assertNoEvents();
8675
8676 // Move to another window.
8677 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8678 injectMotionEvent(mDispatcher,
8679 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8680 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8681 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008682 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008683 .x(150)
8684 .y(50))
8685 .build()))
8686 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8687 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8688 mWindow->consumeDragEvent(true, 150, 50);
8689 mSecondWindow->consumeDragEvent(false, 50, 50);
8690
8691 // drop to another window.
8692 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8693 injectMotionEvent(mDispatcher,
8694 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
8695 .buttonState(0)
8696 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008697 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008698 .x(150)
8699 .y(50))
8700 .build()))
8701 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8702 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8703 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8704 mWindow->assertNoEvents();
8705 mSecondWindow->assertNoEvents();
8706}
8707
Vishnu Nair062a8672021-09-03 16:07:44 -07008708class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
8709
8710TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
8711 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008712 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8713 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008714 window->setDropInput(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008715 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8716 window->setFocusable(true);
8717 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8718 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008719 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008720
8721 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008722 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008723 window->assertNoEvents();
8724
Prabir Pradhan678438e2023-04-13 19:32:51 +00008725 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8726 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008727 window->assertNoEvents();
8728
8729 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008730 window->setDropInput(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008731 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8732
Prabir Pradhan678438e2023-04-13 19:32:51 +00008733 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008734 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8735
Prabir Pradhan678438e2023-04-13 19:32:51 +00008736 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8737 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008738 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8739 window->assertNoEvents();
8740}
8741
8742TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
8743 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8744 std::make_shared<FakeApplicationHandle>();
8745 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008746 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8747 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008748 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
8749 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008750 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008751 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008752 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8753 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008754 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008755 window->setOwnerInfo(222, 222);
8756 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8757 window->setFocusable(true);
8758 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8759 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008760 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008761
8762 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008763 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008764 window->assertNoEvents();
8765
Prabir Pradhan678438e2023-04-13 19:32:51 +00008766 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8767 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008768 window->assertNoEvents();
8769
8770 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008771 window->setDropInputIfObscured(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008772 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8773
Prabir Pradhan678438e2023-04-13 19:32:51 +00008774 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008775 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8776
Prabir Pradhan678438e2023-04-13 19:32:51 +00008777 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8778 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008779 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
8780 window->assertNoEvents();
8781}
8782
8783TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
8784 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8785 std::make_shared<FakeApplicationHandle>();
8786 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008787 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8788 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008789 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
8790 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008791 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008792 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008793 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8794 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008795 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008796 window->setOwnerInfo(222, 222);
8797 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8798 window->setFocusable(true);
8799 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8800 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008801 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008802
8803 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008804 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008805 window->assertNoEvents();
8806
Prabir Pradhan678438e2023-04-13 19:32:51 +00008807 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8808 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008809 window->assertNoEvents();
8810
8811 // When the window is no longer obscured because it went on top, it should get input
8812 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}});
8813
Prabir Pradhan678438e2023-04-13 19:32:51 +00008814 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008815 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8816
Prabir Pradhan678438e2023-04-13 19:32:51 +00008817 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8818 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008819 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8820 window->assertNoEvents();
8821}
8822
Antonio Kantekf16f2832021-09-28 04:39:20 +00008823class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
8824protected:
8825 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +00008826 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008827 sp<FakeWindowHandle> mWindow;
8828 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +00008829 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008830
8831 void SetUp() override {
8832 InputDispatcherTest::SetUp();
8833
8834 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +00008835 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008836 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008837 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008838 setFocusedWindow(mWindow);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008839 mSecondWindow =
8840 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008841 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +00008842 mThirdWindow =
8843 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
8844 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
8845 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008846
8847 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Antonio Kantek15beb512022-06-13 22:35:41 +00008848 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}},
8849 {SECOND_DISPLAY_ID, {mThirdWindow}}});
8850 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008851 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008852
Antonio Kantek15beb512022-06-13 22:35:41 +00008853 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +00008854 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008855 WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -07008856 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
8857 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008858 mThirdWindow->assertNoEvents();
8859 }
8860
8861 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
8862 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008863 SECONDARY_WINDOW_UID, /*hasPermission=*/true,
Antonio Kantek15beb512022-06-13 22:35:41 +00008864 SECOND_DISPLAY_ID)) {
8865 mWindow->assertNoEvents();
8866 mSecondWindow->assertNoEvents();
8867 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -07008868 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00008869 }
8870
Antonio Kantek15beb512022-06-13 22:35:41 +00008871 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid,
8872 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -07008873 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
8874 ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008875 mWindow->consumeTouchModeEvent(inTouchMode);
8876 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008877 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +00008878 }
8879};
8880
Antonio Kantek26defcf2022-02-08 01:12:27 +00008881TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008882 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +00008883 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
8884 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008885 /* hasPermission=*/false);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008886}
8887
Antonio Kantek26defcf2022-02-08 01:12:27 +00008888TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
8889 const WindowInfo& windowInfo = *mWindow->getInfo();
8890 int32_t ownerPid = windowInfo.ownerPid;
8891 int32_t ownerUid = windowInfo.ownerUid;
8892 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
8893 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008894 ownerUid, /*hasPermission=*/false,
Antonio Kanteka042c022022-07-06 16:51:07 -07008895 ADISPLAY_ID_DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +00008896 mWindow->assertNoEvents();
8897 mSecondWindow->assertNoEvents();
8898}
8899
8900TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
8901 const WindowInfo& windowInfo = *mWindow->getInfo();
8902 int32_t ownerPid = windowInfo.ownerPid;
8903 int32_t ownerUid = windowInfo.ownerUid;
8904 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
Antonio Kantek15beb512022-06-13 22:35:41 +00008905 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008906 ownerUid, /*hasPermission=*/true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008907}
8908
Antonio Kantekf16f2832021-09-28 04:39:20 +00008909TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008910 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +00008911 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
8912 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008913 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008914 mWindow->assertNoEvents();
8915 mSecondWindow->assertNoEvents();
8916}
8917
Antonio Kantek15beb512022-06-13 22:35:41 +00008918TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
8919 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
8920 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8921 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008922 /*hasPermission=*/true, SECOND_DISPLAY_ID));
Antonio Kantek15beb512022-06-13 22:35:41 +00008923 mWindow->assertNoEvents();
8924 mSecondWindow->assertNoEvents();
8925 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
8926}
8927
Antonio Kantek48710e42022-03-24 14:19:30 -07008928TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
8929 // Interact with the window first.
8930 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
8931 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8932 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
8933
8934 // Then remove focus.
8935 mWindow->setFocusable(false);
8936 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
8937
8938 // Assert that caller can switch touch mode by owning one of the last interacted window.
8939 const WindowInfo& windowInfo = *mWindow->getInfo();
8940 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8941 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008942 /*hasPermission=*/false, ADISPLAY_ID_DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -07008943}
8944
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008945class InputDispatcherSpyWindowTest : public InputDispatcherTest {
8946public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008947 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008948 std::shared_ptr<FakeApplicationHandle> application =
8949 std::make_shared<FakeApplicationHandle>();
8950 std::string name = "Fake Spy ";
8951 name += std::to_string(mSpyCount++);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008952 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher,
8953 name.c_str(), ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008954 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008955 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008956 return spy;
8957 }
8958
8959 sp<FakeWindowHandle> createForeground() {
8960 std::shared_ptr<FakeApplicationHandle> application =
8961 std::make_shared<FakeApplicationHandle>();
8962 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008963 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
8964 ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008965 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008966 return window;
8967 }
8968
8969private:
8970 int mSpyCount{0};
8971};
8972
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008973using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008974/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008975 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
8976 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008977TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
8978 ScopedSilentDeath _silentDeath;
8979
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008980 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008981 spy->setTrustedOverlay(false);
8982 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}),
8983 ".* not a trusted overlay");
8984}
8985
8986/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008987 * Input injection into a display with a spy window but no foreground windows should succeed.
8988 */
8989TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008990 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008991 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}});
8992
8993 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8994 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8995 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8996 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8997}
8998
8999/**
9000 * Verify the order in which different input windows receive events. The touched foreground window
9001 * (if there is one) should always receive the event first. When there are multiple spy windows, the
9002 * spy windows will receive the event according to their Z-order, where the top-most spy window will
9003 * receive events before ones belows it.
9004 *
9005 * Here, we set up a scenario with four windows in the following Z order from the top:
9006 * spy1, spy2, window, spy3.
9007 * We then inject an event and verify that the foreground "window" receives it first, followed by
9008 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
9009 * window.
9010 */
9011TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
9012 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009013 auto spy1 = createSpy();
9014 auto spy2 = createSpy();
9015 auto spy3 = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009016 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}});
9017 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
9018 const size_t numChannels = channels.size();
9019
Michael Wright8e9a8562022-02-09 13:44:29 +00009020 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009021 if (!epollFd.ok()) {
9022 FAIL() << "Failed to create epoll fd";
9023 }
9024
9025 for (size_t i = 0; i < numChannels; i++) {
9026 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
9027 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
9028 FAIL() << "Failed to add fd to epoll";
9029 }
9030 }
9031
9032 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9033 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9034 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9035
9036 std::vector<size_t> eventOrder;
9037 std::vector<struct epoll_event> events(numChannels);
9038 for (;;) {
9039 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
9040 (100ms).count());
9041 if (nFds < 0) {
9042 FAIL() << "Failed to call epoll_wait";
9043 }
9044 if (nFds == 0) {
9045 break; // epoll_wait timed out
9046 }
9047 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -07009048 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -07009049 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009050 channels[i]->consumeMotionDown();
9051 }
9052 }
9053
9054 // Verify the order in which the events were received.
9055 EXPECT_EQ(3u, eventOrder.size());
9056 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
9057 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
9058 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
9059}
9060
9061/**
9062 * A spy window using the NOT_TOUCHABLE flag does not receive events.
9063 */
9064TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
9065 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009066 auto spy = createSpy();
9067 spy->setTouchable(false);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009068 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9069
9070 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9071 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9072 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9073 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9074 spy->assertNoEvents();
9075}
9076
9077/**
9078 * A spy window will only receive gestures that originate within its touchable region. Gestures that
9079 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
9080 * to the window.
9081 */
9082TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
9083 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009084 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009085 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
9086 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9087
9088 // Inject an event outside the spy window's touchable region.
9089 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9090 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9091 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9092 window->consumeMotionDown();
9093 spy->assertNoEvents();
9094 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9095 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9096 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9097 window->consumeMotionUp();
9098 spy->assertNoEvents();
9099
9100 // Inject an event inside the spy window's touchable region.
9101 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9102 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9103 {5, 10}))
9104 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9105 window->consumeMotionDown();
9106 spy->consumeMotionDown();
9107}
9108
9109/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009110 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009111 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009112 */
9113TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
9114 auto window = createForeground();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009115 window->setOwnerInfo(12, 34);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009116 auto spy = createSpy();
9117 spy->setWatchOutsideTouch(true);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009118 spy->setOwnerInfo(56, 78);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009119 spy->setFrame(Rect{0, 0, 20, 20});
9120 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9121
9122 // Inject an event outside the spy window's frame and touchable region.
9123 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009124 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9125 {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009126 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9127 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009128 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009129}
9130
9131/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009132 * Even when a spy window spans over multiple foreground windows, the spy should receive all
9133 * pointers that are down within its bounds.
9134 */
9135TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
9136 auto windowLeft = createForeground();
9137 windowLeft->setFrame({0, 0, 100, 200});
9138 auto windowRight = createForeground();
9139 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009140 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009141 spy->setFrame({0, 0, 200, 200});
9142 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}});
9143
9144 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9145 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9146 {50, 50}))
9147 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9148 windowLeft->consumeMotionDown();
9149 spy->consumeMotionDown();
9150
9151 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08009152 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009153 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009154 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
9155 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009156 .build();
9157 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9158 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9159 InputEventInjectionSync::WAIT_FOR_RESULT))
9160 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9161 windowRight->consumeMotionDown();
Harry Cutts33476232023-01-30 19:57:29 +00009162 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009163}
9164
9165/**
9166 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
9167 * the spy should receive the second pointer with ACTION_DOWN.
9168 */
9169TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
9170 auto window = createForeground();
9171 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009172 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009173 spyRight->setFrame({100, 0, 200, 200});
9174 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}});
9175
9176 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9177 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9178 {50, 50}))
9179 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9180 window->consumeMotionDown();
9181 spyRight->assertNoEvents();
9182
9183 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08009184 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009185 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009186 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
9187 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009188 .build();
9189 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9190 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9191 InputEventInjectionSync::WAIT_FOR_RESULT))
9192 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00009193 window->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009194 spyRight->consumeMotionDown();
9195}
9196
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009197/**
9198 * The spy window should not be able to affect whether or not touches are split. Only the foreground
9199 * windows should be allowed to control split touch.
9200 */
9201TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08009202 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009203 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009204 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08009205 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009206
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009207 auto window = createForeground();
9208 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009209
9210 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9211
9212 // First finger down, no window touched.
9213 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9214 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9215 {100, 200}))
9216 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9217 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9218 window->assertNoEvents();
9219
9220 // Second finger down on window, the window should receive touch down.
9221 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08009222 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009223 .displayId(ADISPLAY_ID_DEFAULT)
9224 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009225 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9226 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009227 .build();
9228 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9229 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9230 InputEventInjectionSync::WAIT_FOR_RESULT))
9231 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9232
9233 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00009234 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009235}
9236
9237/**
9238 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
9239 * do not receive key events.
9240 */
9241TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009242 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009243 spy->setFocusable(false);
9244
9245 auto window = createForeground();
9246 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9247 setFocusedWindow(window);
9248 window->consumeFocusEvent(true);
9249
9250 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
9251 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
9252 window->consumeKeyDown(ADISPLAY_ID_NONE);
9253
9254 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
9255 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
9256 window->consumeKeyUp(ADISPLAY_ID_NONE);
9257
9258 spy->assertNoEvents();
9259}
9260
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009261using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
9262
9263/**
9264 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
9265 * are currently sent to any other windows - including other spy windows - will also be cancelled.
9266 */
9267TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
9268 auto window = createForeground();
9269 auto spy1 = createSpy();
9270 auto spy2 = createSpy();
9271 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}});
9272
9273 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9274 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9275 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9276 window->consumeMotionDown();
9277 spy1->consumeMotionDown();
9278 spy2->consumeMotionDown();
9279
9280 // Pilfer pointers from the second spy window.
9281 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
9282 spy2->assertNoEvents();
9283 spy1->consumeMotionCancel();
9284 window->consumeMotionCancel();
9285
9286 // The rest of the gesture should only be sent to the second spy window.
9287 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9288 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
9289 ADISPLAY_ID_DEFAULT))
9290 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9291 spy2->consumeMotionMove();
9292 spy1->assertNoEvents();
9293 window->assertNoEvents();
9294}
9295
9296/**
9297 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
9298 * in the middle of the gesture.
9299 */
9300TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
9301 auto window = createForeground();
9302 auto spy = createSpy();
9303 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9304
9305 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9306 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9307 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9308 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9309 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9310
9311 window->releaseChannel();
9312
9313 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9314
9315 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9316 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9317 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9318 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
9319}
9320
9321/**
9322 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
9323 * the spy, but not to any other windows.
9324 */
9325TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
9326 auto spy = createSpy();
9327 auto window = createForeground();
9328
9329 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9330
9331 // First finger down on the window and the spy.
9332 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9333 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9334 {100, 200}))
9335 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9336 spy->consumeMotionDown();
9337 window->consumeMotionDown();
9338
9339 // Spy window pilfers the pointers.
9340 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9341 window->consumeMotionCancel();
9342
9343 // Second finger down on the window and spy, but the window should not receive the pointer down.
9344 const MotionEvent secondFingerDownEvent =
9345 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9346 .displayId(ADISPLAY_ID_DEFAULT)
9347 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009348 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9349 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009350 .build();
9351 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9352 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9353 InputEventInjectionSync::WAIT_FOR_RESULT))
9354 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9355
Harry Cutts33476232023-01-30 19:57:29 +00009356 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009357
9358 // Third finger goes down outside all windows, so injection should fail.
9359 const MotionEvent thirdFingerDownEvent =
9360 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9361 .displayId(ADISPLAY_ID_DEFAULT)
9362 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009363 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9364 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
9365 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009366 .build();
9367 ASSERT_EQ(InputEventInjectionResult::FAILED,
9368 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
9369 InputEventInjectionSync::WAIT_FOR_RESULT))
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08009370 << "Inject motion event should return InputEventInjectionResult::FAILED";
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009371
9372 spy->assertNoEvents();
9373 window->assertNoEvents();
9374}
9375
9376/**
9377 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
9378 */
9379TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
9380 auto spy = createSpy();
9381 spy->setFrame(Rect(0, 0, 100, 100));
9382 auto window = createForeground();
9383 window->setFrame(Rect(0, 0, 200, 200));
9384
9385 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9386
9387 // First finger down on the window only
9388 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9389 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9390 {150, 150}))
9391 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9392 window->consumeMotionDown();
9393
9394 // Second finger down on the spy and window
9395 const MotionEvent secondFingerDownEvent =
9396 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9397 .displayId(ADISPLAY_ID_DEFAULT)
9398 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009399 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
9400 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009401 .build();
9402 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9403 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9404 InputEventInjectionSync::WAIT_FOR_RESULT))
9405 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9406 spy->consumeMotionDown();
9407 window->consumeMotionPointerDown(1);
9408
9409 // Third finger down on the spy and window
9410 const MotionEvent thirdFingerDownEvent =
9411 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9412 .displayId(ADISPLAY_ID_DEFAULT)
9413 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009414 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
9415 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
9416 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009417 .build();
9418 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9419 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
9420 InputEventInjectionSync::WAIT_FOR_RESULT))
9421 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9422 spy->consumeMotionPointerDown(1);
9423 window->consumeMotionPointerDown(2);
9424
9425 // Spy window pilfers the pointers.
9426 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9427 window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
9428 window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
9429
9430 spy->assertNoEvents();
9431 window->assertNoEvents();
9432}
9433
9434/**
9435 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
9436 * other windows should be canceled. If this results in the cancellation of all pointers for some
9437 * window, then that window should receive ACTION_CANCEL.
9438 */
9439TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
9440 auto spy = createSpy();
9441 spy->setFrame(Rect(0, 0, 100, 100));
9442 auto window = createForeground();
9443 window->setFrame(Rect(0, 0, 200, 200));
9444
9445 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9446
9447 // First finger down on both spy and window
9448 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9449 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9450 {10, 10}))
9451 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9452 window->consumeMotionDown();
9453 spy->consumeMotionDown();
9454
9455 // Second finger down on the spy and window
9456 const MotionEvent secondFingerDownEvent =
9457 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9458 .displayId(ADISPLAY_ID_DEFAULT)
9459 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009460 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9461 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009462 .build();
9463 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9464 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9465 InputEventInjectionSync::WAIT_FOR_RESULT))
9466 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9467 spy->consumeMotionPointerDown(1);
9468 window->consumeMotionPointerDown(1);
9469
9470 // Spy window pilfers the pointers.
9471 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9472 window->consumeMotionCancel();
9473
9474 spy->assertNoEvents();
9475 window->assertNoEvents();
9476}
9477
9478/**
9479 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
9480 * be sent to other windows
9481 */
9482TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
9483 auto spy = createSpy();
9484 spy->setFrame(Rect(0, 0, 100, 100));
9485 auto window = createForeground();
9486 window->setFrame(Rect(0, 0, 200, 200));
9487
9488 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9489
9490 // First finger down on both window and spy
9491 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9492 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9493 {10, 10}))
9494 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9495 window->consumeMotionDown();
9496 spy->consumeMotionDown();
9497
9498 // Spy window pilfers the pointers.
9499 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9500 window->consumeMotionCancel();
9501
9502 // Second finger down on the window only
9503 const MotionEvent secondFingerDownEvent =
9504 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9505 .displayId(ADISPLAY_ID_DEFAULT)
9506 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009507 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9508 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009509 .build();
9510 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9511 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9512 InputEventInjectionSync::WAIT_FOR_RESULT))
9513 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9514 window->consumeMotionDown();
9515 window->assertNoEvents();
9516
9517 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
9518 spy->consumeMotionMove();
9519 spy->assertNoEvents();
9520}
9521
Prabir Pradhand65552b2021-10-07 11:23:50 -07009522class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
9523public:
9524 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
9525 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9526 std::make_shared<FakeApplicationHandle>();
9527 sp<FakeWindowHandle> overlay =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009528 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
9529 "Stylus interceptor window", ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009530 overlay->setFocusable(false);
9531 overlay->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009532 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009533 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009534 overlay->setTrustedOverlay(true);
9535
9536 std::shared_ptr<FakeApplicationHandle> application =
9537 std::make_shared<FakeApplicationHandle>();
9538 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009539 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
9540 ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009541 window->setFocusable(true);
9542 window->setOwnerInfo(222, 222);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009543
9544 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
9545 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9546 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00009547 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009548 return {std::move(overlay), std::move(window)};
9549 }
9550
9551 void sendFingerEvent(int32_t action) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00009552 mDispatcher->notifyMotion(
Prabir Pradhand65552b2021-10-07 11:23:50 -07009553 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
Prabir Pradhan678438e2023-04-13 19:32:51 +00009554 ADISPLAY_ID_DEFAULT, {PointF{20, 20}}));
Prabir Pradhand65552b2021-10-07 11:23:50 -07009555 }
9556
9557 void sendStylusEvent(int32_t action) {
9558 NotifyMotionArgs motionArgs =
9559 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
9560 ADISPLAY_ID_DEFAULT, {PointF{30, 40}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009561 motionArgs.pointerProperties[0].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +00009562 mDispatcher->notifyMotion(motionArgs);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009563 }
9564};
9565
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08009566using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
9567
9568TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
9569 ScopedSilentDeath _silentDeath;
9570
Prabir Pradhand65552b2021-10-07 11:23:50 -07009571 auto [overlay, window] = setupStylusOverlayScenario();
9572 overlay->setTrustedOverlay(false);
9573 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
9574 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}),
9575 ".* not a trusted overlay");
9576}
9577
9578TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
9579 auto [overlay, window] = setupStylusOverlayScenario();
9580 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9581
9582 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9583 overlay->consumeMotionDown();
9584 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9585 overlay->consumeMotionUp();
9586
9587 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9588 window->consumeMotionDown();
9589 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9590 window->consumeMotionUp();
9591
9592 overlay->assertNoEvents();
9593 window->assertNoEvents();
9594}
9595
9596TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
9597 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009598 overlay->setSpy(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009599 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9600
9601 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9602 overlay->consumeMotionDown();
9603 window->consumeMotionDown();
9604 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9605 overlay->consumeMotionUp();
9606 window->consumeMotionUp();
9607
9608 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9609 window->consumeMotionDown();
9610 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9611 window->consumeMotionUp();
9612
9613 overlay->assertNoEvents();
9614 window->assertNoEvents();
9615}
9616
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00009617/**
9618 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
9619 * The scenario is as follows:
9620 * - The stylus interceptor overlay is configured as a spy window.
9621 * - The stylus interceptor spy receives the start of a new stylus gesture.
9622 * - It pilfers pointers and then configures itself to no longer be a spy.
9623 * - The stylus interceptor continues to receive the rest of the gesture.
9624 */
9625TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
9626 auto [overlay, window] = setupStylusOverlayScenario();
9627 overlay->setSpy(true);
9628 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9629
9630 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9631 overlay->consumeMotionDown();
9632 window->consumeMotionDown();
9633
9634 // The interceptor pilfers the pointers.
9635 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
9636 window->consumeMotionCancel();
9637
9638 // The interceptor configures itself so that it is no longer a spy.
9639 overlay->setSpy(false);
9640 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9641
9642 // It continues to receive the rest of the stylus gesture.
9643 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
9644 overlay->consumeMotionMove();
9645 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9646 overlay->consumeMotionUp();
9647
9648 window->assertNoEvents();
9649}
9650
Prabir Pradhan5735a322022-04-11 17:23:34 +00009651struct User {
9652 int32_t mPid;
9653 int32_t mUid;
9654 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
9655 std::unique_ptr<InputDispatcher>& mDispatcher;
9656
9657 User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid)
9658 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
9659
9660 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
9661 return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
9662 ADISPLAY_ID_DEFAULT, {100, 200},
9663 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
9664 AMOTION_EVENT_INVALID_CURSOR_POSITION},
9665 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
9666 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
9667 }
9668
9669 InputEventInjectionResult injectTargetedKey(int32_t action) const {
Harry Cutts33476232023-01-30 19:57:29 +00009670 return inputdispatcher::injectKey(mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +00009671 InputEventInjectionSync::WAIT_FOR_RESULT,
Harry Cutts33476232023-01-30 19:57:29 +00009672 INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid},
Prabir Pradhan5735a322022-04-11 17:23:34 +00009673 mPolicyFlags);
9674 }
9675
9676 sp<FakeWindowHandle> createWindow() const {
9677 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9678 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009679 sp<FakeWindowHandle> window =
9680 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window",
9681 ADISPLAY_ID_DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +00009682 window->setOwnerInfo(mPid, mUid);
9683 return window;
9684 }
9685};
9686
9687using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
9688
9689TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
9690 auto owner = User(mDispatcher, 10, 11);
9691 auto window = owner.createWindow();
9692 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9693
9694 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9695 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9696 window->consumeMotionDown();
9697
9698 setFocusedWindow(window);
9699 window->consumeFocusEvent(true);
9700
9701 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9702 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9703 window->consumeKeyDown(ADISPLAY_ID_NONE);
9704}
9705
9706TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
9707 auto owner = User(mDispatcher, 10, 11);
9708 auto window = owner.createWindow();
9709 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9710
9711 auto rando = User(mDispatcher, 20, 21);
9712 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9713 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9714
9715 setFocusedWindow(window);
9716 window->consumeFocusEvent(true);
9717
9718 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9719 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9720 window->assertNoEvents();
9721}
9722
9723TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
9724 auto owner = User(mDispatcher, 10, 11);
9725 auto window = owner.createWindow();
9726 auto spy = owner.createWindow();
9727 spy->setSpy(true);
9728 spy->setTrustedOverlay(true);
9729 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9730
9731 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9732 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9733 spy->consumeMotionDown();
9734 window->consumeMotionDown();
9735}
9736
9737TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
9738 auto owner = User(mDispatcher, 10, 11);
9739 auto window = owner.createWindow();
9740
9741 auto rando = User(mDispatcher, 20, 21);
9742 auto randosSpy = rando.createWindow();
9743 randosSpy->setSpy(true);
9744 randosSpy->setTrustedOverlay(true);
9745 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9746
9747 // The event is targeted at owner's window, so injection should succeed, but the spy should
9748 // not receive the event.
9749 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9750 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9751 randosSpy->assertNoEvents();
9752 window->consumeMotionDown();
9753}
9754
9755TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
9756 auto owner = User(mDispatcher, 10, 11);
9757 auto window = owner.createWindow();
9758
9759 auto rando = User(mDispatcher, 20, 21);
9760 auto randosSpy = rando.createWindow();
9761 randosSpy->setSpy(true);
9762 randosSpy->setTrustedOverlay(true);
9763 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9764
9765 // A user that has injection permission can inject into any window.
9766 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9767 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
9768 ADISPLAY_ID_DEFAULT));
9769 randosSpy->consumeMotionDown();
9770 window->consumeMotionDown();
9771
9772 setFocusedWindow(randosSpy);
9773 randosSpy->consumeFocusEvent(true);
9774
9775 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
9776 randosSpy->consumeKeyDown(ADISPLAY_ID_NONE);
9777 window->assertNoEvents();
9778}
9779
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -07009780TEST_F(InputDispatcherTargetedInjectionTest, CannotGenerateActionOutsideToOtherUids) {
Prabir Pradhan5735a322022-04-11 17:23:34 +00009781 auto owner = User(mDispatcher, 10, 11);
9782 auto window = owner.createWindow();
9783
9784 auto rando = User(mDispatcher, 20, 21);
9785 auto randosWindow = rando.createWindow();
9786 randosWindow->setFrame(Rect{-10, -10, -5, -5});
9787 randosWindow->setWatchOutsideTouch(true);
9788 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}});
9789
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -07009790 // Do not allow generation of ACTION_OUTSIDE events into windows owned by different uids.
Prabir Pradhan5735a322022-04-11 17:23:34 +00009791 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9792 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9793 window->consumeMotionDown();
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -07009794 randosWindow->assertNoEvents();
Prabir Pradhan5735a322022-04-11 17:23:34 +00009795}
9796
Garfield Tane84e6f92019-08-29 17:28:41 -07009797} // namespace android::inputdispatcher