blob: 51202b7536c0bcb9ff51d327d96e280afee7aa95 [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"
Michael Wrightd02c5b62014-02-10 15:10:22 -080019
Cody Heiner166a5af2023-07-07 12:25:00 -070020#include <NotifyArgsBuilders.h>
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 Pradhane59c6dc2023-06-13 19:53:03 +000098static constexpr gui::Pid WINDOW_PID{999};
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000099static constexpr gui::Uid WINDOW_UID{1001};
Prabir Pradhan5735a322022-04-11 17:23:34 +0000100
Antonio Kantek15beb512022-06-13 22:35:41 +0000101// The default pid and uid for the windows created on the secondary display by the test.
Prabir Pradhane59c6dc2023-06-13 19:53:03 +0000102static constexpr gui::Pid SECONDARY_WINDOW_PID{1010};
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000103static constexpr gui::Uid SECONDARY_WINDOW_UID{1012};
Antonio Kantek15beb512022-06-13 22:35:41 +0000104
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +0000105// An arbitrary pid of the gesture monitor window
Prabir Pradhane59c6dc2023-06-13 19:53:03 +0000106static constexpr gui::Pid MONITOR_PID{2001};
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +0000107
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 Pradhane59c6dc2023-06-13 19:53:03 +0000211 struct AnrResult {
212 sp<IBinder> token{};
213 gui::Pid pid{-1};
214 };
Prabir Pradhanedd96402022-02-15 01:46:16 -0800215
Michael Wrightd02c5b62014-02-10 15:10:22 -0800216public:
Prabir Pradhana41d2442023-04-20 21:30:40 +0000217 FakeInputDispatcherPolicy() = default;
218 virtual ~FakeInputDispatcherPolicy() = default;
Jackal Guof9696682018-10-05 12:23:23 +0800219
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800220 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700221 assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700222 ASSERT_EQ(event.getType(), InputEventType::KEY);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700223 EXPECT_EQ(event.getDisplayId(), args.displayId);
224
225 const auto& keyEvent = static_cast<const KeyEvent&>(event);
226 EXPECT_EQ(keyEvent.getEventTime(), args.eventTime);
227 EXPECT_EQ(keyEvent.getAction(), args.action);
228 });
Jackal Guof9696682018-10-05 12:23:23 +0800229 }
230
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700231 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) {
232 assertFilterInputEventWasCalledInternal([&](const InputEvent& event) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700233 ASSERT_EQ(event.getType(), InputEventType::MOTION);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700234 EXPECT_EQ(event.getDisplayId(), args.displayId);
235
236 const auto& motionEvent = static_cast<const MotionEvent&>(event);
237 EXPECT_EQ(motionEvent.getEventTime(), args.eventTime);
238 EXPECT_EQ(motionEvent.getAction(), args.action);
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000239 EXPECT_NEAR(motionEvent.getX(0), point.x, MotionEvent::ROUNDING_PRECISION);
240 EXPECT_NEAR(motionEvent.getY(0), point.y, MotionEvent::ROUNDING_PRECISION);
241 EXPECT_NEAR(motionEvent.getRawX(0), point.x, MotionEvent::ROUNDING_PRECISION);
242 EXPECT_NEAR(motionEvent.getRawY(0), point.y, MotionEvent::ROUNDING_PRECISION);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700243 });
Jackal Guof9696682018-10-05 12:23:23 +0800244 }
245
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700246 void assertFilterInputEventWasNotCalled() {
247 std::scoped_lock lock(mLock);
248 ASSERT_EQ(nullptr, mFilteredEvent);
249 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800250
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800251 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700252 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800253 ASSERT_TRUE(mConfigurationChangedTime)
254 << "Timed out waiting for configuration changed call";
255 ASSERT_EQ(*mConfigurationChangedTime, when);
256 mConfigurationChangedTime = std::nullopt;
257 }
258
259 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700260 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800261 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800262 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800263 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
264 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
265 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
266 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
267 mLastNotifySwitch = std::nullopt;
268 }
269
chaviwfd6d3512019-03-25 13:23:49 -0700270 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700271 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800272 ASSERT_EQ(touchedToken, mOnPointerDownToken);
273 mOnPointerDownToken.clear();
274 }
275
276 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700277 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800278 ASSERT_TRUE(mOnPointerDownToken == nullptr)
279 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700280 }
281
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700282 // This function must be called soon after the expected ANR timer starts,
283 // because we are also checking how much time has passed.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500284 void assertNotifyNoFocusedWindowAnrWasCalled(
Chris Yea209fde2020-07-22 13:54:51 -0700285 std::chrono::nanoseconds timeout,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500286 const std::shared_ptr<InputApplicationHandle>& expectedApplication) {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800287 std::unique_lock lock(mLock);
288 android::base::ScopedLockAssertion assumeLocked(mLock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500289 std::shared_ptr<InputApplicationHandle> application;
Prabir Pradhanedd96402022-02-15 01:46:16 -0800290 ASSERT_NO_FATAL_FAILURE(
291 application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock));
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500292 ASSERT_EQ(expectedApplication, application);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700293 }
294
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000295 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
Prabir Pradhanedd96402022-02-15 01:46:16 -0800296 const sp<WindowInfoHandle>& window) {
297 LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null");
298 assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(),
299 window->getInfo()->ownerPid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500300 }
301
Prabir Pradhanedd96402022-02-15 01:46:16 -0800302 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
303 const sp<IBinder>& expectedToken,
Prabir Pradhane59c6dc2023-06-13 19:53:03 +0000304 gui::Pid expectedPid) {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800305 std::unique_lock lock(mLock);
306 android::base::ScopedLockAssertion assumeLocked(mLock);
307 AnrResult result;
308 ASSERT_NO_FATAL_FAILURE(result =
309 getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock));
Prabir Pradhane59c6dc2023-06-13 19:53:03 +0000310 ASSERT_EQ(expectedToken, result.token);
311 ASSERT_EQ(expectedPid, result.pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500312 }
313
Prabir Pradhanedd96402022-02-15 01:46:16 -0800314 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000315 sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500316 std::unique_lock lock(mLock);
317 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800318 AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock);
319 const auto& [token, _] = result;
320 return token;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000321 }
322
Prabir Pradhanedd96402022-02-15 01:46:16 -0800323 void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken,
Prabir Pradhane59c6dc2023-06-13 19:53:03 +0000324 gui::Pid expectedPid) {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800325 std::unique_lock lock(mLock);
326 android::base::ScopedLockAssertion assumeLocked(mLock);
327 AnrResult result;
328 ASSERT_NO_FATAL_FAILURE(
329 result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock));
Prabir Pradhane59c6dc2023-06-13 19:53:03 +0000330 ASSERT_EQ(expectedToken, result.token);
331 ASSERT_EQ(expectedPid, result.pid);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800332 }
333
334 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000335 sp<IBinder> getResponsiveWindowToken() {
336 std::unique_lock lock(mLock);
337 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800338 AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock);
339 const auto& [token, _] = result;
340 return token;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700341 }
342
343 void assertNotifyAnrWasNotCalled() {
344 std::scoped_lock lock(mLock);
345 ASSERT_TRUE(mAnrApplications.empty());
Prabir Pradhanedd96402022-02-15 01:46:16 -0800346 ASSERT_TRUE(mAnrWindows.empty());
347 ASSERT_TRUE(mResponsiveWindows.empty())
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500348 << "ANR was not called, but please also consume the 'connection is responsive' "
349 "signal";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700350 }
351
Garfield Tan1c7bc862020-01-28 13:24:04 -0800352 void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) {
353 mConfig.keyRepeatTimeout = timeout;
354 mConfig.keyRepeatDelay = delay;
355 }
356
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000357 PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -0800358 std::unique_lock lock(mLock);
359 base::ScopedLockAssertion assumeLocked(mLock);
360
361 if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms,
362 [this, enabled]() REQUIRES(mLock) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000363 return mPointerCaptureRequest->enable ==
Prabir Pradhan99987712020-11-10 18:43:05 -0800364 enabled;
365 })) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000366 ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled
367 << ") to be called.";
368 return {};
Prabir Pradhan99987712020-11-10 18:43:05 -0800369 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000370 auto request = *mPointerCaptureRequest;
371 mPointerCaptureRequest.reset();
372 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -0800373 }
374
375 void assertSetPointerCaptureNotCalled() {
376 std::unique_lock lock(mLock);
377 base::ScopedLockAssertion assumeLocked(mLock);
378
379 if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000380 FAIL() << "Expected setPointerCapture(request) to not be called, but was called. "
Prabir Pradhan99987712020-11-10 18:43:05 -0800381 "enabled = "
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000382 << std::to_string(mPointerCaptureRequest->enable);
Prabir Pradhan99987712020-11-10 18:43:05 -0800383 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000384 mPointerCaptureRequest.reset();
Prabir Pradhan99987712020-11-10 18:43:05 -0800385 }
386
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -0700387 void assertDropTargetEquals(const InputDispatcherInterface& dispatcher,
388 const sp<IBinder>& targetToken) {
389 dispatcher.waitForIdle();
arthurhungf452d0b2021-01-06 00:19:52 +0800390 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800391 ASSERT_TRUE(mNotifyDropWindowWasCalled);
arthurhungf452d0b2021-01-06 00:19:52 +0800392 ASSERT_EQ(targetToken, mDropTargetWindowToken);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800393 mNotifyDropWindowWasCalled = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800394 }
395
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800396 void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) {
397 std::unique_lock lock(mLock);
398 base::ScopedLockAssertion assumeLocked(mLock);
399 std::optional<sp<IBinder>> receivedToken =
400 getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock,
401 mNotifyInputChannelBroken);
402 ASSERT_TRUE(receivedToken.has_value());
403 ASSERT_EQ(token, *receivedToken);
404 }
405
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800406 /**
407 * Set policy timeout. A value of zero means next key will not be intercepted.
408 */
409 void setInterceptKeyTimeout(std::chrono::milliseconds timeout) {
410 mInterceptKeyTimeout = timeout;
411 }
412
Josep del Riob3981622023-04-18 15:49:45 +0000413 void assertUserActivityPoked() {
414 std::scoped_lock lock(mLock);
415 ASSERT_TRUE(mPokedUserActivity) << "Expected user activity to have been poked";
416 }
417
418 void assertUserActivityNotPoked() {
419 std::scoped_lock lock(mLock);
420 ASSERT_FALSE(mPokedUserActivity) << "Expected user activity not to have been poked";
421 }
422
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000423 void assertNotifyDeviceInteractionWasCalled(int32_t deviceId, std::set<gui::Uid> uids) {
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000424 ASSERT_EQ(std::make_pair(deviceId, uids), mNotifiedInteractions.popWithTimeout(100ms));
425 }
426
427 void assertNotifyDeviceInteractionWasNotCalled() {
428 ASSERT_FALSE(mNotifiedInteractions.popWithTimeout(10ms));
429 }
430
Michael Wrightd02c5b62014-02-10 15:10:22 -0800431private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700432 std::mutex mLock;
433 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
434 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
435 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
436 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800437
Prabir Pradhan99987712020-11-10 18:43:05 -0800438 std::condition_variable mPointerCaptureChangedCondition;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000439
440 std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock);
Prabir Pradhan99987712020-11-10 18:43:05 -0800441
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700442 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700443 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800444 std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock);
445 std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700446 std::condition_variable mNotifyAnr;
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800447 std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock);
448 std::condition_variable mNotifyInputChannelBroken;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700449
arthurhungf452d0b2021-01-06 00:19:52 +0800450 sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800451 bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false;
Josep del Riob3981622023-04-18 15:49:45 +0000452 bool mPokedUserActivity GUARDED_BY(mLock) = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800453
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800454 std::chrono::milliseconds mInterceptKeyTimeout = 0ms;
455
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000456 BlockingQueue<std::pair<int32_t /*deviceId*/, std::set<gui::Uid>>> mNotifiedInteractions;
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000457
Prabir Pradhanedd96402022-02-15 01:46:16 -0800458 // All three ANR-related callbacks behave the same way, so we use this generic function to wait
459 // for a specific container to become non-empty. When the container is non-empty, return the
460 // first entry from the container and erase it.
461 template <class T>
462 T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage,
463 std::unique_lock<std::mutex>& lock) REQUIRES(mLock) {
464 // If there is an ANR, Dispatcher won't be idle because there are still events
465 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
466 // before checking if ANR was called.
467 // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need
468 // to provide it some time to act. 100ms seems reasonable.
469 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
470 const std::chrono::time_point start = std::chrono::steady_clock::now();
471 std::optional<T> token =
472 getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr);
473 if (!token.has_value()) {
474 ADD_FAILURE() << "Did not receive the ANR callback";
475 return {};
476 }
477
478 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
479 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
480 // the dispatcher started counting before this function was called
481 if (std::chrono::abs(timeout - waited) > 100ms) {
482 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
483 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
484 << "ms, but waited "
485 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
486 << "ms instead";
487 }
488 return *token;
489 }
490
491 template <class T>
492 std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout,
493 std::queue<T>& storage,
494 std::unique_lock<std::mutex>& lock,
495 std::condition_variable& condition)
496 REQUIRES(mLock) {
497 condition.wait_for(lock, timeout,
498 [&storage]() REQUIRES(mLock) { return !storage.empty(); });
499 if (storage.empty()) {
500 ADD_FAILURE() << "Did not receive the expected callback";
501 return std::nullopt;
502 }
503 T item = storage.front();
504 storage.pop();
505 return std::make_optional(item);
506 }
507
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600508 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700509 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800510 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800511 }
512
Prabir Pradhane59c6dc2023-06-13 19:53:03 +0000513 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<gui::Pid> pid,
Prabir Pradhanedd96402022-02-15 01:46:16 -0800514 const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700515 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800516 ASSERT_TRUE(pid.has_value());
517 mAnrWindows.push({connectionToken, *pid});
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700518 mNotifyAnr.notify_all();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500519 }
520
Prabir Pradhanedd96402022-02-15 01:46:16 -0800521 void notifyWindowResponsive(const sp<IBinder>& connectionToken,
Prabir Pradhane59c6dc2023-06-13 19:53:03 +0000522 std::optional<gui::Pid> pid) override {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500523 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800524 ASSERT_TRUE(pid.has_value());
525 mResponsiveWindows.push({connectionToken, *pid});
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500526 mNotifyAnr.notify_all();
527 }
528
529 void notifyNoFocusedWindowAnr(
530 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
531 std::scoped_lock lock(mLock);
532 mAnrApplications.push(applicationHandle);
533 mNotifyAnr.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800534 }
535
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800536 void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override {
537 std::scoped_lock lock(mLock);
538 mBrokenInputChannels.push(connectionToken);
539 mNotifyInputChannelBroken.notify_all();
540 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800541
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600542 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700543
Chris Yef59a2f42020-10-16 12:55:26 -0700544 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
545 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
546 const std::vector<float>& values) override {}
547
548 void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType,
549 InputDeviceSensorAccuracy accuracy) override {}
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000550
Chris Yefb552902021-02-03 17:18:37 -0800551 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
552
Prabir Pradhana41d2442023-04-20 21:30:40 +0000553 InputDispatcherConfiguration getDispatcherConfiguration() override { return mConfig; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800554
Prabir Pradhana41d2442023-04-20 21:30:40 +0000555 bool filterInputEvent(const InputEvent& inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700556 std::scoped_lock lock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +0000557 switch (inputEvent.getType()) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700558 case InputEventType::KEY: {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000559 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(inputEvent);
560 mFilteredEvent = std::make_unique<KeyEvent>(keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800561 break;
562 }
563
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700564 case InputEventType::MOTION: {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000565 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(inputEvent);
566 mFilteredEvent = std::make_unique<MotionEvent>(motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800567 break;
568 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700569 default: {
570 ADD_FAILURE() << "Should only filter keys or motions";
571 break;
572 }
Jackal Guof9696682018-10-05 12:23:23 +0800573 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800574 return true;
575 }
576
Prabir Pradhana41d2442023-04-20 21:30:40 +0000577 void interceptKeyBeforeQueueing(const KeyEvent& inputEvent, uint32_t&) override {
578 if (inputEvent.getAction() == AKEY_EVENT_ACTION_UP) {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800579 // Clear intercept state when we handled the event.
580 mInterceptKeyTimeout = 0ms;
581 }
582 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800583
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600584 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800585
Prabir Pradhana41d2442023-04-20 21:30:40 +0000586 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&, uint32_t) override {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800587 nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count();
588 // Clear intercept state so we could dispatch the event in next wake.
589 mInterceptKeyTimeout = 0ms;
590 return delay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800591 }
592
Prabir Pradhana41d2442023-04-20 21:30:40 +0000593 std::optional<KeyEvent> dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent&,
594 uint32_t) override {
595 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596 }
597
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600598 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
599 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700600 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800601 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
602 * essentially a passthrough for notifySwitch.
603 */
Harry Cutts33476232023-01-30 19:57:29 +0000604 mLastNotifySwitch = NotifySwitchArgs(/*id=*/1, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800605 }
606
Josep del Riob3981622023-04-18 15:49:45 +0000607 void pokeUserActivity(nsecs_t, int32_t, int32_t) override {
608 std::scoped_lock lock(mLock);
609 mPokedUserActivity = true;
610 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800611
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600612 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700613 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700614 mOnPointerDownToken = newToken;
615 }
616
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000617 void setPointerCapture(const PointerCaptureRequest& request) override {
Prabir Pradhan99987712020-11-10 18:43:05 -0800618 std::scoped_lock lock(mLock);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000619 mPointerCaptureRequest = {request};
Prabir Pradhan99987712020-11-10 18:43:05 -0800620 mPointerCaptureChangedCondition.notify_all();
621 }
622
arthurhungf452d0b2021-01-06 00:19:52 +0800623 void notifyDropWindow(const sp<IBinder>& token, float x, float y) override {
624 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800625 mNotifyDropWindowWasCalled = true;
arthurhungf452d0b2021-01-06 00:19:52 +0800626 mDropTargetWindowToken = token;
627 }
628
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000629 void notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000630 const std::set<gui::Uid>& uids) override {
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000631 ASSERT_TRUE(mNotifiedInteractions.emplace(deviceId, uids));
632 }
633
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700634 void assertFilterInputEventWasCalledInternal(
635 const std::function<void(const InputEvent&)>& verify) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700636 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800637 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700638 verify(*mFilteredEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800639 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800640 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800641};
642
Michael Wrightd02c5b62014-02-10 15:10:22 -0800643// --- InputDispatcherTest ---
644
645class InputDispatcherTest : public testing::Test {
646protected:
Prabir Pradhana41d2442023-04-20 21:30:40 +0000647 std::unique_ptr<FakeInputDispatcherPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700648 std::unique_ptr<InputDispatcher> mDispatcher;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800649
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000650 void SetUp() override {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000651 mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>();
652 mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy, STALE_EVENT_TIMEOUT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800653 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000654 // Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700655 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800656 }
657
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000658 void TearDown() override {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700659 ASSERT_EQ(OK, mDispatcher->stop());
Prabir Pradhana41d2442023-04-20 21:30:40 +0000660 mFakePolicy.reset();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700661 mDispatcher.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800662 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700663
664 /**
665 * Used for debugging when writing the test
666 */
667 void dumpDispatcherState() {
668 std::string dump;
669 mDispatcher->dump(dump);
670 std::stringstream ss(dump);
671 std::string to;
672
673 while (std::getline(ss, to, '\n')) {
674 ALOGE("%s", to.c_str());
675 }
676 }
Vishnu Nair958da932020-08-21 17:12:37 -0700677
Chavi Weingarten847e8512023-03-29 00:26:09 +0000678 void setFocusedWindow(const sp<WindowInfoHandle>& window) {
Vishnu Nair958da932020-08-21 17:12:37 -0700679 FocusRequest request;
680 request.token = window->getToken();
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +0000681 request.windowName = window->getName();
Vishnu Nair958da932020-08-21 17:12:37 -0700682 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
683 request.displayId = window->getInfo()->displayId;
684 mDispatcher->setFocusedWindow(request);
685 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800686};
687
Michael Wrightd02c5b62014-02-10 15:10:22 -0800688TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
689 KeyEvent event;
690
691 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800692 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
693 INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600694 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
695 ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800696 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000697 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000698 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800699 << "Should reject key events with undefined action.";
700
701 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800702 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
703 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600704 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800705 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000706 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000707 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800708 << "Should reject key events with ACTION_MULTIPLE.";
709}
710
711TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
712 MotionEvent event;
713 PointerProperties pointerProperties[MAX_POINTERS + 1];
714 PointerCoords pointerCoords[MAX_POINTERS + 1];
Siarhei Vishniakou01747382022-01-20 13:23:27 -0800715 for (size_t i = 0; i <= MAX_POINTERS; i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800716 pointerProperties[i].clear();
717 pointerProperties[i].id = i;
718 pointerCoords[i].clear();
719 }
720
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800721 // Some constants commonly used below
722 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
723 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
724 constexpr int32_t metaState = AMETA_NONE;
725 constexpr MotionClassification classification = MotionClassification::NONE;
726
chaviw9eaa22c2020-07-01 16:21:27 -0700727 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800728 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800729 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
chaviw9eaa22c2020-07-01 16:21:27 -0700730 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification,
731 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700732 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
733 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700734 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800735 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000736 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000737 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800738 << "Should reject motion events with undefined action.";
739
740 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800741 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800742 POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
743 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
744 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
745 ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500746 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800747 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000748 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000749 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800750 << "Should reject motion events with pointer down index too large.";
751
Garfield Tanfbe732e2020-01-24 11:26:14 -0800752 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700753 AMOTION_EVENT_ACTION_POINTER_DOWN |
754 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700755 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
756 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700757 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500758 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800759 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000760 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000761 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800762 << "Should reject motion events with pointer down index too small.";
763
764 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800765 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800766 POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform,
767 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
768 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
769 ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500770 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800771 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000772 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000773 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800774 << "Should reject motion events with pointer up index too large.";
775
Garfield Tanfbe732e2020-01-24 11:26:14 -0800776 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700777 AMOTION_EVENT_ACTION_POINTER_UP |
778 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700779 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
780 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700781 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500782 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800783 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000784 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000785 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800786 << "Should reject motion events with pointer up index too small.";
787
788 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800789 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
790 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700791 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700792 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
793 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700794 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800795 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000796 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000797 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800798 << "Should reject motion events with 0 pointers.";
799
Garfield Tanfbe732e2020-01-24 11:26:14 -0800800 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
801 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700802 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700803 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
804 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700805 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800806 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000807 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000808 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800809 << "Should reject motion events with more than MAX_POINTERS pointers.";
810
811 // Rejects motion events with invalid pointer ids.
812 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800813 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
814 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700815 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700816 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
817 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700818 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800819 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000820 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000821 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800822 << "Should reject motion events with pointer ids less than 0.";
823
824 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800825 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
826 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700827 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700828 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
829 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700830 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800831 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000832 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000833 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800834 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
835
836 // Rejects motion events with duplicate pointer ids.
837 pointerProperties[0].id = 1;
838 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800839 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
840 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700841 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700842 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
843 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700844 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800845 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000846 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000847 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800848 << "Should reject motion events with duplicate pointer ids.";
849}
850
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800851/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
852
853TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
854 constexpr nsecs_t eventTime = 20;
Prabir Pradhan678438e2023-04-13 19:32:51 +0000855 mDispatcher->notifyConfigurationChanged({/*id=*/10, eventTime});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800856 ASSERT_TRUE(mDispatcher->waitForIdle());
857
858 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
859}
860
861TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Harry Cutts33476232023-01-30 19:57:29 +0000862 NotifySwitchArgs args(/*id=*/10, /*eventTime=*/20, /*policyFlags=*/0, /*switchValues=*/1,
863 /*switchMask=*/2);
Prabir Pradhan678438e2023-04-13 19:32:51 +0000864 mDispatcher->notifySwitch(args);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800865
866 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
867 args.policyFlags |= POLICY_FLAG_TRUSTED;
868 mFakePolicy->assertNotifySwitchWasCalled(args);
869}
870
Siarhei Vishniakouadeb6fa2023-05-26 09:11:06 -0700871namespace {
872
Arthur Hungb92218b2018-08-14 12:00:21 +0800873// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700874static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -0700875// Default input dispatching timeout if there is no focused application or paused window
876// from which to determine an appropriate dispatching timeout.
877static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds(
878 android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS *
879 android::base::HwTimeoutMultiplier());
Arthur Hungb92218b2018-08-14 12:00:21 +0800880
881class FakeApplicationHandle : public InputApplicationHandle {
882public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700883 FakeApplicationHandle() {
884 mInfo.name = "Fake Application";
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700885 mInfo.token = sp<BBinder>::make();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500886 mInfo.dispatchingTimeoutMillis =
887 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700888 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800889 virtual ~FakeApplicationHandle() {}
890
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000891 virtual bool updateInfo() override { return true; }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700892
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500893 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
894 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700895 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800896};
897
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800898class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800899public:
Garfield Tan15601662020-09-22 15:32:38 -0700900 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800901 : mName(name) {
Garfield Tan15601662020-09-22 15:32:38 -0700902 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800903 }
904
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800905 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700906 InputEvent* event;
907 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
908 if (!consumeSeq) {
909 return nullptr;
910 }
911 finishEvent(*consumeSeq);
912 return event;
913 }
914
915 /**
916 * Receive an event without acknowledging it.
917 * Return the sequence number that could later be used to send finished signal.
918 */
919 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800920 uint32_t consumeSeq;
921 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800922
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800923 std::chrono::time_point start = std::chrono::steady_clock::now();
924 status_t status = WOULD_BLOCK;
925 while (status == WOULD_BLOCK) {
Harry Cutts33476232023-01-30 19:57:29 +0000926 status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800927 &event);
928 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
929 if (elapsed > 100ms) {
930 break;
931 }
932 }
933
934 if (status == WOULD_BLOCK) {
935 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700936 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800937 }
938
939 if (status != OK) {
940 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700941 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800942 }
943 if (event == nullptr) {
944 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700945 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800946 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700947 if (outEvent != nullptr) {
948 *outEvent = event;
949 }
950 return consumeSeq;
951 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800952
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700953 /**
954 * To be used together with "receiveEvent" to complete the consumption of an event.
955 */
956 void finishEvent(uint32_t consumeSeq) {
957 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
958 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800959 }
960
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000961 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
962 const status_t status = mConsumer->sendTimeline(inputEventId, timeline);
963 ASSERT_EQ(OK, status);
964 }
965
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700966 void consumeEvent(InputEventType expectedEventType, int32_t expectedAction,
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000967 std::optional<int32_t> expectedDisplayId,
968 std::optional<int32_t> expectedFlags) {
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800969 InputEvent* event = consume();
970
971 ASSERT_NE(nullptr, event) << mName.c_str()
972 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800973 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700974 << mName.c_str() << " expected " << ftl::enum_string(expectedEventType)
975 << " event, got " << *event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800976
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000977 if (expectedDisplayId.has_value()) {
978 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
979 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800980
Tiger Huang8664f8c2018-10-11 19:14:35 +0800981 switch (expectedEventType) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700982 case InputEventType::KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800983 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
984 EXPECT_EQ(expectedAction, keyEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000985 if (expectedFlags.has_value()) {
986 EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags());
987 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800988 break;
989 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700990 case InputEventType::MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800991 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000992 assertMotionAction(expectedAction, motionEvent.getAction());
993
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000994 if (expectedFlags.has_value()) {
995 EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags());
996 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800997 break;
998 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700999 case InputEventType::FOCUS: {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001000 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
1001 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001002 case InputEventType::CAPTURE: {
Prabir Pradhan99987712020-11-10 18:43:05 -08001003 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
1004 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001005 case InputEventType::TOUCH_MODE: {
Antonio Kantekf16f2832021-09-28 04:39:20 +00001006 FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events";
1007 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001008 case InputEventType::DRAG: {
arthurhungb89ccb02020-12-30 16:19:01 +08001009 FAIL() << "Use 'consumeDragEvent' for DRAG events";
1010 }
Tiger Huang8664f8c2018-10-11 19:14:35 +08001011 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001012 }
1013
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001014 MotionEvent* consumeMotion() {
1015 InputEvent* event = consume();
1016
1017 if (event == nullptr) {
1018 ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one.";
1019 return nullptr;
1020 }
1021
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001022 if (event->getType() != InputEventType::MOTION) {
1023 ADD_FAILURE() << mName << " expected a MotionEvent, got " << *event;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001024 return nullptr;
1025 }
1026 return static_cast<MotionEvent*>(event);
1027 }
1028
1029 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
1030 MotionEvent* motionEvent = consumeMotion();
1031 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
1032 ASSERT_THAT(*motionEvent, matcher);
1033 }
1034
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001035 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
1036 InputEvent* event = consume();
1037 ASSERT_NE(nullptr, event) << mName.c_str()
1038 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001039 ASSERT_EQ(InputEventType::FOCUS, event->getType())
1040 << "Instead of FocusEvent, got " << *event;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001041
1042 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1043 << mName.c_str() << ": event displayId should always be NONE.";
1044
1045 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
1046 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001047 }
1048
Prabir Pradhan99987712020-11-10 18:43:05 -08001049 void consumeCaptureEvent(bool hasCapture) {
1050 const InputEvent* event = consume();
1051 ASSERT_NE(nullptr, event) << mName.c_str()
1052 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001053 ASSERT_EQ(InputEventType::CAPTURE, event->getType())
1054 << "Instead of CaptureEvent, got " << *event;
Prabir Pradhan99987712020-11-10 18:43:05 -08001055
1056 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1057 << mName.c_str() << ": event displayId should always be NONE.";
1058
1059 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
1060 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
1061 }
1062
arthurhungb89ccb02020-12-30 16:19:01 +08001063 void consumeDragEvent(bool isExiting, float x, float y) {
1064 const InputEvent* event = consume();
1065 ASSERT_NE(nullptr, event) << mName.c_str()
1066 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001067 ASSERT_EQ(InputEventType::DRAG, event->getType()) << "Instead of DragEvent, got " << *event;
arthurhungb89ccb02020-12-30 16:19:01 +08001068
1069 EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1070 << mName.c_str() << ": event displayId should always be NONE.";
1071
1072 const auto& dragEvent = static_cast<const DragEvent&>(*event);
1073 EXPECT_EQ(isExiting, dragEvent.isExiting());
1074 EXPECT_EQ(x, dragEvent.getX());
1075 EXPECT_EQ(y, dragEvent.getY());
1076 }
1077
Antonio Kantekf16f2832021-09-28 04:39:20 +00001078 void consumeTouchModeEvent(bool inTouchMode) {
1079 const InputEvent* event = consume();
1080 ASSERT_NE(nullptr, event) << mName.c_str()
1081 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001082 ASSERT_EQ(InputEventType::TOUCH_MODE, event->getType())
1083 << "Instead of TouchModeEvent, got " << *event;
Antonio Kantekf16f2832021-09-28 04:39:20 +00001084
1085 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1086 << mName.c_str() << ": event displayId should always be NONE.";
1087 const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event);
1088 EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode());
1089 }
1090
chaviwd1c23182019-12-20 18:44:56 -08001091 void assertNoEvents() {
1092 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001093 if (event == nullptr) {
1094 return;
1095 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001096 if (event->getType() == InputEventType::KEY) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001097 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
1098 ADD_FAILURE() << "Received key event "
1099 << KeyEvent::actionToString(keyEvent.getAction());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001100 } else if (event->getType() == InputEventType::MOTION) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001101 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1102 ADD_FAILURE() << "Received motion event "
1103 << MotionEvent::actionToString(motionEvent.getAction());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001104 } else if (event->getType() == InputEventType::FOCUS) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001105 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
1106 ADD_FAILURE() << "Received focus event, hasFocus = "
1107 << (focusEvent.getHasFocus() ? "true" : "false");
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001108 } else if (event->getType() == InputEventType::CAPTURE) {
Prabir Pradhan99987712020-11-10 18:43:05 -08001109 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
1110 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
1111 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001112 } else if (event->getType() == InputEventType::TOUCH_MODE) {
Antonio Kantekf16f2832021-09-28 04:39:20 +00001113 const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event);
1114 ADD_FAILURE() << "Received touch mode event, inTouchMode = "
1115 << (touchModeEvent.isInTouchMode() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001116 }
1117 FAIL() << mName.c_str()
1118 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -08001119 }
1120
1121 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
1122
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001123 int getChannelFd() { return mConsumer->getChannel()->getFd().get(); }
1124
chaviwd1c23182019-12-20 18:44:56 -08001125protected:
1126 std::unique_ptr<InputConsumer> mConsumer;
1127 PreallocatedInputEventFactory mEventFactory;
1128
1129 std::string mName;
1130};
1131
chaviw3277faf2021-05-19 16:45:23 -05001132class FakeWindowHandle : public WindowInfoHandle {
chaviwd1c23182019-12-20 18:44:56 -08001133public:
1134 static const int32_t WIDTH = 600;
1135 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -08001136
Chris Yea209fde2020-07-22 13:54:51 -07001137 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001138 const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001139 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -08001140 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001141 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -07001142 base::Result<std::unique_ptr<InputChannel>> channel =
1143 dispatcher->createInputChannel(name);
1144 token = (*channel)->getConnectionToken();
1145 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -08001146 }
1147
1148 inputApplicationHandle->updateInfo();
1149 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
1150
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001151 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001152 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -08001153 mInfo.name = name;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001154 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001155 mInfo.alpha = 1.0;
chaviwd1c23182019-12-20 18:44:56 -08001156 mInfo.frameLeft = 0;
1157 mInfo.frameTop = 0;
1158 mInfo.frameRight = WIDTH;
1159 mInfo.frameBottom = HEIGHT;
chaviw1ff3d1e2020-07-01 15:53:47 -07001160 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -08001161 mInfo.globalScaleFactor = 1.0;
1162 mInfo.touchableRegion.clear();
1163 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
Prabir Pradhan5735a322022-04-11 17:23:34 +00001164 mInfo.ownerPid = WINDOW_PID;
1165 mInfo.ownerUid = WINDOW_UID;
chaviwd1c23182019-12-20 18:44:56 -08001166 mInfo.displayId = displayId;
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001167 mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT;
chaviwd1c23182019-12-20 18:44:56 -08001168 }
1169
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07001170 sp<FakeWindowHandle> clone(int32_t displayId) {
1171 sp<FakeWindowHandle> handle = sp<FakeWindowHandle>::make(mInfo.name + "(Mirror)");
1172 handle->mInfo = mInfo;
1173 handle->mInfo.displayId = displayId;
1174 handle->mInfo.id = sId++;
1175 handle->mInputReceiver = mInputReceiver;
1176 return handle;
1177 }
1178
1179 /**
1180 * This is different from clone, because clone will make a "mirror" window - a window with the
1181 * same token, but a different ID. The original window and the clone window are allowed to be
1182 * sent to the dispatcher at the same time - they can coexist inside the dispatcher.
1183 * This function will create a different object of WindowInfoHandle, but with the same
1184 * properties as the original object - including the ID.
1185 * You can use either the old or the new object to consume the events.
1186 * IMPORTANT: The duplicated object is supposed to replace the original object, and not appear
1187 * at the same time inside dispatcher.
1188 */
1189 sp<FakeWindowHandle> duplicate() {
1190 sp<FakeWindowHandle> handle = sp<FakeWindowHandle>::make(mName);
1191 handle->mInfo = mInfo;
1192 handle->mInputReceiver = mInputReceiver;
Arthur Hungabbb9d82021-09-01 14:52:30 +00001193 return handle;
1194 }
1195
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001196 void setTouchable(bool touchable) {
1197 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable);
1198 }
chaviwd1c23182019-12-20 18:44:56 -08001199
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001200 void setFocusable(bool focusable) {
1201 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable);
1202 }
1203
1204 void setVisible(bool visible) {
1205 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible);
1206 }
Vishnu Nair958da932020-08-21 17:12:37 -07001207
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001208 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001209 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001210 }
1211
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001212 void setPaused(bool paused) {
1213 mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused);
1214 }
1215
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001216 void setPreventSplitting(bool preventSplitting) {
1217 mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001218 }
1219
1220 void setSlippery(bool slippery) {
1221 mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery);
1222 }
1223
1224 void setWatchOutsideTouch(bool watchOutside) {
1225 mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside);
1226 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001227
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001228 void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); }
1229
1230 void setInterceptsStylus(bool interceptsStylus) {
1231 mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus);
1232 }
1233
1234 void setDropInput(bool dropInput) {
1235 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput);
1236 }
1237
1238 void setDropInputIfObscured(bool dropInputIfObscured) {
1239 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured);
1240 }
1241
1242 void setNoInputChannel(bool noInputChannel) {
1243 mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel);
1244 }
1245
Josep del Riob3981622023-04-18 15:49:45 +00001246 void setDisableUserActivity(bool disableUserActivity) {
1247 mInfo.setInputConfig(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY, disableUserActivity);
1248 }
1249
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001250 void setAlpha(float alpha) { mInfo.alpha = alpha; }
1251
chaviw3277faf2021-05-19 16:45:23 -05001252 void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; }
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001253
Bernardo Rufino7393d172021-02-26 13:56:11 +00001254 void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
1255
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001256 void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) {
chaviwd1c23182019-12-20 18:44:56 -08001257 mInfo.frameLeft = frame.left;
1258 mInfo.frameTop = frame.top;
1259 mInfo.frameRight = frame.right;
1260 mInfo.frameBottom = frame.bottom;
1261 mInfo.touchableRegion.clear();
1262 mInfo.addTouchableRegion(frame);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001263
1264 const Rect logicalDisplayFrame = displayTransform.transform(frame);
1265 ui::Transform translate;
1266 translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top);
1267 mInfo.transform = translate * displayTransform;
chaviwd1c23182019-12-20 18:44:56 -08001268 }
1269
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001270 void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; }
1271
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001272 void setIsWallpaper(bool isWallpaper) {
1273 mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper);
1274 }
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001275
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001276 void setDupTouchToWallpaper(bool hasWallpaper) {
1277 mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper);
1278 }
chaviwd1c23182019-12-20 18:44:56 -08001279
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001280 void setTrustedOverlay(bool trustedOverlay) {
1281 mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay);
1282 }
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001283
chaviw9eaa22c2020-07-01 16:21:27 -07001284 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
1285 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
1286 }
1287
1288 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -07001289
yunho.shinf4a80b82020-11-16 21:13:57 +09001290 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
1291
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001292 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001293 consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, expectedFlags);
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001294 }
1295
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001296 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001297 consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001298 }
1299
Svet Ganov5d3bc372020-01-26 23:11:07 -08001300 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001301 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001302 consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId),
1303 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001304 }
1305
1306 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001307 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001308 consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
1309 WithDisplayId(expectedDisplayId), WithFlags(expectedFlags)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001310 }
1311
1312 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001313 int32_t expectedFlags = 0) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001314 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
1315 }
1316
1317 void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
1318 std::optional<int32_t> expectedFlags = std::nullopt) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001319 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001320 expectedFlags);
1321 }
1322
Svet Ganov5d3bc372020-01-26 23:11:07 -08001323 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001324 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1325 int32_t expectedFlags = 0) {
1326 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
1327 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001328 consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001329 }
1330
1331 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001332 int32_t expectedFlags = 0) {
1333 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
1334 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001335 consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001336 }
1337
1338 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001339 int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001340 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
Michael Wright3a240c42019-12-10 20:53:41 +00001341 expectedFlags);
1342 }
1343
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001344 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1345 int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001346 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001347 expectedFlags);
1348 }
1349
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001350 void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1351 int32_t expectedFlags = 0) {
1352 InputEvent* event = consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08001353 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001354 ASSERT_EQ(InputEventType::MOTION, event->getType());
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001355 const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1356 EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked());
1357 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getX());
1358 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getY());
1359 }
1360
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001361 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
1362 ASSERT_NE(mInputReceiver, nullptr)
1363 << "Cannot consume events from a window with no receiver";
1364 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
1365 }
1366
Prabir Pradhan99987712020-11-10 18:43:05 -08001367 void consumeCaptureEvent(bool hasCapture) {
1368 ASSERT_NE(mInputReceiver, nullptr)
1369 << "Cannot consume events from a window with no receiver";
1370 mInputReceiver->consumeCaptureEvent(hasCapture);
1371 }
1372
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001373 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
1374 MotionEvent* motionEvent = consumeMotion();
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08001375 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001376 ASSERT_THAT(*motionEvent, matcher);
1377 }
1378
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001379 void consumeEvent(InputEventType expectedEventType, int32_t expectedAction,
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001380 std::optional<int32_t> expectedDisplayId,
1381 std::optional<int32_t> expectedFlags) {
chaviwd1c23182019-12-20 18:44:56 -08001382 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
1383 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
1384 expectedFlags);
1385 }
1386
arthurhungb89ccb02020-12-30 16:19:01 +08001387 void consumeDragEvent(bool isExiting, float x, float y) {
1388 mInputReceiver->consumeDragEvent(isExiting, x, y);
1389 }
1390
Antonio Kantekf16f2832021-09-28 04:39:20 +00001391 void consumeTouchModeEvent(bool inTouchMode) {
1392 ASSERT_NE(mInputReceiver, nullptr)
1393 << "Cannot consume events from a window with no receiver";
1394 mInputReceiver->consumeTouchModeEvent(inTouchMode);
1395 }
1396
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001397 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001398 if (mInputReceiver == nullptr) {
1399 ADD_FAILURE() << "Invalid receive event on window with no receiver";
1400 return std::nullopt;
1401 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001402 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001403 }
1404
1405 void finishEvent(uint32_t sequenceNum) {
1406 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1407 mInputReceiver->finishEvent(sequenceNum);
1408 }
1409
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001410 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
1411 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1412 mInputReceiver->sendTimeline(inputEventId, timeline);
1413 }
1414
chaviwaf87b3e2019-10-01 16:59:28 -07001415 InputEvent* consume() {
1416 if (mInputReceiver == nullptr) {
1417 return nullptr;
1418 }
1419 return mInputReceiver->consume();
1420 }
1421
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001422 MotionEvent* consumeMotion() {
1423 InputEvent* event = consume();
1424 if (event == nullptr) {
1425 ADD_FAILURE() << "Consume failed : no event";
1426 return nullptr;
1427 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001428 if (event->getType() != InputEventType::MOTION) {
1429 ADD_FAILURE() << "Instead of motion event, got " << *event;
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001430 return nullptr;
1431 }
1432 return static_cast<MotionEvent*>(event);
1433 }
1434
Arthur Hungb92218b2018-08-14 12:00:21 +08001435 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001436 if (mInputReceiver == nullptr &&
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001437 mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001438 return; // Can't receive events if the window does not have input channel
1439 }
1440 ASSERT_NE(nullptr, mInputReceiver)
1441 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -08001442 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +08001443 }
1444
chaviwaf87b3e2019-10-01 16:59:28 -07001445 sp<IBinder> getToken() { return mInfo.token; }
1446
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001447 const std::string& getName() { return mName; }
1448
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00001449 void setOwnerInfo(gui::Pid ownerPid, gui::Uid ownerUid) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001450 mInfo.ownerPid = ownerPid;
1451 mInfo.ownerUid = ownerUid;
1452 }
1453
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00001454 gui::Pid getPid() const { return mInfo.ownerPid; }
Prabir Pradhanedd96402022-02-15 01:46:16 -08001455
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001456 void destroyReceiver() { mInputReceiver = nullptr; }
1457
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001458 int getChannelFd() { return mInputReceiver->getChannelFd(); }
1459
chaviwd1c23182019-12-20 18:44:56 -08001460private:
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07001461 FakeWindowHandle(std::string name) : mName(name){};
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001462 const std::string mName;
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07001463 std::shared_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001464 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07001465 friend class sp<FakeWindowHandle>;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001466};
1467
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001468std::atomic<int32_t> FakeWindowHandle::sId{1};
1469
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001470static InputEventInjectionResult injectKey(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001471 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001472 int32_t displayId = ADISPLAY_ID_NONE,
1473 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001474 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00001475 bool allowKeyRepeat = true, std::optional<gui::Uid> targetUid = {},
Prabir Pradhan5735a322022-04-11 17:23:34 +00001476 uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001477 KeyEvent event;
1478 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1479
1480 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001481 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Harry Cutts33476232023-01-30 19:57:29 +00001482 INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount,
1483 currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001484
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001485 if (!allowKeyRepeat) {
1486 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
1487 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001488 // Inject event until dispatch out.
Prabir Pradhan5735a322022-04-11 17:23:34 +00001489 return dispatcher->injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001490}
1491
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001492static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001493 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001494 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001495}
1496
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001497// Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without
1498// sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it
1499// has to be woken up to process the repeating key.
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001500static InputEventInjectionResult injectKeyDownNoRepeat(
1501 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001502 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001503 InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT,
Harry Cutts33476232023-01-30 19:57:29 +00001504 /*allowKeyRepeat=*/false);
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001505}
1506
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001507static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001508 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001509 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001510}
1511
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001512static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001513 const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event,
Garfield Tandf26e862020-07-01 20:18:19 -07001514 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001515 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00001516 std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Prabir Pradhan5735a322022-04-11 17:23:34 +00001517 return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout,
1518 policyFlags);
Garfield Tandf26e862020-07-01 20:18:19 -07001519}
1520
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001521static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001522 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source,
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001523 int32_t displayId, const PointF& position = {100, 200},
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001524 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001525 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1526 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001527 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001528 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC),
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00001529 std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07001530 MotionEventBuilder motionBuilder =
1531 MotionEventBuilder(action, source)
1532 .displayId(displayId)
1533 .eventTime(eventTime)
1534 .rawXCursorPosition(cursorPosition.x)
1535 .rawYCursorPosition(cursorPosition.y)
1536 .pointer(
1537 PointerBuilder(/*id=*/0, ToolType::FINGER).x(position.x).y(position.y));
1538 if (MotionEvent::getActionMasked(action) == ACTION_DOWN) {
1539 motionBuilder.downTime(eventTime);
1540 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001541
1542 // Inject event until dispatch out.
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07001543 return injectMotionEvent(dispatcher, motionBuilder.build(), injectionTimeout, injectionMode,
1544 targetUid, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001545}
1546
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001547static InputEventInjectionResult injectMotionDown(
1548 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId,
1549 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001550 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001551}
1552
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001553static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001554 int32_t source, int32_t displayId,
1555 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001556 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001557}
1558
Jackal Guof9696682018-10-05 12:23:23 +08001559static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1560 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1561 // Define a valid key event.
Harry Cutts33476232023-01-30 19:57:29 +00001562 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001563 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A,
1564 KEY_A, AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001565
1566 return args;
1567}
1568
Josep del Riob3981622023-04-18 15:49:45 +00001569static NotifyKeyArgs generateSystemShortcutArgs(int32_t action,
1570 int32_t displayId = ADISPLAY_ID_NONE) {
1571 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1572 // Define a valid key event.
1573 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1574 displayId, 0, action, /* flags */ 0, AKEYCODE_C, KEY_C, AMETA_META_ON,
1575 currentTime);
1576
1577 return args;
1578}
1579
1580static NotifyKeyArgs generateAssistantKeyArgs(int32_t action,
1581 int32_t displayId = ADISPLAY_ID_NONE) {
1582 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1583 // Define a valid key event.
1584 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1585 displayId, 0, action, /* flags */ 0, AKEYCODE_ASSIST, KEY_ASSISTANT,
1586 AMETA_NONE, currentTime);
1587
1588 return args;
1589}
1590
Prabir Pradhan678438e2023-04-13 19:32:51 +00001591[[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source,
1592 int32_t displayId,
1593 const std::vector<PointF>& points) {
chaviwd1c23182019-12-20 18:44:56 -08001594 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001595 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1596 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1597 }
1598
chaviwd1c23182019-12-20 18:44:56 -08001599 PointerProperties pointerProperties[pointerCount];
1600 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001601
chaviwd1c23182019-12-20 18:44:56 -08001602 for (size_t i = 0; i < pointerCount; i++) {
1603 pointerProperties[i].clear();
1604 pointerProperties[i].id = i;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001605 pointerProperties[i].toolType = ToolType::FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001606
chaviwd1c23182019-12-20 18:44:56 -08001607 pointerCoords[i].clear();
1608 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1609 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1610 }
Jackal Guof9696682018-10-05 12:23:23 +08001611
1612 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1613 // Define a valid motion event.
Harry Cutts33476232023-01-30 19:57:29 +00001614 NotifyMotionArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001615 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1616 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001617 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1618 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001619 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1620 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001621
1622 return args;
1623}
1624
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001625static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) {
1626 return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points);
1627}
1628
chaviwd1c23182019-12-20 18:44:56 -08001629static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1630 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1631}
1632
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001633static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(
1634 const PointerCaptureRequest& request) {
Harry Cutts33476232023-01-30 19:57:29 +00001635 return NotifyPointerCaptureChangedArgs(/*id=*/0, systemTime(SYSTEM_TIME_MONOTONIC), request);
Prabir Pradhan99987712020-11-10 18:43:05 -08001636}
1637
Siarhei Vishniakouadeb6fa2023-05-26 09:11:06 -07001638} // namespace
1639
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001640/**
1641 * When a window unexpectedly disposes of its input channel, policy should be notified about the
1642 * broken channel.
1643 */
1644TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) {
1645 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1646 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001647 sp<FakeWindowHandle>::make(application, mDispatcher,
1648 "Window that breaks its input channel", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001649
1650 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1651
1652 // Window closes its channel, but the window remains.
1653 window->destroyReceiver();
1654 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token);
1655}
1656
Arthur Hungb92218b2018-08-14 12:00:21 +08001657TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001658 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001659 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1660 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001661
Arthur Hung72d8dc32020-03-28 00:48:39 +00001662 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001663 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1664 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1665 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001666
1667 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001668 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001669}
1670
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001671TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) {
1672 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001673 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1674 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001675
1676 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1677 // Inject a MotionEvent to an unknown display.
1678 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1679 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE))
1680 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1681
1682 // Window should receive motion event.
1683 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1684}
1685
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001686/**
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001687 * Calling setInputWindows once should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001688 * This test serves as a sanity check for the next test, where setInputWindows is
1689 * called twice.
1690 */
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001691TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) {
Chris Yea209fde2020-07-22 13:54:51 -07001692 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001693 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1694 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001695 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001696
1697 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001698 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001699 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1700 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001701 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001702
1703 // Window should receive motion event.
1704 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1705}
1706
1707/**
1708 * Calling setInputWindows twice, with the same info, should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001709 */
1710TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001711 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001712 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1713 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001714 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001715
1716 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1717 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001718 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001719 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1720 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001721 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001722
1723 // Window should receive motion event.
1724 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1725}
1726
Arthur Hungb92218b2018-08-14 12:00:21 +08001727// The foreground window should receive the first touch down event.
1728TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001729 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001730 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001731 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001732 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001733 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001734
Arthur Hung72d8dc32020-03-28 00:48:39 +00001735 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001736 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1737 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1738 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001739
1740 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001741 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001742 windowSecond->assertNoEvents();
1743}
1744
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001745/**
1746 * Two windows: A top window, and a wallpaper behind the window.
1747 * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window
1748 * gets ACTION_CANCEL.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001749 * 1. foregroundWindow <-- dup touch to wallpaper
1750 * 2. wallpaperWindow <-- is wallpaper
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001751 */
1752TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) {
1753 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1754 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001755 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001756 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001757 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001758 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001759 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001760
1761 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1762 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1763 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1764 {100, 200}))
1765 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1766
1767 // Both foreground window and its wallpaper should receive the touch down
1768 foregroundWindow->consumeMotionDown();
1769 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1770
1771 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1772 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1773 ADISPLAY_ID_DEFAULT, {110, 200}))
1774 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1775
1776 foregroundWindow->consumeMotionMove();
1777 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1778
1779 // Now the foreground window goes away, but the wallpaper stays
1780 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
1781 foregroundWindow->consumeMotionCancel();
1782 // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1783 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1784}
1785
1786/**
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001787 * Two fingers down on the window, and lift off the first finger.
1788 * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event
1789 * contains a single pointer.
1790 */
1791TEST_F(InputDispatcherTest, CancelAfterPointer0Up) {
1792 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1793 sp<FakeWindowHandle> window =
1794 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1795
1796 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001797 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00001798 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1799 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1800 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001801 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00001802 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1803 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1804 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
1805 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001806 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00001807 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
1808 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1809 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
1810 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001811 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
1812 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
1813 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
1814
1815 // Remove the window. The gesture should be canceled
1816 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
1817 const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}};
1818 window->consumeMotionEvent(
1819 AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers)));
1820}
1821
1822/**
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001823 * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above,
1824 * with the following differences:
1825 * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to
1826 * clean up the connection.
1827 * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful.
1828 * Ensure that there's no crash in the dispatcher.
1829 */
1830TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) {
1831 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1832 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001833 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001834 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001835 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001836 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001837 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001838
1839 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1840 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1841 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1842 {100, 200}))
1843 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1844
1845 // Both foreground window and its wallpaper should receive the touch down
1846 foregroundWindow->consumeMotionDown();
1847 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1848
1849 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1850 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1851 ADISPLAY_ID_DEFAULT, {110, 200}))
1852 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1853
1854 foregroundWindow->consumeMotionMove();
1855 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1856
1857 // Wallpaper closes its channel, but the window remains.
1858 wallpaperWindow->destroyReceiver();
1859 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token);
1860
1861 // Now the foreground window goes away, but the wallpaper stays, even though its channel
1862 // is no longer valid.
1863 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
1864 foregroundWindow->consumeMotionCancel();
1865}
1866
Arthur Hungc539dbb2022-12-08 07:45:36 +00001867class ShouldSplitTouchFixture : public InputDispatcherTest,
1868 public ::testing::WithParamInterface<bool> {};
1869INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture,
1870 ::testing::Values(true, false));
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001871/**
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001872 * A single window that receives touch (on top), and a wallpaper window underneath it.
1873 * The top window gets a multitouch gesture.
1874 * Ensure that wallpaper gets the same gesture.
1875 */
Arthur Hungc539dbb2022-12-08 07:45:36 +00001876TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001877 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00001878 sp<FakeWindowHandle> foregroundWindow =
1879 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
1880 foregroundWindow->setDupTouchToWallpaper(true);
1881 foregroundWindow->setPreventSplitting(GetParam());
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001882
1883 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001884 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001885 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001886
Arthur Hungc539dbb2022-12-08 07:45:36 +00001887 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001888
1889 // Touch down on top window
1890 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1891 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1892 {100, 100}))
1893 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1894
1895 // Both top window and its wallpaper should receive the touch down
Arthur Hungc539dbb2022-12-08 07:45:36 +00001896 foregroundWindow->consumeMotionDown();
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001897 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1898
1899 // Second finger down on the top window
1900 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001901 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001902 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001903 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1904 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001905 .build();
1906 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1907 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
1908 InputEventInjectionSync::WAIT_FOR_RESULT))
1909 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1910
Harry Cutts33476232023-01-30 19:57:29 +00001911 foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
1912 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001913 expectedWallpaperFlags);
Arthur Hungc539dbb2022-12-08 07:45:36 +00001914
1915 const MotionEvent secondFingerUpEvent =
1916 MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
1917 .displayId(ADISPLAY_ID_DEFAULT)
1918 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001919 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1920 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Arthur Hungc539dbb2022-12-08 07:45:36 +00001921 .build();
1922 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1923 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
1924 InputEventInjectionSync::WAIT_FOR_RESULT))
1925 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1926 foregroundWindow->consumeMotionPointerUp(0);
1927 wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1928
1929 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08001930 injectMotionEvent(mDispatcher,
1931 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
1932 AINPUT_SOURCE_TOUCHSCREEN)
1933 .displayId(ADISPLAY_ID_DEFAULT)
1934 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
1935 .pointer(PointerBuilder(/* id */ 1,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001936 ToolType::FINGER)
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08001937 .x(100)
1938 .y(100))
1939 .build(),
1940 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT))
Arthur Hungc539dbb2022-12-08 07:45:36 +00001941 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1942 foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1943 wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001944}
1945
1946/**
1947 * Two windows: a window on the left and window on the right.
1948 * A third window, wallpaper, is behind both windows, and spans both top windows.
1949 * The first touch down goes to the left window. A second pointer touches down on the right window.
1950 * The touch is split, so both left and right windows should receive ACTION_DOWN.
1951 * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by
1952 * ACTION_POINTER_DOWN(1).
1953 */
1954TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) {
1955 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1956 sp<FakeWindowHandle> leftWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001957 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001958 leftWindow->setFrame(Rect(0, 0, 200, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001959 leftWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001960
1961 sp<FakeWindowHandle> rightWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001962 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001963 rightWindow->setFrame(Rect(200, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001964 rightWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001965
1966 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001967 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001968 wallpaperWindow->setFrame(Rect(0, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001969 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001970
1971 mDispatcher->setInputWindows(
1972 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
1973
1974 // Touch down on left window
1975 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1976 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1977 {100, 100}))
1978 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1979
1980 // Both foreground window and its wallpaper should receive the touch down
1981 leftWindow->consumeMotionDown();
1982 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1983
1984 // Second finger down on the right window
1985 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001986 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001987 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001988 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1989 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001990 .build();
1991 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1992 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
1993 InputEventInjectionSync::WAIT_FOR_RESULT))
1994 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1995
1996 leftWindow->consumeMotionMove();
1997 // Since the touch is split, right window gets ACTION_DOWN
1998 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00001999 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002000 expectedWallpaperFlags);
2001
2002 // Now, leftWindow, which received the first finger, disappears.
2003 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}});
2004 leftWindow->consumeMotionCancel();
2005 // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
2006 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2007
2008 // The pointer that's still down on the right window moves, and goes to the right window only.
2009 // As far as the dispatcher's concerned though, both pointers are still present.
2010 const MotionEvent secondFingerMoveEvent =
2011 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2012 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002013 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2014 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002015 .build();
2016 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2017 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
2018 InputEventInjectionSync::WAIT_FOR_RESULT));
2019 rightWindow->consumeMotionMove();
2020
2021 leftWindow->assertNoEvents();
2022 rightWindow->assertNoEvents();
2023 wallpaperWindow->assertNoEvents();
2024}
2025
Arthur Hungc539dbb2022-12-08 07:45:36 +00002026/**
2027 * Two windows: a window on the left with dup touch to wallpaper and window on the right without it.
2028 * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL
2029 * The right window should receive ACTION_DOWN.
2030 */
2031TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) {
Arthur Hung74c248d2022-11-23 07:09:59 +00002032 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00002033 sp<FakeWindowHandle> leftWindow =
2034 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2035 leftWindow->setFrame(Rect(0, 0, 200, 200));
2036 leftWindow->setDupTouchToWallpaper(true);
2037 leftWindow->setSlippery(true);
2038
2039 sp<FakeWindowHandle> rightWindow =
2040 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2041 rightWindow->setFrame(Rect(200, 0, 400, 200));
Arthur Hung74c248d2022-11-23 07:09:59 +00002042
2043 sp<FakeWindowHandle> wallpaperWindow =
2044 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
2045 wallpaperWindow->setIsWallpaper(true);
Arthur Hung74c248d2022-11-23 07:09:59 +00002046
Arthur Hungc539dbb2022-12-08 07:45:36 +00002047 mDispatcher->setInputWindows(
2048 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
Arthur Hung74c248d2022-11-23 07:09:59 +00002049
Arthur Hungc539dbb2022-12-08 07:45:36 +00002050 // Touch down on left window
Arthur Hung74c248d2022-11-23 07:09:59 +00002051 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2052 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002053 {100, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002054 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungc539dbb2022-12-08 07:45:36 +00002055
2056 // Both foreground window and its wallpaper should receive the touch down
2057 leftWindow->consumeMotionDown();
Arthur Hung74c248d2022-11-23 07:09:59 +00002058 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2059
Arthur Hungc539dbb2022-12-08 07:45:36 +00002060 // Move to right window, the left window should receive cancel.
Arthur Hung74c248d2022-11-23 07:09:59 +00002061 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002062 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2063 ADISPLAY_ID_DEFAULT, {201, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002064 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2065
Arthur Hungc539dbb2022-12-08 07:45:36 +00002066 leftWindow->consumeMotionCancel();
2067 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2068 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Arthur Hung74c248d2022-11-23 07:09:59 +00002069}
2070
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002071/**
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002072 * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not
2073 * interactive, it might stop sending this flag.
2074 * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure
2075 * to have a consistent input stream.
2076 *
2077 * Test procedure:
2078 * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL.
2079 * DOWN (new gesture).
2080 *
2081 * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent
2082 * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app.
2083 *
2084 * We technically just need a single window here, but we are using two windows (spy on top and a
2085 * regular window below) to emulate the actual situation where it happens on the device.
2086 */
2087TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) {
2088 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2089 sp<FakeWindowHandle> spyWindow =
2090 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2091 spyWindow->setFrame(Rect(0, 0, 200, 200));
2092 spyWindow->setTrustedOverlay(true);
2093 spyWindow->setSpy(true);
2094
2095 sp<FakeWindowHandle> window =
2096 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2097 window->setFrame(Rect(0, 0, 200, 200));
2098
2099 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2100 const int32_t touchDeviceId = 4;
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002101
2102 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002103 mDispatcher->notifyMotion(
2104 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2105 .deviceId(touchDeviceId)
2106 .policyFlags(DEFAULT_POLICY_FLAGS)
2107 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2108 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002109
Prabir Pradhan678438e2023-04-13 19:32:51 +00002110 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2111 .deviceId(touchDeviceId)
2112 .policyFlags(DEFAULT_POLICY_FLAGS)
2113 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2114 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2115 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002116 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2117 spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2118 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2119 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2120
2121 // Cancel the current gesture. Send the cancel without the default policy flags.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002122 mDispatcher->notifyMotion(
2123 MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN)
2124 .deviceId(touchDeviceId)
2125 .policyFlags(0)
2126 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2127 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2128 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002129 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2130 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2131
2132 // We don't need to reset the device to reproduce the issue, but the reset event typically
2133 // follows, so we keep it here to model the actual listener behaviour more closely.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002134 mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId});
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002135
2136 // Start new gesture
Prabir Pradhan678438e2023-04-13 19:32:51 +00002137 mDispatcher->notifyMotion(
2138 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2139 .deviceId(touchDeviceId)
2140 .policyFlags(DEFAULT_POLICY_FLAGS)
2141 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2142 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002143 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2144 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2145
2146 // No more events
2147 spyWindow->assertNoEvents();
2148 window->assertNoEvents();
2149}
2150
2151/**
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002152 * Two windows: a window on the left and a window on the right.
2153 * Mouse is hovered from the right window into the left window.
2154 * Next, we tap on the left window, where the cursor was last seen.
2155 * The second tap is done onto the right window.
2156 * The mouse and tap are from two different devices.
2157 * We technically don't need to set the downtime / eventtime for these events, but setting these
2158 * explicitly helps during debugging.
2159 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2160 * In the buggy implementation, a tap on the right window would cause a crash.
2161 */
2162TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) {
2163 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2164 sp<FakeWindowHandle> leftWindow =
2165 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2166 leftWindow->setFrame(Rect(0, 0, 200, 200));
2167
2168 sp<FakeWindowHandle> rightWindow =
2169 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2170 rightWindow->setFrame(Rect(200, 0, 400, 200));
2171
2172 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2173 // All times need to start at the current time, otherwise the dispatcher will drop the events as
2174 // stale.
2175 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
2176 const int32_t mouseDeviceId = 6;
2177 const int32_t touchDeviceId = 4;
2178 // Move the cursor from right
2179 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2180 injectMotionEvent(mDispatcher,
2181 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2182 AINPUT_SOURCE_MOUSE)
2183 .deviceId(mouseDeviceId)
2184 .downTime(baseTime + 10)
2185 .eventTime(baseTime + 20)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002186 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002187 .x(300)
2188 .y(100))
2189 .build()));
2190 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2191
2192 // .. to the left window
2193 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2194 injectMotionEvent(mDispatcher,
2195 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2196 AINPUT_SOURCE_MOUSE)
2197 .deviceId(mouseDeviceId)
2198 .downTime(baseTime + 10)
2199 .eventTime(baseTime + 30)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002200 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002201 .x(110)
2202 .y(100))
2203 .build()));
2204 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2205 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2206 // Now tap the left window
2207 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2208 injectMotionEvent(mDispatcher,
2209 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2210 AINPUT_SOURCE_TOUCHSCREEN)
2211 .deviceId(touchDeviceId)
2212 .downTime(baseTime + 40)
2213 .eventTime(baseTime + 40)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002214 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002215 .x(100)
2216 .y(100))
2217 .build()));
2218 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2219 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2220
2221 // release tap
2222 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2223 injectMotionEvent(mDispatcher,
2224 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2225 AINPUT_SOURCE_TOUCHSCREEN)
2226 .deviceId(touchDeviceId)
2227 .downTime(baseTime + 40)
2228 .eventTime(baseTime + 50)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002229 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002230 .x(100)
2231 .y(100))
2232 .build()));
2233 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2234
2235 // Tap the window on the right
2236 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2237 injectMotionEvent(mDispatcher,
2238 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2239 AINPUT_SOURCE_TOUCHSCREEN)
2240 .deviceId(touchDeviceId)
2241 .downTime(baseTime + 60)
2242 .eventTime(baseTime + 60)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002243 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002244 .x(300)
2245 .y(100))
2246 .build()));
2247 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2248
2249 // release tap
2250 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2251 injectMotionEvent(mDispatcher,
2252 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2253 AINPUT_SOURCE_TOUCHSCREEN)
2254 .deviceId(touchDeviceId)
2255 .downTime(baseTime + 60)
2256 .eventTime(baseTime + 70)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002257 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002258 .x(300)
2259 .y(100))
2260 .build()));
2261 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2262
2263 // No more events
2264 leftWindow->assertNoEvents();
2265 rightWindow->assertNoEvents();
2266}
2267
2268/**
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002269 * Start hovering in a window. While this hover is still active, make another window appear on top.
2270 * The top, obstructing window has no input channel, so it's not supposed to receive input.
2271 * While the top window is present, the hovering is stopped.
2272 * Later, hovering gets resumed again.
2273 * Ensure that new hover gesture is handled correctly.
2274 * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly
2275 * to the window that's currently being hovered over.
2276 */
2277TEST_F(InputDispatcherTest, HoverWhileWindowAppears) {
2278 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2279 sp<FakeWindowHandle> window =
2280 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2281 window->setFrame(Rect(0, 0, 200, 200));
2282
2283 // Only a single window is present at first
2284 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2285
2286 // Start hovering in the window
2287 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2288 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2289 .build());
2290 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2291
2292 // Now, an obscuring window appears!
2293 sp<FakeWindowHandle> obscuringWindow =
2294 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
2295 ADISPLAY_ID_DEFAULT,
2296 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
2297 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
2298 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
2299 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
2300 obscuringWindow->setNoInputChannel(true);
2301 obscuringWindow->setFocusable(false);
2302 obscuringWindow->setAlpha(1.0);
2303 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
2304
2305 // While this new obscuring window is present, the hovering is stopped
2306 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2307 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2308 .build());
2309 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2310
2311 // Now the obscuring window goes away.
2312 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2313
2314 // And a new hover gesture starts.
2315 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2316 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2317 .build());
2318 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2319}
2320
2321/**
2322 * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to
2323 * the obscuring window.
2324 */
2325TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) {
2326 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2327 sp<FakeWindowHandle> window =
2328 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2329 window->setFrame(Rect(0, 0, 200, 200));
2330
2331 // Only a single window is present at first
2332 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2333
2334 // Start hovering in the window
2335 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2336 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2337 .build());
2338 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2339
2340 // Now, an obscuring window appears!
2341 sp<FakeWindowHandle> obscuringWindow =
2342 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
2343 ADISPLAY_ID_DEFAULT,
2344 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
2345 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
2346 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
2347 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
2348 obscuringWindow->setNoInputChannel(true);
2349 obscuringWindow->setFocusable(false);
2350 obscuringWindow->setAlpha(1.0);
2351 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
2352
2353 // While this new obscuring window is present, the hovering continues. The event can't go to the
2354 // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window.
2355 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2356 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2357 .build());
2358 obscuringWindow->assertNoEvents();
2359 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2360
2361 // Now the obscuring window goes away.
2362 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2363
2364 // Hovering continues in the same position. The hovering pointer re-enters the bottom window,
2365 // so it should generate a HOVER_ENTER
2366 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2367 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2368 .build());
2369 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2370
2371 // Now the MOVE should be getting dispatched normally
2372 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2373 .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110))
2374 .build());
2375 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
2376}
2377
2378/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002379 * Two windows: a window on the left and a window on the right.
2380 * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains
2381 * down. Then, on the left window, also place second touch pointer down.
2382 * This test tries to reproduce a crash.
2383 * In the buggy implementation, second pointer down on the left window would cause a crash.
2384 */
2385TEST_F(InputDispatcherTest, MultiDeviceSplitTouch) {
2386 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2387 sp<FakeWindowHandle> leftWindow =
2388 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2389 leftWindow->setFrame(Rect(0, 0, 200, 200));
2390
2391 sp<FakeWindowHandle> rightWindow =
2392 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2393 rightWindow->setFrame(Rect(200, 0, 400, 200));
2394
2395 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2396
2397 const int32_t touchDeviceId = 4;
2398 const int32_t mouseDeviceId = 6;
2399 NotifyMotionArgs args;
2400
2401 // Start hovering over the left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002402 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2403 .deviceId(mouseDeviceId)
2404 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2405 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002406 leftWindow->consumeMotionEvent(
2407 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2408
2409 // Mouse down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002410 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2411 .deviceId(mouseDeviceId)
2412 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2413 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2414 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002415
2416 leftWindow->consumeMotionEvent(
2417 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
2418 leftWindow->consumeMotionEvent(
2419 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2420
Prabir Pradhan678438e2023-04-13 19:32:51 +00002421 mDispatcher->notifyMotion(
2422 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2423 .deviceId(mouseDeviceId)
2424 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2425 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2426 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2427 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002428 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2429
2430 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002431 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2432 .deviceId(touchDeviceId)
2433 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2434 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002435 leftWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
2436
2437 rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2438
2439 // Second touch pointer down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002440 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2441 .deviceId(touchDeviceId)
2442 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2443 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
2444 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002445 leftWindow->consumeMotionEvent(
2446 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2447 // This MOVE event is not necessary (doesn't carry any new information), but it's there in the
2448 // current implementation.
2449 const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}};
2450 rightWindow->consumeMotionEvent(
2451 AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers)));
2452
2453 leftWindow->assertNoEvents();
2454 rightWindow->assertNoEvents();
2455}
2456
2457/**
2458 * On a single window, use two different devices: mouse and touch.
2459 * Touch happens first, with two pointers going down, and then the first pointer leaving.
2460 * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL.
2461 * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored,
2462 * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not
2463 * represent a new gesture.
2464 */
2465TEST_F(InputDispatcherTest, MixedTouchAndMouseWithPointerDown) {
2466 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2467 sp<FakeWindowHandle> window =
2468 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2469 window->setFrame(Rect(0, 0, 400, 400));
2470
2471 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2472
2473 const int32_t touchDeviceId = 4;
2474 const int32_t mouseDeviceId = 6;
2475 NotifyMotionArgs args;
2476
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08002477 // First touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002478 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2479 .deviceId(touchDeviceId)
2480 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2481 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002482 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002483 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2484 .deviceId(touchDeviceId)
2485 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2486 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2487 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002488 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002489 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
2490 .deviceId(touchDeviceId)
2491 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2492 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2493 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002494 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2495 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2496 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
2497
2498 // Mouse down. The touch should be canceled
Prabir Pradhan678438e2023-04-13 19:32:51 +00002499 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2500 .deviceId(mouseDeviceId)
2501 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2502 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
2503 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002504
2505 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08002506 WithPointerCount(1u)));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002507 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2508
Prabir Pradhan678438e2023-04-13 19:32:51 +00002509 mDispatcher->notifyMotion(
2510 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2511 .deviceId(mouseDeviceId)
2512 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2513 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2514 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
2515 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002516 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2517
2518 // Second touch pointer down.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002519 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2520 .deviceId(touchDeviceId)
2521 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2522 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2523 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002524 // The pointer_down event should be ignored
2525 window->assertNoEvents();
2526}
2527
2528/**
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002529 * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels
2530 * the injected event.
2531 */
2532TEST_F(InputDispatcherTest, UnfinishedInjectedEvent) {
2533 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2534 sp<FakeWindowHandle> window =
2535 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2536 window->setFrame(Rect(0, 0, 400, 400));
2537
2538 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2539
2540 const int32_t touchDeviceId = 4;
2541 NotifyMotionArgs args;
2542 // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after
2543 // completion.
2544 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2545 injectMotionEvent(mDispatcher,
2546 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2547 .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002548 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002549 .x(50)
2550 .y(50))
2551 .build()));
2552 window->consumeMotionEvent(
2553 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
2554
2555 // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer
2556 // should be canceled and the new gesture should take over.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002557 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2558 .deviceId(touchDeviceId)
2559 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2560 .build());
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002561
2562 window->consumeMotionEvent(
2563 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
2564 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2565}
2566
2567/**
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002568 * This test is similar to the test above, but the sequence of injected events is different.
2569 *
2570 * Two windows: a window on the left and a window on the right.
2571 * Mouse is hovered over the left window.
2572 * Next, we tap on the left window, where the cursor was last seen.
2573 *
2574 * After that, we inject one finger down onto the right window, and then a second finger down onto
2575 * the left window.
2576 * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right
2577 * window (first), and then another on the left window (second).
2578 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2579 * In the buggy implementation, second finger down on the left window would cause a crash.
2580 */
2581TEST_F(InputDispatcherTest, HoverTapAndSplitTouch) {
2582 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2583 sp<FakeWindowHandle> leftWindow =
2584 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2585 leftWindow->setFrame(Rect(0, 0, 200, 200));
2586
2587 sp<FakeWindowHandle> rightWindow =
2588 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2589 rightWindow->setFrame(Rect(200, 0, 400, 200));
2590
2591 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2592
2593 const int32_t mouseDeviceId = 6;
2594 const int32_t touchDeviceId = 4;
2595 // Hover over the left window. Keep the cursor there.
2596 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2597 injectMotionEvent(mDispatcher,
2598 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2599 AINPUT_SOURCE_MOUSE)
2600 .deviceId(mouseDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002601 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002602 .x(50)
2603 .y(50))
2604 .build()));
2605 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2606
2607 // Tap on left window
2608 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2609 injectMotionEvent(mDispatcher,
2610 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2611 AINPUT_SOURCE_TOUCHSCREEN)
2612 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002613 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002614 .x(100)
2615 .y(100))
2616 .build()));
2617
2618 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2619 injectMotionEvent(mDispatcher,
2620 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2621 AINPUT_SOURCE_TOUCHSCREEN)
2622 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002623 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002624 .x(100)
2625 .y(100))
2626 .build()));
2627 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2628 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2629 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2630
2631 // First finger down on right window
2632 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2633 injectMotionEvent(mDispatcher,
2634 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2635 AINPUT_SOURCE_TOUCHSCREEN)
2636 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002637 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002638 .x(300)
2639 .y(100))
2640 .build()));
2641 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2642
2643 // Second finger down on the left window
2644 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2645 injectMotionEvent(mDispatcher,
2646 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2647 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002648 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002649 .x(300)
2650 .y(100))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002651 .pointer(PointerBuilder(1, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002652 .x(100)
2653 .y(100))
2654 .build()));
2655 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2656 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE));
2657
2658 // No more events
2659 leftWindow->assertNoEvents();
2660 rightWindow->assertNoEvents();
2661}
2662
2663/**
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002664 * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs.
2665 * While the touch is down, new hover events from the stylus device should be ignored. After the
2666 * touch is gone, stylus hovering should start working again.
2667 */
2668TEST_F(InputDispatcherTest, StylusHoverAndTouchTap) {
2669 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2670 sp<FakeWindowHandle> window =
2671 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2672 window->setFrame(Rect(0, 0, 200, 200));
2673
2674 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2675
2676 const int32_t stylusDeviceId = 5;
2677 const int32_t touchDeviceId = 4;
2678 // Start hovering with stylus
2679 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2680 injectMotionEvent(mDispatcher,
2681 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2682 AINPUT_SOURCE_STYLUS)
2683 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002684 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002685 .x(50)
2686 .y(50))
2687 .build()));
2688 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2689
2690 // Finger down on the window
2691 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2692 injectMotionEvent(mDispatcher,
2693 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2694 AINPUT_SOURCE_TOUCHSCREEN)
2695 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002696 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002697 .x(100)
2698 .y(100))
2699 .build()));
2700 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2701 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2702
2703 // Try to continue hovering with stylus. Since we are already down, injection should fail
2704 ASSERT_EQ(InputEventInjectionResult::FAILED,
2705 injectMotionEvent(mDispatcher,
2706 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2707 AINPUT_SOURCE_STYLUS)
2708 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002709 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002710 .x(50)
2711 .y(50))
2712 .build()));
2713 // No event should be sent. This event should be ignored because a pointer from another device
2714 // is already down.
2715
2716 // Lift up the finger
2717 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2718 injectMotionEvent(mDispatcher,
2719 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2720 AINPUT_SOURCE_TOUCHSCREEN)
2721 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002722 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002723 .x(100)
2724 .y(100))
2725 .build()));
2726 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2727
2728 // Now that the touch is gone, stylus hovering should start working again
2729 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2730 injectMotionEvent(mDispatcher,
2731 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2732 AINPUT_SOURCE_STYLUS)
2733 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002734 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002735 .x(50)
2736 .y(50))
2737 .build()));
2738 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2739 // No more events
2740 window->assertNoEvents();
2741}
2742
2743/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002744 * A spy window above a window with no input channel.
2745 * Start hovering with a stylus device, and then tap with it.
2746 * Ensure spy window receives the entire sequence.
2747 */
2748TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) {
2749 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2750 sp<FakeWindowHandle> spyWindow =
2751 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2752 spyWindow->setFrame(Rect(0, 0, 200, 200));
2753 spyWindow->setTrustedOverlay(true);
2754 spyWindow->setSpy(true);
2755 sp<FakeWindowHandle> window =
2756 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2757 window->setNoInputChannel(true);
2758 window->setFrame(Rect(0, 0, 200, 200));
2759
2760 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2761
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002762 // Start hovering with stylus
Prabir Pradhan678438e2023-04-13 19:32:51 +00002763 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2764 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2765 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002766 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2767 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00002768 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2769 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2770 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002771 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2772
2773 // Stylus touches down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002774 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2775 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2776 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002777 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2778
2779 // Stylus goes up
Prabir Pradhan678438e2023-04-13 19:32:51 +00002780 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS)
2781 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2782 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002783 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP));
2784
2785 // Again hover
Prabir Pradhan678438e2023-04-13 19:32:51 +00002786 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2787 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2788 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002789 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2790 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00002791 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2792 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2793 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002794 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2795
2796 // No more events
2797 spyWindow->assertNoEvents();
2798 window->assertNoEvents();
2799}
2800
2801/**
2802 * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream.
2803 * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse
2804 * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active.
2805 * While the mouse is down, new move events from the touch device should be ignored.
2806 */
2807TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) {
2808 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2809 sp<FakeWindowHandle> spyWindow =
2810 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2811 spyWindow->setFrame(Rect(0, 0, 200, 200));
2812 spyWindow->setTrustedOverlay(true);
2813 spyWindow->setSpy(true);
2814 sp<FakeWindowHandle> window =
2815 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2816 window->setFrame(Rect(0, 0, 200, 200));
2817
2818 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2819
2820 const int32_t mouseDeviceId = 7;
2821 const int32_t touchDeviceId = 4;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002822
2823 // Hover a bit with mouse first
Prabir Pradhan678438e2023-04-13 19:32:51 +00002824 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2825 .deviceId(mouseDeviceId)
2826 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2827 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002828 spyWindow->consumeMotionEvent(
2829 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2830 window->consumeMotionEvent(
2831 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2832
2833 // Start touching
Prabir Pradhan678438e2023-04-13 19:32:51 +00002834 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2835 .deviceId(touchDeviceId)
2836 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
2837 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002838 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2839 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2840 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2841 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2842
Prabir Pradhan678438e2023-04-13 19:32:51 +00002843 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2844 .deviceId(touchDeviceId)
2845 .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55))
2846 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002847 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2848 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2849
2850 // Pilfer the stream
2851 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
2852 window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
2853
Prabir Pradhan678438e2023-04-13 19:32:51 +00002854 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2855 .deviceId(touchDeviceId)
2856 .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60))
2857 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002858 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2859
2860 // Mouse down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002861 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2862 .deviceId(mouseDeviceId)
2863 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2864 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2865 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002866
2867 spyWindow->consumeMotionEvent(
2868 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
2869 spyWindow->consumeMotionEvent(
2870 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2871 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2872
Prabir Pradhan678438e2023-04-13 19:32:51 +00002873 mDispatcher->notifyMotion(
2874 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2875 .deviceId(mouseDeviceId)
2876 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2877 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2878 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2879 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002880 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2881 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2882
2883 // Mouse move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00002884 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
2885 .deviceId(mouseDeviceId)
2886 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2887 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110))
2888 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002889 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2890 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2891
2892 // Touch move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00002893 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2894 .deviceId(touchDeviceId)
2895 .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65))
2896 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002897
2898 // No more events
2899 spyWindow->assertNoEvents();
2900 window->assertNoEvents();
2901}
2902
2903/**
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002904 * On the display, have a single window, and also an area where there's no window.
2905 * First pointer touches the "no window" area of the screen. Second pointer touches the window.
2906 * Make sure that the window receives the second pointer, and first pointer is simply ignored.
2907 */
2908TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) {
2909 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2910 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002911 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002912
2913 mDispatcher->setInputWindows({{DISPLAY_ID, {window}}});
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002914
2915 // Touch down on the empty space
Prabir Pradhan678438e2023-04-13 19:32:51 +00002916 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002917
2918 mDispatcher->waitForIdle();
2919 window->assertNoEvents();
2920
2921 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00002922 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002923 mDispatcher->waitForIdle();
2924 window->consumeMotionDown();
2925}
2926
2927/**
2928 * Same test as above, but instead of touching the empty space, the first touch goes to
2929 * non-touchable window.
2930 */
2931TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) {
2932 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2933 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002934 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002935 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
2936 window1->setTouchable(false);
2937 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002938 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002939 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
2940
2941 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
2942
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002943 // Touch down on the non-touchable window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002944 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002945
2946 mDispatcher->waitForIdle();
2947 window1->assertNoEvents();
2948 window2->assertNoEvents();
2949
2950 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00002951 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002952 mDispatcher->waitForIdle();
2953 window2->consumeMotionDown();
2954}
2955
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002956/**
2957 * When splitting touch events the downTime should be adjusted such that the downTime corresponds
2958 * to the event time of the first ACTION_DOWN sent to the particular window.
2959 */
2960TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) {
2961 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2962 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002963 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002964 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
2965 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002966 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002967 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
2968
2969 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
2970
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002971 // Touch down on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002972 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002973
2974 mDispatcher->waitForIdle();
2975 InputEvent* inputEvent1 = window1->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002976 ASSERT_NE(inputEvent1, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002977 window2->assertNoEvents();
2978 MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1);
2979 nsecs_t downTimeForWindow1 = motionEvent1.getDownTime();
2980 ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime());
2981
2982 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00002983 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002984 mDispatcher->waitForIdle();
2985 InputEvent* inputEvent2 = window2->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002986 ASSERT_NE(inputEvent2, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002987 MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2);
2988 nsecs_t downTimeForWindow2 = motionEvent2.getDownTime();
2989 ASSERT_NE(downTimeForWindow1, downTimeForWindow2);
2990 ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime());
2991
2992 // Now move the pointer on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002993 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002994 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002995 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002996
2997 // Now add new touch down on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002998 mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002999 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003000 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003001
3002 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
3003 window1->consumeMotionMove();
3004 window1->assertNoEvents();
3005
3006 // Now move the pointer on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003007 mDispatcher->notifyMotion(
3008 generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003009 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003010 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003011
Prabir Pradhan678438e2023-04-13 19:32:51 +00003012 mDispatcher->notifyMotion(
3013 generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003014 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003015 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003016}
3017
Garfield Tandf26e862020-07-01 20:18:19 -07003018TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07003019 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07003020 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003021 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003022 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003023 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003024 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003025 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003026
3027 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3028
3029 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
3030
3031 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003032 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003033 injectMotionEvent(mDispatcher,
3034 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3035 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003036 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003037 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003038 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003039
3040 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003041 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003042 injectMotionEvent(mDispatcher,
3043 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3044 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003045 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003046 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003047 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3048 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003049
3050 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003051 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003052 injectMotionEvent(mDispatcher,
3053 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3054 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003055 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003056 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003057 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3058 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003059
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003060 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003061 injectMotionEvent(mDispatcher,
3062 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3063 AINPUT_SOURCE_MOUSE)
3064 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3065 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003066 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003067 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003068 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003069
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003070 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003071 injectMotionEvent(mDispatcher,
3072 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3073 AINPUT_SOURCE_MOUSE)
3074 .buttonState(0)
3075 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003076 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003077 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003078 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003079
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003080 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003081 injectMotionEvent(mDispatcher,
3082 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3083 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003084 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003085 .build()));
3086 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3087
3088 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003089 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003090 injectMotionEvent(mDispatcher,
3091 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3092 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003093 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003094 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003095 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003096
3097 // No more events
3098 windowLeft->assertNoEvents();
3099 windowRight->assertNoEvents();
3100}
3101
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003102/**
3103 * Put two fingers down (and don't release them) and click the mouse button.
3104 * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the
3105 * currently active gesture should be canceled, and the new one should proceed.
3106 */
3107TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) {
3108 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3109 sp<FakeWindowHandle> window =
3110 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3111 window->setFrame(Rect(0, 0, 600, 800));
3112
3113 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3114
3115 const int32_t touchDeviceId = 4;
3116 const int32_t mouseDeviceId = 6;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003117
3118 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003119 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3120 .deviceId(touchDeviceId)
3121 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3122 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003123
Prabir Pradhan678438e2023-04-13 19:32:51 +00003124 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3125 .deviceId(touchDeviceId)
3126 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3127 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
3128 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003129 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3130 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
3131
3132 // Inject a series of mouse events for a mouse click
Prabir Pradhan678438e2023-04-13 19:32:51 +00003133 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3134 .deviceId(mouseDeviceId)
3135 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3136 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
3137 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003138 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
3139 WithPointerCount(2u)));
3140 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3141
Prabir Pradhan678438e2023-04-13 19:32:51 +00003142 mDispatcher->notifyMotion(
3143 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
3144 .deviceId(mouseDeviceId)
3145 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3146 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
3147 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
3148 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003149 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3150
3151 // Try to send more touch events while the mouse is down. Since it's a continuation of an
3152 // already canceled gesture, it should be ignored.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003153 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3154 .deviceId(touchDeviceId)
3155 .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101))
3156 .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121))
3157 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003158 window->assertNoEvents();
3159}
3160
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003161TEST_F(InputDispatcherTest, HoverWithSpyWindows) {
3162 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3163
3164 sp<FakeWindowHandle> spyWindow =
3165 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3166 spyWindow->setFrame(Rect(0, 0, 600, 800));
3167 spyWindow->setTrustedOverlay(true);
3168 spyWindow->setSpy(true);
3169 sp<FakeWindowHandle> window =
3170 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3171 window->setFrame(Rect(0, 0, 600, 800));
3172
3173 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3174 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
3175
3176 // Send mouse cursor to the window
3177 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3178 injectMotionEvent(mDispatcher,
3179 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3180 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003181 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003182 .x(100)
3183 .y(100))
3184 .build()));
3185
3186 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3187 WithSource(AINPUT_SOURCE_MOUSE)));
3188 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3189 WithSource(AINPUT_SOURCE_MOUSE)));
3190
3191 window->assertNoEvents();
3192 spyWindow->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07003193}
3194
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003195TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) {
3196 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3197
3198 sp<FakeWindowHandle> spyWindow =
3199 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3200 spyWindow->setFrame(Rect(0, 0, 600, 800));
3201 spyWindow->setTrustedOverlay(true);
3202 spyWindow->setSpy(true);
3203 sp<FakeWindowHandle> window =
3204 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3205 window->setFrame(Rect(0, 0, 600, 800));
3206
3207 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3208 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
3209
3210 // Send mouse cursor to the window
3211 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3212 injectMotionEvent(mDispatcher,
3213 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3214 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003215 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003216 .x(100)
3217 .y(100))
3218 .build()));
3219
3220 // Move mouse cursor
3221 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3222 injectMotionEvent(mDispatcher,
3223 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3224 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003225 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003226 .x(110)
3227 .y(110))
3228 .build()));
3229
3230 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3231 WithSource(AINPUT_SOURCE_MOUSE)));
3232 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3233 WithSource(AINPUT_SOURCE_MOUSE)));
3234 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3235 WithSource(AINPUT_SOURCE_MOUSE)));
3236 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3237 WithSource(AINPUT_SOURCE_MOUSE)));
3238 // Touch down on the window
3239 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3240 injectMotionEvent(mDispatcher,
3241 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3242 AINPUT_SOURCE_TOUCHSCREEN)
3243 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003244 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003245 .x(200)
3246 .y(200))
3247 .build()));
3248 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3249 WithSource(AINPUT_SOURCE_MOUSE)));
3250 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3251 WithSource(AINPUT_SOURCE_MOUSE)));
3252 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3253 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3254 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3255 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3256
3257 // pilfer the motion, retaining the gesture on the spy window.
3258 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
3259 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
3260 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3261
3262 // Touch UP on the window
3263 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3264 injectMotionEvent(mDispatcher,
3265 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3266 AINPUT_SOURCE_TOUCHSCREEN)
3267 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003268 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003269 .x(200)
3270 .y(200))
3271 .build()));
3272 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3273 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3274
3275 // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going
3276 // to send a new gesture. It should again go to both windows (spy and the window below), just
3277 // like the first gesture did, before pilfering. The window configuration has not changed.
3278
3279 // One more tap - DOWN
3280 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3281 injectMotionEvent(mDispatcher,
3282 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3283 AINPUT_SOURCE_TOUCHSCREEN)
3284 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003285 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003286 .x(250)
3287 .y(250))
3288 .build()));
3289 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3290 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3291 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3292 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3293
3294 // Touch UP on the window
3295 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3296 injectMotionEvent(mDispatcher,
3297 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3298 AINPUT_SOURCE_TOUCHSCREEN)
3299 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003300 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003301 .x(250)
3302 .y(250))
3303 .build()));
3304 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3305 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3306 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3307 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3308
3309 window->assertNoEvents();
3310 spyWindow->assertNoEvents();
3311}
3312
Garfield Tandf26e862020-07-01 20:18:19 -07003313// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
3314// directly in this test.
3315TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07003316 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07003317 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003318 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003319 window->setFrame(Rect(0, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003320
3321 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3322
3323 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3324
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003325 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003326 injectMotionEvent(mDispatcher,
3327 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3328 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003329 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003330 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003331 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003332 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003333 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003334 injectMotionEvent(mDispatcher,
3335 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3336 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003337 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003338 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003339 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3340 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003341
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003342 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003343 injectMotionEvent(mDispatcher,
3344 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3345 AINPUT_SOURCE_MOUSE)
3346 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3347 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003348 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003349 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003350 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003351
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003352 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003353 injectMotionEvent(mDispatcher,
3354 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3355 AINPUT_SOURCE_MOUSE)
3356 .buttonState(0)
3357 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003358 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003359 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003360 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003361
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003362 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003363 injectMotionEvent(mDispatcher,
3364 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3365 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003366 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003367 .build()));
3368 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3369
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07003370 // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the
3371 // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail.
3372 ASSERT_EQ(InputEventInjectionResult::FAILED,
Garfield Tandf26e862020-07-01 20:18:19 -07003373 injectMotionEvent(mDispatcher,
3374 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
3375 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003376 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003377 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003378 window->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07003379}
3380
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003381/**
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003382 * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event
3383 * is generated.
3384 */
3385TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) {
3386 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3387 sp<FakeWindowHandle> window =
3388 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3389 window->setFrame(Rect(0, 0, 1200, 800));
3390
3391 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3392
3393 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3394
3395 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3396 injectMotionEvent(mDispatcher,
3397 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3398 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003399 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003400 .x(300)
3401 .y(400))
3402 .build()));
3403 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
3404
3405 // Remove the window, but keep the channel.
3406 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
3407 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3408}
3409
3410/**
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003411 * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT.
3412 */
3413TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) {
3414 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3415 sp<FakeWindowHandle> window =
3416 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3417 window->setFrame(Rect(0, 0, 100, 100));
3418
3419 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3420
3421 const int32_t mouseDeviceId = 7;
3422 const int32_t touchDeviceId = 4;
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003423
3424 // Start hovering with the mouse
Prabir Pradhan678438e2023-04-13 19:32:51 +00003425 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
3426 .deviceId(mouseDeviceId)
3427 .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10))
3428 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003429 window->consumeMotionEvent(
3430 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3431
3432 // Touch goes down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003433 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3434 .deviceId(touchDeviceId)
3435 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
3436 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003437
3438 window->consumeMotionEvent(
3439 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
3440 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3441}
3442
3443/**
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003444 * Inject a mouse hover event followed by a tap from touchscreen.
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003445 * The tap causes a HOVER_EXIT event to be generated because the current event
3446 * stream's source has been switched.
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003447 */
3448TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) {
3449 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3450 sp<FakeWindowHandle> window =
3451 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3452 window->setFrame(Rect(0, 0, 100, 100));
3453
3454 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3455
3456 // Inject a hover_move from mouse.
3457 NotifyMotionArgs motionArgs =
3458 generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE,
3459 ADISPLAY_ID_DEFAULT, {{50, 50}});
3460 motionArgs.xCursorPosition = 50;
3461 motionArgs.yCursorPosition = 50;
Prabir Pradhan678438e2023-04-13 19:32:51 +00003462 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003463 ASSERT_NO_FATAL_FAILURE(
3464 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3465 WithSource(AINPUT_SOURCE_MOUSE))));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003466
3467 // Tap on the window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003468 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3469 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3470 {{10, 10}}));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003471 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003472 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3473 WithSource(AINPUT_SOURCE_MOUSE))));
3474
3475 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003476 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3477 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
3478
Prabir Pradhan678438e2023-04-13 19:32:51 +00003479 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3480 ADISPLAY_ID_DEFAULT, {{10, 10}}));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003481 ASSERT_NO_FATAL_FAILURE(
3482 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3483 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
3484}
3485
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003486TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) {
3487 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3488 sp<FakeWindowHandle> windowDefaultDisplay =
3489 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
3490 ADISPLAY_ID_DEFAULT);
3491 windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800));
3492 sp<FakeWindowHandle> windowSecondDisplay =
3493 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay",
3494 SECOND_DISPLAY_ID);
3495 windowSecondDisplay->setFrame(Rect(0, 0, 600, 800));
3496
3497 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
3498 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
3499
3500 // Set cursor position in window in default display and check that hover enter and move
3501 // events are generated.
3502 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3503 injectMotionEvent(mDispatcher,
3504 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3505 AINPUT_SOURCE_MOUSE)
3506 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003507 .pointer(PointerBuilder(0, ToolType::MOUSE)
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003508 .x(300)
3509 .y(600))
3510 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003511 windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003512
3513 // Remove all windows in secondary display and check that no event happens on window in
3514 // primary display.
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003515 mDispatcher->setInputWindows(
3516 {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003517 windowDefaultDisplay->assertNoEvents();
3518
3519 // Move cursor position in window in default display and check that only hover move
3520 // event is generated and not hover enter event.
3521 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
3522 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
3523 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3524 injectMotionEvent(mDispatcher,
3525 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3526 AINPUT_SOURCE_MOUSE)
3527 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003528 .pointer(PointerBuilder(0, ToolType::MOUSE)
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003529 .x(400)
3530 .y(700))
3531 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003532 windowDefaultDisplay->consumeMotionEvent(
3533 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3534 WithSource(AINPUT_SOURCE_MOUSE)));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003535 windowDefaultDisplay->assertNoEvents();
3536}
3537
Garfield Tan00f511d2019-06-12 16:55:40 -07003538TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07003539 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07003540
3541 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003542 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003543 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07003544 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003545 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003546 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07003547
3548 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3549
Arthur Hung72d8dc32020-03-28 00:48:39 +00003550 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07003551
3552 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
3553 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003554 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07003555 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003556 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003557 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003558 windowRight->assertNoEvents();
3559}
3560
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003561TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07003562 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003563 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3564 "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07003565 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003566
Arthur Hung72d8dc32020-03-28 00:48:39 +00003567 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003568 setFocusedWindow(window);
3569
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003570 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003571
Prabir Pradhan678438e2023-04-13 19:32:51 +00003572 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003573
3574 // Window should receive key down event.
3575 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3576
3577 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
3578 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003579 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07003580 window->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003581 AKEY_EVENT_FLAG_CANCELED);
3582}
3583
3584TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07003585 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003586 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3587 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003588
Arthur Hung72d8dc32020-03-28 00:48:39 +00003589 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003590
Prabir Pradhan678438e2023-04-13 19:32:51 +00003591 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3592 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003593
3594 // Window should receive motion down event.
3595 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3596
3597 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
3598 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003599 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08003600 window->consumeMotionEvent(
3601 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003602}
3603
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -07003604TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) {
3605 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3606 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3607 "Fake Window", ADISPLAY_ID_DEFAULT);
3608
3609 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3610
3611 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3612 .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10))
3613 .build());
3614
3615 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3616
3617 // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT
3618 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
3619 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3620
3621 // After the device has been reset, a new hovering stream can be sent to the window
3622 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3623 .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15))
3624 .build());
3625 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3626}
3627
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003628TEST_F(InputDispatcherTest, InterceptKeyByPolicy) {
3629 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003630 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3631 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003632 window->setFocusable(true);
3633
3634 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3635 setFocusedWindow(window);
3636
3637 window->consumeFocusEvent(true);
3638
Prabir Pradhan678438e2023-04-13 19:32:51 +00003639 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003640 const std::chrono::milliseconds interceptKeyTimeout = 50ms;
3641 const nsecs_t injectTime = keyArgs.eventTime;
3642 mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout);
Prabir Pradhan678438e2023-04-13 19:32:51 +00003643 mDispatcher->notifyKey(keyArgs);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003644 // The dispatching time should be always greater than or equal to intercept key timeout.
3645 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3646 ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >=
3647 std::chrono::nanoseconds(interceptKeyTimeout).count());
3648}
3649
Siarhei Vishniakouf54d2ab2023-06-09 13:23:53 -07003650/**
3651 * Keys with ACTION_UP are delivered immediately, even if a long 'intercept key timeout' is set.
3652 */
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003653TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) {
3654 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003655 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3656 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003657 window->setFocusable(true);
3658
3659 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3660 setFocusedWindow(window);
3661
3662 window->consumeFocusEvent(true);
3663
Prabir Pradhan678438e2023-04-13 19:32:51 +00003664 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003665 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf54d2ab2023-06-09 13:23:53 -07003666
3667 // Set a value that's significantly larger than the default consumption timeout. If the
3668 // implementation is correct, the actual value doesn't matter; it won't slow down the test.
3669 mFakePolicy->setInterceptKeyTimeout(600ms);
3670 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
3671 // Window should receive key event immediately when same key up.
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003672 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3673}
3674
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003675/**
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003676 * Two windows. First is a regular window. Second does not overlap with the first, and has
3677 * WATCH_OUTSIDE_TOUCH.
3678 * Both windows are owned by the same UID.
3679 * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero
3680 * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID.
3681 */
3682TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) {
3683 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3684 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3685 "First Window", ADISPLAY_ID_DEFAULT);
3686 window->setFrame(Rect{0, 0, 100, 100});
3687
3688 sp<FakeWindowHandle> outsideWindow =
3689 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3690 ADISPLAY_ID_DEFAULT);
3691 outsideWindow->setFrame(Rect{100, 100, 200, 200});
3692 outsideWindow->setWatchOutsideTouch(true);
3693 // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped
3694 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {outsideWindow, window}}});
3695
3696 // Tap on first window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003697 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3698 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3699 {PointF{50, 50}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003700 window->consumeMotionDown();
3701 // The coordinates of the tap in 'outsideWindow' are relative to its top left corner.
3702 // Therefore, we should offset them by (100, 100) relative to the screen's top left corner.
3703 outsideWindow->consumeMotionEvent(
3704 AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50)));
3705}
3706
3707/**
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003708 * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when
3709 * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one
3710 * ACTION_OUTSIDE event is sent per gesture.
3711 */
3712TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) {
3713 // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH.
3714 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003715 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3716 "First Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003717 window->setWatchOutsideTouch(true);
3718 window->setFrame(Rect{0, 0, 100, 100});
3719 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003720 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3721 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003722 secondWindow->setFrame(Rect{100, 100, 200, 200});
3723 sp<FakeWindowHandle> thirdWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003724 sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window",
3725 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003726 thirdWindow->setFrame(Rect{200, 200, 300, 300});
3727 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}});
3728
3729 // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003730 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3731 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3732 {PointF{-10, -10}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003733 window->assertNoEvents();
3734 secondWindow->assertNoEvents();
3735
3736 // The second pointer lands inside `secondWindow`, which should receive a DOWN event.
3737 // Now, `window` should get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003738 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3739 ADISPLAY_ID_DEFAULT,
3740 {PointF{-10, -10}, PointF{105, 105}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003741 const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}};
3742 window->consumeMotionEvent(
3743 AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers)));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003744 secondWindow->consumeMotionDown();
3745 thirdWindow->assertNoEvents();
3746
3747 // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is
3748 // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003749 mDispatcher->notifyMotion(
3750 generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3751 {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003752 window->assertNoEvents();
3753 secondWindow->consumeMotionMove();
3754 thirdWindow->consumeMotionDown();
3755}
3756
Prabir Pradhan814fe082022-07-22 20:22:18 +00003757TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) {
3758 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003759 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3760 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003761 window->setFocusable(true);
3762
Patrick Williamsd828f302023-04-28 17:52:08 -05003763 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00003764 setFocusedWindow(window);
3765
3766 window->consumeFocusEvent(true);
3767
Prabir Pradhan678438e2023-04-13 19:32:51 +00003768 const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3769 const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
3770 mDispatcher->notifyKey(keyDown);
3771 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003772
3773 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3774 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3775
3776 // All windows are removed from the display. Ensure that we can no longer dispatch to it.
Patrick Williamsd828f302023-04-28 17:52:08 -05003777 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00003778
3779 window->consumeFocusEvent(false);
3780
Prabir Pradhan678438e2023-04-13 19:32:51 +00003781 mDispatcher->notifyKey(keyDown);
3782 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003783 window->assertNoEvents();
3784}
3785
Arthur Hung96483742022-11-15 03:30:48 +00003786TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) {
3787 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3788 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3789 "Fake Window", ADISPLAY_ID_DEFAULT);
3790 // Ensure window is non-split and have some transform.
3791 window->setPreventSplitting(true);
3792 window->setWindowOffset(20, 40);
Patrick Williamsd828f302023-04-28 17:52:08 -05003793 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung96483742022-11-15 03:30:48 +00003794
3795 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3796 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3797 {50, 50}))
3798 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3799 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3800
3801 const MotionEvent secondFingerDownEvent =
3802 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3803 .displayId(ADISPLAY_ID_DEFAULT)
3804 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003805 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
3806 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50))
Arthur Hung96483742022-11-15 03:30:48 +00003807 .build();
3808 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3809 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
3810 InputEventInjectionSync::WAIT_FOR_RESULT))
3811 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3812
3813 const MotionEvent* event = window->consumeMotion();
3814 EXPECT_EQ(POINTER_1_DOWN, event->getAction());
3815 EXPECT_EQ(70, event->getX(0)); // 50 + 20
3816 EXPECT_EQ(90, event->getY(0)); // 50 + 40
3817 EXPECT_EQ(-10, event->getX(1)); // -30 + 20
3818 EXPECT_EQ(-10, event->getY(1)); // -50 + 40
3819}
3820
Harry Cuttsb166c002023-05-09 13:06:05 +00003821TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) {
3822 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3823 sp<FakeWindowHandle> window =
3824 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3825 window->setFrame(Rect(0, 0, 400, 400));
3826 sp<FakeWindowHandle> trustedOverlay =
3827 sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay",
3828 ADISPLAY_ID_DEFAULT);
3829 trustedOverlay->setSpy(true);
3830 trustedOverlay->setTrustedOverlay(true);
3831
3832 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {trustedOverlay, window}}});
3833
3834 // Start a three-finger touchpad swipe
3835 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3836 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3837 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3838 .build());
3839 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
3840 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3841 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3842 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3843 .build());
3844 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
3845 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3846 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3847 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
3848 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3849 .build());
3850
3851 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3852 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
3853 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN));
3854
3855 // Move the swipe a bit
3856 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
3857 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3858 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3859 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3860 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3861 .build());
3862
3863 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3864
3865 // End the swipe
3866 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
3867 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3868 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3869 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3870 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3871 .build());
3872 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
3873 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3874 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3875 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3876 .build());
3877 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
3878 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3879 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3880 .build());
3881
3882 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP));
3883 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
3884 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP));
3885
3886 window->assertNoEvents();
3887}
3888
3889TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) {
3890 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3891 sp<FakeWindowHandle> window =
3892 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3893 window->setFrame(Rect(0, 0, 400, 400));
3894 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3895
3896 // Start a three-finger touchpad swipe
3897 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3898 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3899 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3900 .build());
3901 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
3902 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3903 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3904 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3905 .build());
3906 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
3907 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3908 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3909 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
3910 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3911 .build());
3912
3913 // Move the swipe a bit
3914 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, 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
3921 // End the swipe
3922 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
3923 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3924 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3925 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3926 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3927 .build());
3928 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
3929 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3930 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3931 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3932 .build());
3933 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
3934 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3935 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3936 .build());
3937
3938 window->assertNoEvents();
3939}
3940
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003941/**
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07003942 * Send a two-pointer gesture to a single window. The window's orientation changes in response to
3943 * the first pointer.
3944 * Ensure that the second pointer is not sent to the window.
3945 *
3946 * The subsequent gesture should be correctly delivered to the window.
3947 */
3948TEST_F(InputDispatcherTest, MultiplePointersWithRotatingWindow) {
3949 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3950 sp<FakeWindowHandle> window =
3951 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3952 window->setFrame(Rect(0, 0, 400, 400));
3953 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3954
3955 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
3956 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3957 .downTime(baseTime + 10)
3958 .eventTime(baseTime + 10)
3959 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3960 .build());
3961
3962 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3963
3964 // We need a new window object for the same window, because dispatcher will store objects by
3965 // reference. That means that the testing code and the dispatcher will refer to the same shared
3966 // object. Calling window->setTransform here would affect dispatcher's comparison
3967 // of the old window to the new window, since both the old window and the new window would be
3968 // updated to the same value.
3969 sp<FakeWindowHandle> windowDup = window->duplicate();
3970
3971 // Change the transform so that the orientation is now different from original.
3972 windowDup->setWindowTransform(0, -1, 1, 0);
3973
3974 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDup}}});
3975
3976 window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
3977
3978 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3979 .downTime(baseTime + 10)
3980 .eventTime(baseTime + 30)
3981 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3982 .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200))
3983 .build());
3984
3985 // Finish the gesture and start a new one. Ensure the new gesture is sent to the window
3986 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
3987 .downTime(baseTime + 10)
3988 .eventTime(baseTime + 40)
3989 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3990 .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200))
3991 .build());
3992 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
3993 .downTime(baseTime + 10)
3994 .eventTime(baseTime + 50)
3995 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3996 .build());
3997
3998 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3999 .downTime(baseTime + 60)
4000 .eventTime(baseTime + 60)
4001 .pointer(PointerBuilder(0, ToolType::FINGER).x(40).y(40))
4002 .build());
4003
4004 windowDup->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
4005}
4006
4007/**
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004008 * Ensure the correct coordinate spaces are used by InputDispatcher.
4009 *
4010 * InputDispatcher works in the display space, so its coordinate system is relative to the display
4011 * panel. Windows get events in the window space, and get raw coordinates in the logical display
4012 * space.
4013 */
4014class InputDispatcherDisplayProjectionTest : public InputDispatcherTest {
4015public:
4016 void SetUp() override {
4017 InputDispatcherTest::SetUp();
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004018 removeAllWindowsAndDisplays();
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004019 }
4020
4021 void addDisplayInfo(int displayId, const ui::Transform& transform) {
4022 gui::DisplayInfo info;
4023 info.displayId = displayId;
4024 info.transform = transform;
4025 mDisplayInfos.push_back(std::move(info));
Patrick Williamsd828f302023-04-28 17:52:08 -05004026 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004027 }
4028
4029 void addWindow(const sp<WindowInfoHandle>& windowHandle) {
4030 mWindowInfos.push_back(*windowHandle->getInfo());
Patrick Williamsd828f302023-04-28 17:52:08 -05004031 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004032 }
4033
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004034 void removeAllWindowsAndDisplays() {
4035 mDisplayInfos.clear();
4036 mWindowInfos.clear();
4037 }
4038
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004039 // Set up a test scenario where the display has a scaled projection and there are two windows
4040 // on the display.
4041 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() {
4042 // The display has a projection that has a scale factor of 2 and 4 in the x and y directions
4043 // respectively.
4044 ui::Transform displayTransform;
4045 displayTransform.set(2, 0, 0, 4);
4046 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
4047
4048 std::shared_ptr<FakeApplicationHandle> application =
4049 std::make_shared<FakeApplicationHandle>();
4050
4051 // Add two windows to the display. Their frames are represented in the display space.
4052 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004053 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4054 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004055 firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform);
4056 addWindow(firstWindow);
4057
4058 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004059 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4060 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004061 secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform);
4062 addWindow(secondWindow);
4063 return {std::move(firstWindow), std::move(secondWindow)};
4064 }
4065
4066private:
4067 std::vector<gui::DisplayInfo> mDisplayInfos;
4068 std::vector<gui::WindowInfo> mWindowInfos;
4069};
4070
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004071TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) {
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004072 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4073 // Send down to the first window. The point is represented in the display space. The point is
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004074 // selected so that if the hit test was performed with the point and the bounds being in
4075 // different coordinate spaces, the event would end up in the incorrect window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004076 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4077 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4078 {PointF{75, 55}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004079
4080 firstWindow->consumeMotionDown();
4081 secondWindow->assertNoEvents();
4082}
4083
4084// Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API,
4085// the event should be treated as being in the logical display space.
4086TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) {
4087 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4088 // Send down to the first window. The point is represented in the logical display space. The
4089 // point is selected so that if the hit test was done in logical display space, then it would
4090 // end up in the incorrect window.
4091 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4092 PointF{75 * 2, 55 * 4});
4093
4094 firstWindow->consumeMotionDown();
4095 secondWindow->assertNoEvents();
4096}
4097
Prabir Pradhandaa2f142021-12-10 09:30:08 +00004098// Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed
4099// event should be treated as being in the logical display space.
4100TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) {
4101 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4102
4103 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
4104 ui::Transform injectedEventTransform;
4105 injectedEventTransform.set(matrix);
4106 const vec2 expectedPoint{75, 55}; // The injected point in the logical display space.
4107 const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint);
4108
4109 MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4110 .displayId(ADISPLAY_ID_DEFAULT)
4111 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004112 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER)
Prabir Pradhandaa2f142021-12-10 09:30:08 +00004113 .x(untransformedPoint.x)
4114 .y(untransformedPoint.y))
4115 .build();
4116 event.transform(matrix);
4117
4118 injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT,
4119 InputEventInjectionSync::WAIT_FOR_RESULT);
4120
4121 firstWindow->consumeMotionDown();
4122 secondWindow->assertNoEvents();
4123}
4124
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004125TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) {
4126 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4127
4128 // Send down to the second window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004129 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4130 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4131 {PointF{150, 220}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004132
4133 firstWindow->assertNoEvents();
4134 const MotionEvent* event = secondWindow->consumeMotion();
Siarhei Vishniakoub681c202023-05-01 11:22:33 -07004135 ASSERT_NE(nullptr, event);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004136 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction());
4137
4138 // Ensure that the events from the "getRaw" API are in logical display coordinates.
4139 EXPECT_EQ(300, event->getRawX(0));
4140 EXPECT_EQ(880, event->getRawY(0));
4141
4142 // Ensure that the x and y values are in the window's coordinate space.
4143 // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in
4144 // the logical display space. This will be the origin of the window space.
4145 EXPECT_EQ(100, event->getX(0));
4146 EXPECT_EQ(80, event->getY(0));
4147}
4148
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004149/** Ensure consistent behavior of InputDispatcher in all orientations. */
4150class InputDispatcherDisplayOrientationFixture
4151 : public InputDispatcherDisplayProjectionTest,
4152 public ::testing::WithParamInterface<ui::Rotation> {};
4153
4154// This test verifies the touchable region of a window for all rotations of the display by tapping
4155// in different locations on the display, specifically points close to the four corners of a
4156// window.
4157TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) {
4158 constexpr static int32_t displayWidth = 400;
4159 constexpr static int32_t displayHeight = 800;
4160
4161 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4162
4163 const auto rotation = GetParam();
4164
4165 // Set up the display with the specified rotation.
4166 const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270;
4167 const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth;
4168 const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight;
4169 const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation),
4170 logicalDisplayWidth, logicalDisplayHeight);
4171 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
4172
4173 // Create a window with its bounds determined in the logical display.
4174 const Rect frameInLogicalDisplay(100, 100, 200, 300);
4175 const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay);
4176 sp<FakeWindowHandle> window =
4177 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4178 window->setFrame(frameInDisplay, displayTransform);
4179 addWindow(window);
4180
4181 // The following points in logical display space should be inside the window.
4182 static const std::array<vec2, 4> insidePoints{
4183 {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}};
4184 for (const auto pointInsideWindow : insidePoints) {
4185 const vec2 p = displayTransform.inverse().transform(pointInsideWindow);
4186 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004187 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4188 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4189 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004190 window->consumeMotionDown();
4191
Prabir Pradhan678438e2023-04-13 19:32:51 +00004192 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4193 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4194 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004195 window->consumeMotionUp();
4196 }
4197
4198 // The following points in logical display space should be outside the window.
4199 static const std::array<vec2, 5> outsidePoints{
4200 {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}};
4201 for (const auto pointOutsideWindow : outsidePoints) {
4202 const vec2 p = displayTransform.inverse().transform(pointOutsideWindow);
4203 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004204 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4205 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4206 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004207
Prabir Pradhan678438e2023-04-13 19:32:51 +00004208 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4209 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4210 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004211 }
4212 window->assertNoEvents();
4213}
4214
4215// Run the precision tests for all rotations.
4216INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests,
4217 InputDispatcherDisplayOrientationFixture,
4218 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
4219 ui::ROTATION_270),
4220 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
4221 return ftl::enum_string(testParamInfo.param);
4222 });
4223
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004224using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher,
4225 sp<IBinder>, sp<IBinder>)>;
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004226
4227class TransferTouchFixture : public InputDispatcherTest,
4228 public ::testing::WithParamInterface<TransferFunction> {};
4229
4230TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07004231 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004232
4233 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004234 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004235 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4236 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004237 firstWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004238 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004239 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4240 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004241 sp<FakeWindowHandle> wallpaper =
4242 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
4243 wallpaper->setIsWallpaper(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004244 // Add the windows to the dispatcher
Arthur Hungc539dbb2022-12-08 07:45:36 +00004245 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004246
4247 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004248 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4249 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004250
Svet Ganov5d3bc372020-01-26 23:11:07 -08004251 // Only the first window should get the down event
4252 firstWindow->consumeMotionDown();
4253 secondWindow->assertNoEvents();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004254 wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004255
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004256 // Transfer touch to the second window
4257 TransferFunction f = GetParam();
4258 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4259 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004260 // The first window gets cancel and the second gets down
4261 firstWindow->consumeMotionCancel();
4262 secondWindow->consumeMotionDown();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004263 wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004264
4265 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004266 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4267 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004268 // The first window gets no events and the second gets up
4269 firstWindow->assertNoEvents();
4270 secondWindow->consumeMotionUp();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004271 wallpaper->assertNoEvents();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004272}
4273
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004274/**
4275 * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch
4276 * from. When we have spy windows, there are several windows to choose from: either spy, or the
4277 * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most
4278 * natural to the user.
4279 * In this test, we are sending a pointer to both spy window and first window. We then try to
4280 * transfer touch to the second window. The dispatcher should identify the first window as the
4281 * one that should lose the gesture, and therefore the action should be to move the gesture from
4282 * the first window to the second.
4283 * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test
4284 * the other API, as well.
4285 */
4286TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) {
4287 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4288
4289 // Create a couple of windows + a spy window
4290 sp<FakeWindowHandle> spyWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004291 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004292 spyWindow->setTrustedOverlay(true);
4293 spyWindow->setSpy(true);
4294 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004295 sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004296 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004297 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004298
4299 // Add the windows to the dispatcher
4300 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}});
4301
4302 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004303 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4304 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004305 // Only the first window and spy should get the down event
4306 spyWindow->consumeMotionDown();
4307 firstWindow->consumeMotionDown();
4308
4309 // Transfer touch to the second window. Non-spy window should be preferred over the spy window
4310 // if f === 'transferTouch'.
4311 TransferFunction f = GetParam();
4312 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4313 ASSERT_TRUE(success);
4314 // The first window gets cancel and the second gets down
4315 firstWindow->consumeMotionCancel();
4316 secondWindow->consumeMotionDown();
4317
4318 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004319 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4320 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004321 // The first window gets no events and the second+spy get up
4322 firstWindow->assertNoEvents();
4323 spyWindow->consumeMotionUp();
4324 secondWindow->consumeMotionUp();
4325}
4326
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004327TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004328 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004329
4330 PointF touchPoint = {10, 10};
4331
4332 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004333 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004334 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4335 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004336 firstWindow->setPreventSplitting(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004337 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004338 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4339 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004340 secondWindow->setPreventSplitting(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004341
4342 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004343 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004344
4345 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004346 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4347 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4348 {touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004349 // Only the first window should get the down event
4350 firstWindow->consumeMotionDown();
4351 secondWindow->assertNoEvents();
4352
4353 // Send pointer down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004354 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4355 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004356 // Only the first window should get the pointer down event
4357 firstWindow->consumeMotionPointerDown(1);
4358 secondWindow->assertNoEvents();
4359
4360 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004361 TransferFunction f = GetParam();
4362 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4363 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004364 // The first window gets cancel and the second gets down and pointer down
4365 firstWindow->consumeMotionCancel();
4366 secondWindow->consumeMotionDown();
4367 secondWindow->consumeMotionPointerDown(1);
4368
4369 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004370 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4371 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004372 // The first window gets nothing and the second gets pointer up
4373 firstWindow->assertNoEvents();
4374 secondWindow->consumeMotionPointerUp(1);
4375
4376 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004377 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4378 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004379 // The first window gets nothing and the second gets up
4380 firstWindow->assertNoEvents();
4381 secondWindow->consumeMotionUp();
4382}
4383
Arthur Hungc539dbb2022-12-08 07:45:36 +00004384TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) {
4385 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4386
4387 // Create a couple of windows
4388 sp<FakeWindowHandle> firstWindow =
4389 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4390 ADISPLAY_ID_DEFAULT);
4391 firstWindow->setDupTouchToWallpaper(true);
4392 sp<FakeWindowHandle> secondWindow =
4393 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4394 ADISPLAY_ID_DEFAULT);
4395 secondWindow->setDupTouchToWallpaper(true);
4396
4397 sp<FakeWindowHandle> wallpaper1 =
4398 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT);
4399 wallpaper1->setIsWallpaper(true);
4400
4401 sp<FakeWindowHandle> wallpaper2 =
4402 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT);
4403 wallpaper2->setIsWallpaper(true);
4404 // Add the windows to the dispatcher
4405 mDispatcher->setInputWindows(
4406 {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}});
4407
4408 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004409 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4410 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004411
4412 // Only the first window should get the down event
4413 firstWindow->consumeMotionDown();
4414 secondWindow->assertNoEvents();
4415 wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4416 wallpaper2->assertNoEvents();
4417
4418 // Transfer touch focus to the second window
4419 TransferFunction f = GetParam();
4420 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4421 ASSERT_TRUE(success);
4422
4423 // The first window gets cancel and the second gets down
4424 firstWindow->consumeMotionCancel();
4425 secondWindow->consumeMotionDown();
4426 wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4427 wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4428
4429 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004430 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4431 ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004432 // The first window gets no events and the second gets up
4433 firstWindow->assertNoEvents();
4434 secondWindow->consumeMotionUp();
4435 wallpaper1->assertNoEvents();
4436 wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4437}
4438
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004439// For the cases of single pointer touch and two pointers non-split touch, the api's
4440// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
4441// for the case where there are multiple pointers split across several windows.
4442INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
4443 ::testing::Values(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004444 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4445 sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004446 return dispatcher->transferTouch(destChannelToken,
4447 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004448 },
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004449 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4450 sp<IBinder> from, sp<IBinder> to) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004451 return dispatcher->transferTouchFocus(from, to,
Harry Cutts33476232023-01-30 19:57:29 +00004452 /*isDragAndDrop=*/false);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004453 }));
4454
Svet Ganov5d3bc372020-01-26 23:11:07 -08004455TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004456 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004457
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004458 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004459 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4460 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004461 firstWindow->setFrame(Rect(0, 0, 600, 400));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004462
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004463 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004464 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4465 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004466 secondWindow->setFrame(Rect(0, 400, 600, 800));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004467
4468 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004469 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004470
4471 PointF pointInFirst = {300, 200};
4472 PointF pointInSecond = {300, 600};
4473
4474 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004475 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4476 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4477 {pointInFirst}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004478 // Only the first window should get the down event
4479 firstWindow->consumeMotionDown();
4480 secondWindow->assertNoEvents();
4481
4482 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004483 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4484 ADISPLAY_ID_DEFAULT,
4485 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004486 // The first window gets a move and the second a down
4487 firstWindow->consumeMotionMove();
4488 secondWindow->consumeMotionDown();
4489
4490 // Transfer touch focus to the second window
4491 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
4492 // The first window gets cancel and the new gets pointer down (it already saw down)
4493 firstWindow->consumeMotionCancel();
4494 secondWindow->consumeMotionPointerDown(1);
4495
4496 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004497 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4498 ADISPLAY_ID_DEFAULT,
4499 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004500 // The first window gets nothing and the second gets pointer up
4501 firstWindow->assertNoEvents();
4502 secondWindow->consumeMotionPointerUp(1);
4503
4504 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004505 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4506 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004507 // The first window gets nothing and the second gets up
4508 firstWindow->assertNoEvents();
4509 secondWindow->consumeMotionUp();
4510}
4511
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004512// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
4513// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
4514// touch is not supported, so the touch should continue on those windows and the transferred-to
4515// window should get nothing.
4516TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
4517 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4518
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004519 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004520 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4521 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004522 firstWindow->setFrame(Rect(0, 0, 600, 400));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004523
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004524 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004525 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4526 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004527 secondWindow->setFrame(Rect(0, 400, 600, 800));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004528
4529 // Add the windows to the dispatcher
4530 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4531
4532 PointF pointInFirst = {300, 200};
4533 PointF pointInSecond = {300, 600};
4534
4535 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004536 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4537 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4538 {pointInFirst}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004539 // Only the first window should get the down event
4540 firstWindow->consumeMotionDown();
4541 secondWindow->assertNoEvents();
4542
4543 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004544 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4545 ADISPLAY_ID_DEFAULT,
4546 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004547 // The first window gets a move and the second a down
4548 firstWindow->consumeMotionMove();
4549 secondWindow->consumeMotionDown();
4550
4551 // Transfer touch focus to the second window
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004552 const bool transferred =
4553 mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004554 // The 'transferTouch' call should not succeed, because there are 2 touched windows
4555 ASSERT_FALSE(transferred);
4556 firstWindow->assertNoEvents();
4557 secondWindow->assertNoEvents();
4558
4559 // The rest of the dispatch should proceed as normal
4560 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004561 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4562 ADISPLAY_ID_DEFAULT,
4563 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004564 // The first window gets MOVE and the second gets pointer up
4565 firstWindow->consumeMotionMove();
4566 secondWindow->consumeMotionUp();
4567
4568 // Send up event to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004569 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4570 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004571 // The first window gets nothing and the second gets up
4572 firstWindow->consumeMotionUp();
4573 secondWindow->assertNoEvents();
4574}
4575
Arthur Hungabbb9d82021-09-01 14:52:30 +00004576// This case will create two windows and one mirrored window on the default display and mirror
4577// two windows on the second display. It will test if 'transferTouchFocus' works fine if we put
4578// the windows info of second display before default display.
4579TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) {
4580 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4581 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004582 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004583 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004584 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004585 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004586 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004587
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004588 sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004589 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004590
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004591 sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004592 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004593
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004594 sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004595 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004596
4597 // Update window info, let it find window handle of second display first.
4598 mDispatcher->setInputWindows(
4599 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4600 {ADISPLAY_ID_DEFAULT,
4601 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4602
4603 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4604 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4605 {50, 50}))
4606 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4607
4608 // Window should receive motion event.
4609 firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4610
4611 // Transfer touch focus
4612 ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(),
4613 secondWindowInPrimary->getToken()));
4614 // The first window gets cancel.
4615 firstWindowInPrimary->consumeMotionCancel();
4616 secondWindowInPrimary->consumeMotionDown();
4617
4618 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4619 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4620 ADISPLAY_ID_DEFAULT, {150, 50}))
4621 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4622 firstWindowInPrimary->assertNoEvents();
4623 secondWindowInPrimary->consumeMotionMove();
4624
4625 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4626 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4627 {150, 50}))
4628 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4629 firstWindowInPrimary->assertNoEvents();
4630 secondWindowInPrimary->consumeMotionUp();
4631}
4632
4633// Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use
4634// 'transferTouch' api.
4635TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
4636 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4637 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004638 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004639 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004640 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004641 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004642 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004643
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004644 sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004645 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004646
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004647 sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004648 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004649
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004650 sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004651 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004652
4653 // Update window info, let it find window handle of second display first.
4654 mDispatcher->setInputWindows(
4655 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4656 {ADISPLAY_ID_DEFAULT,
4657 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4658
4659 // Touch on second display.
4660 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4661 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50}))
4662 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4663
4664 // Window should receive motion event.
4665 firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4666
4667 // Transfer touch focus
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004668 ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004669
4670 // The first window gets cancel.
4671 firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID);
4672 secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4673
4674 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4675 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4676 SECOND_DISPLAY_ID, {150, 50}))
4677 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4678 firstWindowInPrimary->assertNoEvents();
4679 secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID);
4680
4681 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4682 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
4683 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4684 firstWindowInPrimary->assertNoEvents();
4685 secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID);
4686}
4687
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004688TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004689 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004690 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4691 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004692
Vishnu Nair47074b82020-08-14 11:54:47 -07004693 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004694 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004695 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004696
4697 window->consumeFocusEvent(true);
4698
Prabir Pradhan678438e2023-04-13 19:32:51 +00004699 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004700
4701 // Window should receive key down event.
4702 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Josep del Riob3981622023-04-18 15:49:45 +00004703
4704 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00004705 mDispatcher->waitForIdle();
Josep del Riob3981622023-04-18 15:49:45 +00004706 mFakePolicy->assertUserActivityPoked();
4707}
4708
4709TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) {
4710 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4711 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4712 "Fake Window", ADISPLAY_ID_DEFAULT);
4713
4714 window->setDisableUserActivity(true);
4715 window->setFocusable(true);
4716 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4717 setFocusedWindow(window);
4718
4719 window->consumeFocusEvent(true);
4720
4721 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4722
4723 // Window should receive key down event.
4724 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4725
4726 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00004727 mDispatcher->waitForIdle();
Josep del Riob3981622023-04-18 15:49:45 +00004728 mFakePolicy->assertUserActivityNotPoked();
4729}
4730
4731TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) {
4732 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4733 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4734 "Fake Window", ADISPLAY_ID_DEFAULT);
4735
4736 window->setFocusable(true);
4737 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4738 setFocusedWindow(window);
4739
4740 window->consumeFocusEvent(true);
4741
4742 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4743 mDispatcher->waitForIdle();
4744
4745 // System key is not passed down
4746 window->assertNoEvents();
4747
4748 // Should have poked user activity
4749 mFakePolicy->assertUserActivityPoked();
4750}
4751
4752TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) {
4753 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4754 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4755 "Fake Window", ADISPLAY_ID_DEFAULT);
4756
4757 window->setFocusable(true);
4758 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4759 setFocusedWindow(window);
4760
4761 window->consumeFocusEvent(true);
4762
4763 mDispatcher->notifyKey(generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4764 mDispatcher->waitForIdle();
4765
4766 // System key is not passed down
4767 window->assertNoEvents();
4768
4769 // Should have poked user activity
4770 mFakePolicy->assertUserActivityPoked();
4771}
4772
4773TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) {
4774 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4775 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4776 "Fake Window", ADISPLAY_ID_DEFAULT);
4777
4778 window->setDisableUserActivity(true);
4779 window->setFocusable(true);
4780 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4781 setFocusedWindow(window);
4782
4783 window->consumeFocusEvent(true);
4784
4785 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4786 mDispatcher->waitForIdle();
4787
4788 // System key is not passed down
4789 window->assertNoEvents();
4790
4791 // Should have poked user activity
4792 mFakePolicy->assertUserActivityPoked();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004793}
4794
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07004795TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) {
4796 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4797 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4798 "Fake Window", ADISPLAY_ID_DEFAULT);
4799
4800 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4801
4802 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4803 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4804 ADISPLAY_ID_DEFAULT, {100, 100}))
4805 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4806
4807 window->consumeMotionEvent(
4808 AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT)));
4809
4810 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00004811 mDispatcher->waitForIdle();
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07004812 mFakePolicy->assertUserActivityPoked();
4813}
4814
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004815TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004816 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004817 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4818 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004819
Arthur Hung72d8dc32020-03-28 00:48:39 +00004820 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004821
Prabir Pradhan678438e2023-04-13 19:32:51 +00004822 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004823 mDispatcher->waitForIdle();
4824
4825 window->assertNoEvents();
4826}
4827
4828// If a window is touchable, but does not have focus, it should receive motion events, but not keys
4829TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07004830 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004831 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4832 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004833
Arthur Hung72d8dc32020-03-28 00:48:39 +00004834 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004835
4836 // Send key
Prabir Pradhan678438e2023-04-13 19:32:51 +00004837 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004838 // Send motion
Prabir Pradhan678438e2023-04-13 19:32:51 +00004839 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4840 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004841
4842 // Window should receive only the motion event
4843 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4844 window->assertNoEvents(); // Key event or focus event will not be received
4845}
4846
arthurhungea3f4fc2020-12-21 23:18:53 +08004847TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
4848 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4849
arthurhungea3f4fc2020-12-21 23:18:53 +08004850 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004851 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4852 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004853 firstWindow->setFrame(Rect(0, 0, 600, 400));
arthurhungea3f4fc2020-12-21 23:18:53 +08004854
arthurhungea3f4fc2020-12-21 23:18:53 +08004855 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004856 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4857 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004858 secondWindow->setFrame(Rect(0, 400, 600, 800));
arthurhungea3f4fc2020-12-21 23:18:53 +08004859
4860 // Add the windows to the dispatcher
4861 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4862
4863 PointF pointInFirst = {300, 200};
4864 PointF pointInSecond = {300, 600};
4865
4866 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004867 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4868 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4869 {pointInFirst}));
arthurhungea3f4fc2020-12-21 23:18:53 +08004870 // Only the first window should get the down event
4871 firstWindow->consumeMotionDown();
4872 secondWindow->assertNoEvents();
4873
4874 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004875 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4876 ADISPLAY_ID_DEFAULT,
4877 {pointInFirst, pointInSecond}));
arthurhungea3f4fc2020-12-21 23:18:53 +08004878 // The first window gets a move and the second a down
4879 firstWindow->consumeMotionMove();
4880 secondWindow->consumeMotionDown();
4881
4882 // Send pointer cancel to the second window
4883 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004884 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08004885 {pointInFirst, pointInSecond});
4886 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
Prabir Pradhan678438e2023-04-13 19:32:51 +00004887 mDispatcher->notifyMotion(pointerUpMotionArgs);
arthurhungea3f4fc2020-12-21 23:18:53 +08004888 // The first window gets move and the second gets cancel.
4889 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
4890 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
4891
4892 // Send up event.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004893 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4894 ADISPLAY_ID_DEFAULT));
arthurhungea3f4fc2020-12-21 23:18:53 +08004895 // The first window gets up and the second gets nothing.
4896 firstWindow->consumeMotionUp();
4897 secondWindow->assertNoEvents();
4898}
4899
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004900TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
4901 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4902
4903 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004904 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004905 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4906 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
4907 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
4908 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
4909
Harry Cutts33476232023-01-30 19:57:29 +00004910 window->sendTimeline(/*inputEventId=*/1, graphicsTimeline);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004911 window->assertNoEvents();
4912 mDispatcher->waitForIdle();
4913}
4914
chaviwd1c23182019-12-20 18:44:56 -08004915class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00004916public:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004917 FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004918 int32_t displayId) {
Garfield Tan15601662020-09-22 15:32:38 -07004919 base::Result<std::unique_ptr<InputChannel>> channel =
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08004920 dispatcher->createInputMonitor(displayId, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07004921 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00004922 }
4923
chaviwd1c23182019-12-20 18:44:56 -08004924 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
4925
4926 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004927 mInputReceiver->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
4928 expectedFlags);
chaviwd1c23182019-12-20 18:44:56 -08004929 }
4930
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004931 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
4932
4933 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
4934
chaviwd1c23182019-12-20 18:44:56 -08004935 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004936 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
chaviwd1c23182019-12-20 18:44:56 -08004937 expectedDisplayId, expectedFlags);
4938 }
4939
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004940 void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004941 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004942 expectedDisplayId, expectedFlags);
4943 }
4944
chaviwd1c23182019-12-20 18:44:56 -08004945 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004946 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP,
chaviwd1c23182019-12-20 18:44:56 -08004947 expectedDisplayId, expectedFlags);
4948 }
4949
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004950 void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08004951 mInputReceiver->consumeMotionEvent(
4952 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
4953 WithDisplayId(expectedDisplayId),
4954 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004955 }
4956
Arthur Hungfbfa5722021-11-16 02:45:54 +00004957 void consumeMotionPointerDown(int32_t pointerIdx) {
4958 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
4959 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004960 mInputReceiver->consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00004961 /*expectedFlags=*/0);
Arthur Hungfbfa5722021-11-16 02:45:54 +00004962 }
4963
Evan Rosky84f07f02021-04-16 10:42:42 -07004964 MotionEvent* consumeMotion() {
4965 InputEvent* event = mInputReceiver->consume();
4966 if (!event) {
4967 ADD_FAILURE() << "No event was produced";
4968 return nullptr;
4969 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004970 if (event->getType() != InputEventType::MOTION) {
4971 ADD_FAILURE() << "Expected MotionEvent, got " << *event;
Evan Rosky84f07f02021-04-16 10:42:42 -07004972 return nullptr;
4973 }
4974 return static_cast<MotionEvent*>(event);
4975 }
4976
chaviwd1c23182019-12-20 18:44:56 -08004977 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
4978
4979private:
4980 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00004981};
4982
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004983using InputDispatcherMonitorTest = InputDispatcherTest;
4984
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004985/**
4986 * Two entities that receive touch: A window, and a global monitor.
4987 * The touch goes to the window, and then the window disappears.
4988 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
4989 * for the monitor, as well.
4990 * 1. foregroundWindow
4991 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
4992 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004993TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004994 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4995 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004996 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004997
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004998 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004999
5000 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5001 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5002 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5003 {100, 200}))
5004 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5005
5006 // Both the foreground window and the global monitor should receive the touch down
5007 window->consumeMotionDown();
5008 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
5009
5010 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5011 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5012 ADISPLAY_ID_DEFAULT, {110, 200}))
5013 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5014
5015 window->consumeMotionMove();
5016 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
5017
5018 // Now the foreground window goes away
5019 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
5020 window->consumeMotionCancel();
5021 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
5022
5023 // If more events come in, there will be no more foreground window to send them to. This will
5024 // cause a cancel for the monitor, as well.
5025 ASSERT_EQ(InputEventInjectionResult::FAILED,
5026 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5027 ADISPLAY_ID_DEFAULT, {120, 200}))
5028 << "Injection should fail because the window was removed";
5029 window->assertNoEvents();
5030 // Global monitor now gets the cancel
5031 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5032}
5033
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005034TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07005035 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005036 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5037 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005038 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00005039
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005040 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005041
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005042 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00005043 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005044 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00005045 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08005046 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005047}
5048
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005049TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
5050 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005051
Chris Yea209fde2020-07-22 13:54:51 -07005052 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005053 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5054 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005055 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00005056
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005057 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00005058 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005059 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08005060 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005061 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005062
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005063 // Pilfer pointers from the monitor.
5064 // This should not do anything and the window should continue to receive events.
5065 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00005066
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005067 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005068 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5069 ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005070 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005071
5072 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
5073 window->consumeMotionMove(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005074}
5075
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005076TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07005077 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005078 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5079 "Fake Window", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07005080 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5081 window->setWindowOffset(20, 40);
5082 window->setWindowTransform(0, 1, -1, 0);
5083
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005084 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07005085
5086 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5087 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5088 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5089 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5090 MotionEvent* event = monitor.consumeMotion();
5091 // Even though window has transform, gesture monitor must not.
5092 ASSERT_EQ(ui::Transform(), event->getTransform());
5093}
5094
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005095TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00005096 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005097 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00005098
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005099 ASSERT_EQ(InputEventInjectionResult::FAILED,
Arthur Hungb3307ee2021-10-14 10:57:37 +00005100 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005101 << "Injection should fail if there is a monitor, but no touchable window";
5102 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00005103}
5104
chaviw81e2bb92019-12-18 15:03:51 -08005105TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005106 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005107 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5108 "Fake Window", ADISPLAY_ID_DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08005109
Arthur Hung72d8dc32020-03-28 00:48:39 +00005110 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08005111
5112 NotifyMotionArgs motionArgs =
5113 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5114 ADISPLAY_ID_DEFAULT);
5115
Prabir Pradhan678438e2023-04-13 19:32:51 +00005116 mDispatcher->notifyMotion(motionArgs);
chaviw81e2bb92019-12-18 15:03:51 -08005117 // Window should receive motion down event.
5118 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5119
5120 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08005121 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08005122 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5123 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
5124 motionArgs.pointerCoords[0].getX() - 10);
5125
Prabir Pradhan678438e2023-04-13 19:32:51 +00005126 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005127 window->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005128 /*expectedFlags=*/0);
chaviw81e2bb92019-12-18 15:03:51 -08005129}
5130
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005131/**
5132 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
5133 * the device default right away. In the test scenario, we check both the default value,
5134 * and the action of enabling / disabling.
5135 */
5136TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07005137 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005138 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5139 "Test window", ADISPLAY_ID_DEFAULT);
Antonio Kantekea47acb2021-12-23 12:41:25 -08005140 const WindowInfo& windowInfo = *window->getInfo();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005141
5142 // Set focused application.
5143 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005144 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005145
5146 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00005147 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005148 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005149 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005150
5151 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07005152 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005153 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00005154 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005155
5156 SCOPED_TRACE("Disable touch mode");
Antonio Kantekea47acb2021-12-23 12:41:25 -08005157 mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00005158 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005159 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07005160 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005161 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005162 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005163 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005164
5165 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07005166 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005167 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00005168 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005169
5170 SCOPED_TRACE("Enable touch mode again");
Antonio Kantekea47acb2021-12-23 12:41:25 -08005171 mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00005172 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005173 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07005174 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005175 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005176 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005177 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005178
5179 window->assertNoEvents();
5180}
5181
Gang Wange9087892020-01-07 12:17:14 -05005182TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005183 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005184 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5185 "Test window", ADISPLAY_ID_DEFAULT);
Gang Wange9087892020-01-07 12:17:14 -05005186
5187 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005188 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05005189
Arthur Hung72d8dc32020-03-28 00:48:39 +00005190 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005191 setFocusedWindow(window);
5192
Harry Cutts33476232023-01-30 19:57:29 +00005193 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Gang Wange9087892020-01-07 12:17:14 -05005194
Prabir Pradhan678438e2023-04-13 19:32:51 +00005195 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
5196 mDispatcher->notifyKey(keyArgs);
Gang Wange9087892020-01-07 12:17:14 -05005197
5198 InputEvent* event = window->consume();
5199 ASSERT_NE(event, nullptr);
5200
5201 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
5202 ASSERT_NE(verified, nullptr);
5203 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
5204
5205 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
5206 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
5207 ASSERT_EQ(keyArgs.source, verified->source);
5208 ASSERT_EQ(keyArgs.displayId, verified->displayId);
5209
5210 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
5211
5212 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05005213 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005214 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05005215 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
5216 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
5217 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
5218 ASSERT_EQ(0, verifiedKey.repeatCount);
5219}
5220
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005221TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005222 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005223 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5224 "Test window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005225
5226 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5227
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07005228 ui::Transform transform;
5229 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
5230
5231 gui::DisplayInfo displayInfo;
5232 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
5233 displayInfo.transform = transform;
5234
Patrick Williamsd828f302023-04-28 17:52:08 -05005235 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005236
Prabir Pradhan678438e2023-04-13 19:32:51 +00005237 const NotifyMotionArgs motionArgs =
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005238 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5239 ADISPLAY_ID_DEFAULT);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005240 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005241
5242 InputEvent* event = window->consume();
5243 ASSERT_NE(event, nullptr);
5244
5245 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
5246 ASSERT_NE(verified, nullptr);
5247 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
5248
5249 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
5250 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
5251 EXPECT_EQ(motionArgs.source, verified->source);
5252 EXPECT_EQ(motionArgs.displayId, verified->displayId);
5253
5254 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
5255
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07005256 const vec2 rawXY =
5257 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
5258 motionArgs.pointerCoords[0].getXYValue());
5259 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
5260 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005261 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005262 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005263 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005264 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
5265 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
5266}
5267
chaviw09c8d2d2020-08-24 15:48:26 -07005268/**
5269 * Ensure that separate calls to sign the same data are generating the same key.
5270 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
5271 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
5272 * tests.
5273 */
5274TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
5275 KeyEvent event = getTestKeyEvent();
5276 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5277
5278 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
5279 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
5280 ASSERT_EQ(hmac1, hmac2);
5281}
5282
5283/**
5284 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
5285 */
5286TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
5287 KeyEvent event = getTestKeyEvent();
5288 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5289 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
5290
5291 verifiedEvent.deviceId += 1;
5292 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5293
5294 verifiedEvent.source += 1;
5295 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5296
5297 verifiedEvent.eventTimeNanos += 1;
5298 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5299
5300 verifiedEvent.displayId += 1;
5301 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5302
5303 verifiedEvent.action += 1;
5304 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5305
5306 verifiedEvent.downTimeNanos += 1;
5307 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5308
5309 verifiedEvent.flags += 1;
5310 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5311
5312 verifiedEvent.keyCode += 1;
5313 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5314
5315 verifiedEvent.scanCode += 1;
5316 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5317
5318 verifiedEvent.metaState += 1;
5319 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5320
5321 verifiedEvent.repeatCount += 1;
5322 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5323}
5324
Vishnu Nair958da932020-08-21 17:12:37 -07005325TEST_F(InputDispatcherTest, SetFocusedWindow) {
5326 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5327 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005328 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005329 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005330 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005331 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5332
5333 // Top window is also focusable but is not granted focus.
5334 windowTop->setFocusable(true);
5335 windowSecond->setFocusable(true);
5336 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5337 setFocusedWindow(windowSecond);
5338
5339 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005340 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5341 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005342
5343 // Focused window should receive event.
5344 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5345 windowTop->assertNoEvents();
5346}
5347
5348TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
5349 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5350 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005351 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005352 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5353
5354 window->setFocusable(true);
5355 // Release channel for window is no longer valid.
5356 window->releaseChannel();
5357 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5358 setFocusedWindow(window);
5359
5360 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005361 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5362 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005363
5364 // window channel is invalid, so it should not receive any input event.
5365 window->assertNoEvents();
5366}
5367
5368TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
5369 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5370 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005371 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005372 window->setFocusable(false);
Vishnu Nair958da932020-08-21 17:12:37 -07005373 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5374
Vishnu Nair958da932020-08-21 17:12:37 -07005375 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5376 setFocusedWindow(window);
5377
5378 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005379 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5380 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005381
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005382 // window is not focusable, so it should not receive any input event.
Vishnu Nair958da932020-08-21 17:12:37 -07005383 window->assertNoEvents();
5384}
5385
5386TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
5387 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5388 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005389 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005390 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005391 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005392 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5393
5394 windowTop->setFocusable(true);
5395 windowSecond->setFocusable(true);
5396 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5397 setFocusedWindow(windowTop);
5398 windowTop->consumeFocusEvent(true);
5399
Chavi Weingarten847e8512023-03-29 00:26:09 +00005400 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
5401 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005402 windowSecond->consumeFocusEvent(true);
5403 windowTop->consumeFocusEvent(false);
5404
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005405 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5406 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005407
5408 // Focused window should receive event.
5409 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5410}
5411
Chavi Weingarten847e8512023-03-29 00:26:09 +00005412TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) {
Vishnu Nair958da932020-08-21 17:12:37 -07005413 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5414 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005415 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005416 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005417 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005418 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5419
5420 windowTop->setFocusable(true);
Chavi Weingarten847e8512023-03-29 00:26:09 +00005421 windowSecond->setFocusable(false);
5422 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
Vishnu Nair958da932020-08-21 17:12:37 -07005423 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Chavi Weingarten847e8512023-03-29 00:26:09 +00005424 setFocusedWindow(windowTop);
5425 windowTop->consumeFocusEvent(true);
Vishnu Nair958da932020-08-21 17:12:37 -07005426
Chavi Weingarten847e8512023-03-29 00:26:09 +00005427 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5428 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005429
5430 // Event should be dropped.
Chavi Weingarten847e8512023-03-29 00:26:09 +00005431 windowTop->consumeKeyDown(ADISPLAY_ID_NONE);
Vishnu Nair958da932020-08-21 17:12:37 -07005432 windowSecond->assertNoEvents();
5433}
5434
5435TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
5436 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5437 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005438 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005439 sp<FakeWindowHandle> previousFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005440 sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow",
5441 ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005442 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5443
5444 window->setFocusable(true);
5445 previousFocusedWindow->setFocusable(true);
5446 window->setVisible(false);
5447 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
5448 setFocusedWindow(previousFocusedWindow);
5449 previousFocusedWindow->consumeFocusEvent(true);
5450
5451 // Requesting focus on invisible window takes focus from currently focused window.
5452 setFocusedWindow(window);
5453 previousFocusedWindow->consumeFocusEvent(false);
5454
5455 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005456 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005457 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
5458 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07005459
5460 // Window does not get focus event or key down.
5461 window->assertNoEvents();
5462
5463 // Window becomes visible.
5464 window->setVisible(true);
5465 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5466
5467 // Window receives focus event.
5468 window->consumeFocusEvent(true);
5469 // Focused window receives key down.
5470 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5471}
5472
Vishnu Nair599f1412021-06-21 10:39:58 -07005473TEST_F(InputDispatcherTest, DisplayRemoved) {
5474 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5475 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005476 sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
Vishnu Nair599f1412021-06-21 10:39:58 -07005477 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5478
5479 // window is granted focus.
5480 window->setFocusable(true);
5481 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5482 setFocusedWindow(window);
5483 window->consumeFocusEvent(true);
5484
5485 // When a display is removed window loses focus.
5486 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
5487 window->consumeFocusEvent(false);
5488}
5489
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005490/**
5491 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
5492 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
5493 * of the 'slipperyEnterWindow'.
5494 *
5495 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
5496 * a way so that the touched location is no longer covered by the top window.
5497 *
5498 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
5499 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
5500 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
5501 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
5502 * with ACTION_DOWN).
5503 * Thus, the touch has been transferred from the top window into the bottom window, because the top
5504 * window moved itself away from the touched location and had Flag::SLIPPERY.
5505 *
5506 * Even though the top window moved away from the touched location, it is still obscuring the bottom
5507 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
5508 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
5509 *
5510 * In this test, we ensure that the event received by the bottom window has
5511 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
5512 */
5513TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00005514 constexpr gui::Pid SLIPPERY_PID{WINDOW_PID.val() + 1};
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00005515 constexpr gui::Uid SLIPPERY_UID{WINDOW_UID.val() + 1};
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005516
5517 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5518 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5519
5520 sp<FakeWindowHandle> slipperyExitWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005521 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005522 slipperyExitWindow->setSlippery(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005523 // Make sure this one overlaps the bottom window
5524 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
5525 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
5526 // one. Windows with the same owner are not considered to be occluding each other.
5527 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
5528
5529 sp<FakeWindowHandle> slipperyEnterWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005530 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005531 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
5532
5533 mDispatcher->setInputWindows(
5534 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5535
5536 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
Prabir Pradhan678438e2023-04-13 19:32:51 +00005537 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5538 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5539 {{50, 50}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005540 slipperyExitWindow->consumeMotionDown();
5541 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
5542 mDispatcher->setInputWindows(
5543 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5544
Prabir Pradhan678438e2023-04-13 19:32:51 +00005545 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE,
5546 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5547 {{51, 51}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005548
5549 slipperyExitWindow->consumeMotionCancel();
5550
5551 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
5552 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
5553}
5554
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07005555/**
5556 * Two windows, one on the left and another on the right. The left window is slippery. The right
5557 * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the
5558 * touch moves from the left window into the right window, the gesture should continue to go to the
5559 * left window. Touch shouldn't slip because the right window can't receive touches. This test
5560 * reproduces a crash.
5561 */
5562TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) {
5563 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5564
5565 sp<FakeWindowHandle> leftSlipperyWindow =
5566 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
5567 leftSlipperyWindow->setSlippery(true);
5568 leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100));
5569
5570 sp<FakeWindowHandle> rightDropTouchesWindow =
5571 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
5572 rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100));
5573 rightDropTouchesWindow->setDropInput(true);
5574
5575 mDispatcher->setInputWindows(
5576 {{ADISPLAY_ID_DEFAULT, {leftSlipperyWindow, rightDropTouchesWindow}}});
5577
5578 // Start touch in the left window
5579 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5580 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
5581 .build());
5582 leftSlipperyWindow->consumeMotionDown();
5583
5584 // And move it into the right window
5585 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
5586 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
5587 .build());
5588
5589 // Since the right window isn't eligible to receive input, touch does not slip.
5590 // The left window continues to receive the gesture.
5591 leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
5592 rightDropTouchesWindow->assertNoEvents();
5593}
5594
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005595TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithMotions) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00005596 using Uid = gui::Uid;
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005597 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5598
5599 sp<FakeWindowHandle> leftWindow =
5600 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
5601 leftWindow->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00005602 leftWindow->setOwnerInfo(gui::Pid{1}, Uid{101});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005603
5604 sp<FakeWindowHandle> rightSpy =
5605 sp<FakeWindowHandle>::make(application, mDispatcher, "Right spy", ADISPLAY_ID_DEFAULT);
5606 rightSpy->setFrame(Rect(100, 0, 200, 100));
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00005607 rightSpy->setOwnerInfo(gui::Pid{2}, Uid{102});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005608 rightSpy->setSpy(true);
5609 rightSpy->setTrustedOverlay(true);
5610
5611 sp<FakeWindowHandle> rightWindow =
5612 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
5613 rightWindow->setFrame(Rect(100, 0, 200, 100));
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00005614 rightWindow->setOwnerInfo(gui::Pid{3}, Uid{103});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005615
5616 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightSpy, rightWindow, leftWindow}}});
5617
5618 // Touch in the left window
5619 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5620 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
5621 .build());
5622 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionDown());
5623 mDispatcher->waitForIdle();
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00005624 ASSERT_NO_FATAL_FAILURE(
5625 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{101}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005626
5627 // Touch another finger over the right windows
5628 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5629 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
5630 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
5631 .build());
5632 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionDown());
5633 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionDown());
5634 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionMove());
5635 mDispatcher->waitForIdle();
5636 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00005637 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID,
5638 {Uid{101}, Uid{102}, Uid{103}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005639
5640 // Release finger over left window. The UP actions are not treated as device interaction.
5641 // The windows that did not receive the UP pointer will receive MOVE events, but since this
5642 // is part of the UP action, we do not treat this as device interaction.
5643 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
5644 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
5645 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
5646 .build());
5647 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionUp());
5648 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove());
5649 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove());
5650 mDispatcher->waitForIdle();
5651 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
5652
5653 // Move remaining finger
5654 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
5655 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
5656 .build());
5657 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove());
5658 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove());
5659 mDispatcher->waitForIdle();
5660 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00005661 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{102}, Uid{103}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005662
5663 // Release all fingers
5664 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
5665 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
5666 .build());
5667 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionUp());
5668 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionUp());
5669 mDispatcher->waitForIdle();
5670 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
5671}
5672
5673TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithKeys) {
5674 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5675
5676 sp<FakeWindowHandle> window =
5677 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
5678 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00005679 window->setOwnerInfo(gui::Pid{1}, gui::Uid{101});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005680
5681 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5682 setFocusedWindow(window);
5683 ASSERT_NO_FATAL_FAILURE(window->consumeFocusEvent(true));
5684
5685 mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).build());
5686 ASSERT_NO_FATAL_FAILURE(window->consumeKeyDown(ADISPLAY_ID_DEFAULT));
5687 mDispatcher->waitForIdle();
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00005688 ASSERT_NO_FATAL_FAILURE(
5689 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {gui::Uid{101}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005690
5691 // The UP actions are not treated as device interaction.
5692 mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).build());
5693 ASSERT_NO_FATAL_FAILURE(window->consumeKeyUp(ADISPLAY_ID_DEFAULT));
5694 mDispatcher->waitForIdle();
5695 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
5696}
5697
Garfield Tan1c7bc862020-01-28 13:24:04 -08005698class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
5699protected:
5700 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
5701 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
5702
Chris Yea209fde2020-07-22 13:54:51 -07005703 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005704 sp<FakeWindowHandle> mWindow;
5705
5706 virtual void SetUp() override {
Prabir Pradhana41d2442023-04-20 21:30:40 +00005707 mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005708 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Prabir Pradhana41d2442023-04-20 21:30:40 +00005709 mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy);
Prabir Pradhan87112a72023-04-20 19:13:39 +00005710 mDispatcher->requestRefreshConfiguration();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005711 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
5712 ASSERT_EQ(OK, mDispatcher->start());
5713
5714 setUpWindow();
5715 }
5716
5717 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07005718 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005719 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005720
Vishnu Nair47074b82020-08-14 11:54:47 -07005721 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005722 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005723 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005724 mWindow->consumeFocusEvent(true);
5725 }
5726
Chris Ye2ad95392020-09-01 13:44:44 -07005727 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005728 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005729 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005730 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005731 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005732
5733 // Window should receive key down event.
5734 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5735 }
5736
5737 void expectKeyRepeatOnce(int32_t repeatCount) {
5738 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
5739 InputEvent* repeatEvent = mWindow->consume();
5740 ASSERT_NE(nullptr, repeatEvent);
5741
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005742 ASSERT_EQ(InputEventType::KEY, repeatEvent->getType());
Garfield Tan1c7bc862020-01-28 13:24:04 -08005743
5744 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
5745 uint32_t eventAction = repeatKeyEvent->getAction();
5746 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
5747 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
5748 }
5749
Chris Ye2ad95392020-09-01 13:44:44 -07005750 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005751 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005752 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005753 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005754 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005755
5756 // Window should receive key down event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005757 mWindow->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005758 /*expectedFlags=*/0);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005759 }
5760};
5761
5762TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Harry Cutts33476232023-01-30 19:57:29 +00005763 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005764 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5765 expectKeyRepeatOnce(repeatCount);
5766 }
5767}
5768
5769TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
Harry Cutts33476232023-01-30 19:57:29 +00005770 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005771 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5772 expectKeyRepeatOnce(repeatCount);
5773 }
Harry Cutts33476232023-01-30 19:57:29 +00005774 sendAndConsumeKeyDown(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005775 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08005776 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5777 expectKeyRepeatOnce(repeatCount);
5778 }
5779}
5780
5781TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005782 sendAndConsumeKeyDown(/*deviceId=*/1);
5783 expectKeyRepeatOnce(/*repeatCount=*/1);
5784 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005785 mWindow->assertNoEvents();
5786}
5787
5788TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005789 sendAndConsumeKeyDown(/*deviceId=*/1);
5790 expectKeyRepeatOnce(/*repeatCount=*/1);
5791 sendAndConsumeKeyDown(/*deviceId=*/2);
5792 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005793 // Stale key up from device 1.
Harry Cutts33476232023-01-30 19:57:29 +00005794 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005795 // Device 2 is still down, keep repeating
Harry Cutts33476232023-01-30 19:57:29 +00005796 expectKeyRepeatOnce(/*repeatCount=*/2);
5797 expectKeyRepeatOnce(/*repeatCount=*/3);
Chris Ye2ad95392020-09-01 13:44:44 -07005798 // Device 2 key up
Harry Cutts33476232023-01-30 19:57:29 +00005799 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005800 mWindow->assertNoEvents();
5801}
5802
5803TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005804 sendAndConsumeKeyDown(/*deviceId=*/1);
5805 expectKeyRepeatOnce(/*repeatCount=*/1);
5806 sendAndConsumeKeyDown(/*deviceId=*/2);
5807 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005808 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
Harry Cutts33476232023-01-30 19:57:29 +00005809 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005810 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08005811 mWindow->assertNoEvents();
5812}
5813
liushenxiang42232912021-05-21 20:24:09 +08005814TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
5815 sendAndConsumeKeyDown(DEVICE_ID);
Harry Cutts33476232023-01-30 19:57:29 +00005816 expectKeyRepeatOnce(/*repeatCount=*/1);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005817 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
liushenxiang42232912021-05-21 20:24:09 +08005818 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
5819 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
5820 mWindow->assertNoEvents();
5821}
5822
Garfield Tan1c7bc862020-01-28 13:24:04 -08005823TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005824 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005825 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005826 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5827 InputEvent* repeatEvent = mWindow->consume();
5828 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5829 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
5830 IdGenerator::getSource(repeatEvent->getId()));
5831 }
5832}
5833
5834TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005835 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005836 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005837
5838 std::unordered_set<int32_t> idSet;
5839 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5840 InputEvent* repeatEvent = mWindow->consume();
5841 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5842 int32_t id = repeatEvent->getId();
5843 EXPECT_EQ(idSet.end(), idSet.find(id));
5844 idSet.insert(id);
5845 }
5846}
5847
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005848/* Test InputDispatcher for MultiDisplay */
5849class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
5850public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005851 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005852 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08005853
Chris Yea209fde2020-07-22 13:54:51 -07005854 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005855 windowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005856 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005857
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005858 // Set focus window for primary display, but focused display would be second one.
5859 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07005860 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005861 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005862 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005863 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08005864
Chris Yea209fde2020-07-22 13:54:51 -07005865 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005866 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005867 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005868 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005869 // Set focus display to second one.
5870 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
5871 // Set focus window for second display.
5872 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07005873 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005874 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005875 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005876 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005877 }
5878
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005879 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005880 InputDispatcherTest::TearDown();
5881
Chris Yea209fde2020-07-22 13:54:51 -07005882 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005883 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07005884 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005885 windowInSecondary.clear();
5886 }
5887
5888protected:
Chris Yea209fde2020-07-22 13:54:51 -07005889 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005890 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07005891 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005892 sp<FakeWindowHandle> windowInSecondary;
5893};
5894
5895TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
5896 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005897 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5898 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5899 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005900 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08005901 windowInSecondary->assertNoEvents();
5902
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005903 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005904 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5905 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5906 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005907 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005908 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08005909}
5910
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005911TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08005912 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005913 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5914 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005915 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005916 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08005917 windowInSecondary->assertNoEvents();
5918
5919 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005920 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005921 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005922 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005923 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08005924
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005925 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00005926 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08005927
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005928 // Old focus should receive a cancel event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005929 windowInSecondary->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005930 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08005931
5932 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005933 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005934 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08005935 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005936 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08005937 windowInSecondary->assertNoEvents();
5938}
5939
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005940// Test per-display input monitors for motion event.
5941TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08005942 FakeMonitorReceiver monitorInPrimary =
5943 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5944 FakeMonitorReceiver monitorInSecondary =
5945 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005946
5947 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005948 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5949 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5950 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005951 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08005952 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005953 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005954 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005955
5956 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005957 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5958 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5959 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005960 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005961 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005962 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08005963 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005964
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08005965 // Lift up the touch from the second display
5966 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5967 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5968 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5969 windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID);
5970 monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID);
5971
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005972 // Test inject a non-pointer motion event.
5973 // If specific a display, it will dispatch to the focused window of particular display,
5974 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005975 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5976 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
5977 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005978 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005979 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005980 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08005981 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005982}
5983
5984// Test per-display input monitors for key event.
5985TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005986 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08005987 FakeMonitorReceiver monitorInPrimary =
5988 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5989 FakeMonitorReceiver monitorInSecondary =
5990 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005991
5992 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005993 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5994 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005995 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005996 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005997 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08005998 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005999}
6000
Vishnu Nair958da932020-08-21 17:12:37 -07006001TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
6002 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006003 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006004 secondWindowInPrimary->setFocusable(true);
6005 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
6006 setFocusedWindow(secondWindowInPrimary);
6007 windowInPrimary->consumeFocusEvent(false);
6008 secondWindowInPrimary->consumeFocusEvent(true);
6009
6010 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006011 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
6012 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006013 windowInPrimary->assertNoEvents();
6014 windowInSecondary->assertNoEvents();
6015 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6016}
6017
Arthur Hungdfd528e2021-12-08 13:23:04 +00006018TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
6019 FakeMonitorReceiver monitorInPrimary =
6020 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6021 FakeMonitorReceiver monitorInSecondary =
6022 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
6023
6024 // Test touch down on primary display.
6025 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6026 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
6027 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6028 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6029 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
6030
6031 // Test touch down on second display.
6032 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6033 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
6034 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6035 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
6036 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
6037
6038 // Trigger cancel touch.
6039 mDispatcher->cancelCurrentTouch();
6040 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6041 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6042 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
6043 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
6044
6045 // Test inject a move motion event, no window/monitor should receive the event.
6046 ASSERT_EQ(InputEventInjectionResult::FAILED,
6047 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6048 ADISPLAY_ID_DEFAULT, {110, 200}))
6049 << "Inject motion event should return InputEventInjectionResult::FAILED";
6050 windowInPrimary->assertNoEvents();
6051 monitorInPrimary.assertNoEvents();
6052
6053 ASSERT_EQ(InputEventInjectionResult::FAILED,
6054 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6055 SECOND_DISPLAY_ID, {110, 200}))
6056 << "Inject motion event should return InputEventInjectionResult::FAILED";
6057 windowInSecondary->assertNoEvents();
6058 monitorInSecondary.assertNoEvents();
6059}
6060
Jackal Guof9696682018-10-05 12:23:23 +08006061class InputFilterTest : public InputDispatcherTest {
6062protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006063 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
6064 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08006065 NotifyMotionArgs motionArgs;
6066
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006067 motionArgs =
6068 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006069 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006070 motionArgs =
6071 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006072 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006073 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08006074 if (expectToBeFiltered) {
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006075 const auto xy = transform.transform(motionArgs.pointerCoords[0].getXYValue());
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006076 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08006077 } else {
6078 mFakePolicy->assertFilterInputEventWasNotCalled();
6079 }
6080 }
6081
6082 void testNotifyKey(bool expectToBeFiltered) {
6083 NotifyKeyArgs keyArgs;
6084
6085 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006086 mDispatcher->notifyKey(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08006087 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006088 mDispatcher->notifyKey(keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006089 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08006090
6091 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08006092 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08006093 } else {
6094 mFakePolicy->assertFilterInputEventWasNotCalled();
6095 }
6096 }
6097};
6098
6099// Test InputFilter for MotionEvent
6100TEST_F(InputFilterTest, MotionEvent_InputFilter) {
6101 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
6102 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
6103 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
6104
6105 // Enable InputFilter
6106 mDispatcher->setInputFilterEnabled(true);
6107 // Test touch on both primary and second display, and check if both events are filtered.
6108 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
6109 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
6110
6111 // Disable InputFilter
6112 mDispatcher->setInputFilterEnabled(false);
6113 // Test touch on both primary and second display, and check if both events aren't filtered.
6114 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
6115 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
6116}
6117
6118// Test InputFilter for KeyEvent
6119TEST_F(InputFilterTest, KeyEvent_InputFilter) {
6120 // Since the InputFilter is disabled by default, check if key event aren't filtered.
6121 testNotifyKey(/*expectToBeFiltered*/ false);
6122
6123 // Enable InputFilter
6124 mDispatcher->setInputFilterEnabled(true);
6125 // Send a key event, and check if it is filtered.
6126 testNotifyKey(/*expectToBeFiltered*/ true);
6127
6128 // Disable InputFilter
6129 mDispatcher->setInputFilterEnabled(false);
6130 // Send a key event, and check if it isn't filtered.
6131 testNotifyKey(/*expectToBeFiltered*/ false);
6132}
6133
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006134// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
6135// logical display coordinate space.
6136TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
6137 ui::Transform firstDisplayTransform;
6138 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
6139 ui::Transform secondDisplayTransform;
6140 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
6141
6142 std::vector<gui::DisplayInfo> displayInfos(2);
6143 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
6144 displayInfos[0].transform = firstDisplayTransform;
6145 displayInfos[1].displayId = SECOND_DISPLAY_ID;
6146 displayInfos[1].transform = secondDisplayTransform;
6147
Patrick Williamsd828f302023-04-28 17:52:08 -05006148 mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0});
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006149
6150 // Enable InputFilter
6151 mDispatcher->setInputFilterEnabled(true);
6152
6153 // Ensure the correct transforms are used for the displays.
6154 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform);
6155 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform);
6156}
6157
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006158class InputFilterInjectionPolicyTest : public InputDispatcherTest {
6159protected:
6160 virtual void SetUp() override {
6161 InputDispatcherTest::SetUp();
6162
6163 /**
6164 * We don't need to enable input filter to test the injected event policy, but we enabled it
6165 * here to make the tests more realistic, since this policy only matters when inputfilter is
6166 * on.
6167 */
6168 mDispatcher->setInputFilterEnabled(true);
6169
6170 std::shared_ptr<InputApplicationHandle> application =
6171 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006172 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
6173 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006174
6175 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6176 mWindow->setFocusable(true);
6177 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6178 setFocusedWindow(mWindow);
6179 mWindow->consumeFocusEvent(true);
6180 }
6181
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006182 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
6183 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006184 KeyEvent event;
6185
6186 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
6187 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
6188 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
Harry Cutts33476232023-01-30 19:57:29 +00006189 KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006190 const int32_t additionalPolicyFlags =
6191 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
6192 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006193 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006194 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
6195 policyFlags | additionalPolicyFlags));
6196
6197 InputEvent* received = mWindow->consume();
6198 ASSERT_NE(nullptr, received);
6199 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006200 ASSERT_EQ(received->getType(), InputEventType::KEY);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006201 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
6202 ASSERT_EQ(flags, keyEvent.getFlags());
6203 }
6204
6205 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
6206 int32_t flags) {
6207 MotionEvent event;
6208 PointerProperties pointerProperties[1];
6209 PointerCoords pointerCoords[1];
6210 pointerProperties[0].clear();
6211 pointerProperties[0].id = 0;
6212 pointerCoords[0].clear();
6213 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
6214 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
6215
6216 ui::Transform identityTransform;
6217 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
6218 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
6219 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
6220 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
6221 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07006222 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07006223 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006224 /*pointerCount*/ 1, pointerProperties, pointerCoords);
6225
6226 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
6227 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006228 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006229 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
6230 policyFlags | additionalPolicyFlags));
6231
6232 InputEvent* received = mWindow->consume();
6233 ASSERT_NE(nullptr, received);
6234 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006235 ASSERT_EQ(received->getType(), InputEventType::MOTION);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006236 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
6237 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006238 }
6239
6240private:
6241 sp<FakeWindowHandle> mWindow;
6242};
6243
6244TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006245 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
6246 // filter. Without it, the event will no different from a regularly injected event, and the
6247 // injected device id will be overwritten.
Harry Cutts33476232023-01-30 19:57:29 +00006248 testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
6249 /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006250}
6251
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006252TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006253 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00006254 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006255 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
6256}
6257
6258TEST_F(InputFilterInjectionPolicyTest,
6259 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
6260 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00006261 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006262 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006263}
6264
6265TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
Harry Cutts33476232023-01-30 19:57:29 +00006266 testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3,
6267 /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006268}
6269
chaviwfd6d3512019-03-25 13:23:49 -07006270class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006271 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07006272 InputDispatcherTest::SetUp();
6273
Chris Yea209fde2020-07-22 13:54:51 -07006274 std::shared_ptr<FakeApplicationHandle> application =
6275 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006276 mUnfocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006277 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07006278 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07006279
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006280 mFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006281 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006282 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07006283
6284 // Set focused application.
6285 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07006286 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07006287
6288 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00006289 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006290 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006291 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07006292 }
6293
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006294 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07006295 InputDispatcherTest::TearDown();
6296
6297 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006298 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07006299 }
6300
6301protected:
6302 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006303 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006304 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07006305};
6306
6307// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
6308// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
6309// the onPointerDownOutsideFocus callback.
6310TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006311 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006312 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6313 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006314 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006315 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006316
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006317 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07006318 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
6319}
6320
6321// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
6322// DOWN on the window that doesn't have focus. Ensure no window received the
6323// onPointerDownOutsideFocus callback.
6324TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006325 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006326 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006327 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006328 mFocusedWindow->consumeMotionDown();
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 KeyEvent with action DOWN on the window that doesn't
6335// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
6336TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006337 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6338 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006339 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006340 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07006341
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006342 ASSERT_TRUE(mDispatcher->waitForIdle());
6343 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006344}
6345
6346// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
6347// DOWN on the window that already has focus. Ensure no window received the
6348// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006349TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006350 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006351 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006352 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006353 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006354 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006355
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006356 ASSERT_TRUE(mDispatcher->waitForIdle());
6357 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006358}
6359
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006360// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
6361// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
6362TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
6363 const MotionEvent event =
6364 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
6365 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006366 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006367 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
6368 .build();
6369 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
6370 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6371 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
6372
6373 ASSERT_TRUE(mDispatcher->waitForIdle());
6374 mFakePolicy->assertOnPointerDownWasNotCalled();
6375 // Ensure that the unfocused window did not receive any FOCUS events.
6376 mUnfocusedWindow->assertNoEvents();
6377}
6378
chaviwaf87b3e2019-10-01 16:59:28 -07006379// These tests ensures we can send touch events to a single client when there are multiple input
6380// windows that point to the same client token.
6381class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
6382 virtual void SetUp() override {
6383 InputDispatcherTest::SetUp();
6384
Chris Yea209fde2020-07-22 13:54:51 -07006385 std::shared_ptr<FakeApplicationHandle> application =
6386 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006387 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
6388 ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07006389 mWindow1->setFrame(Rect(0, 0, 100, 100));
6390
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006391 mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2",
6392 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviwaf87b3e2019-10-01 16:59:28 -07006393 mWindow2->setFrame(Rect(100, 100, 200, 200));
6394
Arthur Hung72d8dc32020-03-28 00:48:39 +00006395 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07006396 }
6397
6398protected:
6399 sp<FakeWindowHandle> mWindow1;
6400 sp<FakeWindowHandle> mWindow2;
6401
6402 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05006403 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07006404 vec2 vals = windowInfo->transform.transform(point.x, point.y);
6405 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07006406 }
6407
6408 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
6409 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006410 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07006411 InputEvent* event = window->consume();
6412
6413 ASSERT_NE(nullptr, event) << name.c_str()
6414 << ": consumer should have returned non-NULL event.";
6415
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006416 ASSERT_EQ(InputEventType::MOTION, event->getType())
6417 << name.c_str() << ": expected MotionEvent, got " << *event;
chaviwaf87b3e2019-10-01 16:59:28 -07006418
6419 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006420 assertMotionAction(expectedAction, motionEvent.getAction());
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08006421 ASSERT_EQ(points.size(), motionEvent.getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07006422
6423 for (size_t i = 0; i < points.size(); i++) {
6424 float expectedX = points[i].x;
6425 float expectedY = points[i].y;
6426
6427 EXPECT_EQ(expectedX, motionEvent.getX(i))
6428 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
6429 << ", got " << motionEvent.getX(i);
6430 EXPECT_EQ(expectedY, motionEvent.getY(i))
6431 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
6432 << ", got " << motionEvent.getY(i);
6433 }
6434 }
chaviw9eaa22c2020-07-01 16:21:27 -07006435
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08006436 void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07006437 std::vector<PointF> expectedPoints) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00006438 mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
6439 ADISPLAY_ID_DEFAULT, touchedPoints));
chaviw9eaa22c2020-07-01 16:21:27 -07006440
6441 // Always consume from window1 since it's the window that has the InputReceiver
6442 consumeMotionEvent(mWindow1, action, expectedPoints);
6443 }
chaviwaf87b3e2019-10-01 16:59:28 -07006444};
6445
6446TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
6447 // Touch Window 1
6448 PointF touchedPoint = {10, 10};
6449 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006450 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006451
6452 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006453 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006454
6455 // Touch Window 2
6456 touchedPoint = {150, 150};
6457 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006458 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006459}
6460
chaviw9eaa22c2020-07-01 16:21:27 -07006461TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
6462 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07006463 mWindow2->setWindowScale(0.5f, 0.5f);
6464
6465 // Touch Window 1
6466 PointF touchedPoint = {10, 10};
6467 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006468 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006469 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006470 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006471
6472 // Touch Window 2
6473 touchedPoint = {150, 150};
6474 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006475 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
6476 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006477
chaviw9eaa22c2020-07-01 16:21:27 -07006478 // Update the transform so rotation is set
6479 mWindow2->setWindowTransform(0, -1, 1, 0);
6480 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
6481 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006482}
6483
chaviw9eaa22c2020-07-01 16:21:27 -07006484TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006485 mWindow2->setWindowScale(0.5f, 0.5f);
6486
6487 // Touch Window 1
6488 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6489 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006490 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006491
6492 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006493 touchedPoints.push_back(PointF{150, 150});
6494 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006495 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006496
chaviw9eaa22c2020-07-01 16:21:27 -07006497 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006498 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006499 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006500
chaviw9eaa22c2020-07-01 16:21:27 -07006501 // Update the transform so rotation is set for Window 2
6502 mWindow2->setWindowTransform(0, -1, 1, 0);
6503 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006504 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006505}
6506
chaviw9eaa22c2020-07-01 16:21:27 -07006507TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006508 mWindow2->setWindowScale(0.5f, 0.5f);
6509
6510 // Touch Window 1
6511 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6512 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006513 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006514
6515 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006516 touchedPoints.push_back(PointF{150, 150});
6517 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006518
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006519 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006520
6521 // Move both windows
6522 touchedPoints = {{20, 20}, {175, 175}};
6523 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6524 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6525
chaviw9eaa22c2020-07-01 16:21:27 -07006526 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006527
chaviw9eaa22c2020-07-01 16:21:27 -07006528 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006529 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006530 expectedPoints.pop_back();
6531
6532 // Touch Window 2
6533 mWindow2->setWindowTransform(0, -1, 1, 0);
6534 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006535 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006536
6537 // Move both windows
6538 touchedPoints = {{20, 20}, {175, 175}};
6539 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6540 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6541
6542 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006543}
6544
6545TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
6546 mWindow1->setWindowScale(0.5f, 0.5f);
6547
6548 // Touch Window 1
6549 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6550 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006551 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006552
6553 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006554 touchedPoints.push_back(PointF{150, 150});
6555 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006556
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006557 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006558
6559 // Move both windows
6560 touchedPoints = {{20, 20}, {175, 175}};
6561 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6562 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6563
chaviw9eaa22c2020-07-01 16:21:27 -07006564 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006565}
6566
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07006567/**
6568 * When one of the windows is slippery, the touch should not slip into the other window with the
6569 * same input channel.
6570 */
6571TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) {
6572 mWindow1->setSlippery(true);
6573 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
6574
6575 // Touch down in window 1
6576 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6577 ADISPLAY_ID_DEFAULT, {{50, 50}}));
6578 consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}});
6579
6580 // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip.
6581 // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN
6582 // getting generated.
6583 mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6584 ADISPLAY_ID_DEFAULT, {{150, 150}}));
6585
6586 consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}});
6587}
6588
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07006589/**
6590 * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and
6591 * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window
6592 * that the pointer is hovering over may have a different transform.
6593 */
6594TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) {
6595 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
6596
6597 // Start hover in window 1
6598 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN,
6599 ADISPLAY_ID_DEFAULT, {{50, 50}}));
6600 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
6601 {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})});
6602
6603 // Move hover to window 2.
6604 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6605 ADISPLAY_ID_DEFAULT, {{150, 150}}));
6606
6607 consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}});
6608 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
6609 {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})});
6610}
6611
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006612class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
6613 virtual void SetUp() override {
6614 InputDispatcherTest::SetUp();
6615
Chris Yea209fde2020-07-22 13:54:51 -07006616 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006617 mApplication->setDispatchingTimeout(20ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006618 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
6619 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006620 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05006621 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07006622 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006623
6624 // Set focused application.
6625 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
6626
6627 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006628 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006629 mWindow->consumeFocusEvent(true);
6630 }
6631
6632 virtual void TearDown() override {
6633 InputDispatcherTest::TearDown();
6634 mWindow.clear();
6635 }
6636
6637protected:
Chris Yea209fde2020-07-22 13:54:51 -07006638 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006639 sp<FakeWindowHandle> mWindow;
6640 static constexpr PointF WINDOW_LOCATION = {20, 20};
6641
6642 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006643 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006644 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6645 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006646 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006647 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6648 WINDOW_LOCATION));
6649 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006650
6651 sp<FakeWindowHandle> addSpyWindow() {
6652 sp<FakeWindowHandle> spy =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006653 sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006654 spy->setTrustedOverlay(true);
6655 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006656 spy->setSpy(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006657 spy->setDispatchingTimeout(30ms);
6658 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}});
6659 return spy;
6660 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006661};
6662
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006663// Send a tap and respond, which should not cause an ANR.
6664TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
6665 tapOnWindow();
6666 mWindow->consumeMotionDown();
6667 mWindow->consumeMotionUp();
6668 ASSERT_TRUE(mDispatcher->waitForIdle());
6669 mFakePolicy->assertNotifyAnrWasNotCalled();
6670}
6671
6672// Send a regular key and respond, which should not cause an ANR.
6673TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006674 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006675 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
6676 ASSERT_TRUE(mDispatcher->waitForIdle());
6677 mFakePolicy->assertNotifyAnrWasNotCalled();
6678}
6679
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006680TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
6681 mWindow->setFocusable(false);
6682 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6683 mWindow->consumeFocusEvent(false);
6684
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006685 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006686 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6687 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
6688 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006689 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006690 // Key will not go to window because we have no focused window.
6691 // The 'no focused window' ANR timer should start instead.
6692
6693 // Now, the focused application goes away.
6694 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
6695 // The key should get dropped and there should be no ANR.
6696
6697 ASSERT_TRUE(mDispatcher->waitForIdle());
6698 mFakePolicy->assertNotifyAnrWasNotCalled();
6699}
6700
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006701// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006702// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
6703// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006704TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006705 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006706 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6707 WINDOW_LOCATION));
6708
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006709 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
6710 ASSERT_TRUE(sequenceNum);
6711 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006712 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006713
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006714 mWindow->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006715 mWindow->consumeMotionEvent(
6716 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006717 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006718 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006719}
6720
6721// Send a key to the app and have the app not respond right away.
6722TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
6723 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006724 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006725 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
6726 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006727 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006728 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006729 ASSERT_TRUE(mDispatcher->waitForIdle());
6730}
6731
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006732// We have a focused application, but no focused window
6733TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006734 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006735 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6736 mWindow->consumeFocusEvent(false);
6737
6738 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006739 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006740 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6741 WINDOW_LOCATION));
6742 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
6743 mDispatcher->waitForIdle();
6744 mFakePolicy->assertNotifyAnrWasNotCalled();
6745
6746 // Once a focused event arrives, we get an ANR for this application
6747 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6748 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006749 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006750 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6751 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006752 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006753 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07006754 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006755 ASSERT_TRUE(mDispatcher->waitForIdle());
6756}
6757
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006758/**
6759 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
6760 * there will not be an ANR.
6761 */
6762TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
6763 mWindow->setFocusable(false);
6764 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6765 mWindow->consumeFocusEvent(false);
6766
6767 KeyEvent event;
6768 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
6769 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
6770
6771 // Define a valid key down event that is stale (too old).
6772 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
6773 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A,
Harry Cutts33476232023-01-30 19:57:29 +00006774 AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006775
6776 const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
6777
6778 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006779 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006780 InputEventInjectionSync::WAIT_FOR_RESULT,
6781 INJECT_EVENT_TIMEOUT, policyFlags);
6782 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
6783 << "Injection should fail because the event is stale";
6784
6785 ASSERT_TRUE(mDispatcher->waitForIdle());
6786 mFakePolicy->assertNotifyAnrWasNotCalled();
6787 mWindow->assertNoEvents();
6788}
6789
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006790// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006791// Make sure that we don't notify policy twice about the same ANR.
6792TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006793 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006794 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6795 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006796
6797 // Once a focused event arrives, we get an ANR for this application
6798 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6799 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006800 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006801 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6802 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006803 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Vishnu Naire4df8752022-09-08 09:17:55 -07006804 const std::chrono::duration appTimeout =
6805 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6806 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006807
Vishnu Naire4df8752022-09-08 09:17:55 -07006808 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006809 // ANR should not be raised again. It is up to policy to do that if it desires.
6810 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006811
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006812 // If we now get a focused window, the ANR should stop, but the policy handles that via
6813 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006814 ASSERT_TRUE(mDispatcher->waitForIdle());
6815}
6816
6817// We have a focused application, but no focused window
6818TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006819 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006820 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6821 mWindow->consumeFocusEvent(false);
6822
6823 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006824 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006825 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006826 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
6827 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006828
Vishnu Naire4df8752022-09-08 09:17:55 -07006829 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6830 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006831
6832 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006833 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006834 ASSERT_TRUE(mDispatcher->waitForIdle());
6835 mWindow->assertNoEvents();
6836}
6837
6838/**
6839 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
6840 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
6841 * If we process 1 of the events, but ANR on the second event with the same timestamp,
6842 * the ANR mechanism should still work.
6843 *
6844 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
6845 * DOWN event, while not responding on the second one.
6846 */
6847TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
6848 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
6849 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6850 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6851 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6852 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006853 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006854
6855 // Now send ACTION_UP, with identical timestamp
6856 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
6857 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6858 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6859 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006860 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006861
6862 // We have now sent down and up. Let's consume first event and then ANR on the second.
6863 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6864 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006865 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006866}
6867
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006868// A spy window can receive an ANR
6869TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
6870 sp<FakeWindowHandle> spy = addSpyWindow();
6871
6872 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6873 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6874 WINDOW_LOCATION));
6875 mWindow->consumeMotionDown();
6876
6877 std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN
6878 ASSERT_TRUE(sequenceNum);
6879 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006880 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006881
6882 spy->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006883 spy->consumeMotionEvent(
6884 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006885 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006886 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006887}
6888
6889// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006890// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006891TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
6892 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006893
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006894 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6895 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006896 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006897 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006898
6899 // Stuck on the ACTION_UP
6900 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006901 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006902
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006903 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006904 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006905 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6906 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006907
6908 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6909 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006910 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006911 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006912 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006913}
6914
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006915// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006916// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006917TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
6918 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006919
6920 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006921 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6922 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006923
6924 mWindow->consumeMotionDown();
6925 // Stuck on the ACTION_UP
6926 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006927 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006928
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006929 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006930 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006931 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6932 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006933
6934 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6935 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006936 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006937 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006938 spy->assertNoEvents();
6939}
6940
6941TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
6942 mDispatcher->setMonitorDispatchingTimeoutForTest(30ms);
6943
6944 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6945
6946 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6947 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6948 WINDOW_LOCATION));
6949
6950 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6951 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
6952 ASSERT_TRUE(consumeSeq);
6953
Prabir Pradhanedd96402022-02-15 01:46:16 -08006954 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006955
6956 monitor.finishEvent(*consumeSeq);
6957 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6958
6959 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006960 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006961}
6962
6963// If a window is unresponsive, then you get anr. if the window later catches up and starts to
6964// process events, you don't get an anr. When the window later becomes unresponsive again, you
6965// get an ANR again.
6966// 1. tap -> block on ACTION_UP -> receive ANR
6967// 2. consume all pending events (= queue becomes healthy again)
6968// 3. tap again -> block on ACTION_UP again -> receive ANR second time
6969TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
6970 tapOnWindow();
6971
6972 mWindow->consumeMotionDown();
6973 // Block on ACTION_UP
6974 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006975 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006976 mWindow->consumeMotionUp(); // Now the connection should be healthy again
6977 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006978 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006979 mWindow->assertNoEvents();
6980
6981 tapOnWindow();
6982 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006983 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006984 mWindow->consumeMotionUp();
6985
6986 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006987 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006988 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006989 mWindow->assertNoEvents();
6990}
6991
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006992// If a connection remains unresponsive for a while, make sure policy is only notified once about
6993// it.
6994TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006995 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006996 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6997 WINDOW_LOCATION));
6998
6999 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007000 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007001 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007002 // 'notifyConnectionUnresponsive' should only be called once per connection
7003 mFakePolicy->assertNotifyAnrWasNotCalled();
7004 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007005 mWindow->consumeMotionDown();
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08007006 mWindow->consumeMotionEvent(
7007 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007008 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007009 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08007010 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007011 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007012}
7013
7014/**
7015 * If a window is processing a motion event, and then a key event comes in, the key event should
7016 * not to to the focused window until the motion is processed.
7017 *
7018 * Warning!!!
7019 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
7020 * and the injection timeout that we specify when injecting the key.
7021 * We must have the injection timeout (10ms) be smaller than
7022 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
7023 *
7024 * If that value changes, this test should also change.
7025 */
7026TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
7027 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
7028 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
7029
7030 tapOnWindow();
7031 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
7032 ASSERT_TRUE(downSequenceNum);
7033 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
7034 ASSERT_TRUE(upSequenceNum);
7035 // Don't finish the events yet, and send a key
7036 // Injection will "succeed" because we will eventually give up and send the key to the focused
7037 // window even if motions are still being processed. But because the injection timeout is short,
7038 // we will receive INJECTION_TIMED_OUT as the result.
7039
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007040 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007041 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007042 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
7043 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007044 // Key will not be sent to the window, yet, because the window is still processing events
7045 // and the key remains pending, waiting for the touch events to be processed
7046 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
7047 ASSERT_FALSE(keySequenceNum);
7048
7049 std::this_thread::sleep_for(500ms);
7050 // if we wait long enough though, dispatcher will give up, and still send the key
7051 // to the focused window, even though we have not yet finished the motion event
7052 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7053 mWindow->finishEvent(*downSequenceNum);
7054 mWindow->finishEvent(*upSequenceNum);
7055}
7056
7057/**
7058 * If a window is processing a motion event, and then a key event comes in, the key event should
7059 * not go to the focused window until the motion is processed.
7060 * If then a new motion comes in, then the pending key event should be going to the currently
7061 * focused window right away.
7062 */
7063TEST_F(InputDispatcherSingleWindowAnr,
7064 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
7065 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
7066 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
7067
7068 tapOnWindow();
7069 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
7070 ASSERT_TRUE(downSequenceNum);
7071 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
7072 ASSERT_TRUE(upSequenceNum);
7073 // Don't finish the events yet, and send a key
7074 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007075 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007076 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7077 InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007078 // At this point, key is still pending, and should not be sent to the application yet.
7079 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
7080 ASSERT_FALSE(keySequenceNum);
7081
7082 // Now tap down again. It should cause the pending key to go to the focused window right away.
7083 tapOnWindow();
7084 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
7085 // the other events yet. We can finish events in any order.
7086 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
7087 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
7088 mWindow->consumeMotionDown();
7089 mWindow->consumeMotionUp();
7090 mWindow->assertNoEvents();
7091}
7092
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07007093/**
7094 * Send an event to the app and have the app not respond right away.
7095 * When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
7096 * So InputDispatcher will enqueue ACTION_CANCEL event as well.
7097 * At some point, the window becomes responsive again.
7098 * Ensure that subsequent events get dropped, and the next gesture is delivered.
7099 */
7100TEST_F(InputDispatcherSingleWindowAnr, TwoGesturesWithAnr) {
7101 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7102 .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10))
7103 .build());
7104
7105 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
7106 ASSERT_TRUE(sequenceNum);
7107 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
7108 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
7109
7110 mWindow->finishEvent(*sequenceNum);
7111 mWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
7112 ASSERT_TRUE(mDispatcher->waitForIdle());
7113 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
7114
7115 // Now that the window is responsive, let's continue the gesture.
7116 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
7117 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
7118 .build());
7119
7120 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7121 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
7122 .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3))
7123 .build());
7124
7125 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
7126 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
7127 .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3))
7128 .build());
7129 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
7130 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
7131 .build());
7132 // We already canceled this pointer, so the window shouldn't get any new events.
7133 mWindow->assertNoEvents();
7134
7135 // Start another one.
7136 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7137 .pointer(PointerBuilder(0, ToolType::FINGER).x(15).y(15))
7138 .build());
7139 mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
7140}
7141
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007142class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
7143 virtual void SetUp() override {
7144 InputDispatcherTest::SetUp();
7145
Chris Yea209fde2020-07-22 13:54:51 -07007146 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007147 mApplication->setDispatchingTimeout(10ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007148 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
7149 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007150 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007151 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007152 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007153
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007154 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
7155 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05007156 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007157 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007158
7159 // Set focused application.
7160 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07007161 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007162
7163 // Expect one focus window exist in display.
7164 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07007165 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007166 mFocusedWindow->consumeFocusEvent(true);
7167 }
7168
7169 virtual void TearDown() override {
7170 InputDispatcherTest::TearDown();
7171
7172 mUnfocusedWindow.clear();
7173 mFocusedWindow.clear();
7174 }
7175
7176protected:
Chris Yea209fde2020-07-22 13:54:51 -07007177 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007178 sp<FakeWindowHandle> mUnfocusedWindow;
7179 sp<FakeWindowHandle> mFocusedWindow;
7180 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
7181 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
7182 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
7183
7184 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
7185
7186 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
7187
7188private:
7189 void tap(const PointF& location) {
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 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007193 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007194 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7195 location));
7196 }
7197};
7198
7199// If we have 2 windows that are both unresponsive, the one with the shortest timeout
7200// should be ANR'd first.
7201TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007202 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007203 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7204 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007205 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007206 mFocusedWindow->consumeMotionDown();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007207 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007208 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007209 // We consumed all events, so no ANR
7210 ASSERT_TRUE(mDispatcher->waitForIdle());
7211 mFakePolicy->assertNotifyAnrWasNotCalled();
7212
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007213 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007214 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7215 FOCUSED_WINDOW_LOCATION));
7216 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
7217 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007218
7219 const std::chrono::duration timeout =
7220 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007221 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007222 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
7223 // sequence to make it consistent
7224 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007225 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007226 mFocusedWindow->consumeMotionDown();
7227 // This cancel is generated because the connection was unresponsive
7228 mFocusedWindow->consumeMotionCancel();
7229 mFocusedWindow->assertNoEvents();
7230 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007231 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08007232 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7233 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007234 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007235}
7236
7237// If we have 2 windows with identical timeouts that are both unresponsive,
7238// it doesn't matter which order they should have ANR.
7239// But we should receive ANR for both.
7240TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
7241 // Set the timeout for unfocused window to match the focused window
7242 mUnfocusedWindow->setDispatchingTimeout(10ms);
7243 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
7244
7245 tapOnFocusedWindow();
7246 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Prabir Pradhanedd96402022-02-15 01:46:16 -08007247 sp<IBinder> anrConnectionToken1, anrConnectionToken2;
7248 ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms));
7249 ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007250
7251 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007252 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
7253 mFocusedWindow->getToken() == anrConnectionToken2);
7254 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
7255 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007256
7257 ASSERT_TRUE(mDispatcher->waitForIdle());
7258 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007259
7260 mFocusedWindow->consumeMotionDown();
7261 mFocusedWindow->consumeMotionUp();
7262 mUnfocusedWindow->consumeMotionOutside();
7263
Prabir Pradhanedd96402022-02-15 01:46:16 -08007264 sp<IBinder> responsiveToken1, responsiveToken2;
7265 ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken());
7266 ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007267
7268 // Both applications should be marked as responsive, in any order
7269 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
7270 mFocusedWindow->getToken() == responsiveToken2);
7271 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
7272 mUnfocusedWindow->getToken() == responsiveToken2);
7273 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007274}
7275
7276// If a window is already not responding, the second tap on the same window should be ignored.
7277// We should also log an error to account for the dropped event (not tested here).
7278// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
7279TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
7280 tapOnFocusedWindow();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007281 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007282 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007283 // Receive the events, but don't respond
7284 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
7285 ASSERT_TRUE(downEventSequenceNum);
7286 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
7287 ASSERT_TRUE(upEventSequenceNum);
7288 const std::chrono::duration timeout =
7289 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007290 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007291
7292 // Tap once again
7293 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007294 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007295 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7296 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007297 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007298 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7299 FOCUSED_WINDOW_LOCATION));
7300 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
7301 // valid touch target
7302 mUnfocusedWindow->assertNoEvents();
7303
7304 // Consume the first tap
7305 mFocusedWindow->finishEvent(*downEventSequenceNum);
7306 mFocusedWindow->finishEvent(*upEventSequenceNum);
7307 ASSERT_TRUE(mDispatcher->waitForIdle());
7308 // The second tap did not go to the focused window
7309 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007310 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -08007311 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7312 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007313 mFakePolicy->assertNotifyAnrWasNotCalled();
7314}
7315
7316// If you tap outside of all windows, there will not be ANR
7317TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
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 LOCATION_OUTSIDE_ALL_WINDOWS));
7321 ASSERT_TRUE(mDispatcher->waitForIdle());
7322 mFakePolicy->assertNotifyAnrWasNotCalled();
7323}
7324
7325// Since the focused window is paused, tapping on it should not produce any events
7326TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
7327 mFocusedWindow->setPaused(true);
7328 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
7329
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007330 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007331 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7332 FOCUSED_WINDOW_LOCATION));
7333
7334 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
7335 ASSERT_TRUE(mDispatcher->waitForIdle());
7336 // Should not ANR because the window is paused, and touches shouldn't go to it
7337 mFakePolicy->assertNotifyAnrWasNotCalled();
7338
7339 mFocusedWindow->assertNoEvents();
7340 mUnfocusedWindow->assertNoEvents();
7341}
7342
7343/**
7344 * If a window is processing a motion event, and then a key event comes in, the key event should
7345 * not to to the focused window until the motion is processed.
7346 * If a different window becomes focused at this time, the key should go to that window instead.
7347 *
7348 * Warning!!!
7349 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
7350 * and the injection timeout that we specify when injecting the key.
7351 * We must have the injection timeout (10ms) be smaller than
7352 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
7353 *
7354 * If that value changes, this test should also change.
7355 */
7356TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
7357 // Set a long ANR timeout to prevent it from triggering
7358 mFocusedWindow->setDispatchingTimeout(2s);
7359 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7360
7361 tapOnUnfocusedWindow();
7362 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
7363 ASSERT_TRUE(downSequenceNum);
7364 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
7365 ASSERT_TRUE(upSequenceNum);
7366 // Don't finish the events yet, and send a key
7367 // Injection will succeed because we will eventually give up and send the key to the focused
7368 // window even if motions are still being processed.
7369
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007370 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007371 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7372 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007373 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007374 // Key will not be sent to the window, yet, because the window is still processing events
7375 // and the key remains pending, waiting for the touch events to be processed
7376 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
7377 ASSERT_FALSE(keySequenceNum);
7378
7379 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07007380 mFocusedWindow->setFocusable(false);
7381 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007382 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07007383 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007384
7385 // Focus events should precede the key events
7386 mUnfocusedWindow->consumeFocusEvent(true);
7387 mFocusedWindow->consumeFocusEvent(false);
7388
7389 // Finish the tap events, which should unblock dispatcher
7390 mUnfocusedWindow->finishEvent(*downSequenceNum);
7391 mUnfocusedWindow->finishEvent(*upSequenceNum);
7392
7393 // Now that all queues are cleared and no backlog in the connections, the key event
7394 // can finally go to the newly focused "mUnfocusedWindow".
7395 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7396 mFocusedWindow->assertNoEvents();
7397 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007398 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007399}
7400
7401// When the touch stream is split across 2 windows, and one of them does not respond,
7402// then ANR should be raised and the touch should be canceled for the unresponsive window.
7403// The other window should not be affected by that.
7404TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
7405 // Touch Window 1
Prabir Pradhan678438e2023-04-13 19:32:51 +00007406 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7407 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7408 {FOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007409 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007410 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007411
7412 // Touch Window 2
Prabir Pradhan678438e2023-04-13 19:32:51 +00007413 mDispatcher->notifyMotion(
7414 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7415 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007416
7417 const std::chrono::duration timeout =
7418 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007419 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007420
7421 mUnfocusedWindow->consumeMotionDown();
7422 mFocusedWindow->consumeMotionDown();
7423 // Focused window may or may not receive ACTION_MOVE
7424 // But it should definitely receive ACTION_CANCEL due to the ANR
7425 InputEvent* event;
7426 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
7427 ASSERT_TRUE(moveOrCancelSequenceNum);
7428 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
7429 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007430 ASSERT_EQ(event->getType(), InputEventType::MOTION);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007431 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
7432 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
7433 mFocusedWindow->consumeMotionCancel();
7434 } else {
7435 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
7436 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007437 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08007438 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7439 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007440
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007441 mUnfocusedWindow->assertNoEvents();
7442 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007443 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007444}
7445
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007446/**
7447 * If we have no focused window, and a key comes in, we start the ANR timer.
7448 * The focused application should add a focused window before the timer runs out to prevent ANR.
7449 *
7450 * If the user touches another application during this time, the key should be dropped.
7451 * Next, if a new focused window comes in, without toggling the focused application,
7452 * then no ANR should occur.
7453 *
7454 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
7455 * but in some cases the policy may not update the focused application.
7456 */
7457TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
7458 std::shared_ptr<FakeApplicationHandle> focusedApplication =
7459 std::make_shared<FakeApplicationHandle>();
7460 focusedApplication->setDispatchingTimeout(60ms);
7461 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
7462 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
7463 mFocusedWindow->setFocusable(false);
7464
7465 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7466 mFocusedWindow->consumeFocusEvent(false);
7467
7468 // Send a key. The ANR timer should start because there is no focused window.
7469 // 'focusedApplication' will get blamed if this timer completes.
7470 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007471 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007472 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7473 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
7474 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007475 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007476
7477 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
7478 // then the injected touches won't cause the focused event to get dropped.
7479 // The dispatcher only checks for whether the queue should be pruned upon queueing.
7480 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
7481 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
7482 // For this test, it means that the key would get delivered to the window once it becomes
7483 // focused.
7484 std::this_thread::sleep_for(10ms);
7485
7486 // Touch unfocused window. This should force the pending key to get dropped.
Prabir Pradhan678438e2023-04-13 19:32:51 +00007487 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7488 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7489 {UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007490
7491 // We do not consume the motion right away, because that would require dispatcher to first
7492 // process (== drop) the key event, and by that time, ANR will be raised.
7493 // Set the focused window first.
7494 mFocusedWindow->setFocusable(true);
7495 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7496 setFocusedWindow(mFocusedWindow);
7497 mFocusedWindow->consumeFocusEvent(true);
7498 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
7499 // to another application. This could be a bug / behaviour in the policy.
7500
7501 mUnfocusedWindow->consumeMotionDown();
7502
7503 ASSERT_TRUE(mDispatcher->waitForIdle());
7504 // Should not ANR because we actually have a focused window. It was just added too slowly.
7505 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
7506}
7507
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007508// These tests ensure we cannot send touch events to a window that's positioned behind a window
7509// that has feature NO_INPUT_CHANNEL.
7510// Layout:
7511// Top (closest to user)
7512// mNoInputWindow (above all windows)
7513// mBottomWindow
7514// Bottom (furthest from user)
7515class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
7516 virtual void SetUp() override {
7517 InputDispatcherTest::SetUp();
7518
7519 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007520 mNoInputWindow =
7521 sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7522 "Window without input channel", ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00007523 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007524 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007525 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7526 // It's perfectly valid for this window to not have an associated input channel
7527
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007528 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
7529 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007530 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
7531
7532 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7533 }
7534
7535protected:
7536 std::shared_ptr<FakeApplicationHandle> mApplication;
7537 sp<FakeWindowHandle> mNoInputWindow;
7538 sp<FakeWindowHandle> mBottomWindow;
7539};
7540
7541TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
7542 PointF touchedPoint = {10, 10};
7543
Prabir Pradhan678438e2023-04-13 19:32:51 +00007544 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7545 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7546 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007547
7548 mNoInputWindow->assertNoEvents();
7549 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
7550 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
7551 // and therefore should prevent mBottomWindow from receiving touches
7552 mBottomWindow->assertNoEvents();
7553}
7554
7555/**
7556 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
7557 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
7558 */
7559TEST_F(InputDispatcherMultiWindowOcclusionTests,
7560 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007561 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7562 "Window with input channel and NO_INPUT_CHANNEL",
7563 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007564
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007565 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007566 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7567 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7568
7569 PointF touchedPoint = {10, 10};
7570
Prabir Pradhan678438e2023-04-13 19:32:51 +00007571 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7572 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7573 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007574
7575 mNoInputWindow->assertNoEvents();
7576 mBottomWindow->assertNoEvents();
7577}
7578
Vishnu Nair958da932020-08-21 17:12:37 -07007579class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
7580protected:
7581 std::shared_ptr<FakeApplicationHandle> mApp;
7582 sp<FakeWindowHandle> mWindow;
7583 sp<FakeWindowHandle> mMirror;
7584
7585 virtual void SetUp() override {
7586 InputDispatcherTest::SetUp();
7587 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007588 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
7589 mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror",
7590 ADISPLAY_ID_DEFAULT, mWindow->getToken());
Vishnu Nair958da932020-08-21 17:12:37 -07007591 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7592 mWindow->setFocusable(true);
7593 mMirror->setFocusable(true);
7594 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7595 }
7596};
7597
7598TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
7599 // Request focus on a mirrored window
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);
7607}
7608
7609// A focused & mirrored window remains focused only if the window and its mirror are both
7610// focusable.
7611TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
7612 setFocusedWindow(mMirror);
7613
7614 // window gets focused
7615 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007616 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7617 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007618 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007619 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7620 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007621 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7622
7623 mMirror->setFocusable(false);
7624 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7625
7626 // window loses focus since one of the windows associated with the token in not focusable
7627 mWindow->consumeFocusEvent(false);
7628
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007629 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7630 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007631 mWindow->assertNoEvents();
7632}
7633
7634// A focused & mirrored window remains focused until the window and its mirror both become
7635// invisible.
7636TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
7637 setFocusedWindow(mMirror);
7638
7639 // window gets focused
7640 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007641 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7642 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007643 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007644 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7645 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007646 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7647
7648 mMirror->setVisible(false);
7649 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7650
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007651 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7652 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007653 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007654 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7655 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007656 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7657
7658 mWindow->setVisible(false);
7659 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7660
7661 // window loses focus only after all windows associated with the token become invisible.
7662 mWindow->consumeFocusEvent(false);
7663
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007664 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7665 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007666 mWindow->assertNoEvents();
7667}
7668
7669// A focused & mirrored window remains focused until both windows are removed.
7670TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
7671 setFocusedWindow(mMirror);
7672
7673 // window gets focused
7674 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007675 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7676 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007677 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007678 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7679 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007680 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7681
7682 // single window is removed but the window token remains focused
7683 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
7684
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007685 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7686 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007687 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007688 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7689 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007690 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7691
7692 // Both windows are removed
7693 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
7694 mWindow->consumeFocusEvent(false);
7695
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007696 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7697 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007698 mWindow->assertNoEvents();
7699}
7700
7701// Focus request can be pending until one window becomes visible.
7702TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
7703 // Request focus on an invisible mirror.
7704 mWindow->setVisible(false);
7705 mMirror->setVisible(false);
7706 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7707 setFocusedWindow(mMirror);
7708
7709 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007710 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007711 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7712 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07007713
7714 mMirror->setVisible(true);
7715 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7716
7717 // window gets focused
7718 mWindow->consumeFocusEvent(true);
7719 // window gets the pending key event
7720 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7721}
Prabir Pradhan99987712020-11-10 18:43:05 -08007722
7723class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
7724protected:
7725 std::shared_ptr<FakeApplicationHandle> mApp;
7726 sp<FakeWindowHandle> mWindow;
7727 sp<FakeWindowHandle> mSecondWindow;
7728
7729 void SetUp() override {
7730 InputDispatcherTest::SetUp();
7731 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007732 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007733 mWindow->setFocusable(true);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007734 mSecondWindow =
7735 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007736 mSecondWindow->setFocusable(true);
7737
7738 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7739 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
7740
7741 setFocusedWindow(mWindow);
7742 mWindow->consumeFocusEvent(true);
7743 }
7744
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007745 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007746 mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request));
Prabir Pradhan99987712020-11-10 18:43:05 -08007747 }
7748
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007749 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
7750 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08007751 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007752 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
7753 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007754 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007755 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08007756 }
7757};
7758
7759TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
7760 // Ensure that capture cannot be obtained for unfocused windows.
7761 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
7762 mFakePolicy->assertSetPointerCaptureNotCalled();
7763 mSecondWindow->assertNoEvents();
7764
7765 // Ensure that capture can be enabled from the focus window.
7766 requestAndVerifyPointerCapture(mWindow, true);
7767
7768 // Ensure that capture cannot be disabled from a window that does not have capture.
7769 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
7770 mFakePolicy->assertSetPointerCaptureNotCalled();
7771
7772 // Ensure that capture can be disabled from the window with capture.
7773 requestAndVerifyPointerCapture(mWindow, false);
7774}
7775
7776TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007777 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007778
7779 setFocusedWindow(mSecondWindow);
7780
7781 // Ensure that the capture disabled event was sent first.
7782 mWindow->consumeCaptureEvent(false);
7783 mWindow->consumeFocusEvent(false);
7784 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007785 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007786
7787 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007788 notifyPointerCaptureChanged({});
7789 notifyPointerCaptureChanged(request);
7790 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08007791 mWindow->assertNoEvents();
7792 mSecondWindow->assertNoEvents();
7793 mFakePolicy->assertSetPointerCaptureNotCalled();
7794}
7795
7796TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007797 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007798
7799 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007800 notifyPointerCaptureChanged({});
7801 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007802
7803 // Ensure that Pointer Capture is disabled.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007804 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007805 mWindow->consumeCaptureEvent(false);
7806 mWindow->assertNoEvents();
7807}
7808
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007809TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
7810 requestAndVerifyPointerCapture(mWindow, true);
7811
7812 // The first window loses focus.
7813 setFocusedWindow(mSecondWindow);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007814 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007815 mWindow->consumeCaptureEvent(false);
7816
7817 // Request Pointer Capture from the second window before the notification from InputReader
7818 // arrives.
7819 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007820 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007821
7822 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007823 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007824
7825 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007826 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007827
7828 mSecondWindow->consumeFocusEvent(true);
7829 mSecondWindow->consumeCaptureEvent(true);
7830}
7831
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007832TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
7833 // App repeatedly enables and disables capture.
7834 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7835 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7836 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7837 mFakePolicy->assertSetPointerCaptureCalled(false);
7838 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7839 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7840
7841 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
7842 // first request is now stale, this should do nothing.
7843 notifyPointerCaptureChanged(firstRequest);
7844 mWindow->assertNoEvents();
7845
7846 // InputReader notifies that the second request was enabled.
7847 notifyPointerCaptureChanged(secondRequest);
7848 mWindow->consumeCaptureEvent(true);
7849}
7850
Prabir Pradhan7092e262022-05-03 16:51:09 +00007851TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
7852 requestAndVerifyPointerCapture(mWindow, true);
7853
7854 // App toggles pointer capture off and on.
7855 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7856 mFakePolicy->assertSetPointerCaptureCalled(false);
7857
7858 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7859 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7860
7861 // InputReader notifies that the latest "enable" request was processed, while skipping over the
7862 // preceding "disable" request.
7863 notifyPointerCaptureChanged(enableRequest);
7864
7865 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
7866 // any notifications.
7867 mWindow->assertNoEvents();
7868}
7869
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007870class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
7871protected:
7872 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00007873
7874 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
7875 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
7876
7877 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
7878 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7879
7880 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
7881 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
7882 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7883 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
7884 MAXIMUM_OBSCURING_OPACITY);
7885
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00007886 static constexpr gui::Uid TOUCHED_APP_UID{10001};
7887 static constexpr gui::Uid APP_B_UID{10002};
7888 static constexpr gui::Uid APP_C_UID{10003};
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007889
7890 sp<FakeWindowHandle> mTouchWindow;
7891
7892 virtual void SetUp() override {
7893 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007894 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007895 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
7896 }
7897
7898 virtual void TearDown() override {
7899 InputDispatcherTest::TearDown();
7900 mTouchWindow.clear();
7901 }
7902
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00007903 sp<FakeWindowHandle> getOccludingWindow(gui::Uid uid, std::string name, TouchOcclusionMode mode,
chaviw3277faf2021-05-19 16:45:23 -05007904 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007905 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007906 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007907 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007908 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007909 return window;
7910 }
7911
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00007912 sp<FakeWindowHandle> getWindow(gui::Uid uid, std::string name) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007913 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
7914 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007915 sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007916 // Generate an arbitrary PID based on the UID
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00007917 window->setOwnerInfo(gui::Pid{static_cast<pid_t>(1777 + (uid.val() % 10000))}, uid);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007918 return window;
7919 }
7920
7921 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007922 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7923 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7924 points));
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007925 }
7926};
7927
7928TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007929 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007930 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007931 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007932
7933 touch();
7934
7935 mTouchWindow->assertNoEvents();
7936}
7937
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007938TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00007939 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
7940 const sp<FakeWindowHandle>& w =
7941 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
7942 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7943
7944 touch();
7945
7946 mTouchWindow->assertNoEvents();
7947}
7948
7949TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007950 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
7951 const sp<FakeWindowHandle>& w =
7952 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
7953 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7954
7955 touch();
7956
7957 w->assertNoEvents();
7958}
7959
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007960TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007961 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
7962 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007963
7964 touch();
7965
7966 mTouchWindow->consumeAnyMotionDown();
7967}
7968
7969TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007970 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007971 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007972 w->setFrame(Rect(0, 0, 50, 50));
7973 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007974
7975 touch({PointF{100, 100}});
7976
7977 mTouchWindow->consumeAnyMotionDown();
7978}
7979
7980TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007981 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007982 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007983 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7984
7985 touch();
7986
7987 mTouchWindow->consumeAnyMotionDown();
7988}
7989
7990TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
7991 const sp<FakeWindowHandle>& w =
7992 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
7993 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007994
7995 touch();
7996
7997 mTouchWindow->consumeAnyMotionDown();
7998}
7999
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00008000TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
8001 const sp<FakeWindowHandle>& w =
8002 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
8003 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8004
8005 touch();
8006
8007 w->assertNoEvents();
8008}
8009
8010/**
8011 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
8012 * inside) while letting them pass-through. Note that even though touch passes through the occluding
8013 * window, the occluding window will still receive ACTION_OUTSIDE event.
8014 */
8015TEST_F(InputDispatcherUntrustedTouchesTest,
8016 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
8017 const sp<FakeWindowHandle>& w =
8018 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008019 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00008020 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8021
8022 touch();
8023
8024 w->consumeMotionOutside();
8025}
8026
8027TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
8028 const sp<FakeWindowHandle>& w =
8029 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008030 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00008031 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8032
8033 touch();
8034
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008035 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00008036}
8037
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00008038TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008039 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008040 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8041 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008042 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8043
8044 touch();
8045
8046 mTouchWindow->consumeAnyMotionDown();
8047}
8048
8049TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
8050 const sp<FakeWindowHandle>& w =
8051 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8052 MAXIMUM_OBSCURING_OPACITY);
8053 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00008054
8055 touch();
8056
8057 mTouchWindow->consumeAnyMotionDown();
8058}
8059
8060TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008061 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008062 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8063 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008064 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8065
8066 touch();
8067
8068 mTouchWindow->assertNoEvents();
8069}
8070
8071TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
8072 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
8073 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008074 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
8075 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008076 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008077 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
8078 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008079 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
8080
8081 touch();
8082
8083 mTouchWindow->assertNoEvents();
8084}
8085
8086TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
8087 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
8088 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008089 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
8090 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008091 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008092 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
8093 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008094 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
8095
8096 touch();
8097
8098 mTouchWindow->consumeAnyMotionDown();
8099}
8100
8101TEST_F(InputDispatcherUntrustedTouchesTest,
8102 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
8103 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008104 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8105 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008106 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008107 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
8108 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008109 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
8110
8111 touch();
8112
8113 mTouchWindow->consumeAnyMotionDown();
8114}
8115
8116TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
8117 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008118 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8119 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008120 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008121 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
8122 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008123 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00008124
8125 touch();
8126
8127 mTouchWindow->assertNoEvents();
8128}
8129
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008130TEST_F(InputDispatcherUntrustedTouchesTest,
8131 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
8132 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008133 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
8134 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008135 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008136 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8137 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008138 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
8139
8140 touch();
8141
8142 mTouchWindow->assertNoEvents();
8143}
8144
8145TEST_F(InputDispatcherUntrustedTouchesTest,
8146 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
8147 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008148 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
8149 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008150 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008151 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8152 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008153 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
8154
8155 touch();
8156
8157 mTouchWindow->consumeAnyMotionDown();
8158}
8159
8160TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
8161 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008162 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
8163 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008164 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8165
8166 touch();
8167
8168 mTouchWindow->consumeAnyMotionDown();
8169}
8170
8171TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
8172 const sp<FakeWindowHandle>& w =
8173 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
8174 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8175
8176 touch();
8177
8178 mTouchWindow->consumeAnyMotionDown();
8179}
8180
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00008181TEST_F(InputDispatcherUntrustedTouchesTest,
8182 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
8183 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
8184 const sp<FakeWindowHandle>& w =
8185 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
8186 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8187
8188 touch();
8189
8190 mTouchWindow->assertNoEvents();
8191}
8192
8193TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
8194 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
8195 const sp<FakeWindowHandle>& w =
8196 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
8197 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8198
8199 touch();
8200
8201 mTouchWindow->consumeAnyMotionDown();
8202}
8203
8204TEST_F(InputDispatcherUntrustedTouchesTest,
8205 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
8206 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
8207 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008208 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8209 OPACITY_ABOVE_THRESHOLD);
8210 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8211
8212 touch();
8213
8214 mTouchWindow->consumeAnyMotionDown();
8215}
8216
8217TEST_F(InputDispatcherUntrustedTouchesTest,
8218 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
8219 const sp<FakeWindowHandle>& w1 =
8220 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
8221 OPACITY_BELOW_THRESHOLD);
8222 const sp<FakeWindowHandle>& w2 =
8223 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8224 OPACITY_BELOW_THRESHOLD);
8225 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
8226
8227 touch();
8228
8229 mTouchWindow->assertNoEvents();
8230}
8231
8232/**
8233 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
8234 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
8235 * (which alone would result in allowing touches) does not affect the blocking behavior.
8236 */
8237TEST_F(InputDispatcherUntrustedTouchesTest,
8238 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
8239 const sp<FakeWindowHandle>& wB =
8240 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
8241 OPACITY_BELOW_THRESHOLD);
8242 const sp<FakeWindowHandle>& wC =
8243 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
8244 OPACITY_BELOW_THRESHOLD);
8245 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
8246
8247 touch();
8248
8249 mTouchWindow->assertNoEvents();
8250}
8251
8252/**
8253 * This test is testing that a window from a different UID but with same application token doesn't
8254 * block the touch. Apps can share the application token for close UI collaboration for example.
8255 */
8256TEST_F(InputDispatcherUntrustedTouchesTest,
8257 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
8258 const sp<FakeWindowHandle>& w =
8259 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
8260 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00008261 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8262
8263 touch();
8264
8265 mTouchWindow->consumeAnyMotionDown();
8266}
8267
arthurhungb89ccb02020-12-30 16:19:01 +08008268class InputDispatcherDragTests : public InputDispatcherTest {
8269protected:
8270 std::shared_ptr<FakeApplicationHandle> mApp;
8271 sp<FakeWindowHandle> mWindow;
8272 sp<FakeWindowHandle> mSecondWindow;
8273 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008274 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008275 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
8276 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +08008277
8278 void SetUp() override {
8279 InputDispatcherTest::SetUp();
8280 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008281 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08008282 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08008283
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008284 mSecondWindow =
8285 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08008286 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08008287
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008288 mSpyWindow =
8289 sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008290 mSpyWindow->setSpy(true);
8291 mSpyWindow->setTrustedOverlay(true);
8292 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
8293
arthurhungb89ccb02020-12-30 16:19:01 +08008294 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008295 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08008296 }
8297
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008298 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
8299 switch (fromSource) {
8300 case AINPUT_SOURCE_TOUCHSCREEN:
8301 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8302 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
8303 ADISPLAY_ID_DEFAULT, {50, 50}))
8304 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8305 break;
8306 case AINPUT_SOURCE_STYLUS:
8307 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8308 injectMotionEvent(
8309 mDispatcher,
8310 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8311 AINPUT_SOURCE_STYLUS)
8312 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008313 .pointer(PointerBuilder(0, ToolType::STYLUS)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008314 .x(50)
8315 .y(50))
8316 .build()));
8317 break;
8318 case AINPUT_SOURCE_MOUSE:
8319 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8320 injectMotionEvent(
8321 mDispatcher,
8322 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
8323 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8324 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008325 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008326 .x(50)
8327 .y(50))
8328 .build()));
8329 break;
8330 default:
8331 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
8332 }
arthurhungb89ccb02020-12-30 16:19:01 +08008333
8334 // Window should receive motion event.
8335 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008336 // Spy window should also receive motion event
8337 mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +00008338 }
8339
8340 // Start performing drag, we will create a drag window and transfer touch to it.
8341 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
8342 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008343 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +00008344 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008345 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +00008346 }
arthurhungb89ccb02020-12-30 16:19:01 +08008347
8348 // The drag window covers the entire display
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008349 mDragWindow =
8350 sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008351 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
arthurhungb89ccb02020-12-30 16:19:01 +08008352 mDispatcher->setInputWindows(
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008353 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08008354
8355 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +00008356 bool transferred =
8357 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
Harry Cutts33476232023-01-30 19:57:29 +00008358 /*isDragDrop=*/true);
Arthur Hung54745652022-04-20 07:17:41 +00008359 if (transferred) {
8360 mWindow->consumeMotionCancel();
8361 mDragWindow->consumeMotionDown();
8362 }
8363 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +08008364 }
8365};
8366
8367TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008368 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +08008369
8370 // Move on window.
8371 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8372 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8373 ADISPLAY_ID_DEFAULT, {50, 50}))
8374 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8375 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8376 mWindow->consumeDragEvent(false, 50, 50);
8377 mSecondWindow->assertNoEvents();
8378
8379 // Move to another window.
8380 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8381 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8382 ADISPLAY_ID_DEFAULT, {150, 50}))
8383 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8384 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8385 mWindow->consumeDragEvent(true, 150, 50);
8386 mSecondWindow->consumeDragEvent(false, 50, 50);
8387
8388 // Move back to original window.
8389 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8390 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8391 ADISPLAY_ID_DEFAULT, {50, 50}))
8392 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8393 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8394 mWindow->consumeDragEvent(false, 50, 50);
8395 mSecondWindow->consumeDragEvent(true, -50, 50);
8396
8397 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8398 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
8399 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8400 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8401 mWindow->assertNoEvents();
8402 mSecondWindow->assertNoEvents();
8403}
8404
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008405TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008406 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008407
8408 // No cancel event after drag start
8409 mSpyWindow->assertNoEvents();
8410
8411 const MotionEvent secondFingerDownEvent =
8412 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8413 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008414 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8415 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008416 .build();
8417 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8418 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8419 InputEventInjectionSync::WAIT_FOR_RESULT))
8420 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8421
8422 // Receives cancel for first pointer after next pointer down
8423 mSpyWindow->consumeMotionCancel();
8424 mSpyWindow->consumeMotionDown();
8425
8426 mSpyWindow->assertNoEvents();
8427}
8428
arthurhungf452d0b2021-01-06 00:19:52 +08008429TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008430 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +08008431
8432 // Move on window.
8433 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8434 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8435 ADISPLAY_ID_DEFAULT, {50, 50}))
8436 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8437 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8438 mWindow->consumeDragEvent(false, 50, 50);
8439 mSecondWindow->assertNoEvents();
8440
8441 // Move to another window.
8442 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8443 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8444 ADISPLAY_ID_DEFAULT, {150, 50}))
8445 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8446 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8447 mWindow->consumeDragEvent(true, 150, 50);
8448 mSecondWindow->consumeDragEvent(false, 50, 50);
8449
8450 // drop to another window.
8451 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8452 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8453 {150, 50}))
8454 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8455 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -07008456 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
arthurhungf452d0b2021-01-06 00:19:52 +08008457 mWindow->assertNoEvents();
8458 mSecondWindow->assertNoEvents();
8459}
8460
arthurhung6d4bed92021-03-17 11:59:33 +08008461TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008462 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +08008463
8464 // Move on window and keep button pressed.
8465 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8466 injectMotionEvent(mDispatcher,
8467 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
8468 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008469 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).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->consumeDragEvent(false, 50, 50);
8474 mSecondWindow->assertNoEvents();
8475
8476 // Move to another window and release button, expect to drop item.
8477 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8478 injectMotionEvent(mDispatcher,
8479 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
8480 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008481 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008482 .build()))
8483 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8484 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8485 mWindow->assertNoEvents();
8486 mSecondWindow->assertNoEvents();
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -07008487 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
arthurhung6d4bed92021-03-17 11:59:33 +08008488
8489 // nothing to the window.
8490 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8491 injectMotionEvent(mDispatcher,
8492 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
8493 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008494 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008495 .build()))
8496 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8497 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8498 mWindow->assertNoEvents();
8499 mSecondWindow->assertNoEvents();
8500}
8501
Arthur Hung54745652022-04-20 07:17:41 +00008502TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008503 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +08008504
8505 // Set second window invisible.
8506 mSecondWindow->setVisible(false);
8507 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
8508
8509 // Move on window.
8510 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8511 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8512 ADISPLAY_ID_DEFAULT, {50, 50}))
8513 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8514 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8515 mWindow->consumeDragEvent(false, 50, 50);
8516 mSecondWindow->assertNoEvents();
8517
8518 // Move to another window.
8519 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8520 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8521 ADISPLAY_ID_DEFAULT, {150, 50}))
8522 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8523 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8524 mWindow->consumeDragEvent(true, 150, 50);
8525 mSecondWindow->assertNoEvents();
8526
8527 // drop to another window.
8528 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8529 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8530 {150, 50}))
8531 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8532 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -07008533 mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr);
Arthur Hung6d0571e2021-04-09 20:18:16 +08008534 mWindow->assertNoEvents();
8535 mSecondWindow->assertNoEvents();
8536}
8537
Arthur Hung54745652022-04-20 07:17:41 +00008538TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008539 // Ensure window could track pointerIds if it didn't support split touch.
8540 mWindow->setPreventSplitting(true);
8541
Arthur Hung54745652022-04-20 07:17:41 +00008542 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8543 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8544 {50, 50}))
8545 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8546 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8547
8548 const MotionEvent secondFingerDownEvent =
8549 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8550 .displayId(ADISPLAY_ID_DEFAULT)
8551 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008552 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8553 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008554 .build();
8555 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8556 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8557 InputEventInjectionSync::WAIT_FOR_RESULT))
8558 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00008559 mWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
Arthur Hung54745652022-04-20 07:17:41 +00008560
8561 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008562 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008563}
8564
8565TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
8566 // First down on second window.
8567 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8568 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8569 {150, 50}))
8570 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8571
8572 mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8573
8574 // Second down on first window.
8575 const MotionEvent secondFingerDownEvent =
8576 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8577 .displayId(ADISPLAY_ID_DEFAULT)
8578 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008579 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8580 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008581 .build();
8582 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8583 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8584 InputEventInjectionSync::WAIT_FOR_RESULT))
8585 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8586 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8587
8588 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008589 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008590
8591 // Move on window.
8592 const MotionEvent secondFingerMoveEvent =
8593 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
8594 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008595 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8596 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008597 .build();
8598 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8599 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
8600 InputEventInjectionSync::WAIT_FOR_RESULT));
8601 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8602 mWindow->consumeDragEvent(false, 50, 50);
8603 mSecondWindow->consumeMotionMove();
8604
8605 // Release the drag pointer should perform drop.
8606 const MotionEvent secondFingerUpEvent =
8607 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
8608 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008609 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8610 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008611 .build();
8612 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8613 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
8614 InputEventInjectionSync::WAIT_FOR_RESULT));
8615 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -07008616 mFakePolicy->assertDropTargetEquals(*mDispatcher, mWindow->getToken());
Arthur Hung54745652022-04-20 07:17:41 +00008617 mWindow->assertNoEvents();
8618 mSecondWindow->consumeMotionMove();
8619}
8620
Arthur Hung3915c1f2022-05-31 07:17:17 +00008621TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008622 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +00008623
8624 // Update window of second display.
8625 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008626 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008627 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8628
8629 // Let second display has a touch state.
8630 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8631 injectMotionEvent(mDispatcher,
8632 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8633 AINPUT_SOURCE_TOUCHSCREEN)
8634 .displayId(SECOND_DISPLAY_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008635 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Arthur Hung3915c1f2022-05-31 07:17:17 +00008636 .build()));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07008637 windowInSecondary->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
Harry Cutts33476232023-01-30 19:57:29 +00008638 SECOND_DISPLAY_ID, /*expectedFlag=*/0);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008639 // Update window again.
8640 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8641
8642 // Move on window.
8643 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8644 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8645 ADISPLAY_ID_DEFAULT, {50, 50}))
8646 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8647 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8648 mWindow->consumeDragEvent(false, 50, 50);
8649 mSecondWindow->assertNoEvents();
8650
8651 // Move to another window.
8652 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8653 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8654 ADISPLAY_ID_DEFAULT, {150, 50}))
8655 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8656 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8657 mWindow->consumeDragEvent(true, 150, 50);
8658 mSecondWindow->consumeDragEvent(false, 50, 50);
8659
8660 // drop to another window.
8661 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8662 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8663 {150, 50}))
8664 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8665 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -07008666 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
Arthur Hung3915c1f2022-05-31 07:17:17 +00008667 mWindow->assertNoEvents();
8668 mSecondWindow->assertNoEvents();
8669}
8670
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008671TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
8672 startDrag(true, AINPUT_SOURCE_MOUSE);
8673 // Move on window.
8674 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8675 injectMotionEvent(mDispatcher,
8676 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8677 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8678 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008679 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008680 .x(50)
8681 .y(50))
8682 .build()))
8683 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8684 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8685 mWindow->consumeDragEvent(false, 50, 50);
8686 mSecondWindow->assertNoEvents();
8687
8688 // Move to another window.
8689 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8690 injectMotionEvent(mDispatcher,
8691 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8692 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8693 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008694 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008695 .x(150)
8696 .y(50))
8697 .build()))
8698 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8699 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8700 mWindow->consumeDragEvent(true, 150, 50);
8701 mSecondWindow->consumeDragEvent(false, 50, 50);
8702
8703 // drop to another window.
8704 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8705 injectMotionEvent(mDispatcher,
8706 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
8707 .buttonState(0)
8708 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008709 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008710 .x(150)
8711 .y(50))
8712 .build()))
8713 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8714 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -07008715 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008716 mWindow->assertNoEvents();
8717 mSecondWindow->assertNoEvents();
8718}
8719
Vishnu Nair062a8672021-09-03 16:07:44 -07008720class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
8721
8722TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
8723 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008724 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8725 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008726 window->setDropInput(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008727 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8728 window->setFocusable(true);
8729 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8730 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008731 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008732
8733 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008734 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008735 window->assertNoEvents();
8736
Prabir Pradhan678438e2023-04-13 19:32:51 +00008737 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8738 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -07008739 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
8740 ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008741 window->assertNoEvents();
8742
8743 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008744 window->setDropInput(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008745 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8746
Prabir Pradhan678438e2023-04-13 19:32:51 +00008747 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008748 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8749
Prabir Pradhan678438e2023-04-13 19:32:51 +00008750 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8751 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008752 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8753 window->assertNoEvents();
8754}
8755
8756TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
8757 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8758 std::make_shared<FakeApplicationHandle>();
8759 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008760 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8761 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008762 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00008763 obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008764 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008765 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008766 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8767 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008768 window->setDropInputIfObscured(true);
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00008769 window->setOwnerInfo(gui::Pid{222}, gui::Uid{222});
Vishnu Nair062a8672021-09-03 16:07:44 -07008770 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8771 window->setFocusable(true);
8772 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8773 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008774 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008775
8776 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008777 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008778 window->assertNoEvents();
8779
Prabir Pradhan678438e2023-04-13 19:32:51 +00008780 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8781 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -07008782 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
8783 ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008784 window->assertNoEvents();
8785
8786 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008787 window->setDropInputIfObscured(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008788 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8789
Prabir Pradhan678438e2023-04-13 19:32:51 +00008790 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008791 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8792
Prabir Pradhan678438e2023-04-13 19:32:51 +00008793 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8794 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008795 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
8796 window->assertNoEvents();
8797}
8798
8799TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
8800 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8801 std::make_shared<FakeApplicationHandle>();
8802 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008803 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8804 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008805 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00008806 obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008807 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008808 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008809 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8810 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008811 window->setDropInputIfObscured(true);
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00008812 window->setOwnerInfo(gui::Pid{222}, gui::Uid{222});
Vishnu Nair062a8672021-09-03 16:07:44 -07008813 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8814 window->setFocusable(true);
8815 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8816 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008817 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008818
8819 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008820 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008821 window->assertNoEvents();
8822
Prabir Pradhan678438e2023-04-13 19:32:51 +00008823 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8824 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -07008825 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
8826 ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008827 window->assertNoEvents();
8828
8829 // When the window is no longer obscured because it went on top, it should get input
8830 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}});
8831
Prabir Pradhan678438e2023-04-13 19:32:51 +00008832 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008833 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8834
Prabir Pradhan678438e2023-04-13 19:32:51 +00008835 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8836 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008837 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8838 window->assertNoEvents();
8839}
8840
Antonio Kantekf16f2832021-09-28 04:39:20 +00008841class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
8842protected:
8843 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +00008844 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008845 sp<FakeWindowHandle> mWindow;
8846 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +00008847 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008848
8849 void SetUp() override {
8850 InputDispatcherTest::SetUp();
8851
8852 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +00008853 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008854 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008855 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008856 setFocusedWindow(mWindow);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008857 mSecondWindow =
8858 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008859 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +00008860 mThirdWindow =
8861 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
8862 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
8863 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008864
8865 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Antonio Kantek15beb512022-06-13 22:35:41 +00008866 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}},
8867 {SECOND_DISPLAY_ID, {mThirdWindow}}});
8868 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008869 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008870
Antonio Kantek15beb512022-06-13 22:35:41 +00008871 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +00008872 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008873 WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -07008874 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
8875 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008876 mThirdWindow->assertNoEvents();
8877 }
8878
8879 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
8880 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008881 SECONDARY_WINDOW_UID, /*hasPermission=*/true,
Antonio Kantek15beb512022-06-13 22:35:41 +00008882 SECOND_DISPLAY_ID)) {
8883 mWindow->assertNoEvents();
8884 mSecondWindow->assertNoEvents();
8885 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -07008886 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00008887 }
8888
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00008889 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, gui::Pid pid, gui::Uid uid,
Antonio Kantek15beb512022-06-13 22:35:41 +00008890 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -07008891 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
8892 ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008893 mWindow->consumeTouchModeEvent(inTouchMode);
8894 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008895 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +00008896 }
8897};
8898
Antonio Kantek26defcf2022-02-08 01:12:27 +00008899TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008900 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +00008901 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
8902 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008903 /* hasPermission=*/false);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008904}
8905
Antonio Kantek26defcf2022-02-08 01:12:27 +00008906TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
8907 const WindowInfo& windowInfo = *mWindow->getInfo();
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00008908 gui::Pid ownerPid = windowInfo.ownerPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00008909 gui::Uid ownerUid = windowInfo.ownerUid;
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00008910 mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008911 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008912 ownerUid, /*hasPermission=*/false,
Antonio Kanteka042c022022-07-06 16:51:07 -07008913 ADISPLAY_ID_DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +00008914 mWindow->assertNoEvents();
8915 mSecondWindow->assertNoEvents();
8916}
8917
8918TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
8919 const WindowInfo& windowInfo = *mWindow->getInfo();
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00008920 gui::Pid ownerPid = windowInfo.ownerPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00008921 gui::Uid ownerUid = windowInfo.ownerUid;
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00008922 mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID);
Antonio Kantek15beb512022-06-13 22:35:41 +00008923 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008924 ownerUid, /*hasPermission=*/true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008925}
8926
Antonio Kantekf16f2832021-09-28 04:39:20 +00008927TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008928 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +00008929 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
8930 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008931 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008932 mWindow->assertNoEvents();
8933 mSecondWindow->assertNoEvents();
8934}
8935
Antonio Kantek15beb512022-06-13 22:35:41 +00008936TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
8937 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
8938 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8939 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008940 /*hasPermission=*/true, SECOND_DISPLAY_ID));
Antonio Kantek15beb512022-06-13 22:35:41 +00008941 mWindow->assertNoEvents();
8942 mSecondWindow->assertNoEvents();
8943 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
8944}
8945
Antonio Kantek48710e42022-03-24 14:19:30 -07008946TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
8947 // Interact with the window first.
8948 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
8949 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8950 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
8951
8952 // Then remove focus.
8953 mWindow->setFocusable(false);
8954 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
8955
8956 // Assert that caller can switch touch mode by owning one of the last interacted window.
8957 const WindowInfo& windowInfo = *mWindow->getInfo();
8958 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8959 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008960 /*hasPermission=*/false, ADISPLAY_ID_DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -07008961}
8962
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008963class InputDispatcherSpyWindowTest : public InputDispatcherTest {
8964public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008965 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008966 std::shared_ptr<FakeApplicationHandle> application =
8967 std::make_shared<FakeApplicationHandle>();
8968 std::string name = "Fake Spy ";
8969 name += std::to_string(mSpyCount++);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008970 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher,
8971 name.c_str(), ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008972 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008973 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008974 return spy;
8975 }
8976
8977 sp<FakeWindowHandle> createForeground() {
8978 std::shared_ptr<FakeApplicationHandle> application =
8979 std::make_shared<FakeApplicationHandle>();
8980 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008981 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
8982 ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008983 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008984 return window;
8985 }
8986
8987private:
8988 int mSpyCount{0};
8989};
8990
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008991using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008992/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008993 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
8994 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008995TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
8996 ScopedSilentDeath _silentDeath;
8997
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008998 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008999 spy->setTrustedOverlay(false);
9000 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}),
9001 ".* not a trusted overlay");
9002}
9003
9004/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009005 * Input injection into a display with a spy window but no foreground windows should succeed.
9006 */
9007TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009008 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009009 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}});
9010
9011 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9012 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9013 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9014 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9015}
9016
9017/**
9018 * Verify the order in which different input windows receive events. The touched foreground window
9019 * (if there is one) should always receive the event first. When there are multiple spy windows, the
9020 * spy windows will receive the event according to their Z-order, where the top-most spy window will
9021 * receive events before ones belows it.
9022 *
9023 * Here, we set up a scenario with four windows in the following Z order from the top:
9024 * spy1, spy2, window, spy3.
9025 * We then inject an event and verify that the foreground "window" receives it first, followed by
9026 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
9027 * window.
9028 */
9029TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
9030 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009031 auto spy1 = createSpy();
9032 auto spy2 = createSpy();
9033 auto spy3 = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009034 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}});
9035 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
9036 const size_t numChannels = channels.size();
9037
Michael Wright8e9a8562022-02-09 13:44:29 +00009038 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009039 if (!epollFd.ok()) {
9040 FAIL() << "Failed to create epoll fd";
9041 }
9042
9043 for (size_t i = 0; i < numChannels; i++) {
9044 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
9045 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
9046 FAIL() << "Failed to add fd to epoll";
9047 }
9048 }
9049
9050 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9051 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9052 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9053
9054 std::vector<size_t> eventOrder;
9055 std::vector<struct epoll_event> events(numChannels);
9056 for (;;) {
9057 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
9058 (100ms).count());
9059 if (nFds < 0) {
9060 FAIL() << "Failed to call epoll_wait";
9061 }
9062 if (nFds == 0) {
9063 break; // epoll_wait timed out
9064 }
9065 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -07009066 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -07009067 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009068 channels[i]->consumeMotionDown();
9069 }
9070 }
9071
9072 // Verify the order in which the events were received.
9073 EXPECT_EQ(3u, eventOrder.size());
9074 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
9075 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
9076 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
9077}
9078
9079/**
9080 * A spy window using the NOT_TOUCHABLE flag does not receive events.
9081 */
9082TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
9083 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009084 auto spy = createSpy();
9085 spy->setTouchable(false);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009086 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9087
9088 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9089 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9090 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9091 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9092 spy->assertNoEvents();
9093}
9094
9095/**
9096 * A spy window will only receive gestures that originate within its touchable region. Gestures that
9097 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
9098 * to the window.
9099 */
9100TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
9101 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009102 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009103 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
9104 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9105
9106 // Inject an event outside the spy window's touchable region.
9107 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9108 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9109 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9110 window->consumeMotionDown();
9111 spy->assertNoEvents();
9112 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9113 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9114 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9115 window->consumeMotionUp();
9116 spy->assertNoEvents();
9117
9118 // Inject an event inside the spy window's touchable region.
9119 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9120 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9121 {5, 10}))
9122 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9123 window->consumeMotionDown();
9124 spy->consumeMotionDown();
9125}
9126
9127/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009128 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009129 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009130 */
9131TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
9132 auto window = createForeground();
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00009133 window->setOwnerInfo(gui::Pid{12}, gui::Uid{34});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009134 auto spy = createSpy();
9135 spy->setWatchOutsideTouch(true);
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00009136 spy->setOwnerInfo(gui::Pid{56}, gui::Uid{78});
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009137 spy->setFrame(Rect{0, 0, 20, 20});
9138 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9139
9140 // Inject an event outside the spy window's frame and touchable region.
9141 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009142 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9143 {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009144 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9145 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009146 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009147}
9148
9149/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009150 * Even when a spy window spans over multiple foreground windows, the spy should receive all
9151 * pointers that are down within its bounds.
9152 */
9153TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
9154 auto windowLeft = createForeground();
9155 windowLeft->setFrame({0, 0, 100, 200});
9156 auto windowRight = createForeground();
9157 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009158 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009159 spy->setFrame({0, 0, 200, 200});
9160 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}});
9161
9162 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9163 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9164 {50, 50}))
9165 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9166 windowLeft->consumeMotionDown();
9167 spy->consumeMotionDown();
9168
9169 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08009170 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009171 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009172 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
9173 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009174 .build();
9175 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9176 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9177 InputEventInjectionSync::WAIT_FOR_RESULT))
9178 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9179 windowRight->consumeMotionDown();
Harry Cutts33476232023-01-30 19:57:29 +00009180 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009181}
9182
9183/**
9184 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
9185 * the spy should receive the second pointer with ACTION_DOWN.
9186 */
9187TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
9188 auto window = createForeground();
9189 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009190 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009191 spyRight->setFrame({100, 0, 200, 200});
9192 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}});
9193
9194 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9195 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9196 {50, 50}))
9197 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9198 window->consumeMotionDown();
9199 spyRight->assertNoEvents();
9200
9201 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08009202 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009203 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009204 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
9205 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009206 .build();
9207 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9208 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9209 InputEventInjectionSync::WAIT_FOR_RESULT))
9210 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00009211 window->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009212 spyRight->consumeMotionDown();
9213}
9214
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009215/**
9216 * The spy window should not be able to affect whether or not touches are split. Only the foreground
9217 * windows should be allowed to control split touch.
9218 */
9219TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08009220 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009221 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009222 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08009223 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009224
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009225 auto window = createForeground();
9226 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009227
9228 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9229
9230 // First finger down, no window touched.
9231 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9232 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9233 {100, 200}))
9234 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9235 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9236 window->assertNoEvents();
9237
9238 // Second finger down on window, the window should receive touch down.
9239 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08009240 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009241 .displayId(ADISPLAY_ID_DEFAULT)
9242 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009243 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9244 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009245 .build();
9246 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9247 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9248 InputEventInjectionSync::WAIT_FOR_RESULT))
9249 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9250
9251 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00009252 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009253}
9254
9255/**
9256 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
9257 * do not receive key events.
9258 */
9259TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009260 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009261 spy->setFocusable(false);
9262
9263 auto window = createForeground();
9264 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9265 setFocusedWindow(window);
9266 window->consumeFocusEvent(true);
9267
9268 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
9269 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
9270 window->consumeKeyDown(ADISPLAY_ID_NONE);
9271
9272 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
9273 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
9274 window->consumeKeyUp(ADISPLAY_ID_NONE);
9275
9276 spy->assertNoEvents();
9277}
9278
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009279using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
9280
9281/**
9282 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
9283 * are currently sent to any other windows - including other spy windows - will also be cancelled.
9284 */
9285TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
9286 auto window = createForeground();
9287 auto spy1 = createSpy();
9288 auto spy2 = createSpy();
9289 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}});
9290
9291 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9292 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9293 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9294 window->consumeMotionDown();
9295 spy1->consumeMotionDown();
9296 spy2->consumeMotionDown();
9297
9298 // Pilfer pointers from the second spy window.
9299 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
9300 spy2->assertNoEvents();
9301 spy1->consumeMotionCancel();
9302 window->consumeMotionCancel();
9303
9304 // The rest of the gesture should only be sent to the second spy window.
9305 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9306 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
9307 ADISPLAY_ID_DEFAULT))
9308 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9309 spy2->consumeMotionMove();
9310 spy1->assertNoEvents();
9311 window->assertNoEvents();
9312}
9313
9314/**
9315 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
9316 * in the middle of the gesture.
9317 */
9318TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
9319 auto window = createForeground();
9320 auto spy = createSpy();
9321 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9322
9323 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9324 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9325 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9326 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9327 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9328
9329 window->releaseChannel();
9330
9331 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9332
9333 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9334 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9335 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9336 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
9337}
9338
9339/**
9340 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
9341 * the spy, but not to any other windows.
9342 */
9343TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
9344 auto spy = createSpy();
9345 auto window = createForeground();
9346
9347 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9348
9349 // First finger down on the window and the spy.
9350 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9351 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9352 {100, 200}))
9353 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9354 spy->consumeMotionDown();
9355 window->consumeMotionDown();
9356
9357 // Spy window pilfers the pointers.
9358 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9359 window->consumeMotionCancel();
9360
9361 // Second finger down on the window and spy, but the window should not receive the pointer down.
9362 const MotionEvent secondFingerDownEvent =
9363 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9364 .displayId(ADISPLAY_ID_DEFAULT)
9365 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009366 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9367 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009368 .build();
9369 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9370 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9371 InputEventInjectionSync::WAIT_FOR_RESULT))
9372 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9373
Harry Cutts33476232023-01-30 19:57:29 +00009374 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009375
9376 // Third finger goes down outside all windows, so injection should fail.
9377 const MotionEvent thirdFingerDownEvent =
9378 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9379 .displayId(ADISPLAY_ID_DEFAULT)
9380 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009381 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9382 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
9383 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009384 .build();
9385 ASSERT_EQ(InputEventInjectionResult::FAILED,
9386 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
9387 InputEventInjectionSync::WAIT_FOR_RESULT))
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08009388 << "Inject motion event should return InputEventInjectionResult::FAILED";
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009389
9390 spy->assertNoEvents();
9391 window->assertNoEvents();
9392}
9393
9394/**
9395 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
9396 */
9397TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
9398 auto spy = createSpy();
9399 spy->setFrame(Rect(0, 0, 100, 100));
9400 auto window = createForeground();
9401 window->setFrame(Rect(0, 0, 200, 200));
9402
9403 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9404
9405 // First finger down on the window only
9406 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9407 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9408 {150, 150}))
9409 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9410 window->consumeMotionDown();
9411
9412 // Second finger down on the spy and window
9413 const MotionEvent secondFingerDownEvent =
9414 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9415 .displayId(ADISPLAY_ID_DEFAULT)
9416 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009417 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
9418 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009419 .build();
9420 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9421 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9422 InputEventInjectionSync::WAIT_FOR_RESULT))
9423 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9424 spy->consumeMotionDown();
9425 window->consumeMotionPointerDown(1);
9426
9427 // Third finger down on the spy and window
9428 const MotionEvent thirdFingerDownEvent =
9429 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9430 .displayId(ADISPLAY_ID_DEFAULT)
9431 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009432 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
9433 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
9434 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009435 .build();
9436 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9437 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
9438 InputEventInjectionSync::WAIT_FOR_RESULT))
9439 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9440 spy->consumeMotionPointerDown(1);
9441 window->consumeMotionPointerDown(2);
9442
9443 // Spy window pilfers the pointers.
9444 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9445 window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
9446 window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
9447
9448 spy->assertNoEvents();
9449 window->assertNoEvents();
9450}
9451
9452/**
9453 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
9454 * other windows should be canceled. If this results in the cancellation of all pointers for some
9455 * window, then that window should receive ACTION_CANCEL.
9456 */
9457TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
9458 auto spy = createSpy();
9459 spy->setFrame(Rect(0, 0, 100, 100));
9460 auto window = createForeground();
9461 window->setFrame(Rect(0, 0, 200, 200));
9462
9463 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9464
9465 // First finger down on both spy and window
9466 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9467 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9468 {10, 10}))
9469 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9470 window->consumeMotionDown();
9471 spy->consumeMotionDown();
9472
9473 // Second finger down on the spy and window
9474 const MotionEvent secondFingerDownEvent =
9475 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9476 .displayId(ADISPLAY_ID_DEFAULT)
9477 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009478 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9479 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009480 .build();
9481 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9482 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9483 InputEventInjectionSync::WAIT_FOR_RESULT))
9484 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9485 spy->consumeMotionPointerDown(1);
9486 window->consumeMotionPointerDown(1);
9487
9488 // Spy window pilfers the pointers.
9489 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9490 window->consumeMotionCancel();
9491
9492 spy->assertNoEvents();
9493 window->assertNoEvents();
9494}
9495
9496/**
9497 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
9498 * be sent to other windows
9499 */
9500TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
9501 auto spy = createSpy();
9502 spy->setFrame(Rect(0, 0, 100, 100));
9503 auto window = createForeground();
9504 window->setFrame(Rect(0, 0, 200, 200));
9505
9506 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9507
9508 // First finger down on both window and spy
9509 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9510 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9511 {10, 10}))
9512 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9513 window->consumeMotionDown();
9514 spy->consumeMotionDown();
9515
9516 // Spy window pilfers the pointers.
9517 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9518 window->consumeMotionCancel();
9519
9520 // Second finger down on the window only
9521 const MotionEvent secondFingerDownEvent =
9522 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9523 .displayId(ADISPLAY_ID_DEFAULT)
9524 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009525 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9526 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009527 .build();
9528 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9529 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9530 InputEventInjectionSync::WAIT_FOR_RESULT))
9531 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9532 window->consumeMotionDown();
9533 window->assertNoEvents();
9534
9535 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
9536 spy->consumeMotionMove();
9537 spy->assertNoEvents();
9538}
9539
Prabir Pradhand65552b2021-10-07 11:23:50 -07009540class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
9541public:
9542 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
9543 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9544 std::make_shared<FakeApplicationHandle>();
9545 sp<FakeWindowHandle> overlay =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009546 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
9547 "Stylus interceptor window", ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009548 overlay->setFocusable(false);
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00009549 overlay->setOwnerInfo(gui::Pid{111}, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009550 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009551 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009552 overlay->setTrustedOverlay(true);
9553
9554 std::shared_ptr<FakeApplicationHandle> application =
9555 std::make_shared<FakeApplicationHandle>();
9556 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009557 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
9558 ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009559 window->setFocusable(true);
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00009560 window->setOwnerInfo(gui::Pid{222}, gui::Uid{222});
Prabir Pradhand65552b2021-10-07 11:23:50 -07009561
9562 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
9563 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9564 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00009565 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009566 return {std::move(overlay), std::move(window)};
9567 }
9568
9569 void sendFingerEvent(int32_t action) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00009570 mDispatcher->notifyMotion(
Prabir Pradhand65552b2021-10-07 11:23:50 -07009571 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
Prabir Pradhan678438e2023-04-13 19:32:51 +00009572 ADISPLAY_ID_DEFAULT, {PointF{20, 20}}));
Prabir Pradhand65552b2021-10-07 11:23:50 -07009573 }
9574
9575 void sendStylusEvent(int32_t action) {
9576 NotifyMotionArgs motionArgs =
9577 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
9578 ADISPLAY_ID_DEFAULT, {PointF{30, 40}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009579 motionArgs.pointerProperties[0].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +00009580 mDispatcher->notifyMotion(motionArgs);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009581 }
9582};
9583
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08009584using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
9585
9586TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
Siarhei Vishniakouad3b6822023-06-22 14:17:35 -07009587 testing::GTEST_FLAG(death_test_style) = "threadsafe";
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08009588 ScopedSilentDeath _silentDeath;
9589
Prabir Pradhand65552b2021-10-07 11:23:50 -07009590 auto [overlay, window] = setupStylusOverlayScenario();
9591 overlay->setTrustedOverlay(false);
9592 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
9593 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}),
9594 ".* not a trusted overlay");
9595}
9596
9597TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
9598 auto [overlay, window] = setupStylusOverlayScenario();
9599 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9600
9601 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9602 overlay->consumeMotionDown();
9603 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9604 overlay->consumeMotionUp();
9605
9606 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9607 window->consumeMotionDown();
9608 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9609 window->consumeMotionUp();
9610
9611 overlay->assertNoEvents();
9612 window->assertNoEvents();
9613}
9614
9615TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
9616 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009617 overlay->setSpy(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009618 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9619
9620 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9621 overlay->consumeMotionDown();
9622 window->consumeMotionDown();
9623 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9624 overlay->consumeMotionUp();
9625 window->consumeMotionUp();
9626
9627 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9628 window->consumeMotionDown();
9629 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9630 window->consumeMotionUp();
9631
9632 overlay->assertNoEvents();
9633 window->assertNoEvents();
9634}
9635
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00009636/**
9637 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
9638 * The scenario is as follows:
9639 * - The stylus interceptor overlay is configured as a spy window.
9640 * - The stylus interceptor spy receives the start of a new stylus gesture.
9641 * - It pilfers pointers and then configures itself to no longer be a spy.
9642 * - The stylus interceptor continues to receive the rest of the gesture.
9643 */
9644TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
9645 auto [overlay, window] = setupStylusOverlayScenario();
9646 overlay->setSpy(true);
9647 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9648
9649 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9650 overlay->consumeMotionDown();
9651 window->consumeMotionDown();
9652
9653 // The interceptor pilfers the pointers.
9654 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
9655 window->consumeMotionCancel();
9656
9657 // The interceptor configures itself so that it is no longer a spy.
9658 overlay->setSpy(false);
9659 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9660
9661 // It continues to receive the rest of the stylus gesture.
9662 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
9663 overlay->consumeMotionMove();
9664 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9665 overlay->consumeMotionUp();
9666
9667 window->assertNoEvents();
9668}
9669
Prabir Pradhan5735a322022-04-11 17:23:34 +00009670struct User {
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00009671 gui::Pid mPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00009672 gui::Uid mUid;
Prabir Pradhan5735a322022-04-11 17:23:34 +00009673 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
9674 std::unique_ptr<InputDispatcher>& mDispatcher;
9675
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00009676 User(std::unique_ptr<InputDispatcher>& dispatcher, gui::Pid pid, gui::Uid uid)
Prabir Pradhan5735a322022-04-11 17:23:34 +00009677 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
9678
9679 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
9680 return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
9681 ADISPLAY_ID_DEFAULT, {100, 200},
9682 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
9683 AMOTION_EVENT_INVALID_CURSOR_POSITION},
9684 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
9685 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
9686 }
9687
9688 InputEventInjectionResult injectTargetedKey(int32_t action) const {
Harry Cutts33476232023-01-30 19:57:29 +00009689 return inputdispatcher::injectKey(mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +00009690 InputEventInjectionSync::WAIT_FOR_RESULT,
Harry Cutts33476232023-01-30 19:57:29 +00009691 INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid},
Prabir Pradhan5735a322022-04-11 17:23:34 +00009692 mPolicyFlags);
9693 }
9694
9695 sp<FakeWindowHandle> createWindow() const {
9696 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9697 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009698 sp<FakeWindowHandle> window =
9699 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window",
9700 ADISPLAY_ID_DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +00009701 window->setOwnerInfo(mPid, mUid);
9702 return window;
9703 }
9704};
9705
9706using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
9707
9708TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00009709 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009710 auto window = owner.createWindow();
9711 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9712
9713 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9714 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9715 window->consumeMotionDown();
9716
9717 setFocusedWindow(window);
9718 window->consumeFocusEvent(true);
9719
9720 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9721 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9722 window->consumeKeyDown(ADISPLAY_ID_NONE);
9723}
9724
9725TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00009726 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009727 auto window = owner.createWindow();
9728 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9729
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00009730 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009731 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9732 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9733
9734 setFocusedWindow(window);
9735 window->consumeFocusEvent(true);
9736
9737 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9738 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9739 window->assertNoEvents();
9740}
9741
9742TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00009743 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009744 auto window = owner.createWindow();
9745 auto spy = owner.createWindow();
9746 spy->setSpy(true);
9747 spy->setTrustedOverlay(true);
9748 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9749
9750 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9751 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9752 spy->consumeMotionDown();
9753 window->consumeMotionDown();
9754}
9755
9756TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00009757 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009758 auto window = owner.createWindow();
9759
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00009760 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009761 auto randosSpy = rando.createWindow();
9762 randosSpy->setSpy(true);
9763 randosSpy->setTrustedOverlay(true);
9764 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9765
9766 // The event is targeted at owner's window, so injection should succeed, but the spy should
9767 // not receive the event.
9768 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9769 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9770 randosSpy->assertNoEvents();
9771 window->consumeMotionDown();
9772}
9773
9774TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00009775 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009776 auto window = owner.createWindow();
9777
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00009778 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009779 auto randosSpy = rando.createWindow();
9780 randosSpy->setSpy(true);
9781 randosSpy->setTrustedOverlay(true);
9782 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9783
9784 // A user that has injection permission can inject into any window.
9785 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9786 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
9787 ADISPLAY_ID_DEFAULT));
9788 randosSpy->consumeMotionDown();
9789 window->consumeMotionDown();
9790
9791 setFocusedWindow(randosSpy);
9792 randosSpy->consumeFocusEvent(true);
9793
9794 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
9795 randosSpy->consumeKeyDown(ADISPLAY_ID_NONE);
9796 window->assertNoEvents();
9797}
9798
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -07009799TEST_F(InputDispatcherTargetedInjectionTest, CannotGenerateActionOutsideToOtherUids) {
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00009800 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009801 auto window = owner.createWindow();
9802
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00009803 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009804 auto randosWindow = rando.createWindow();
9805 randosWindow->setFrame(Rect{-10, -10, -5, -5});
9806 randosWindow->setWatchOutsideTouch(true);
9807 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}});
9808
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -07009809 // Do not allow generation of ACTION_OUTSIDE events into windows owned by different uids.
Prabir Pradhan5735a322022-04-11 17:23:34 +00009810 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9811 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9812 window->consumeMotionDown();
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -07009813 randosWindow->assertNoEvents();
Prabir Pradhan5735a322022-04-11 17:23:34 +00009814}
9815
Garfield Tane84e6f92019-08-29 17:28:41 -07009816} // namespace android::inputdispatcher