blob: f4f0bab25e2342f83e3626702157f8d4955ea03b [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/**
Daniel Norman2f99cdb2023-08-02 16:39:45 -07003411 * Test that invalid HOVER events sent by accessibility do not cause a fatal crash.
3412 */
3413TEST_F(InputDispatcherTest, InvalidA11yHoverStreamDoesNotCrash) {
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, 1200, 800));
3418 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3419 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3420
3421 MotionEventBuilder hoverEnterBuilder =
3422 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
3423 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
3424 .addFlag(AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
3425 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3426 injectMotionEvent(mDispatcher, hoverEnterBuilder.build()));
3427 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3428 injectMotionEvent(mDispatcher, hoverEnterBuilder.build()));
3429 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
3430 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
3431}
3432
3433/**
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003434 * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT.
3435 */
3436TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) {
3437 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3438 sp<FakeWindowHandle> window =
3439 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3440 window->setFrame(Rect(0, 0, 100, 100));
3441
3442 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3443
3444 const int32_t mouseDeviceId = 7;
3445 const int32_t touchDeviceId = 4;
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003446
3447 // Start hovering with the mouse
Prabir Pradhan678438e2023-04-13 19:32:51 +00003448 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
3449 .deviceId(mouseDeviceId)
3450 .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10))
3451 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003452 window->consumeMotionEvent(
3453 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3454
3455 // Touch goes down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003456 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3457 .deviceId(touchDeviceId)
3458 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
3459 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003460
3461 window->consumeMotionEvent(
3462 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
3463 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3464}
3465
3466/**
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003467 * Inject a mouse hover event followed by a tap from touchscreen.
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003468 * The tap causes a HOVER_EXIT event to be generated because the current event
3469 * stream's source has been switched.
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003470 */
3471TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) {
3472 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3473 sp<FakeWindowHandle> window =
3474 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3475 window->setFrame(Rect(0, 0, 100, 100));
3476
3477 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3478
3479 // Inject a hover_move from mouse.
3480 NotifyMotionArgs motionArgs =
3481 generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE,
3482 ADISPLAY_ID_DEFAULT, {{50, 50}});
3483 motionArgs.xCursorPosition = 50;
3484 motionArgs.yCursorPosition = 50;
Prabir Pradhan678438e2023-04-13 19:32:51 +00003485 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003486 ASSERT_NO_FATAL_FAILURE(
3487 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3488 WithSource(AINPUT_SOURCE_MOUSE))));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003489
3490 // Tap on the window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003491 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3492 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3493 {{10, 10}}));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003494 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003495 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3496 WithSource(AINPUT_SOURCE_MOUSE))));
3497
3498 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003499 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3500 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
3501
Prabir Pradhan678438e2023-04-13 19:32:51 +00003502 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3503 ADISPLAY_ID_DEFAULT, {{10, 10}}));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003504 ASSERT_NO_FATAL_FAILURE(
3505 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3506 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
3507}
3508
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003509TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) {
3510 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3511 sp<FakeWindowHandle> windowDefaultDisplay =
3512 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
3513 ADISPLAY_ID_DEFAULT);
3514 windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800));
3515 sp<FakeWindowHandle> windowSecondDisplay =
3516 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay",
3517 SECOND_DISPLAY_ID);
3518 windowSecondDisplay->setFrame(Rect(0, 0, 600, 800));
3519
3520 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
3521 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
3522
3523 // Set cursor position in window in default display and check that hover enter and move
3524 // events are generated.
3525 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3526 injectMotionEvent(mDispatcher,
3527 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3528 AINPUT_SOURCE_MOUSE)
3529 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003530 .pointer(PointerBuilder(0, ToolType::MOUSE)
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003531 .x(300)
3532 .y(600))
3533 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003534 windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003535
3536 // Remove all windows in secondary display and check that no event happens on window in
3537 // primary display.
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003538 mDispatcher->setInputWindows(
3539 {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003540 windowDefaultDisplay->assertNoEvents();
3541
3542 // Move cursor position in window in default display and check that only hover move
3543 // event is generated and not hover enter event.
3544 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
3545 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
3546 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3547 injectMotionEvent(mDispatcher,
3548 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3549 AINPUT_SOURCE_MOUSE)
3550 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003551 .pointer(PointerBuilder(0, ToolType::MOUSE)
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003552 .x(400)
3553 .y(700))
3554 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003555 windowDefaultDisplay->consumeMotionEvent(
3556 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3557 WithSource(AINPUT_SOURCE_MOUSE)));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003558 windowDefaultDisplay->assertNoEvents();
3559}
3560
Garfield Tan00f511d2019-06-12 16:55:40 -07003561TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07003562 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07003563
3564 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003565 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003566 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07003567 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003568 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003569 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07003570
3571 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3572
Arthur Hung72d8dc32020-03-28 00:48:39 +00003573 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07003574
3575 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
3576 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003577 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07003578 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003579 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003580 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003581 windowRight->assertNoEvents();
3582}
3583
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003584TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
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);
Vishnu Nair47074b82020-08-14 11:54:47 -07003588 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003589
Arthur Hung72d8dc32020-03-28 00:48:39 +00003590 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003591 setFocusedWindow(window);
3592
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003593 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003594
Prabir Pradhan678438e2023-04-13 19:32:51 +00003595 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003596
3597 // Window should receive key down event.
3598 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3599
3600 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
3601 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003602 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07003603 window->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003604 AKEY_EVENT_FLAG_CANCELED);
3605}
3606
3607TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07003608 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003609 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3610 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003611
Arthur Hung72d8dc32020-03-28 00:48:39 +00003612 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003613
Prabir Pradhan678438e2023-04-13 19:32:51 +00003614 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3615 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003616
3617 // Window should receive motion down event.
3618 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3619
3620 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
3621 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003622 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08003623 window->consumeMotionEvent(
3624 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003625}
3626
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -07003627TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) {
3628 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3629 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3630 "Fake Window", ADISPLAY_ID_DEFAULT);
3631
3632 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3633
3634 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3635 .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10))
3636 .build());
3637
3638 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3639
3640 // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT
3641 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
3642 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3643
3644 // After the device has been reset, a new hovering stream can be sent to the window
3645 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3646 .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15))
3647 .build());
3648 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3649}
3650
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003651TEST_F(InputDispatcherTest, InterceptKeyByPolicy) {
3652 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003653 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3654 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003655 window->setFocusable(true);
3656
3657 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3658 setFocusedWindow(window);
3659
3660 window->consumeFocusEvent(true);
3661
Prabir Pradhan678438e2023-04-13 19:32:51 +00003662 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003663 const std::chrono::milliseconds interceptKeyTimeout = 50ms;
3664 const nsecs_t injectTime = keyArgs.eventTime;
3665 mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout);
Prabir Pradhan678438e2023-04-13 19:32:51 +00003666 mDispatcher->notifyKey(keyArgs);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003667 // The dispatching time should be always greater than or equal to intercept key timeout.
3668 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3669 ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >=
3670 std::chrono::nanoseconds(interceptKeyTimeout).count());
3671}
3672
Siarhei Vishniakouf54d2ab2023-06-09 13:23:53 -07003673/**
3674 * Keys with ACTION_UP are delivered immediately, even if a long 'intercept key timeout' is set.
3675 */
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003676TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) {
3677 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003678 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3679 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003680 window->setFocusable(true);
3681
3682 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3683 setFocusedWindow(window);
3684
3685 window->consumeFocusEvent(true);
3686
Prabir Pradhan678438e2023-04-13 19:32:51 +00003687 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003688 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf54d2ab2023-06-09 13:23:53 -07003689
3690 // Set a value that's significantly larger than the default consumption timeout. If the
3691 // implementation is correct, the actual value doesn't matter; it won't slow down the test.
3692 mFakePolicy->setInterceptKeyTimeout(600ms);
3693 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
3694 // Window should receive key event immediately when same key up.
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003695 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3696}
3697
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003698/**
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003699 * Two windows. First is a regular window. Second does not overlap with the first, and has
3700 * WATCH_OUTSIDE_TOUCH.
3701 * Both windows are owned by the same UID.
3702 * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero
3703 * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID.
3704 */
3705TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) {
3706 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3707 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3708 "First Window", ADISPLAY_ID_DEFAULT);
3709 window->setFrame(Rect{0, 0, 100, 100});
3710
3711 sp<FakeWindowHandle> outsideWindow =
3712 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3713 ADISPLAY_ID_DEFAULT);
3714 outsideWindow->setFrame(Rect{100, 100, 200, 200});
3715 outsideWindow->setWatchOutsideTouch(true);
3716 // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped
3717 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {outsideWindow, window}}});
3718
3719 // Tap on first window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003720 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3721 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3722 {PointF{50, 50}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003723 window->consumeMotionDown();
3724 // The coordinates of the tap in 'outsideWindow' are relative to its top left corner.
3725 // Therefore, we should offset them by (100, 100) relative to the screen's top left corner.
3726 outsideWindow->consumeMotionEvent(
3727 AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50)));
3728}
3729
3730/**
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003731 * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when
3732 * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one
3733 * ACTION_OUTSIDE event is sent per gesture.
3734 */
3735TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) {
3736 // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH.
3737 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003738 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3739 "First Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003740 window->setWatchOutsideTouch(true);
3741 window->setFrame(Rect{0, 0, 100, 100});
3742 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003743 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3744 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003745 secondWindow->setFrame(Rect{100, 100, 200, 200});
3746 sp<FakeWindowHandle> thirdWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003747 sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window",
3748 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003749 thirdWindow->setFrame(Rect{200, 200, 300, 300});
3750 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}});
3751
3752 // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003753 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3754 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3755 {PointF{-10, -10}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003756 window->assertNoEvents();
3757 secondWindow->assertNoEvents();
3758
3759 // The second pointer lands inside `secondWindow`, which should receive a DOWN event.
3760 // Now, `window` should get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003761 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3762 ADISPLAY_ID_DEFAULT,
3763 {PointF{-10, -10}, PointF{105, 105}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003764 const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}};
3765 window->consumeMotionEvent(
3766 AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers)));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003767 secondWindow->consumeMotionDown();
3768 thirdWindow->assertNoEvents();
3769
3770 // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is
3771 // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003772 mDispatcher->notifyMotion(
3773 generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3774 {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003775 window->assertNoEvents();
3776 secondWindow->consumeMotionMove();
3777 thirdWindow->consumeMotionDown();
3778}
3779
Prabir Pradhan814fe082022-07-22 20:22:18 +00003780TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) {
3781 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003782 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3783 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003784 window->setFocusable(true);
3785
Patrick Williamsd828f302023-04-28 17:52:08 -05003786 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00003787 setFocusedWindow(window);
3788
3789 window->consumeFocusEvent(true);
3790
Prabir Pradhan678438e2023-04-13 19:32:51 +00003791 const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3792 const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
3793 mDispatcher->notifyKey(keyDown);
3794 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003795
3796 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3797 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3798
3799 // All windows are removed from the display. Ensure that we can no longer dispatch to it.
Patrick Williamsd828f302023-04-28 17:52:08 -05003800 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00003801
3802 window->consumeFocusEvent(false);
3803
Prabir Pradhan678438e2023-04-13 19:32:51 +00003804 mDispatcher->notifyKey(keyDown);
3805 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003806 window->assertNoEvents();
3807}
3808
Arthur Hung96483742022-11-15 03:30:48 +00003809TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) {
3810 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3811 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3812 "Fake Window", ADISPLAY_ID_DEFAULT);
3813 // Ensure window is non-split and have some transform.
3814 window->setPreventSplitting(true);
3815 window->setWindowOffset(20, 40);
Patrick Williamsd828f302023-04-28 17:52:08 -05003816 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung96483742022-11-15 03:30:48 +00003817
3818 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3819 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3820 {50, 50}))
3821 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3822 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3823
3824 const MotionEvent secondFingerDownEvent =
3825 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3826 .displayId(ADISPLAY_ID_DEFAULT)
3827 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003828 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
3829 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50))
Arthur Hung96483742022-11-15 03:30:48 +00003830 .build();
3831 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3832 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
3833 InputEventInjectionSync::WAIT_FOR_RESULT))
3834 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3835
3836 const MotionEvent* event = window->consumeMotion();
3837 EXPECT_EQ(POINTER_1_DOWN, event->getAction());
3838 EXPECT_EQ(70, event->getX(0)); // 50 + 20
3839 EXPECT_EQ(90, event->getY(0)); // 50 + 40
3840 EXPECT_EQ(-10, event->getX(1)); // -30 + 20
3841 EXPECT_EQ(-10, event->getY(1)); // -50 + 40
3842}
3843
Harry Cuttsb166c002023-05-09 13:06:05 +00003844TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) {
3845 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3846 sp<FakeWindowHandle> window =
3847 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3848 window->setFrame(Rect(0, 0, 400, 400));
3849 sp<FakeWindowHandle> trustedOverlay =
3850 sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay",
3851 ADISPLAY_ID_DEFAULT);
3852 trustedOverlay->setSpy(true);
3853 trustedOverlay->setTrustedOverlay(true);
3854
3855 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {trustedOverlay, window}}});
3856
3857 // Start a three-finger touchpad swipe
3858 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3859 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3860 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3861 .build());
3862 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
3863 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3864 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3865 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3866 .build());
3867 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
3868 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3869 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3870 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
3871 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3872 .build());
3873
3874 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3875 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
3876 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN));
3877
3878 // Move the swipe a bit
3879 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
3880 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3881 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3882 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3883 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3884 .build());
3885
3886 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3887
3888 // End the swipe
3889 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
3890 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3891 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3892 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3893 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3894 .build());
3895 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
3896 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3897 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3898 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3899 .build());
3900 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
3901 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3902 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3903 .build());
3904
3905 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP));
3906 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
3907 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP));
3908
3909 window->assertNoEvents();
3910}
3911
3912TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) {
3913 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3914 sp<FakeWindowHandle> window =
3915 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3916 window->setFrame(Rect(0, 0, 400, 400));
3917 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3918
3919 // Start a three-finger touchpad swipe
3920 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3921 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3922 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3923 .build());
3924 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
3925 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3926 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3927 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3928 .build());
3929 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
3930 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3931 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3932 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
3933 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3934 .build());
3935
3936 // Move the swipe a bit
3937 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
3938 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3939 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3940 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3941 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3942 .build());
3943
3944 // End the swipe
3945 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
3946 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3947 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3948 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3949 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3950 .build());
3951 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
3952 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3953 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3954 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3955 .build());
3956 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
3957 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3958 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3959 .build());
3960
3961 window->assertNoEvents();
3962}
3963
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003964/**
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07003965 * Send a two-pointer gesture to a single window. The window's orientation changes in response to
3966 * the first pointer.
3967 * Ensure that the second pointer is not sent to the window.
3968 *
3969 * The subsequent gesture should be correctly delivered to the window.
3970 */
3971TEST_F(InputDispatcherTest, MultiplePointersWithRotatingWindow) {
3972 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3973 sp<FakeWindowHandle> window =
3974 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3975 window->setFrame(Rect(0, 0, 400, 400));
3976 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3977
3978 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
3979 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3980 .downTime(baseTime + 10)
3981 .eventTime(baseTime + 10)
3982 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3983 .build());
3984
3985 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3986
3987 // We need a new window object for the same window, because dispatcher will store objects by
3988 // reference. That means that the testing code and the dispatcher will refer to the same shared
3989 // object. Calling window->setTransform here would affect dispatcher's comparison
3990 // of the old window to the new window, since both the old window and the new window would be
3991 // updated to the same value.
3992 sp<FakeWindowHandle> windowDup = window->duplicate();
3993
3994 // Change the transform so that the orientation is now different from original.
3995 windowDup->setWindowTransform(0, -1, 1, 0);
3996
3997 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDup}}});
3998
3999 window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
4000
4001 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4002 .downTime(baseTime + 10)
4003 .eventTime(baseTime + 30)
4004 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4005 .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200))
4006 .build());
4007
4008 // Finish the gesture and start a new one. Ensure the new gesture is sent to the window
4009 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
4010 .downTime(baseTime + 10)
4011 .eventTime(baseTime + 40)
4012 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4013 .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200))
4014 .build());
4015 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
4016 .downTime(baseTime + 10)
4017 .eventTime(baseTime + 50)
4018 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4019 .build());
4020
4021 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4022 .downTime(baseTime + 60)
4023 .eventTime(baseTime + 60)
4024 .pointer(PointerBuilder(0, ToolType::FINGER).x(40).y(40))
4025 .build());
4026
4027 windowDup->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
4028}
4029
4030/**
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004031 * Ensure the correct coordinate spaces are used by InputDispatcher.
4032 *
4033 * InputDispatcher works in the display space, so its coordinate system is relative to the display
4034 * panel. Windows get events in the window space, and get raw coordinates in the logical display
4035 * space.
4036 */
4037class InputDispatcherDisplayProjectionTest : public InputDispatcherTest {
4038public:
4039 void SetUp() override {
4040 InputDispatcherTest::SetUp();
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004041 removeAllWindowsAndDisplays();
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004042 }
4043
4044 void addDisplayInfo(int displayId, const ui::Transform& transform) {
4045 gui::DisplayInfo info;
4046 info.displayId = displayId;
4047 info.transform = transform;
4048 mDisplayInfos.push_back(std::move(info));
Patrick Williamsd828f302023-04-28 17:52:08 -05004049 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004050 }
4051
4052 void addWindow(const sp<WindowInfoHandle>& windowHandle) {
4053 mWindowInfos.push_back(*windowHandle->getInfo());
Patrick Williamsd828f302023-04-28 17:52:08 -05004054 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004055 }
4056
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004057 void removeAllWindowsAndDisplays() {
4058 mDisplayInfos.clear();
4059 mWindowInfos.clear();
4060 }
4061
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004062 // Set up a test scenario where the display has a scaled projection and there are two windows
4063 // on the display.
4064 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() {
4065 // The display has a projection that has a scale factor of 2 and 4 in the x and y directions
4066 // respectively.
4067 ui::Transform displayTransform;
4068 displayTransform.set(2, 0, 0, 4);
4069 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
4070
4071 std::shared_ptr<FakeApplicationHandle> application =
4072 std::make_shared<FakeApplicationHandle>();
4073
4074 // Add two windows to the display. Their frames are represented in the display space.
4075 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004076 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4077 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004078 firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform);
4079 addWindow(firstWindow);
4080
4081 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004082 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4083 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004084 secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform);
4085 addWindow(secondWindow);
4086 return {std::move(firstWindow), std::move(secondWindow)};
4087 }
4088
4089private:
4090 std::vector<gui::DisplayInfo> mDisplayInfos;
4091 std::vector<gui::WindowInfo> mWindowInfos;
4092};
4093
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004094TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) {
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004095 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4096 // Send down to the first window. The point is represented in the display space. The point is
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004097 // selected so that if the hit test was performed with the point and the bounds being in
4098 // different coordinate spaces, the event would end up in the incorrect window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004099 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4100 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4101 {PointF{75, 55}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004102
4103 firstWindow->consumeMotionDown();
4104 secondWindow->assertNoEvents();
4105}
4106
4107// Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API,
4108// the event should be treated as being in the logical display space.
4109TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) {
4110 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4111 // Send down to the first window. The point is represented in the logical display space. The
4112 // point is selected so that if the hit test was done in logical display space, then it would
4113 // end up in the incorrect window.
4114 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4115 PointF{75 * 2, 55 * 4});
4116
4117 firstWindow->consumeMotionDown();
4118 secondWindow->assertNoEvents();
4119}
4120
Prabir Pradhandaa2f142021-12-10 09:30:08 +00004121// Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed
4122// event should be treated as being in the logical display space.
4123TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) {
4124 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4125
4126 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
4127 ui::Transform injectedEventTransform;
4128 injectedEventTransform.set(matrix);
4129 const vec2 expectedPoint{75, 55}; // The injected point in the logical display space.
4130 const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint);
4131
4132 MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4133 .displayId(ADISPLAY_ID_DEFAULT)
4134 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004135 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER)
Prabir Pradhandaa2f142021-12-10 09:30:08 +00004136 .x(untransformedPoint.x)
4137 .y(untransformedPoint.y))
4138 .build();
4139 event.transform(matrix);
4140
4141 injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT,
4142 InputEventInjectionSync::WAIT_FOR_RESULT);
4143
4144 firstWindow->consumeMotionDown();
4145 secondWindow->assertNoEvents();
4146}
4147
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004148TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) {
4149 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4150
4151 // Send down to the second window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004152 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4153 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4154 {PointF{150, 220}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004155
4156 firstWindow->assertNoEvents();
4157 const MotionEvent* event = secondWindow->consumeMotion();
Siarhei Vishniakoub681c202023-05-01 11:22:33 -07004158 ASSERT_NE(nullptr, event);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004159 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction());
4160
4161 // Ensure that the events from the "getRaw" API are in logical display coordinates.
4162 EXPECT_EQ(300, event->getRawX(0));
4163 EXPECT_EQ(880, event->getRawY(0));
4164
4165 // Ensure that the x and y values are in the window's coordinate space.
4166 // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in
4167 // the logical display space. This will be the origin of the window space.
4168 EXPECT_EQ(100, event->getX(0));
4169 EXPECT_EQ(80, event->getY(0));
4170}
4171
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004172/** Ensure consistent behavior of InputDispatcher in all orientations. */
4173class InputDispatcherDisplayOrientationFixture
4174 : public InputDispatcherDisplayProjectionTest,
4175 public ::testing::WithParamInterface<ui::Rotation> {};
4176
4177// This test verifies the touchable region of a window for all rotations of the display by tapping
4178// in different locations on the display, specifically points close to the four corners of a
4179// window.
4180TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) {
4181 constexpr static int32_t displayWidth = 400;
4182 constexpr static int32_t displayHeight = 800;
4183
4184 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4185
4186 const auto rotation = GetParam();
4187
4188 // Set up the display with the specified rotation.
4189 const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270;
4190 const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth;
4191 const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight;
4192 const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation),
4193 logicalDisplayWidth, logicalDisplayHeight);
4194 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
4195
4196 // Create a window with its bounds determined in the logical display.
4197 const Rect frameInLogicalDisplay(100, 100, 200, 300);
4198 const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay);
4199 sp<FakeWindowHandle> window =
4200 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4201 window->setFrame(frameInDisplay, displayTransform);
4202 addWindow(window);
4203
4204 // The following points in logical display space should be inside the window.
4205 static const std::array<vec2, 4> insidePoints{
4206 {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}};
4207 for (const auto pointInsideWindow : insidePoints) {
4208 const vec2 p = displayTransform.inverse().transform(pointInsideWindow);
4209 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004210 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4211 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4212 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004213 window->consumeMotionDown();
4214
Prabir Pradhan678438e2023-04-13 19:32:51 +00004215 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4216 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4217 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004218 window->consumeMotionUp();
4219 }
4220
4221 // The following points in logical display space should be outside the window.
4222 static const std::array<vec2, 5> outsidePoints{
4223 {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}};
4224 for (const auto pointOutsideWindow : outsidePoints) {
4225 const vec2 p = displayTransform.inverse().transform(pointOutsideWindow);
4226 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004227 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4228 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4229 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004230
Prabir Pradhan678438e2023-04-13 19:32:51 +00004231 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4232 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4233 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004234 }
4235 window->assertNoEvents();
4236}
4237
4238// Run the precision tests for all rotations.
4239INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests,
4240 InputDispatcherDisplayOrientationFixture,
4241 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
4242 ui::ROTATION_270),
4243 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
4244 return ftl::enum_string(testParamInfo.param);
4245 });
4246
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004247using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher,
4248 sp<IBinder>, sp<IBinder>)>;
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004249
4250class TransferTouchFixture : public InputDispatcherTest,
4251 public ::testing::WithParamInterface<TransferFunction> {};
4252
4253TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07004254 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004255
4256 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004257 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004258 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4259 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004260 firstWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004261 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004262 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4263 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004264 sp<FakeWindowHandle> wallpaper =
4265 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
4266 wallpaper->setIsWallpaper(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004267 // Add the windows to the dispatcher
Arthur Hungc539dbb2022-12-08 07:45:36 +00004268 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004269
4270 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004271 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4272 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004273
Svet Ganov5d3bc372020-01-26 23:11:07 -08004274 // Only the first window should get the down event
4275 firstWindow->consumeMotionDown();
4276 secondWindow->assertNoEvents();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004277 wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004278
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004279 // Transfer touch to the second window
4280 TransferFunction f = GetParam();
4281 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4282 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004283 // The first window gets cancel and the second gets down
4284 firstWindow->consumeMotionCancel();
4285 secondWindow->consumeMotionDown();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004286 wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004287
4288 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004289 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4290 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004291 // The first window gets no events and the second gets up
4292 firstWindow->assertNoEvents();
4293 secondWindow->consumeMotionUp();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004294 wallpaper->assertNoEvents();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004295}
4296
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004297/**
4298 * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch
4299 * from. When we have spy windows, there are several windows to choose from: either spy, or the
4300 * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most
4301 * natural to the user.
4302 * In this test, we are sending a pointer to both spy window and first window. We then try to
4303 * transfer touch to the second window. The dispatcher should identify the first window as the
4304 * one that should lose the gesture, and therefore the action should be to move the gesture from
4305 * the first window to the second.
4306 * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test
4307 * the other API, as well.
4308 */
4309TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) {
4310 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4311
4312 // Create a couple of windows + a spy window
4313 sp<FakeWindowHandle> spyWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004314 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004315 spyWindow->setTrustedOverlay(true);
4316 spyWindow->setSpy(true);
4317 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004318 sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004319 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004320 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004321
4322 // Add the windows to the dispatcher
4323 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}});
4324
4325 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004326 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4327 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004328 // Only the first window and spy should get the down event
4329 spyWindow->consumeMotionDown();
4330 firstWindow->consumeMotionDown();
4331
4332 // Transfer touch to the second window. Non-spy window should be preferred over the spy window
4333 // if f === 'transferTouch'.
4334 TransferFunction f = GetParam();
4335 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4336 ASSERT_TRUE(success);
4337 // The first window gets cancel and the second gets down
4338 firstWindow->consumeMotionCancel();
4339 secondWindow->consumeMotionDown();
4340
4341 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004342 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4343 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004344 // The first window gets no events and the second+spy get up
4345 firstWindow->assertNoEvents();
4346 spyWindow->consumeMotionUp();
4347 secondWindow->consumeMotionUp();
4348}
4349
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004350TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004351 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004352
4353 PointF touchPoint = {10, 10};
4354
4355 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004356 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004357 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4358 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004359 firstWindow->setPreventSplitting(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004360 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004361 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4362 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004363 secondWindow->setPreventSplitting(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004364
4365 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004366 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004367
4368 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004369 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4370 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4371 {touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004372 // Only the first window should get the down event
4373 firstWindow->consumeMotionDown();
4374 secondWindow->assertNoEvents();
4375
4376 // Send pointer down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004377 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4378 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004379 // Only the first window should get the pointer down event
4380 firstWindow->consumeMotionPointerDown(1);
4381 secondWindow->assertNoEvents();
4382
4383 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004384 TransferFunction f = GetParam();
4385 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4386 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004387 // The first window gets cancel and the second gets down and pointer down
4388 firstWindow->consumeMotionCancel();
4389 secondWindow->consumeMotionDown();
4390 secondWindow->consumeMotionPointerDown(1);
4391
4392 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004393 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4394 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004395 // The first window gets nothing and the second gets pointer up
4396 firstWindow->assertNoEvents();
4397 secondWindow->consumeMotionPointerUp(1);
4398
4399 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004400 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4401 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004402 // The first window gets nothing and the second gets up
4403 firstWindow->assertNoEvents();
4404 secondWindow->consumeMotionUp();
4405}
4406
Arthur Hungc539dbb2022-12-08 07:45:36 +00004407TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) {
4408 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4409
4410 // Create a couple of windows
4411 sp<FakeWindowHandle> firstWindow =
4412 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4413 ADISPLAY_ID_DEFAULT);
4414 firstWindow->setDupTouchToWallpaper(true);
4415 sp<FakeWindowHandle> secondWindow =
4416 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4417 ADISPLAY_ID_DEFAULT);
4418 secondWindow->setDupTouchToWallpaper(true);
4419
4420 sp<FakeWindowHandle> wallpaper1 =
4421 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT);
4422 wallpaper1->setIsWallpaper(true);
4423
4424 sp<FakeWindowHandle> wallpaper2 =
4425 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT);
4426 wallpaper2->setIsWallpaper(true);
4427 // Add the windows to the dispatcher
4428 mDispatcher->setInputWindows(
4429 {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}});
4430
4431 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004432 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4433 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004434
4435 // Only the first window should get the down event
4436 firstWindow->consumeMotionDown();
4437 secondWindow->assertNoEvents();
4438 wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4439 wallpaper2->assertNoEvents();
4440
4441 // Transfer touch focus to the second window
4442 TransferFunction f = GetParam();
4443 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4444 ASSERT_TRUE(success);
4445
4446 // The first window gets cancel and the second gets down
4447 firstWindow->consumeMotionCancel();
4448 secondWindow->consumeMotionDown();
4449 wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4450 wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4451
4452 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004453 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4454 ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004455 // The first window gets no events and the second gets up
4456 firstWindow->assertNoEvents();
4457 secondWindow->consumeMotionUp();
4458 wallpaper1->assertNoEvents();
4459 wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4460}
4461
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004462// For the cases of single pointer touch and two pointers non-split touch, the api's
4463// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
4464// for the case where there are multiple pointers split across several windows.
4465INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
4466 ::testing::Values(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004467 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4468 sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004469 return dispatcher->transferTouch(destChannelToken,
4470 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004471 },
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004472 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4473 sp<IBinder> from, sp<IBinder> to) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004474 return dispatcher->transferTouchFocus(from, to,
Harry Cutts33476232023-01-30 19:57:29 +00004475 /*isDragAndDrop=*/false);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004476 }));
4477
Svet Ganov5d3bc372020-01-26 23:11:07 -08004478TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004479 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004480
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004481 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004482 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4483 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004484 firstWindow->setFrame(Rect(0, 0, 600, 400));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004485
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004486 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004487 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4488 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004489 secondWindow->setFrame(Rect(0, 400, 600, 800));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004490
4491 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004492 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004493
4494 PointF pointInFirst = {300, 200};
4495 PointF pointInSecond = {300, 600};
4496
4497 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004498 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4499 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4500 {pointInFirst}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004501 // Only the first window should get the down event
4502 firstWindow->consumeMotionDown();
4503 secondWindow->assertNoEvents();
4504
4505 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004506 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4507 ADISPLAY_ID_DEFAULT,
4508 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004509 // The first window gets a move and the second a down
4510 firstWindow->consumeMotionMove();
4511 secondWindow->consumeMotionDown();
4512
4513 // Transfer touch focus to the second window
4514 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
4515 // The first window gets cancel and the new gets pointer down (it already saw down)
4516 firstWindow->consumeMotionCancel();
4517 secondWindow->consumeMotionPointerDown(1);
4518
4519 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004520 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4521 ADISPLAY_ID_DEFAULT,
4522 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004523 // The first window gets nothing and the second gets pointer up
4524 firstWindow->assertNoEvents();
4525 secondWindow->consumeMotionPointerUp(1);
4526
4527 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004528 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4529 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004530 // The first window gets nothing and the second gets up
4531 firstWindow->assertNoEvents();
4532 secondWindow->consumeMotionUp();
4533}
4534
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004535// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
4536// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
4537// touch is not supported, so the touch should continue on those windows and the transferred-to
4538// window should get nothing.
4539TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
4540 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4541
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004542 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004543 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4544 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004545 firstWindow->setFrame(Rect(0, 0, 600, 400));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004546
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004547 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004548 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4549 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004550 secondWindow->setFrame(Rect(0, 400, 600, 800));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004551
4552 // Add the windows to the dispatcher
4553 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4554
4555 PointF pointInFirst = {300, 200};
4556 PointF pointInSecond = {300, 600};
4557
4558 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004559 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4560 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4561 {pointInFirst}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004562 // Only the first window should get the down event
4563 firstWindow->consumeMotionDown();
4564 secondWindow->assertNoEvents();
4565
4566 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004567 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4568 ADISPLAY_ID_DEFAULT,
4569 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004570 // The first window gets a move and the second a down
4571 firstWindow->consumeMotionMove();
4572 secondWindow->consumeMotionDown();
4573
4574 // Transfer touch focus to the second window
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004575 const bool transferred =
4576 mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004577 // The 'transferTouch' call should not succeed, because there are 2 touched windows
4578 ASSERT_FALSE(transferred);
4579 firstWindow->assertNoEvents();
4580 secondWindow->assertNoEvents();
4581
4582 // The rest of the dispatch should proceed as normal
4583 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004584 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4585 ADISPLAY_ID_DEFAULT,
4586 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004587 // The first window gets MOVE and the second gets pointer up
4588 firstWindow->consumeMotionMove();
4589 secondWindow->consumeMotionUp();
4590
4591 // Send up event to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004592 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4593 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004594 // The first window gets nothing and the second gets up
4595 firstWindow->consumeMotionUp();
4596 secondWindow->assertNoEvents();
4597}
4598
Arthur Hungabbb9d82021-09-01 14:52:30 +00004599// This case will create two windows and one mirrored window on the default display and mirror
4600// two windows on the second display. It will test if 'transferTouchFocus' works fine if we put
4601// the windows info of second display before default display.
4602TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) {
4603 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4604 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004605 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004606 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004607 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004608 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004609 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004610
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004611 sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004612 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004613
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004614 sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004615 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004616
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004617 sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004618 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004619
4620 // Update window info, let it find window handle of second display first.
4621 mDispatcher->setInputWindows(
4622 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4623 {ADISPLAY_ID_DEFAULT,
4624 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4625
4626 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4627 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4628 {50, 50}))
4629 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4630
4631 // Window should receive motion event.
4632 firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4633
4634 // Transfer touch focus
4635 ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(),
4636 secondWindowInPrimary->getToken()));
4637 // The first window gets cancel.
4638 firstWindowInPrimary->consumeMotionCancel();
4639 secondWindowInPrimary->consumeMotionDown();
4640
4641 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4642 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4643 ADISPLAY_ID_DEFAULT, {150, 50}))
4644 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4645 firstWindowInPrimary->assertNoEvents();
4646 secondWindowInPrimary->consumeMotionMove();
4647
4648 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4649 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4650 {150, 50}))
4651 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4652 firstWindowInPrimary->assertNoEvents();
4653 secondWindowInPrimary->consumeMotionUp();
4654}
4655
4656// Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use
4657// 'transferTouch' api.
4658TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
4659 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4660 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004661 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004662 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004663 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004664 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004665 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004666
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004667 sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004668 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004669
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004670 sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004671 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004672
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004673 sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004674 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004675
4676 // Update window info, let it find window handle of second display first.
4677 mDispatcher->setInputWindows(
4678 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4679 {ADISPLAY_ID_DEFAULT,
4680 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4681
4682 // Touch on second display.
4683 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4684 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50}))
4685 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4686
4687 // Window should receive motion event.
4688 firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4689
4690 // Transfer touch focus
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004691 ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004692
4693 // The first window gets cancel.
4694 firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID);
4695 secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4696
4697 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4698 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4699 SECOND_DISPLAY_ID, {150, 50}))
4700 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4701 firstWindowInPrimary->assertNoEvents();
4702 secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID);
4703
4704 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4705 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
4706 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4707 firstWindowInPrimary->assertNoEvents();
4708 secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID);
4709}
4710
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004711TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004712 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004713 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4714 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004715
Vishnu Nair47074b82020-08-14 11:54:47 -07004716 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004717 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004718 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004719
4720 window->consumeFocusEvent(true);
4721
Prabir Pradhan678438e2023-04-13 19:32:51 +00004722 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004723
4724 // Window should receive key down event.
4725 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Josep del Riob3981622023-04-18 15:49:45 +00004726
4727 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00004728 mDispatcher->waitForIdle();
Josep del Riob3981622023-04-18 15:49:45 +00004729 mFakePolicy->assertUserActivityPoked();
4730}
4731
4732TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) {
4733 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4734 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4735 "Fake Window", ADISPLAY_ID_DEFAULT);
4736
4737 window->setDisableUserActivity(true);
4738 window->setFocusable(true);
4739 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4740 setFocusedWindow(window);
4741
4742 window->consumeFocusEvent(true);
4743
4744 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4745
4746 // Window should receive key down event.
4747 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4748
4749 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00004750 mDispatcher->waitForIdle();
Josep del Riob3981622023-04-18 15:49:45 +00004751 mFakePolicy->assertUserActivityNotPoked();
4752}
4753
4754TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) {
4755 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4756 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4757 "Fake Window", ADISPLAY_ID_DEFAULT);
4758
4759 window->setFocusable(true);
4760 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4761 setFocusedWindow(window);
4762
4763 window->consumeFocusEvent(true);
4764
4765 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4766 mDispatcher->waitForIdle();
4767
4768 // System key is not passed down
4769 window->assertNoEvents();
4770
4771 // Should have poked user activity
4772 mFakePolicy->assertUserActivityPoked();
4773}
4774
4775TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) {
4776 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4777 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4778 "Fake Window", ADISPLAY_ID_DEFAULT);
4779
4780 window->setFocusable(true);
4781 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4782 setFocusedWindow(window);
4783
4784 window->consumeFocusEvent(true);
4785
4786 mDispatcher->notifyKey(generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4787 mDispatcher->waitForIdle();
4788
4789 // System key is not passed down
4790 window->assertNoEvents();
4791
4792 // Should have poked user activity
4793 mFakePolicy->assertUserActivityPoked();
4794}
4795
4796TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) {
4797 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4798 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4799 "Fake Window", ADISPLAY_ID_DEFAULT);
4800
4801 window->setDisableUserActivity(true);
4802 window->setFocusable(true);
4803 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4804 setFocusedWindow(window);
4805
4806 window->consumeFocusEvent(true);
4807
4808 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4809 mDispatcher->waitForIdle();
4810
4811 // System key is not passed down
4812 window->assertNoEvents();
4813
4814 // Should have poked user activity
4815 mFakePolicy->assertUserActivityPoked();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004816}
4817
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07004818TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) {
4819 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4820 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4821 "Fake Window", ADISPLAY_ID_DEFAULT);
4822
4823 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4824
4825 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4826 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4827 ADISPLAY_ID_DEFAULT, {100, 100}))
4828 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4829
4830 window->consumeMotionEvent(
4831 AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT)));
4832
4833 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00004834 mDispatcher->waitForIdle();
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07004835 mFakePolicy->assertUserActivityPoked();
4836}
4837
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004838TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004839 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004840 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4841 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004842
Arthur Hung72d8dc32020-03-28 00:48:39 +00004843 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004844
Prabir Pradhan678438e2023-04-13 19:32:51 +00004845 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004846 mDispatcher->waitForIdle();
4847
4848 window->assertNoEvents();
4849}
4850
4851// If a window is touchable, but does not have focus, it should receive motion events, but not keys
4852TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07004853 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004854 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4855 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004856
Arthur Hung72d8dc32020-03-28 00:48:39 +00004857 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004858
4859 // Send key
Prabir Pradhan678438e2023-04-13 19:32:51 +00004860 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004861 // Send motion
Prabir Pradhan678438e2023-04-13 19:32:51 +00004862 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4863 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004864
4865 // Window should receive only the motion event
4866 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4867 window->assertNoEvents(); // Key event or focus event will not be received
4868}
4869
arthurhungea3f4fc2020-12-21 23:18:53 +08004870TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
4871 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4872
arthurhungea3f4fc2020-12-21 23:18:53 +08004873 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004874 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4875 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004876 firstWindow->setFrame(Rect(0, 0, 600, 400));
arthurhungea3f4fc2020-12-21 23:18:53 +08004877
arthurhungea3f4fc2020-12-21 23:18:53 +08004878 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004879 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4880 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004881 secondWindow->setFrame(Rect(0, 400, 600, 800));
arthurhungea3f4fc2020-12-21 23:18:53 +08004882
4883 // Add the windows to the dispatcher
4884 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4885
4886 PointF pointInFirst = {300, 200};
4887 PointF pointInSecond = {300, 600};
4888
4889 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004890 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4891 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4892 {pointInFirst}));
arthurhungea3f4fc2020-12-21 23:18:53 +08004893 // Only the first window should get the down event
4894 firstWindow->consumeMotionDown();
4895 secondWindow->assertNoEvents();
4896
4897 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004898 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4899 ADISPLAY_ID_DEFAULT,
4900 {pointInFirst, pointInSecond}));
arthurhungea3f4fc2020-12-21 23:18:53 +08004901 // The first window gets a move and the second a down
4902 firstWindow->consumeMotionMove();
4903 secondWindow->consumeMotionDown();
4904
4905 // Send pointer cancel to the second window
4906 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004907 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08004908 {pointInFirst, pointInSecond});
4909 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
Prabir Pradhan678438e2023-04-13 19:32:51 +00004910 mDispatcher->notifyMotion(pointerUpMotionArgs);
arthurhungea3f4fc2020-12-21 23:18:53 +08004911 // The first window gets move and the second gets cancel.
4912 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
4913 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
4914
4915 // Send up event.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004916 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4917 ADISPLAY_ID_DEFAULT));
arthurhungea3f4fc2020-12-21 23:18:53 +08004918 // The first window gets up and the second gets nothing.
4919 firstWindow->consumeMotionUp();
4920 secondWindow->assertNoEvents();
4921}
4922
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004923TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
4924 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4925
4926 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004927 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004928 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4929 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
4930 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
4931 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
4932
Harry Cutts33476232023-01-30 19:57:29 +00004933 window->sendTimeline(/*inputEventId=*/1, graphicsTimeline);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004934 window->assertNoEvents();
4935 mDispatcher->waitForIdle();
4936}
4937
chaviwd1c23182019-12-20 18:44:56 -08004938class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00004939public:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004940 FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004941 int32_t displayId) {
Garfield Tan15601662020-09-22 15:32:38 -07004942 base::Result<std::unique_ptr<InputChannel>> channel =
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08004943 dispatcher->createInputMonitor(displayId, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07004944 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00004945 }
4946
chaviwd1c23182019-12-20 18:44:56 -08004947 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
4948
4949 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004950 mInputReceiver->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
4951 expectedFlags);
chaviwd1c23182019-12-20 18:44:56 -08004952 }
4953
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004954 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
4955
4956 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
4957
chaviwd1c23182019-12-20 18:44:56 -08004958 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004959 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
chaviwd1c23182019-12-20 18:44:56 -08004960 expectedDisplayId, expectedFlags);
4961 }
4962
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004963 void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004964 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004965 expectedDisplayId, expectedFlags);
4966 }
4967
chaviwd1c23182019-12-20 18:44:56 -08004968 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004969 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP,
chaviwd1c23182019-12-20 18:44:56 -08004970 expectedDisplayId, expectedFlags);
4971 }
4972
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004973 void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08004974 mInputReceiver->consumeMotionEvent(
4975 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
4976 WithDisplayId(expectedDisplayId),
4977 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004978 }
4979
Arthur Hungfbfa5722021-11-16 02:45:54 +00004980 void consumeMotionPointerDown(int32_t pointerIdx) {
4981 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
4982 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004983 mInputReceiver->consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00004984 /*expectedFlags=*/0);
Arthur Hungfbfa5722021-11-16 02:45:54 +00004985 }
4986
Evan Rosky84f07f02021-04-16 10:42:42 -07004987 MotionEvent* consumeMotion() {
4988 InputEvent* event = mInputReceiver->consume();
4989 if (!event) {
4990 ADD_FAILURE() << "No event was produced";
4991 return nullptr;
4992 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004993 if (event->getType() != InputEventType::MOTION) {
4994 ADD_FAILURE() << "Expected MotionEvent, got " << *event;
Evan Rosky84f07f02021-04-16 10:42:42 -07004995 return nullptr;
4996 }
4997 return static_cast<MotionEvent*>(event);
4998 }
4999
chaviwd1c23182019-12-20 18:44:56 -08005000 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
5001
5002private:
5003 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00005004};
5005
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005006using InputDispatcherMonitorTest = InputDispatcherTest;
5007
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005008/**
5009 * Two entities that receive touch: A window, and a global monitor.
5010 * The touch goes to the window, and then the window disappears.
5011 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
5012 * for the monitor, as well.
5013 * 1. foregroundWindow
5014 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
5015 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005016TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005017 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5018 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005019 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005020
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005021 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005022
5023 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5024 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5025 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5026 {100, 200}))
5027 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5028
5029 // Both the foreground window and the global monitor should receive the touch down
5030 window->consumeMotionDown();
5031 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
5032
5033 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5034 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5035 ADISPLAY_ID_DEFAULT, {110, 200}))
5036 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5037
5038 window->consumeMotionMove();
5039 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
5040
5041 // Now the foreground window goes away
5042 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
5043 window->consumeMotionCancel();
5044 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
5045
5046 // If more events come in, there will be no more foreground window to send them to. This will
5047 // cause a cancel for the monitor, as well.
5048 ASSERT_EQ(InputEventInjectionResult::FAILED,
5049 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5050 ADISPLAY_ID_DEFAULT, {120, 200}))
5051 << "Injection should fail because the window was removed";
5052 window->assertNoEvents();
5053 // Global monitor now gets the cancel
5054 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5055}
5056
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005057TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07005058 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005059 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5060 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005061 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00005062
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005063 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005064
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005065 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00005066 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005067 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00005068 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08005069 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005070}
5071
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005072TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
5073 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005074
Chris Yea209fde2020-07-22 13:54:51 -07005075 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005076 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5077 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005078 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00005079
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005080 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00005081 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005082 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08005083 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005084 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005085
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005086 // Pilfer pointers from the monitor.
5087 // This should not do anything and the window should continue to receive events.
5088 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00005089
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005090 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005091 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5092 ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005093 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005094
5095 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
5096 window->consumeMotionMove(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005097}
5098
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005099TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07005100 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005101 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5102 "Fake Window", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07005103 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5104 window->setWindowOffset(20, 40);
5105 window->setWindowTransform(0, 1, -1, 0);
5106
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005107 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07005108
5109 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5110 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5111 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5112 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5113 MotionEvent* event = monitor.consumeMotion();
5114 // Even though window has transform, gesture monitor must not.
5115 ASSERT_EQ(ui::Transform(), event->getTransform());
5116}
5117
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005118TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00005119 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005120 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00005121
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005122 ASSERT_EQ(InputEventInjectionResult::FAILED,
Arthur Hungb3307ee2021-10-14 10:57:37 +00005123 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005124 << "Injection should fail if there is a monitor, but no touchable window";
5125 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00005126}
5127
chaviw81e2bb92019-12-18 15:03:51 -08005128TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005129 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005130 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5131 "Fake Window", ADISPLAY_ID_DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08005132
Arthur Hung72d8dc32020-03-28 00:48:39 +00005133 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08005134
5135 NotifyMotionArgs motionArgs =
5136 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5137 ADISPLAY_ID_DEFAULT);
5138
Prabir Pradhan678438e2023-04-13 19:32:51 +00005139 mDispatcher->notifyMotion(motionArgs);
chaviw81e2bb92019-12-18 15:03:51 -08005140 // Window should receive motion down event.
5141 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5142
5143 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08005144 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08005145 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5146 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
5147 motionArgs.pointerCoords[0].getX() - 10);
5148
Prabir Pradhan678438e2023-04-13 19:32:51 +00005149 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005150 window->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005151 /*expectedFlags=*/0);
chaviw81e2bb92019-12-18 15:03:51 -08005152}
5153
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005154/**
5155 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
5156 * the device default right away. In the test scenario, we check both the default value,
5157 * and the action of enabling / disabling.
5158 */
5159TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07005160 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005161 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5162 "Test window", ADISPLAY_ID_DEFAULT);
Antonio Kantekea47acb2021-12-23 12:41:25 -08005163 const WindowInfo& windowInfo = *window->getInfo();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005164
5165 // Set focused application.
5166 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005167 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005168
5169 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00005170 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005171 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005172 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005173
5174 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07005175 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005176 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00005177 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005178
5179 SCOPED_TRACE("Disable touch mode");
Antonio Kantekea47acb2021-12-23 12:41:25 -08005180 mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00005181 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005182 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07005183 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005184 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005185 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005186 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005187
5188 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07005189 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005190 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00005191 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005192
5193 SCOPED_TRACE("Enable touch mode again");
Antonio Kantekea47acb2021-12-23 12:41:25 -08005194 mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00005195 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005196 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07005197 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005198 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005199 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005200 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005201
5202 window->assertNoEvents();
5203}
5204
Gang Wange9087892020-01-07 12:17:14 -05005205TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005206 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005207 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5208 "Test window", ADISPLAY_ID_DEFAULT);
Gang Wange9087892020-01-07 12:17:14 -05005209
5210 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005211 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05005212
Arthur Hung72d8dc32020-03-28 00:48:39 +00005213 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005214 setFocusedWindow(window);
5215
Harry Cutts33476232023-01-30 19:57:29 +00005216 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Gang Wange9087892020-01-07 12:17:14 -05005217
Prabir Pradhan678438e2023-04-13 19:32:51 +00005218 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
5219 mDispatcher->notifyKey(keyArgs);
Gang Wange9087892020-01-07 12:17:14 -05005220
5221 InputEvent* event = window->consume();
5222 ASSERT_NE(event, nullptr);
5223
5224 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
5225 ASSERT_NE(verified, nullptr);
5226 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
5227
5228 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
5229 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
5230 ASSERT_EQ(keyArgs.source, verified->source);
5231 ASSERT_EQ(keyArgs.displayId, verified->displayId);
5232
5233 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
5234
5235 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05005236 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005237 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05005238 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
5239 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
5240 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
5241 ASSERT_EQ(0, verifiedKey.repeatCount);
5242}
5243
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005244TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005245 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005246 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5247 "Test window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005248
5249 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5250
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07005251 ui::Transform transform;
5252 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
5253
5254 gui::DisplayInfo displayInfo;
5255 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
5256 displayInfo.transform = transform;
5257
Patrick Williamsd828f302023-04-28 17:52:08 -05005258 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005259
Prabir Pradhan678438e2023-04-13 19:32:51 +00005260 const NotifyMotionArgs motionArgs =
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005261 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5262 ADISPLAY_ID_DEFAULT);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005263 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005264
5265 InputEvent* event = window->consume();
5266 ASSERT_NE(event, nullptr);
5267
5268 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
5269 ASSERT_NE(verified, nullptr);
5270 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
5271
5272 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
5273 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
5274 EXPECT_EQ(motionArgs.source, verified->source);
5275 EXPECT_EQ(motionArgs.displayId, verified->displayId);
5276
5277 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
5278
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07005279 const vec2 rawXY =
5280 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
5281 motionArgs.pointerCoords[0].getXYValue());
5282 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
5283 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005284 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005285 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005286 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005287 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
5288 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
5289}
5290
chaviw09c8d2d2020-08-24 15:48:26 -07005291/**
5292 * Ensure that separate calls to sign the same data are generating the same key.
5293 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
5294 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
5295 * tests.
5296 */
5297TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
5298 KeyEvent event = getTestKeyEvent();
5299 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5300
5301 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
5302 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
5303 ASSERT_EQ(hmac1, hmac2);
5304}
5305
5306/**
5307 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
5308 */
5309TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
5310 KeyEvent event = getTestKeyEvent();
5311 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5312 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
5313
5314 verifiedEvent.deviceId += 1;
5315 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5316
5317 verifiedEvent.source += 1;
5318 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5319
5320 verifiedEvent.eventTimeNanos += 1;
5321 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5322
5323 verifiedEvent.displayId += 1;
5324 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5325
5326 verifiedEvent.action += 1;
5327 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5328
5329 verifiedEvent.downTimeNanos += 1;
5330 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5331
5332 verifiedEvent.flags += 1;
5333 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5334
5335 verifiedEvent.keyCode += 1;
5336 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5337
5338 verifiedEvent.scanCode += 1;
5339 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5340
5341 verifiedEvent.metaState += 1;
5342 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5343
5344 verifiedEvent.repeatCount += 1;
5345 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5346}
5347
Vishnu Nair958da932020-08-21 17:12:37 -07005348TEST_F(InputDispatcherTest, SetFocusedWindow) {
5349 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5350 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005351 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005352 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005353 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005354 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5355
5356 // Top window is also focusable but is not granted focus.
5357 windowTop->setFocusable(true);
5358 windowSecond->setFocusable(true);
5359 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5360 setFocusedWindow(windowSecond);
5361
5362 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005363 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5364 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005365
5366 // Focused window should receive event.
5367 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5368 windowTop->assertNoEvents();
5369}
5370
5371TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
5372 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5373 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005374 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005375 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5376
5377 window->setFocusable(true);
5378 // Release channel for window is no longer valid.
5379 window->releaseChannel();
5380 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5381 setFocusedWindow(window);
5382
5383 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005384 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5385 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005386
5387 // window channel is invalid, so it should not receive any input event.
5388 window->assertNoEvents();
5389}
5390
5391TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
5392 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5393 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005394 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005395 window->setFocusable(false);
Vishnu Nair958da932020-08-21 17:12:37 -07005396 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5397
Vishnu Nair958da932020-08-21 17:12:37 -07005398 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5399 setFocusedWindow(window);
5400
5401 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005402 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5403 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005404
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005405 // window is not focusable, so it should not receive any input event.
Vishnu Nair958da932020-08-21 17:12:37 -07005406 window->assertNoEvents();
5407}
5408
5409TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
5410 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5411 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005412 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005413 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005414 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005415 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5416
5417 windowTop->setFocusable(true);
5418 windowSecond->setFocusable(true);
5419 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5420 setFocusedWindow(windowTop);
5421 windowTop->consumeFocusEvent(true);
5422
Chavi Weingarten847e8512023-03-29 00:26:09 +00005423 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
5424 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005425 windowSecond->consumeFocusEvent(true);
5426 windowTop->consumeFocusEvent(false);
5427
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005428 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5429 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005430
5431 // Focused window should receive event.
5432 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5433}
5434
Chavi Weingarten847e8512023-03-29 00:26:09 +00005435TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) {
Vishnu Nair958da932020-08-21 17:12:37 -07005436 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5437 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005438 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005439 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005440 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005441 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5442
5443 windowTop->setFocusable(true);
Chavi Weingarten847e8512023-03-29 00:26:09 +00005444 windowSecond->setFocusable(false);
5445 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
Vishnu Nair958da932020-08-21 17:12:37 -07005446 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Chavi Weingarten847e8512023-03-29 00:26:09 +00005447 setFocusedWindow(windowTop);
5448 windowTop->consumeFocusEvent(true);
Vishnu Nair958da932020-08-21 17:12:37 -07005449
Chavi Weingarten847e8512023-03-29 00:26:09 +00005450 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5451 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005452
5453 // Event should be dropped.
Chavi Weingarten847e8512023-03-29 00:26:09 +00005454 windowTop->consumeKeyDown(ADISPLAY_ID_NONE);
Vishnu Nair958da932020-08-21 17:12:37 -07005455 windowSecond->assertNoEvents();
5456}
5457
5458TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
5459 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5460 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005461 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005462 sp<FakeWindowHandle> previousFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005463 sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow",
5464 ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005465 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5466
5467 window->setFocusable(true);
5468 previousFocusedWindow->setFocusable(true);
5469 window->setVisible(false);
5470 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
5471 setFocusedWindow(previousFocusedWindow);
5472 previousFocusedWindow->consumeFocusEvent(true);
5473
5474 // Requesting focus on invisible window takes focus from currently focused window.
5475 setFocusedWindow(window);
5476 previousFocusedWindow->consumeFocusEvent(false);
5477
5478 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005479 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005480 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
5481 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07005482
5483 // Window does not get focus event or key down.
5484 window->assertNoEvents();
5485
5486 // Window becomes visible.
5487 window->setVisible(true);
5488 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5489
5490 // Window receives focus event.
5491 window->consumeFocusEvent(true);
5492 // Focused window receives key down.
5493 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5494}
5495
Vishnu Nair599f1412021-06-21 10:39:58 -07005496TEST_F(InputDispatcherTest, DisplayRemoved) {
5497 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5498 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005499 sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
Vishnu Nair599f1412021-06-21 10:39:58 -07005500 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5501
5502 // window is granted focus.
5503 window->setFocusable(true);
5504 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5505 setFocusedWindow(window);
5506 window->consumeFocusEvent(true);
5507
5508 // When a display is removed window loses focus.
5509 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
5510 window->consumeFocusEvent(false);
5511}
5512
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005513/**
5514 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
5515 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
5516 * of the 'slipperyEnterWindow'.
5517 *
5518 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
5519 * a way so that the touched location is no longer covered by the top window.
5520 *
5521 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
5522 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
5523 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
5524 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
5525 * with ACTION_DOWN).
5526 * Thus, the touch has been transferred from the top window into the bottom window, because the top
5527 * window moved itself away from the touched location and had Flag::SLIPPERY.
5528 *
5529 * Even though the top window moved away from the touched location, it is still obscuring the bottom
5530 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
5531 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
5532 *
5533 * In this test, we ensure that the event received by the bottom window has
5534 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
5535 */
5536TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00005537 constexpr gui::Pid SLIPPERY_PID{WINDOW_PID.val() + 1};
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00005538 constexpr gui::Uid SLIPPERY_UID{WINDOW_UID.val() + 1};
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005539
5540 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5541 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5542
5543 sp<FakeWindowHandle> slipperyExitWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005544 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005545 slipperyExitWindow->setSlippery(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005546 // Make sure this one overlaps the bottom window
5547 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
5548 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
5549 // one. Windows with the same owner are not considered to be occluding each other.
5550 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
5551
5552 sp<FakeWindowHandle> slipperyEnterWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005553 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005554 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
5555
5556 mDispatcher->setInputWindows(
5557 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5558
5559 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
Prabir Pradhan678438e2023-04-13 19:32:51 +00005560 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5561 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5562 {{50, 50}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005563 slipperyExitWindow->consumeMotionDown();
5564 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
5565 mDispatcher->setInputWindows(
5566 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5567
Prabir Pradhan678438e2023-04-13 19:32:51 +00005568 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE,
5569 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5570 {{51, 51}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005571
5572 slipperyExitWindow->consumeMotionCancel();
5573
5574 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
5575 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
5576}
5577
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07005578/**
5579 * Two windows, one on the left and another on the right. The left window is slippery. The right
5580 * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the
5581 * touch moves from the left window into the right window, the gesture should continue to go to the
5582 * left window. Touch shouldn't slip because the right window can't receive touches. This test
5583 * reproduces a crash.
5584 */
5585TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) {
5586 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5587
5588 sp<FakeWindowHandle> leftSlipperyWindow =
5589 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
5590 leftSlipperyWindow->setSlippery(true);
5591 leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100));
5592
5593 sp<FakeWindowHandle> rightDropTouchesWindow =
5594 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
5595 rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100));
5596 rightDropTouchesWindow->setDropInput(true);
5597
5598 mDispatcher->setInputWindows(
5599 {{ADISPLAY_ID_DEFAULT, {leftSlipperyWindow, rightDropTouchesWindow}}});
5600
5601 // Start touch in the left window
5602 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5603 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
5604 .build());
5605 leftSlipperyWindow->consumeMotionDown();
5606
5607 // And move it into the right window
5608 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
5609 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
5610 .build());
5611
5612 // Since the right window isn't eligible to receive input, touch does not slip.
5613 // The left window continues to receive the gesture.
5614 leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
5615 rightDropTouchesWindow->assertNoEvents();
5616}
5617
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005618TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithMotions) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00005619 using Uid = gui::Uid;
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005620 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5621
5622 sp<FakeWindowHandle> leftWindow =
5623 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
5624 leftWindow->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00005625 leftWindow->setOwnerInfo(gui::Pid{1}, Uid{101});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005626
5627 sp<FakeWindowHandle> rightSpy =
5628 sp<FakeWindowHandle>::make(application, mDispatcher, "Right spy", ADISPLAY_ID_DEFAULT);
5629 rightSpy->setFrame(Rect(100, 0, 200, 100));
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00005630 rightSpy->setOwnerInfo(gui::Pid{2}, Uid{102});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005631 rightSpy->setSpy(true);
5632 rightSpy->setTrustedOverlay(true);
5633
5634 sp<FakeWindowHandle> rightWindow =
5635 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
5636 rightWindow->setFrame(Rect(100, 0, 200, 100));
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00005637 rightWindow->setOwnerInfo(gui::Pid{3}, Uid{103});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005638
5639 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightSpy, rightWindow, leftWindow}}});
5640
5641 // Touch in the left window
5642 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5643 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
5644 .build());
5645 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionDown());
5646 mDispatcher->waitForIdle();
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00005647 ASSERT_NO_FATAL_FAILURE(
5648 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{101}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005649
5650 // Touch another finger over the right windows
5651 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5652 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
5653 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
5654 .build());
5655 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionDown());
5656 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionDown());
5657 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionMove());
5658 mDispatcher->waitForIdle();
5659 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00005660 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID,
5661 {Uid{101}, Uid{102}, Uid{103}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005662
5663 // Release finger over left window. The UP actions are not treated as device interaction.
5664 // The windows that did not receive the UP pointer will receive MOVE events, but since this
5665 // is part of the UP action, we do not treat this as device interaction.
5666 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
5667 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
5668 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
5669 .build());
5670 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionUp());
5671 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove());
5672 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove());
5673 mDispatcher->waitForIdle();
5674 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
5675
5676 // Move remaining finger
5677 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
5678 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
5679 .build());
5680 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove());
5681 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove());
5682 mDispatcher->waitForIdle();
5683 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00005684 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{102}, Uid{103}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005685
5686 // Release all fingers
5687 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
5688 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
5689 .build());
5690 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionUp());
5691 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionUp());
5692 mDispatcher->waitForIdle();
5693 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
5694}
5695
5696TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithKeys) {
5697 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5698
5699 sp<FakeWindowHandle> window =
5700 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
5701 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00005702 window->setOwnerInfo(gui::Pid{1}, gui::Uid{101});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005703
5704 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5705 setFocusedWindow(window);
5706 ASSERT_NO_FATAL_FAILURE(window->consumeFocusEvent(true));
5707
5708 mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).build());
5709 ASSERT_NO_FATAL_FAILURE(window->consumeKeyDown(ADISPLAY_ID_DEFAULT));
5710 mDispatcher->waitForIdle();
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00005711 ASSERT_NO_FATAL_FAILURE(
5712 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {gui::Uid{101}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005713
5714 // The UP actions are not treated as device interaction.
5715 mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).build());
5716 ASSERT_NO_FATAL_FAILURE(window->consumeKeyUp(ADISPLAY_ID_DEFAULT));
5717 mDispatcher->waitForIdle();
5718 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
5719}
5720
Garfield Tan1c7bc862020-01-28 13:24:04 -08005721class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
5722protected:
5723 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
5724 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
5725
Chris Yea209fde2020-07-22 13:54:51 -07005726 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005727 sp<FakeWindowHandle> mWindow;
5728
5729 virtual void SetUp() override {
Prabir Pradhana41d2442023-04-20 21:30:40 +00005730 mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005731 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Prabir Pradhana41d2442023-04-20 21:30:40 +00005732 mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy);
Prabir Pradhan87112a72023-04-20 19:13:39 +00005733 mDispatcher->requestRefreshConfiguration();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005734 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
5735 ASSERT_EQ(OK, mDispatcher->start());
5736
5737 setUpWindow();
5738 }
5739
5740 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07005741 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005742 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005743
Vishnu Nair47074b82020-08-14 11:54:47 -07005744 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005745 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005746 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005747 mWindow->consumeFocusEvent(true);
5748 }
5749
Chris Ye2ad95392020-09-01 13:44:44 -07005750 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005751 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, 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; // Otherwise 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.
5757 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5758 }
5759
5760 void expectKeyRepeatOnce(int32_t repeatCount) {
5761 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
5762 InputEvent* repeatEvent = mWindow->consume();
5763 ASSERT_NE(nullptr, repeatEvent);
5764
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005765 ASSERT_EQ(InputEventType::KEY, repeatEvent->getType());
Garfield Tan1c7bc862020-01-28 13:24:04 -08005766
5767 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
5768 uint32_t eventAction = repeatKeyEvent->getAction();
5769 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
5770 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
5771 }
5772
Chris Ye2ad95392020-09-01 13:44:44 -07005773 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005774 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005775 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005776 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005777 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005778
5779 // Window should receive key down event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005780 mWindow->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005781 /*expectedFlags=*/0);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005782 }
5783};
5784
5785TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Harry Cutts33476232023-01-30 19:57:29 +00005786 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005787 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5788 expectKeyRepeatOnce(repeatCount);
5789 }
5790}
5791
5792TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
Harry Cutts33476232023-01-30 19:57:29 +00005793 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005794 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5795 expectKeyRepeatOnce(repeatCount);
5796 }
Harry Cutts33476232023-01-30 19:57:29 +00005797 sendAndConsumeKeyDown(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005798 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08005799 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5800 expectKeyRepeatOnce(repeatCount);
5801 }
5802}
5803
5804TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005805 sendAndConsumeKeyDown(/*deviceId=*/1);
5806 expectKeyRepeatOnce(/*repeatCount=*/1);
5807 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005808 mWindow->assertNoEvents();
5809}
5810
5811TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005812 sendAndConsumeKeyDown(/*deviceId=*/1);
5813 expectKeyRepeatOnce(/*repeatCount=*/1);
5814 sendAndConsumeKeyDown(/*deviceId=*/2);
5815 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005816 // Stale key up from device 1.
Harry Cutts33476232023-01-30 19:57:29 +00005817 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005818 // Device 2 is still down, keep repeating
Harry Cutts33476232023-01-30 19:57:29 +00005819 expectKeyRepeatOnce(/*repeatCount=*/2);
5820 expectKeyRepeatOnce(/*repeatCount=*/3);
Chris Ye2ad95392020-09-01 13:44:44 -07005821 // Device 2 key up
Harry Cutts33476232023-01-30 19:57:29 +00005822 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005823 mWindow->assertNoEvents();
5824}
5825
5826TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005827 sendAndConsumeKeyDown(/*deviceId=*/1);
5828 expectKeyRepeatOnce(/*repeatCount=*/1);
5829 sendAndConsumeKeyDown(/*deviceId=*/2);
5830 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005831 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
Harry Cutts33476232023-01-30 19:57:29 +00005832 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005833 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08005834 mWindow->assertNoEvents();
5835}
5836
liushenxiang42232912021-05-21 20:24:09 +08005837TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
5838 sendAndConsumeKeyDown(DEVICE_ID);
Harry Cutts33476232023-01-30 19:57:29 +00005839 expectKeyRepeatOnce(/*repeatCount=*/1);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005840 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
liushenxiang42232912021-05-21 20:24:09 +08005841 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
5842 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
5843 mWindow->assertNoEvents();
5844}
5845
Garfield Tan1c7bc862020-01-28 13:24:04 -08005846TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005847 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005848 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005849 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5850 InputEvent* repeatEvent = mWindow->consume();
5851 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5852 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
5853 IdGenerator::getSource(repeatEvent->getId()));
5854 }
5855}
5856
5857TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005858 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005859 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005860
5861 std::unordered_set<int32_t> idSet;
5862 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5863 InputEvent* repeatEvent = mWindow->consume();
5864 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5865 int32_t id = repeatEvent->getId();
5866 EXPECT_EQ(idSet.end(), idSet.find(id));
5867 idSet.insert(id);
5868 }
5869}
5870
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005871/* Test InputDispatcher for MultiDisplay */
5872class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
5873public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005874 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005875 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08005876
Chris Yea209fde2020-07-22 13:54:51 -07005877 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005878 windowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005879 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005880
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005881 // Set focus window for primary display, but focused display would be second one.
5882 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07005883 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005884 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005885 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005886 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08005887
Chris Yea209fde2020-07-22 13:54:51 -07005888 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005889 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005890 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005891 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005892 // Set focus display to second one.
5893 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
5894 // Set focus window for second display.
5895 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07005896 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005897 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005898 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005899 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005900 }
5901
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005902 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005903 InputDispatcherTest::TearDown();
5904
Chris Yea209fde2020-07-22 13:54:51 -07005905 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005906 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07005907 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005908 windowInSecondary.clear();
5909 }
5910
5911protected:
Chris Yea209fde2020-07-22 13:54:51 -07005912 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005913 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07005914 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005915 sp<FakeWindowHandle> windowInSecondary;
5916};
5917
5918TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
5919 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005920 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5921 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5922 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005923 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08005924 windowInSecondary->assertNoEvents();
5925
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005926 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005927 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5928 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5929 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005930 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005931 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08005932}
5933
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005934TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08005935 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005936 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5937 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005938 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005939 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08005940 windowInSecondary->assertNoEvents();
5941
5942 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005943 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005944 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005945 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005946 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08005947
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005948 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00005949 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08005950
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005951 // Old focus should receive a cancel event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005952 windowInSecondary->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005953 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08005954
5955 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005956 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005957 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08005958 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005959 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08005960 windowInSecondary->assertNoEvents();
5961}
5962
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005963// Test per-display input monitors for motion event.
5964TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08005965 FakeMonitorReceiver monitorInPrimary =
5966 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5967 FakeMonitorReceiver monitorInSecondary =
5968 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005969
5970 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005971 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5972 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5973 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005974 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08005975 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005976 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005977 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005978
5979 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005980 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5981 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5982 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005983 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005984 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005985 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08005986 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005987
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08005988 // Lift up the touch from the second display
5989 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5990 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5991 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5992 windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID);
5993 monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID);
5994
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005995 // Test inject a non-pointer motion event.
5996 // If specific a display, it will dispatch to the focused window of particular display,
5997 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005998 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5999 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
6000 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006001 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08006002 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08006003 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08006004 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006005}
6006
6007// Test per-display input monitors for key event.
6008TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006009 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08006010 FakeMonitorReceiver monitorInPrimary =
6011 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6012 FakeMonitorReceiver monitorInSecondary =
6013 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006014
6015 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006016 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
6017 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006018 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08006019 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08006020 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08006021 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006022}
6023
Vishnu Nair958da932020-08-21 17:12:37 -07006024TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
6025 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006026 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006027 secondWindowInPrimary->setFocusable(true);
6028 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
6029 setFocusedWindow(secondWindowInPrimary);
6030 windowInPrimary->consumeFocusEvent(false);
6031 secondWindowInPrimary->consumeFocusEvent(true);
6032
6033 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006034 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
6035 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006036 windowInPrimary->assertNoEvents();
6037 windowInSecondary->assertNoEvents();
6038 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6039}
6040
Arthur Hungdfd528e2021-12-08 13:23:04 +00006041TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
6042 FakeMonitorReceiver monitorInPrimary =
6043 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6044 FakeMonitorReceiver monitorInSecondary =
6045 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
6046
6047 // Test touch down on primary display.
6048 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6049 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
6050 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6051 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6052 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
6053
6054 // Test touch down on second display.
6055 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6056 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
6057 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6058 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
6059 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
6060
6061 // Trigger cancel touch.
6062 mDispatcher->cancelCurrentTouch();
6063 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6064 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6065 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
6066 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
6067
6068 // Test inject a move motion event, no window/monitor should receive the event.
6069 ASSERT_EQ(InputEventInjectionResult::FAILED,
6070 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6071 ADISPLAY_ID_DEFAULT, {110, 200}))
6072 << "Inject motion event should return InputEventInjectionResult::FAILED";
6073 windowInPrimary->assertNoEvents();
6074 monitorInPrimary.assertNoEvents();
6075
6076 ASSERT_EQ(InputEventInjectionResult::FAILED,
6077 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6078 SECOND_DISPLAY_ID, {110, 200}))
6079 << "Inject motion event should return InputEventInjectionResult::FAILED";
6080 windowInSecondary->assertNoEvents();
6081 monitorInSecondary.assertNoEvents();
6082}
6083
Jackal Guof9696682018-10-05 12:23:23 +08006084class InputFilterTest : public InputDispatcherTest {
6085protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006086 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
6087 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08006088 NotifyMotionArgs motionArgs;
6089
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006090 motionArgs =
6091 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006092 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006093 motionArgs =
6094 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006095 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006096 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08006097 if (expectToBeFiltered) {
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006098 const auto xy = transform.transform(motionArgs.pointerCoords[0].getXYValue());
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006099 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08006100 } else {
6101 mFakePolicy->assertFilterInputEventWasNotCalled();
6102 }
6103 }
6104
6105 void testNotifyKey(bool expectToBeFiltered) {
6106 NotifyKeyArgs keyArgs;
6107
6108 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006109 mDispatcher->notifyKey(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08006110 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006111 mDispatcher->notifyKey(keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006112 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08006113
6114 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08006115 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08006116 } else {
6117 mFakePolicy->assertFilterInputEventWasNotCalled();
6118 }
6119 }
6120};
6121
6122// Test InputFilter for MotionEvent
6123TEST_F(InputFilterTest, MotionEvent_InputFilter) {
6124 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
6125 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
6126 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
6127
6128 // Enable InputFilter
6129 mDispatcher->setInputFilterEnabled(true);
6130 // Test touch on both primary and second display, and check if both events are filtered.
6131 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
6132 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
6133
6134 // Disable InputFilter
6135 mDispatcher->setInputFilterEnabled(false);
6136 // Test touch on both primary and second display, and check if both events aren't filtered.
6137 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
6138 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
6139}
6140
6141// Test InputFilter for KeyEvent
6142TEST_F(InputFilterTest, KeyEvent_InputFilter) {
6143 // Since the InputFilter is disabled by default, check if key event aren't filtered.
6144 testNotifyKey(/*expectToBeFiltered*/ false);
6145
6146 // Enable InputFilter
6147 mDispatcher->setInputFilterEnabled(true);
6148 // Send a key event, and check if it is filtered.
6149 testNotifyKey(/*expectToBeFiltered*/ true);
6150
6151 // Disable InputFilter
6152 mDispatcher->setInputFilterEnabled(false);
6153 // Send a key event, and check if it isn't filtered.
6154 testNotifyKey(/*expectToBeFiltered*/ false);
6155}
6156
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006157// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
6158// logical display coordinate space.
6159TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
6160 ui::Transform firstDisplayTransform;
6161 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
6162 ui::Transform secondDisplayTransform;
6163 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
6164
6165 std::vector<gui::DisplayInfo> displayInfos(2);
6166 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
6167 displayInfos[0].transform = firstDisplayTransform;
6168 displayInfos[1].displayId = SECOND_DISPLAY_ID;
6169 displayInfos[1].transform = secondDisplayTransform;
6170
Patrick Williamsd828f302023-04-28 17:52:08 -05006171 mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0});
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006172
6173 // Enable InputFilter
6174 mDispatcher->setInputFilterEnabled(true);
6175
6176 // Ensure the correct transforms are used for the displays.
6177 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform);
6178 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform);
6179}
6180
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006181class InputFilterInjectionPolicyTest : public InputDispatcherTest {
6182protected:
6183 virtual void SetUp() override {
6184 InputDispatcherTest::SetUp();
6185
6186 /**
6187 * We don't need to enable input filter to test the injected event policy, but we enabled it
6188 * here to make the tests more realistic, since this policy only matters when inputfilter is
6189 * on.
6190 */
6191 mDispatcher->setInputFilterEnabled(true);
6192
6193 std::shared_ptr<InputApplicationHandle> application =
6194 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006195 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
6196 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006197
6198 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6199 mWindow->setFocusable(true);
6200 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6201 setFocusedWindow(mWindow);
6202 mWindow->consumeFocusEvent(true);
6203 }
6204
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006205 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
6206 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006207 KeyEvent event;
6208
6209 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
6210 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
6211 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
Harry Cutts33476232023-01-30 19:57:29 +00006212 KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006213 const int32_t additionalPolicyFlags =
6214 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
6215 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006216 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006217 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
6218 policyFlags | additionalPolicyFlags));
6219
6220 InputEvent* received = mWindow->consume();
6221 ASSERT_NE(nullptr, received);
6222 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006223 ASSERT_EQ(received->getType(), InputEventType::KEY);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006224 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
6225 ASSERT_EQ(flags, keyEvent.getFlags());
6226 }
6227
6228 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
6229 int32_t flags) {
6230 MotionEvent event;
6231 PointerProperties pointerProperties[1];
6232 PointerCoords pointerCoords[1];
6233 pointerProperties[0].clear();
6234 pointerProperties[0].id = 0;
6235 pointerCoords[0].clear();
6236 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
6237 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
6238
6239 ui::Transform identityTransform;
6240 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
6241 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
6242 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
6243 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
6244 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07006245 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07006246 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006247 /*pointerCount*/ 1, pointerProperties, pointerCoords);
6248
6249 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
6250 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006251 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006252 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
6253 policyFlags | additionalPolicyFlags));
6254
6255 InputEvent* received = mWindow->consume();
6256 ASSERT_NE(nullptr, received);
6257 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006258 ASSERT_EQ(received->getType(), InputEventType::MOTION);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006259 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
6260 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006261 }
6262
6263private:
6264 sp<FakeWindowHandle> mWindow;
6265};
6266
6267TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006268 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
6269 // filter. Without it, the event will no different from a regularly injected event, and the
6270 // injected device id will be overwritten.
Harry Cutts33476232023-01-30 19:57:29 +00006271 testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
6272 /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006273}
6274
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006275TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006276 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00006277 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006278 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
6279}
6280
6281TEST_F(InputFilterInjectionPolicyTest,
6282 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
6283 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00006284 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006285 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006286}
6287
6288TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
Harry Cutts33476232023-01-30 19:57:29 +00006289 testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3,
6290 /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006291}
6292
chaviwfd6d3512019-03-25 13:23:49 -07006293class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006294 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07006295 InputDispatcherTest::SetUp();
6296
Chris Yea209fde2020-07-22 13:54:51 -07006297 std::shared_ptr<FakeApplicationHandle> application =
6298 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006299 mUnfocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006300 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07006301 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07006302
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006303 mFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006304 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006305 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07006306
6307 // Set focused application.
6308 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07006309 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07006310
6311 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00006312 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006313 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006314 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07006315 }
6316
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006317 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07006318 InputDispatcherTest::TearDown();
6319
6320 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006321 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07006322 }
6323
6324protected:
6325 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006326 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006327 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07006328};
6329
6330// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
6331// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
6332// the onPointerDownOutsideFocus callback.
6333TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006334 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006335 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6336 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006337 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006338 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006339
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006340 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07006341 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
6342}
6343
6344// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
6345// DOWN on the window that doesn't have focus. Ensure no window received the
6346// onPointerDownOutsideFocus callback.
6347TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006348 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006349 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006350 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006351 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006352
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006353 ASSERT_TRUE(mDispatcher->waitForIdle());
6354 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006355}
6356
6357// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
6358// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
6359TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006360 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6361 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006362 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006363 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07006364
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006365 ASSERT_TRUE(mDispatcher->waitForIdle());
6366 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006367}
6368
6369// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
6370// DOWN on the window that already has focus. Ensure no window received the
6371// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006372TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006373 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006374 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006375 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006376 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006377 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006378
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006379 ASSERT_TRUE(mDispatcher->waitForIdle());
6380 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006381}
6382
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006383// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
6384// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
6385TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
6386 const MotionEvent event =
6387 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
6388 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006389 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006390 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
6391 .build();
6392 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
6393 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6394 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
6395
6396 ASSERT_TRUE(mDispatcher->waitForIdle());
6397 mFakePolicy->assertOnPointerDownWasNotCalled();
6398 // Ensure that the unfocused window did not receive any FOCUS events.
6399 mUnfocusedWindow->assertNoEvents();
6400}
6401
chaviwaf87b3e2019-10-01 16:59:28 -07006402// These tests ensures we can send touch events to a single client when there are multiple input
6403// windows that point to the same client token.
6404class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
6405 virtual void SetUp() override {
6406 InputDispatcherTest::SetUp();
6407
Chris Yea209fde2020-07-22 13:54:51 -07006408 std::shared_ptr<FakeApplicationHandle> application =
6409 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006410 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
6411 ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07006412 mWindow1->setFrame(Rect(0, 0, 100, 100));
6413
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006414 mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2",
6415 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviwaf87b3e2019-10-01 16:59:28 -07006416 mWindow2->setFrame(Rect(100, 100, 200, 200));
6417
Arthur Hung72d8dc32020-03-28 00:48:39 +00006418 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07006419 }
6420
6421protected:
6422 sp<FakeWindowHandle> mWindow1;
6423 sp<FakeWindowHandle> mWindow2;
6424
6425 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05006426 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07006427 vec2 vals = windowInfo->transform.transform(point.x, point.y);
6428 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07006429 }
6430
6431 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
6432 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006433 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07006434 InputEvent* event = window->consume();
6435
6436 ASSERT_NE(nullptr, event) << name.c_str()
6437 << ": consumer should have returned non-NULL event.";
6438
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006439 ASSERT_EQ(InputEventType::MOTION, event->getType())
6440 << name.c_str() << ": expected MotionEvent, got " << *event;
chaviwaf87b3e2019-10-01 16:59:28 -07006441
6442 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006443 assertMotionAction(expectedAction, motionEvent.getAction());
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08006444 ASSERT_EQ(points.size(), motionEvent.getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07006445
6446 for (size_t i = 0; i < points.size(); i++) {
6447 float expectedX = points[i].x;
6448 float expectedY = points[i].y;
6449
6450 EXPECT_EQ(expectedX, motionEvent.getX(i))
6451 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
6452 << ", got " << motionEvent.getX(i);
6453 EXPECT_EQ(expectedY, motionEvent.getY(i))
6454 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
6455 << ", got " << motionEvent.getY(i);
6456 }
6457 }
chaviw9eaa22c2020-07-01 16:21:27 -07006458
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08006459 void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07006460 std::vector<PointF> expectedPoints) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00006461 mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
6462 ADISPLAY_ID_DEFAULT, touchedPoints));
chaviw9eaa22c2020-07-01 16:21:27 -07006463
6464 // Always consume from window1 since it's the window that has the InputReceiver
6465 consumeMotionEvent(mWindow1, action, expectedPoints);
6466 }
chaviwaf87b3e2019-10-01 16:59:28 -07006467};
6468
6469TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
6470 // Touch Window 1
6471 PointF touchedPoint = {10, 10};
6472 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006473 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006474
6475 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006476 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006477
6478 // Touch Window 2
6479 touchedPoint = {150, 150};
6480 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006481 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006482}
6483
chaviw9eaa22c2020-07-01 16:21:27 -07006484TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
6485 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07006486 mWindow2->setWindowScale(0.5f, 0.5f);
6487
6488 // Touch Window 1
6489 PointF touchedPoint = {10, 10};
6490 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006491 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006492 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006493 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006494
6495 // Touch Window 2
6496 touchedPoint = {150, 150};
6497 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006498 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
6499 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006500
chaviw9eaa22c2020-07-01 16:21:27 -07006501 // Update the transform so rotation is set
6502 mWindow2->setWindowTransform(0, -1, 1, 0);
6503 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
6504 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006505}
6506
chaviw9eaa22c2020-07-01 16:21:27 -07006507TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
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]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006518 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006519
chaviw9eaa22c2020-07-01 16:21:27 -07006520 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006521 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006522 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006523
chaviw9eaa22c2020-07-01 16:21:27 -07006524 // Update the transform so rotation is set for Window 2
6525 mWindow2->setWindowTransform(0, -1, 1, 0);
6526 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006527 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006528}
6529
chaviw9eaa22c2020-07-01 16:21:27 -07006530TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006531 mWindow2->setWindowScale(0.5f, 0.5f);
6532
6533 // Touch Window 1
6534 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6535 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006536 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006537
6538 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006539 touchedPoints.push_back(PointF{150, 150});
6540 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006541
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006542 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006543
6544 // Move both windows
6545 touchedPoints = {{20, 20}, {175, 175}};
6546 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6547 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6548
chaviw9eaa22c2020-07-01 16:21:27 -07006549 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006550
chaviw9eaa22c2020-07-01 16:21:27 -07006551 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006552 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006553 expectedPoints.pop_back();
6554
6555 // Touch Window 2
6556 mWindow2->setWindowTransform(0, -1, 1, 0);
6557 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006558 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006559
6560 // Move both windows
6561 touchedPoints = {{20, 20}, {175, 175}};
6562 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6563 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6564
6565 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006566}
6567
6568TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
6569 mWindow1->setWindowScale(0.5f, 0.5f);
6570
6571 // Touch Window 1
6572 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6573 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006574 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006575
6576 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006577 touchedPoints.push_back(PointF{150, 150});
6578 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006579
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006580 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006581
6582 // Move both windows
6583 touchedPoints = {{20, 20}, {175, 175}};
6584 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6585 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6586
chaviw9eaa22c2020-07-01 16:21:27 -07006587 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006588}
6589
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07006590/**
6591 * When one of the windows is slippery, the touch should not slip into the other window with the
6592 * same input channel.
6593 */
6594TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) {
6595 mWindow1->setSlippery(true);
6596 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
6597
6598 // Touch down in window 1
6599 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6600 ADISPLAY_ID_DEFAULT, {{50, 50}}));
6601 consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}});
6602
6603 // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip.
6604 // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN
6605 // getting generated.
6606 mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6607 ADISPLAY_ID_DEFAULT, {{150, 150}}));
6608
6609 consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}});
6610}
6611
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07006612/**
6613 * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and
6614 * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window
6615 * that the pointer is hovering over may have a different transform.
6616 */
6617TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) {
6618 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
6619
6620 // Start hover in window 1
6621 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN,
6622 ADISPLAY_ID_DEFAULT, {{50, 50}}));
6623 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
6624 {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})});
6625
6626 // Move hover to window 2.
6627 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6628 ADISPLAY_ID_DEFAULT, {{150, 150}}));
6629
6630 consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}});
6631 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
6632 {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})});
6633}
6634
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006635class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
6636 virtual void SetUp() override {
6637 InputDispatcherTest::SetUp();
6638
Chris Yea209fde2020-07-22 13:54:51 -07006639 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006640 mApplication->setDispatchingTimeout(20ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006641 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
6642 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006643 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05006644 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07006645 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006646
6647 // Set focused application.
6648 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
6649
6650 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006651 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006652 mWindow->consumeFocusEvent(true);
6653 }
6654
6655 virtual void TearDown() override {
6656 InputDispatcherTest::TearDown();
6657 mWindow.clear();
6658 }
6659
6660protected:
Chris Yea209fde2020-07-22 13:54:51 -07006661 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006662 sp<FakeWindowHandle> mWindow;
6663 static constexpr PointF WINDOW_LOCATION = {20, 20};
6664
6665 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006666 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006667 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6668 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006669 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006670 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6671 WINDOW_LOCATION));
6672 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006673
6674 sp<FakeWindowHandle> addSpyWindow() {
6675 sp<FakeWindowHandle> spy =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006676 sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006677 spy->setTrustedOverlay(true);
6678 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006679 spy->setSpy(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006680 spy->setDispatchingTimeout(30ms);
6681 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}});
6682 return spy;
6683 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006684};
6685
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006686// Send a tap and respond, which should not cause an ANR.
6687TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
6688 tapOnWindow();
6689 mWindow->consumeMotionDown();
6690 mWindow->consumeMotionUp();
6691 ASSERT_TRUE(mDispatcher->waitForIdle());
6692 mFakePolicy->assertNotifyAnrWasNotCalled();
6693}
6694
6695// Send a regular key and respond, which should not cause an ANR.
6696TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006697 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006698 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
6699 ASSERT_TRUE(mDispatcher->waitForIdle());
6700 mFakePolicy->assertNotifyAnrWasNotCalled();
6701}
6702
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006703TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
6704 mWindow->setFocusable(false);
6705 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6706 mWindow->consumeFocusEvent(false);
6707
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006708 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006709 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6710 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
6711 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006712 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006713 // Key will not go to window because we have no focused window.
6714 // The 'no focused window' ANR timer should start instead.
6715
6716 // Now, the focused application goes away.
6717 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
6718 // The key should get dropped and there should be no ANR.
6719
6720 ASSERT_TRUE(mDispatcher->waitForIdle());
6721 mFakePolicy->assertNotifyAnrWasNotCalled();
6722}
6723
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006724// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006725// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
6726// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006727TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006728 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006729 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6730 WINDOW_LOCATION));
6731
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006732 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
6733 ASSERT_TRUE(sequenceNum);
6734 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006735 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006736
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006737 mWindow->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006738 mWindow->consumeMotionEvent(
6739 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006740 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006741 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006742}
6743
6744// Send a key to the app and have the app not respond right away.
6745TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
6746 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006747 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006748 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
6749 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006750 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006751 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006752 ASSERT_TRUE(mDispatcher->waitForIdle());
6753}
6754
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006755// We have a focused application, but no focused window
6756TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006757 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006758 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6759 mWindow->consumeFocusEvent(false);
6760
6761 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006762 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006763 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6764 WINDOW_LOCATION));
6765 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
6766 mDispatcher->waitForIdle();
6767 mFakePolicy->assertNotifyAnrWasNotCalled();
6768
6769 // Once a focused event arrives, we get an ANR for this application
6770 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6771 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006772 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006773 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6774 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006775 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006776 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07006777 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006778 ASSERT_TRUE(mDispatcher->waitForIdle());
6779}
6780
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006781/**
6782 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
6783 * there will not be an ANR.
6784 */
6785TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
6786 mWindow->setFocusable(false);
6787 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6788 mWindow->consumeFocusEvent(false);
6789
6790 KeyEvent event;
6791 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
6792 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
6793
6794 // Define a valid key down event that is stale (too old).
6795 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
6796 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A,
Harry Cutts33476232023-01-30 19:57:29 +00006797 AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006798
6799 const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
6800
6801 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006802 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006803 InputEventInjectionSync::WAIT_FOR_RESULT,
6804 INJECT_EVENT_TIMEOUT, policyFlags);
6805 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
6806 << "Injection should fail because the event is stale";
6807
6808 ASSERT_TRUE(mDispatcher->waitForIdle());
6809 mFakePolicy->assertNotifyAnrWasNotCalled();
6810 mWindow->assertNoEvents();
6811}
6812
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006813// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006814// Make sure that we don't notify policy twice about the same ANR.
6815TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006816 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006817 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6818 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006819
6820 // Once a focused event arrives, we get an ANR for this application
6821 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6822 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006823 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006824 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6825 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006826 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Vishnu Naire4df8752022-09-08 09:17:55 -07006827 const std::chrono::duration appTimeout =
6828 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6829 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006830
Vishnu Naire4df8752022-09-08 09:17:55 -07006831 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006832 // ANR should not be raised again. It is up to policy to do that if it desires.
6833 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006834
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006835 // If we now get a focused window, the ANR should stop, but the policy handles that via
6836 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006837 ASSERT_TRUE(mDispatcher->waitForIdle());
6838}
6839
6840// We have a focused application, but no focused window
6841TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006842 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006843 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6844 mWindow->consumeFocusEvent(false);
6845
6846 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006847 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006848 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006849 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
6850 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006851
Vishnu Naire4df8752022-09-08 09:17:55 -07006852 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6853 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006854
6855 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006856 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006857 ASSERT_TRUE(mDispatcher->waitForIdle());
6858 mWindow->assertNoEvents();
6859}
6860
6861/**
6862 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
6863 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
6864 * If we process 1 of the events, but ANR on the second event with the same timestamp,
6865 * the ANR mechanism should still work.
6866 *
6867 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
6868 * DOWN event, while not responding on the second one.
6869 */
6870TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
6871 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
6872 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6873 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6874 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6875 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006876 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006877
6878 // Now send ACTION_UP, with identical timestamp
6879 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
6880 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6881 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6882 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006883 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006884
6885 // We have now sent down and up. Let's consume first event and then ANR on the second.
6886 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6887 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006888 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006889}
6890
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006891// A spy window can receive an ANR
6892TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
6893 sp<FakeWindowHandle> spy = addSpyWindow();
6894
6895 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6896 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6897 WINDOW_LOCATION));
6898 mWindow->consumeMotionDown();
6899
6900 std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN
6901 ASSERT_TRUE(sequenceNum);
6902 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006903 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006904
6905 spy->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006906 spy->consumeMotionEvent(
6907 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006908 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006909 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006910}
6911
6912// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006913// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006914TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
6915 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006916
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006917 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6918 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006919 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006920 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006921
6922 // Stuck on the ACTION_UP
6923 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006924 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006925
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006926 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006927 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006928 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6929 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006930
6931 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6932 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006933 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006934 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006935 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006936}
6937
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006938// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006939// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006940TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
6941 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006942
6943 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006944 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6945 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006946
6947 mWindow->consumeMotionDown();
6948 // Stuck on the ACTION_UP
6949 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006950 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006951
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006952 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006953 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006954 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6955 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006956
6957 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6958 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006959 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006960 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006961 spy->assertNoEvents();
6962}
6963
6964TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
6965 mDispatcher->setMonitorDispatchingTimeoutForTest(30ms);
6966
6967 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6968
6969 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6970 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6971 WINDOW_LOCATION));
6972
6973 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6974 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
6975 ASSERT_TRUE(consumeSeq);
6976
Prabir Pradhanedd96402022-02-15 01:46:16 -08006977 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006978
6979 monitor.finishEvent(*consumeSeq);
6980 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6981
6982 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006983 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006984}
6985
6986// If a window is unresponsive, then you get anr. if the window later catches up and starts to
6987// process events, you don't get an anr. When the window later becomes unresponsive again, you
6988// get an ANR again.
6989// 1. tap -> block on ACTION_UP -> receive ANR
6990// 2. consume all pending events (= queue becomes healthy again)
6991// 3. tap again -> block on ACTION_UP again -> receive ANR second time
6992TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
6993 tapOnWindow();
6994
6995 mWindow->consumeMotionDown();
6996 // Block on ACTION_UP
6997 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006998 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006999 mWindow->consumeMotionUp(); // Now the connection should be healthy again
7000 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08007001 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007002 mWindow->assertNoEvents();
7003
7004 tapOnWindow();
7005 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08007006 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007007 mWindow->consumeMotionUp();
7008
7009 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 mWindow->assertNoEvents();
7013}
7014
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007015// If a connection remains unresponsive for a while, make sure policy is only notified once about
7016// it.
7017TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007018 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007019 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7020 WINDOW_LOCATION));
7021
7022 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007023 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007024 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007025 // 'notifyConnectionUnresponsive' should only be called once per connection
7026 mFakePolicy->assertNotifyAnrWasNotCalled();
7027 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007028 mWindow->consumeMotionDown();
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08007029 mWindow->consumeMotionEvent(
7030 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007031 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007032 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08007033 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007034 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007035}
7036
7037/**
7038 * If a window is processing a motion event, and then a key event comes in, the key event should
7039 * not to to the focused window until the motion is processed.
7040 *
7041 * Warning!!!
7042 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
7043 * and the injection timeout that we specify when injecting the key.
7044 * We must have the injection timeout (10ms) be smaller than
7045 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
7046 *
7047 * If that value changes, this test should also change.
7048 */
7049TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
7050 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
7051 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
7052
7053 tapOnWindow();
7054 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
7055 ASSERT_TRUE(downSequenceNum);
7056 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
7057 ASSERT_TRUE(upSequenceNum);
7058 // Don't finish the events yet, and send a key
7059 // Injection will "succeed" because we will eventually give up and send the key to the focused
7060 // window even if motions are still being processed. But because the injection timeout is short,
7061 // we will receive INJECTION_TIMED_OUT as the result.
7062
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007063 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007064 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007065 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
7066 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007067 // Key will not be sent to the window, yet, because the window is still processing events
7068 // and the key remains pending, waiting for the touch events to be processed
7069 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
7070 ASSERT_FALSE(keySequenceNum);
7071
7072 std::this_thread::sleep_for(500ms);
7073 // if we wait long enough though, dispatcher will give up, and still send the key
7074 // to the focused window, even though we have not yet finished the motion event
7075 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7076 mWindow->finishEvent(*downSequenceNum);
7077 mWindow->finishEvent(*upSequenceNum);
7078}
7079
7080/**
7081 * If a window is processing a motion event, and then a key event comes in, the key event should
7082 * not go to the focused window until the motion is processed.
7083 * If then a new motion comes in, then the pending key event should be going to the currently
7084 * focused window right away.
7085 */
7086TEST_F(InputDispatcherSingleWindowAnr,
7087 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
7088 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
7089 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
7090
7091 tapOnWindow();
7092 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
7093 ASSERT_TRUE(downSequenceNum);
7094 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
7095 ASSERT_TRUE(upSequenceNum);
7096 // Don't finish the events yet, and send a key
7097 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007098 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007099 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7100 InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007101 // At this point, key is still pending, and should not be sent to the application yet.
7102 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
7103 ASSERT_FALSE(keySequenceNum);
7104
7105 // Now tap down again. It should cause the pending key to go to the focused window right away.
7106 tapOnWindow();
7107 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
7108 // the other events yet. We can finish events in any order.
7109 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
7110 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
7111 mWindow->consumeMotionDown();
7112 mWindow->consumeMotionUp();
7113 mWindow->assertNoEvents();
7114}
7115
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07007116/**
7117 * Send an event to the app and have the app not respond right away.
7118 * When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
7119 * So InputDispatcher will enqueue ACTION_CANCEL event as well.
7120 * At some point, the window becomes responsive again.
7121 * Ensure that subsequent events get dropped, and the next gesture is delivered.
7122 */
7123TEST_F(InputDispatcherSingleWindowAnr, TwoGesturesWithAnr) {
7124 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7125 .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10))
7126 .build());
7127
7128 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
7129 ASSERT_TRUE(sequenceNum);
7130 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
7131 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
7132
7133 mWindow->finishEvent(*sequenceNum);
7134 mWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
7135 ASSERT_TRUE(mDispatcher->waitForIdle());
7136 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
7137
7138 // Now that the window is responsive, let's continue the gesture.
7139 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
7140 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
7141 .build());
7142
7143 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7144 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
7145 .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3))
7146 .build());
7147
7148 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
7149 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
7150 .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3))
7151 .build());
7152 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
7153 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
7154 .build());
7155 // We already canceled this pointer, so the window shouldn't get any new events.
7156 mWindow->assertNoEvents();
7157
7158 // Start another one.
7159 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7160 .pointer(PointerBuilder(0, ToolType::FINGER).x(15).y(15))
7161 .build());
7162 mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
7163}
7164
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007165class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
7166 virtual void SetUp() override {
7167 InputDispatcherTest::SetUp();
7168
Chris Yea209fde2020-07-22 13:54:51 -07007169 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007170 mApplication->setDispatchingTimeout(10ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007171 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
7172 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007173 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007174 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007175 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007176
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007177 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
7178 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05007179 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007180 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007181
7182 // Set focused application.
7183 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07007184 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007185
7186 // Expect one focus window exist in display.
7187 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07007188 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007189 mFocusedWindow->consumeFocusEvent(true);
7190 }
7191
7192 virtual void TearDown() override {
7193 InputDispatcherTest::TearDown();
7194
7195 mUnfocusedWindow.clear();
7196 mFocusedWindow.clear();
7197 }
7198
7199protected:
Chris Yea209fde2020-07-22 13:54:51 -07007200 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007201 sp<FakeWindowHandle> mUnfocusedWindow;
7202 sp<FakeWindowHandle> mFocusedWindow;
7203 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
7204 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
7205 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
7206
7207 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
7208
7209 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
7210
7211private:
7212 void tap(const PointF& location) {
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 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007216 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007217 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7218 location));
7219 }
7220};
7221
7222// If we have 2 windows that are both unresponsive, the one with the shortest timeout
7223// should be ANR'd first.
7224TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007225 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007226 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7227 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007228 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007229 mFocusedWindow->consumeMotionDown();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007230 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007231 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007232 // We consumed all events, so no ANR
7233 ASSERT_TRUE(mDispatcher->waitForIdle());
7234 mFakePolicy->assertNotifyAnrWasNotCalled();
7235
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007236 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007237 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7238 FOCUSED_WINDOW_LOCATION));
7239 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
7240 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007241
7242 const std::chrono::duration timeout =
7243 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007244 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007245 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
7246 // sequence to make it consistent
7247 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007248 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007249 mFocusedWindow->consumeMotionDown();
7250 // This cancel is generated because the connection was unresponsive
7251 mFocusedWindow->consumeMotionCancel();
7252 mFocusedWindow->assertNoEvents();
7253 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007254 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08007255 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7256 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007257 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007258}
7259
7260// If we have 2 windows with identical timeouts that are both unresponsive,
7261// it doesn't matter which order they should have ANR.
7262// But we should receive ANR for both.
7263TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
7264 // Set the timeout for unfocused window to match the focused window
7265 mUnfocusedWindow->setDispatchingTimeout(10ms);
7266 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
7267
7268 tapOnFocusedWindow();
7269 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Prabir Pradhanedd96402022-02-15 01:46:16 -08007270 sp<IBinder> anrConnectionToken1, anrConnectionToken2;
7271 ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms));
7272 ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007273
7274 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007275 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
7276 mFocusedWindow->getToken() == anrConnectionToken2);
7277 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
7278 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007279
7280 ASSERT_TRUE(mDispatcher->waitForIdle());
7281 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007282
7283 mFocusedWindow->consumeMotionDown();
7284 mFocusedWindow->consumeMotionUp();
7285 mUnfocusedWindow->consumeMotionOutside();
7286
Prabir Pradhanedd96402022-02-15 01:46:16 -08007287 sp<IBinder> responsiveToken1, responsiveToken2;
7288 ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken());
7289 ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007290
7291 // Both applications should be marked as responsive, in any order
7292 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
7293 mFocusedWindow->getToken() == responsiveToken2);
7294 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
7295 mUnfocusedWindow->getToken() == responsiveToken2);
7296 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007297}
7298
7299// If a window is already not responding, the second tap on the same window should be ignored.
7300// We should also log an error to account for the dropped event (not tested here).
7301// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
7302TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
7303 tapOnFocusedWindow();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007304 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007305 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007306 // Receive the events, but don't respond
7307 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
7308 ASSERT_TRUE(downEventSequenceNum);
7309 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
7310 ASSERT_TRUE(upEventSequenceNum);
7311 const std::chrono::duration timeout =
7312 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007313 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007314
7315 // Tap once again
7316 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007317 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007318 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7319 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007320 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007321 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7322 FOCUSED_WINDOW_LOCATION));
7323 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
7324 // valid touch target
7325 mUnfocusedWindow->assertNoEvents();
7326
7327 // Consume the first tap
7328 mFocusedWindow->finishEvent(*downEventSequenceNum);
7329 mFocusedWindow->finishEvent(*upEventSequenceNum);
7330 ASSERT_TRUE(mDispatcher->waitForIdle());
7331 // The second tap did not go to the focused window
7332 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007333 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -08007334 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7335 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007336 mFakePolicy->assertNotifyAnrWasNotCalled();
7337}
7338
7339// If you tap outside of all windows, there will not be ANR
7340TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007341 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007342 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7343 LOCATION_OUTSIDE_ALL_WINDOWS));
7344 ASSERT_TRUE(mDispatcher->waitForIdle());
7345 mFakePolicy->assertNotifyAnrWasNotCalled();
7346}
7347
7348// Since the focused window is paused, tapping on it should not produce any events
7349TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
7350 mFocusedWindow->setPaused(true);
7351 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
7352
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007353 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007354 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7355 FOCUSED_WINDOW_LOCATION));
7356
7357 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
7358 ASSERT_TRUE(mDispatcher->waitForIdle());
7359 // Should not ANR because the window is paused, and touches shouldn't go to it
7360 mFakePolicy->assertNotifyAnrWasNotCalled();
7361
7362 mFocusedWindow->assertNoEvents();
7363 mUnfocusedWindow->assertNoEvents();
7364}
7365
7366/**
7367 * If a window is processing a motion event, and then a key event comes in, the key event should
7368 * not to to the focused window until the motion is processed.
7369 * If a different window becomes focused at this time, the key should go to that window instead.
7370 *
7371 * Warning!!!
7372 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
7373 * and the injection timeout that we specify when injecting the key.
7374 * We must have the injection timeout (10ms) be smaller than
7375 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
7376 *
7377 * If that value changes, this test should also change.
7378 */
7379TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
7380 // Set a long ANR timeout to prevent it from triggering
7381 mFocusedWindow->setDispatchingTimeout(2s);
7382 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7383
7384 tapOnUnfocusedWindow();
7385 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
7386 ASSERT_TRUE(downSequenceNum);
7387 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
7388 ASSERT_TRUE(upSequenceNum);
7389 // Don't finish the events yet, and send a key
7390 // Injection will succeed because we will eventually give up and send the key to the focused
7391 // window even if motions are still being processed.
7392
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007393 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007394 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7395 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007396 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007397 // Key will not be sent to the window, yet, because the window is still processing events
7398 // and the key remains pending, waiting for the touch events to be processed
7399 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
7400 ASSERT_FALSE(keySequenceNum);
7401
7402 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07007403 mFocusedWindow->setFocusable(false);
7404 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007405 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07007406 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007407
7408 // Focus events should precede the key events
7409 mUnfocusedWindow->consumeFocusEvent(true);
7410 mFocusedWindow->consumeFocusEvent(false);
7411
7412 // Finish the tap events, which should unblock dispatcher
7413 mUnfocusedWindow->finishEvent(*downSequenceNum);
7414 mUnfocusedWindow->finishEvent(*upSequenceNum);
7415
7416 // Now that all queues are cleared and no backlog in the connections, the key event
7417 // can finally go to the newly focused "mUnfocusedWindow".
7418 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7419 mFocusedWindow->assertNoEvents();
7420 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007421 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007422}
7423
7424// When the touch stream is split across 2 windows, and one of them does not respond,
7425// then ANR should be raised and the touch should be canceled for the unresponsive window.
7426// The other window should not be affected by that.
7427TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
7428 // Touch Window 1
Prabir Pradhan678438e2023-04-13 19:32:51 +00007429 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7430 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7431 {FOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007432 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007433 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007434
7435 // Touch Window 2
Prabir Pradhan678438e2023-04-13 19:32:51 +00007436 mDispatcher->notifyMotion(
7437 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7438 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007439
7440 const std::chrono::duration timeout =
7441 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007442 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007443
7444 mUnfocusedWindow->consumeMotionDown();
7445 mFocusedWindow->consumeMotionDown();
7446 // Focused window may or may not receive ACTION_MOVE
7447 // But it should definitely receive ACTION_CANCEL due to the ANR
7448 InputEvent* event;
7449 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
7450 ASSERT_TRUE(moveOrCancelSequenceNum);
7451 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
7452 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007453 ASSERT_EQ(event->getType(), InputEventType::MOTION);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007454 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
7455 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
7456 mFocusedWindow->consumeMotionCancel();
7457 } else {
7458 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
7459 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007460 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08007461 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7462 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007463
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007464 mUnfocusedWindow->assertNoEvents();
7465 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007466 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007467}
7468
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007469/**
7470 * If we have no focused window, and a key comes in, we start the ANR timer.
7471 * The focused application should add a focused window before the timer runs out to prevent ANR.
7472 *
7473 * If the user touches another application during this time, the key should be dropped.
7474 * Next, if a new focused window comes in, without toggling the focused application,
7475 * then no ANR should occur.
7476 *
7477 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
7478 * but in some cases the policy may not update the focused application.
7479 */
7480TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
7481 std::shared_ptr<FakeApplicationHandle> focusedApplication =
7482 std::make_shared<FakeApplicationHandle>();
7483 focusedApplication->setDispatchingTimeout(60ms);
7484 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
7485 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
7486 mFocusedWindow->setFocusable(false);
7487
7488 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7489 mFocusedWindow->consumeFocusEvent(false);
7490
7491 // Send a key. The ANR timer should start because there is no focused window.
7492 // 'focusedApplication' will get blamed if this timer completes.
7493 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007494 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007495 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7496 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
7497 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007498 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007499
7500 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
7501 // then the injected touches won't cause the focused event to get dropped.
7502 // The dispatcher only checks for whether the queue should be pruned upon queueing.
7503 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
7504 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
7505 // For this test, it means that the key would get delivered to the window once it becomes
7506 // focused.
7507 std::this_thread::sleep_for(10ms);
7508
7509 // Touch unfocused window. This should force the pending key to get dropped.
Prabir Pradhan678438e2023-04-13 19:32:51 +00007510 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7511 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7512 {UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007513
7514 // We do not consume the motion right away, because that would require dispatcher to first
7515 // process (== drop) the key event, and by that time, ANR will be raised.
7516 // Set the focused window first.
7517 mFocusedWindow->setFocusable(true);
7518 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7519 setFocusedWindow(mFocusedWindow);
7520 mFocusedWindow->consumeFocusEvent(true);
7521 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
7522 // to another application. This could be a bug / behaviour in the policy.
7523
7524 mUnfocusedWindow->consumeMotionDown();
7525
7526 ASSERT_TRUE(mDispatcher->waitForIdle());
7527 // Should not ANR because we actually have a focused window. It was just added too slowly.
7528 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
7529}
7530
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007531// These tests ensure we cannot send touch events to a window that's positioned behind a window
7532// that has feature NO_INPUT_CHANNEL.
7533// Layout:
7534// Top (closest to user)
7535// mNoInputWindow (above all windows)
7536// mBottomWindow
7537// Bottom (furthest from user)
7538class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
7539 virtual void SetUp() override {
7540 InputDispatcherTest::SetUp();
7541
7542 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007543 mNoInputWindow =
7544 sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7545 "Window without input channel", ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00007546 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007547 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007548 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7549 // It's perfectly valid for this window to not have an associated input channel
7550
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007551 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
7552 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007553 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
7554
7555 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7556 }
7557
7558protected:
7559 std::shared_ptr<FakeApplicationHandle> mApplication;
7560 sp<FakeWindowHandle> mNoInputWindow;
7561 sp<FakeWindowHandle> mBottomWindow;
7562};
7563
7564TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
7565 PointF touchedPoint = {10, 10};
7566
Prabir Pradhan678438e2023-04-13 19:32:51 +00007567 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7568 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7569 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007570
7571 mNoInputWindow->assertNoEvents();
7572 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
7573 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
7574 // and therefore should prevent mBottomWindow from receiving touches
7575 mBottomWindow->assertNoEvents();
7576}
7577
7578/**
7579 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
7580 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
7581 */
7582TEST_F(InputDispatcherMultiWindowOcclusionTests,
7583 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007584 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7585 "Window with input channel and NO_INPUT_CHANNEL",
7586 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007587
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007588 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007589 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7590 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7591
7592 PointF touchedPoint = {10, 10};
7593
Prabir Pradhan678438e2023-04-13 19:32:51 +00007594 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7595 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7596 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007597
7598 mNoInputWindow->assertNoEvents();
7599 mBottomWindow->assertNoEvents();
7600}
7601
Vishnu Nair958da932020-08-21 17:12:37 -07007602class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
7603protected:
7604 std::shared_ptr<FakeApplicationHandle> mApp;
7605 sp<FakeWindowHandle> mWindow;
7606 sp<FakeWindowHandle> mMirror;
7607
7608 virtual void SetUp() override {
7609 InputDispatcherTest::SetUp();
7610 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007611 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
7612 mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror",
7613 ADISPLAY_ID_DEFAULT, mWindow->getToken());
Vishnu Nair958da932020-08-21 17:12:37 -07007614 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7615 mWindow->setFocusable(true);
7616 mMirror->setFocusable(true);
7617 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7618 }
7619};
7620
7621TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
7622 // Request focus on a mirrored window
7623 setFocusedWindow(mMirror);
7624
7625 // window gets focused
7626 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007627 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7628 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007629 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
7630}
7631
7632// A focused & mirrored window remains focused only if the window and its mirror are both
7633// focusable.
7634TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
7635 setFocusedWindow(mMirror);
7636
7637 // window gets focused
7638 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007639 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7640 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007641 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007642 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7643 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007644 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7645
7646 mMirror->setFocusable(false);
7647 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7648
7649 // window loses focus since one of the windows associated with the token in not focusable
7650 mWindow->consumeFocusEvent(false);
7651
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007652 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7653 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007654 mWindow->assertNoEvents();
7655}
7656
7657// A focused & mirrored window remains focused until the window and its mirror both become
7658// invisible.
7659TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
7660 setFocusedWindow(mMirror);
7661
7662 // window gets focused
7663 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007664 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7665 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007666 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007667 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7668 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007669 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7670
7671 mMirror->setVisible(false);
7672 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7673
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007674 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7675 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007676 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007677 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7678 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007679 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7680
7681 mWindow->setVisible(false);
7682 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7683
7684 // window loses focus only after all windows associated with the token become invisible.
7685 mWindow->consumeFocusEvent(false);
7686
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007687 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7688 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007689 mWindow->assertNoEvents();
7690}
7691
7692// A focused & mirrored window remains focused until both windows are removed.
7693TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
7694 setFocusedWindow(mMirror);
7695
7696 // window gets focused
7697 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007698 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7699 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007700 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007701 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7702 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007703 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7704
7705 // single window is removed but the window token remains focused
7706 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
7707
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007708 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7709 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007710 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007711 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7712 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007713 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7714
7715 // Both windows are removed
7716 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
7717 mWindow->consumeFocusEvent(false);
7718
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007719 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7720 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007721 mWindow->assertNoEvents();
7722}
7723
7724// Focus request can be pending until one window becomes visible.
7725TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
7726 // Request focus on an invisible mirror.
7727 mWindow->setVisible(false);
7728 mMirror->setVisible(false);
7729 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7730 setFocusedWindow(mMirror);
7731
7732 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007733 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007734 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7735 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07007736
7737 mMirror->setVisible(true);
7738 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7739
7740 // window gets focused
7741 mWindow->consumeFocusEvent(true);
7742 // window gets the pending key event
7743 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7744}
Prabir Pradhan99987712020-11-10 18:43:05 -08007745
7746class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
7747protected:
7748 std::shared_ptr<FakeApplicationHandle> mApp;
7749 sp<FakeWindowHandle> mWindow;
7750 sp<FakeWindowHandle> mSecondWindow;
7751
7752 void SetUp() override {
7753 InputDispatcherTest::SetUp();
7754 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007755 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007756 mWindow->setFocusable(true);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007757 mSecondWindow =
7758 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007759 mSecondWindow->setFocusable(true);
7760
7761 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7762 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
7763
7764 setFocusedWindow(mWindow);
7765 mWindow->consumeFocusEvent(true);
7766 }
7767
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007768 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007769 mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request));
Prabir Pradhan99987712020-11-10 18:43:05 -08007770 }
7771
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007772 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
7773 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08007774 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007775 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
7776 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007777 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007778 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08007779 }
7780};
7781
7782TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
7783 // Ensure that capture cannot be obtained for unfocused windows.
7784 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
7785 mFakePolicy->assertSetPointerCaptureNotCalled();
7786 mSecondWindow->assertNoEvents();
7787
7788 // Ensure that capture can be enabled from the focus window.
7789 requestAndVerifyPointerCapture(mWindow, true);
7790
7791 // Ensure that capture cannot be disabled from a window that does not have capture.
7792 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
7793 mFakePolicy->assertSetPointerCaptureNotCalled();
7794
7795 // Ensure that capture can be disabled from the window with capture.
7796 requestAndVerifyPointerCapture(mWindow, false);
7797}
7798
7799TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007800 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007801
7802 setFocusedWindow(mSecondWindow);
7803
7804 // Ensure that the capture disabled event was sent first.
7805 mWindow->consumeCaptureEvent(false);
7806 mWindow->consumeFocusEvent(false);
7807 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007808 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007809
7810 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007811 notifyPointerCaptureChanged({});
7812 notifyPointerCaptureChanged(request);
7813 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08007814 mWindow->assertNoEvents();
7815 mSecondWindow->assertNoEvents();
7816 mFakePolicy->assertSetPointerCaptureNotCalled();
7817}
7818
7819TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007820 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007821
7822 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007823 notifyPointerCaptureChanged({});
7824 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007825
7826 // Ensure that Pointer Capture is disabled.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007827 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007828 mWindow->consumeCaptureEvent(false);
7829 mWindow->assertNoEvents();
7830}
7831
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007832TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
7833 requestAndVerifyPointerCapture(mWindow, true);
7834
7835 // The first window loses focus.
7836 setFocusedWindow(mSecondWindow);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007837 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007838 mWindow->consumeCaptureEvent(false);
7839
7840 // Request Pointer Capture from the second window before the notification from InputReader
7841 // arrives.
7842 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007843 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007844
7845 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007846 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007847
7848 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007849 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007850
7851 mSecondWindow->consumeFocusEvent(true);
7852 mSecondWindow->consumeCaptureEvent(true);
7853}
7854
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007855TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
7856 // App repeatedly enables and disables capture.
7857 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7858 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7859 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7860 mFakePolicy->assertSetPointerCaptureCalled(false);
7861 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7862 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7863
7864 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
7865 // first request is now stale, this should do nothing.
7866 notifyPointerCaptureChanged(firstRequest);
7867 mWindow->assertNoEvents();
7868
7869 // InputReader notifies that the second request was enabled.
7870 notifyPointerCaptureChanged(secondRequest);
7871 mWindow->consumeCaptureEvent(true);
7872}
7873
Prabir Pradhan7092e262022-05-03 16:51:09 +00007874TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
7875 requestAndVerifyPointerCapture(mWindow, true);
7876
7877 // App toggles pointer capture off and on.
7878 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7879 mFakePolicy->assertSetPointerCaptureCalled(false);
7880
7881 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7882 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7883
7884 // InputReader notifies that the latest "enable" request was processed, while skipping over the
7885 // preceding "disable" request.
7886 notifyPointerCaptureChanged(enableRequest);
7887
7888 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
7889 // any notifications.
7890 mWindow->assertNoEvents();
7891}
7892
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007893class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
7894protected:
7895 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00007896
7897 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
7898 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
7899
7900 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
7901 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7902
7903 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
7904 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
7905 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7906 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
7907 MAXIMUM_OBSCURING_OPACITY);
7908
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00007909 static constexpr gui::Uid TOUCHED_APP_UID{10001};
7910 static constexpr gui::Uid APP_B_UID{10002};
7911 static constexpr gui::Uid APP_C_UID{10003};
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007912
7913 sp<FakeWindowHandle> mTouchWindow;
7914
7915 virtual void SetUp() override {
7916 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007917 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007918 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
7919 }
7920
7921 virtual void TearDown() override {
7922 InputDispatcherTest::TearDown();
7923 mTouchWindow.clear();
7924 }
7925
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00007926 sp<FakeWindowHandle> getOccludingWindow(gui::Uid uid, std::string name, TouchOcclusionMode mode,
chaviw3277faf2021-05-19 16:45:23 -05007927 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007928 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007929 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007930 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007931 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007932 return window;
7933 }
7934
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00007935 sp<FakeWindowHandle> getWindow(gui::Uid uid, std::string name) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007936 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
7937 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007938 sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007939 // Generate an arbitrary PID based on the UID
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00007940 window->setOwnerInfo(gui::Pid{static_cast<pid_t>(1777 + (uid.val() % 10000))}, uid);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007941 return window;
7942 }
7943
7944 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007945 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7946 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7947 points));
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007948 }
7949};
7950
7951TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007952 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007953 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007954 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007955
7956 touch();
7957
7958 mTouchWindow->assertNoEvents();
7959}
7960
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007961TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00007962 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
7963 const sp<FakeWindowHandle>& w =
7964 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
7965 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7966
7967 touch();
7968
7969 mTouchWindow->assertNoEvents();
7970}
7971
7972TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007973 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
7974 const sp<FakeWindowHandle>& w =
7975 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
7976 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7977
7978 touch();
7979
7980 w->assertNoEvents();
7981}
7982
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007983TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007984 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
7985 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007986
7987 touch();
7988
7989 mTouchWindow->consumeAnyMotionDown();
7990}
7991
7992TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007993 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007994 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007995 w->setFrame(Rect(0, 0, 50, 50));
7996 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007997
7998 touch({PointF{100, 100}});
7999
8000 mTouchWindow->consumeAnyMotionDown();
8001}
8002
8003TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008004 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008005 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008006 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8007
8008 touch();
8009
8010 mTouchWindow->consumeAnyMotionDown();
8011}
8012
8013TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
8014 const sp<FakeWindowHandle>& w =
8015 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
8016 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00008017
8018 touch();
8019
8020 mTouchWindow->consumeAnyMotionDown();
8021}
8022
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00008023TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
8024 const sp<FakeWindowHandle>& w =
8025 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
8026 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8027
8028 touch();
8029
8030 w->assertNoEvents();
8031}
8032
8033/**
8034 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
8035 * inside) while letting them pass-through. Note that even though touch passes through the occluding
8036 * window, the occluding window will still receive ACTION_OUTSIDE event.
8037 */
8038TEST_F(InputDispatcherUntrustedTouchesTest,
8039 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
8040 const sp<FakeWindowHandle>& w =
8041 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008042 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00008043 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8044
8045 touch();
8046
8047 w->consumeMotionOutside();
8048}
8049
8050TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
8051 const sp<FakeWindowHandle>& w =
8052 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008053 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00008054 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8055
8056 touch();
8057
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008058 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00008059}
8060
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00008061TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008062 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008063 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8064 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008065 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8066
8067 touch();
8068
8069 mTouchWindow->consumeAnyMotionDown();
8070}
8071
8072TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
8073 const sp<FakeWindowHandle>& w =
8074 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8075 MAXIMUM_OBSCURING_OPACITY);
8076 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00008077
8078 touch();
8079
8080 mTouchWindow->consumeAnyMotionDown();
8081}
8082
8083TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008084 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008085 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8086 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008087 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8088
8089 touch();
8090
8091 mTouchWindow->assertNoEvents();
8092}
8093
8094TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
8095 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
8096 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008097 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
8098 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008099 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008100 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
8101 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008102 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
8103
8104 touch();
8105
8106 mTouchWindow->assertNoEvents();
8107}
8108
8109TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
8110 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
8111 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008112 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
8113 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008114 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008115 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
8116 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008117 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
8118
8119 touch();
8120
8121 mTouchWindow->consumeAnyMotionDown();
8122}
8123
8124TEST_F(InputDispatcherUntrustedTouchesTest,
8125 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
8126 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008127 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8128 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008129 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008130 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
8131 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008132 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
8133
8134 touch();
8135
8136 mTouchWindow->consumeAnyMotionDown();
8137}
8138
8139TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
8140 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008141 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8142 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008143 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008144 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
8145 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008146 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00008147
8148 touch();
8149
8150 mTouchWindow->assertNoEvents();
8151}
8152
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008153TEST_F(InputDispatcherUntrustedTouchesTest,
8154 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
8155 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008156 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
8157 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008158 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008159 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8160 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008161 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
8162
8163 touch();
8164
8165 mTouchWindow->assertNoEvents();
8166}
8167
8168TEST_F(InputDispatcherUntrustedTouchesTest,
8169 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
8170 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008171 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
8172 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008173 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008174 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8175 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008176 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
8177
8178 touch();
8179
8180 mTouchWindow->consumeAnyMotionDown();
8181}
8182
8183TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
8184 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008185 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
8186 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008187 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8188
8189 touch();
8190
8191 mTouchWindow->consumeAnyMotionDown();
8192}
8193
8194TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
8195 const sp<FakeWindowHandle>& w =
8196 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
8197 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8198
8199 touch();
8200
8201 mTouchWindow->consumeAnyMotionDown();
8202}
8203
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00008204TEST_F(InputDispatcherUntrustedTouchesTest,
8205 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
8206 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
8207 const sp<FakeWindowHandle>& w =
8208 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
8209 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8210
8211 touch();
8212
8213 mTouchWindow->assertNoEvents();
8214}
8215
8216TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
8217 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
8218 const sp<FakeWindowHandle>& w =
8219 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
8220 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8221
8222 touch();
8223
8224 mTouchWindow->consumeAnyMotionDown();
8225}
8226
8227TEST_F(InputDispatcherUntrustedTouchesTest,
8228 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
8229 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
8230 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008231 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8232 OPACITY_ABOVE_THRESHOLD);
8233 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8234
8235 touch();
8236
8237 mTouchWindow->consumeAnyMotionDown();
8238}
8239
8240TEST_F(InputDispatcherUntrustedTouchesTest,
8241 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
8242 const sp<FakeWindowHandle>& w1 =
8243 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
8244 OPACITY_BELOW_THRESHOLD);
8245 const sp<FakeWindowHandle>& w2 =
8246 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8247 OPACITY_BELOW_THRESHOLD);
8248 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
8249
8250 touch();
8251
8252 mTouchWindow->assertNoEvents();
8253}
8254
8255/**
8256 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
8257 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
8258 * (which alone would result in allowing touches) does not affect the blocking behavior.
8259 */
8260TEST_F(InputDispatcherUntrustedTouchesTest,
8261 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
8262 const sp<FakeWindowHandle>& wB =
8263 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
8264 OPACITY_BELOW_THRESHOLD);
8265 const sp<FakeWindowHandle>& wC =
8266 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
8267 OPACITY_BELOW_THRESHOLD);
8268 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
8269
8270 touch();
8271
8272 mTouchWindow->assertNoEvents();
8273}
8274
8275/**
8276 * This test is testing that a window from a different UID but with same application token doesn't
8277 * block the touch. Apps can share the application token for close UI collaboration for example.
8278 */
8279TEST_F(InputDispatcherUntrustedTouchesTest,
8280 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
8281 const sp<FakeWindowHandle>& w =
8282 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
8283 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00008284 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8285
8286 touch();
8287
8288 mTouchWindow->consumeAnyMotionDown();
8289}
8290
arthurhungb89ccb02020-12-30 16:19:01 +08008291class InputDispatcherDragTests : public InputDispatcherTest {
8292protected:
8293 std::shared_ptr<FakeApplicationHandle> mApp;
8294 sp<FakeWindowHandle> mWindow;
8295 sp<FakeWindowHandle> mSecondWindow;
8296 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008297 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008298 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
8299 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +08008300
8301 void SetUp() override {
8302 InputDispatcherTest::SetUp();
8303 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008304 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08008305 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08008306
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008307 mSecondWindow =
8308 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08008309 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08008310
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008311 mSpyWindow =
8312 sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008313 mSpyWindow->setSpy(true);
8314 mSpyWindow->setTrustedOverlay(true);
8315 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
8316
arthurhungb89ccb02020-12-30 16:19:01 +08008317 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008318 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08008319 }
8320
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008321 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
8322 switch (fromSource) {
8323 case AINPUT_SOURCE_TOUCHSCREEN:
8324 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8325 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
8326 ADISPLAY_ID_DEFAULT, {50, 50}))
8327 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8328 break;
8329 case AINPUT_SOURCE_STYLUS:
8330 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8331 injectMotionEvent(
8332 mDispatcher,
8333 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8334 AINPUT_SOURCE_STYLUS)
8335 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008336 .pointer(PointerBuilder(0, ToolType::STYLUS)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008337 .x(50)
8338 .y(50))
8339 .build()));
8340 break;
8341 case AINPUT_SOURCE_MOUSE:
8342 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8343 injectMotionEvent(
8344 mDispatcher,
8345 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
8346 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8347 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008348 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008349 .x(50)
8350 .y(50))
8351 .build()));
8352 break;
8353 default:
8354 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
8355 }
arthurhungb89ccb02020-12-30 16:19:01 +08008356
8357 // Window should receive motion event.
8358 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008359 // Spy window should also receive motion event
8360 mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +00008361 }
8362
8363 // Start performing drag, we will create a drag window and transfer touch to it.
8364 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
8365 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008366 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +00008367 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008368 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +00008369 }
arthurhungb89ccb02020-12-30 16:19:01 +08008370
8371 // The drag window covers the entire display
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008372 mDragWindow =
8373 sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008374 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
arthurhungb89ccb02020-12-30 16:19:01 +08008375 mDispatcher->setInputWindows(
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008376 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08008377
8378 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +00008379 bool transferred =
8380 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
Harry Cutts33476232023-01-30 19:57:29 +00008381 /*isDragDrop=*/true);
Arthur Hung54745652022-04-20 07:17:41 +00008382 if (transferred) {
8383 mWindow->consumeMotionCancel();
8384 mDragWindow->consumeMotionDown();
8385 }
8386 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +08008387 }
8388};
8389
8390TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008391 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +08008392
8393 // Move on window.
8394 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8395 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8396 ADISPLAY_ID_DEFAULT, {50, 50}))
8397 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8398 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8399 mWindow->consumeDragEvent(false, 50, 50);
8400 mSecondWindow->assertNoEvents();
8401
8402 // Move to another window.
8403 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8404 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8405 ADISPLAY_ID_DEFAULT, {150, 50}))
8406 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8407 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8408 mWindow->consumeDragEvent(true, 150, 50);
8409 mSecondWindow->consumeDragEvent(false, 50, 50);
8410
8411 // Move back to original window.
8412 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8413 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8414 ADISPLAY_ID_DEFAULT, {50, 50}))
8415 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8416 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8417 mWindow->consumeDragEvent(false, 50, 50);
8418 mSecondWindow->consumeDragEvent(true, -50, 50);
8419
8420 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8421 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
8422 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8423 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8424 mWindow->assertNoEvents();
8425 mSecondWindow->assertNoEvents();
8426}
8427
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008428TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008429 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008430
8431 // No cancel event after drag start
8432 mSpyWindow->assertNoEvents();
8433
8434 const MotionEvent secondFingerDownEvent =
8435 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8436 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008437 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8438 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008439 .build();
8440 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8441 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8442 InputEventInjectionSync::WAIT_FOR_RESULT))
8443 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8444
8445 // Receives cancel for first pointer after next pointer down
8446 mSpyWindow->consumeMotionCancel();
8447 mSpyWindow->consumeMotionDown();
8448
8449 mSpyWindow->assertNoEvents();
8450}
8451
arthurhungf452d0b2021-01-06 00:19:52 +08008452TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008453 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +08008454
8455 // Move on window.
8456 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8457 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8458 ADISPLAY_ID_DEFAULT, {50, 50}))
8459 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8460 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8461 mWindow->consumeDragEvent(false, 50, 50);
8462 mSecondWindow->assertNoEvents();
8463
8464 // Move to another window.
8465 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8466 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8467 ADISPLAY_ID_DEFAULT, {150, 50}))
8468 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8469 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8470 mWindow->consumeDragEvent(true, 150, 50);
8471 mSecondWindow->consumeDragEvent(false, 50, 50);
8472
8473 // drop to another window.
8474 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8475 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8476 {150, 50}))
8477 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8478 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -07008479 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
arthurhungf452d0b2021-01-06 00:19:52 +08008480 mWindow->assertNoEvents();
8481 mSecondWindow->assertNoEvents();
8482}
8483
arthurhung6d4bed92021-03-17 11:59:33 +08008484TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008485 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +08008486
8487 // Move on window and keep button pressed.
8488 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8489 injectMotionEvent(mDispatcher,
8490 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
8491 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008492 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008493 .build()))
8494 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8495 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8496 mWindow->consumeDragEvent(false, 50, 50);
8497 mSecondWindow->assertNoEvents();
8498
8499 // Move to another window and release button, expect to drop item.
8500 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8501 injectMotionEvent(mDispatcher,
8502 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
8503 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008504 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008505 .build()))
8506 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8507 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8508 mWindow->assertNoEvents();
8509 mSecondWindow->assertNoEvents();
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -07008510 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
arthurhung6d4bed92021-03-17 11:59:33 +08008511
8512 // nothing to the window.
8513 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8514 injectMotionEvent(mDispatcher,
8515 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
8516 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008517 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008518 .build()))
8519 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8520 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8521 mWindow->assertNoEvents();
8522 mSecondWindow->assertNoEvents();
8523}
8524
Arthur Hung54745652022-04-20 07:17:41 +00008525TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008526 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +08008527
8528 // Set second window invisible.
8529 mSecondWindow->setVisible(false);
8530 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
8531
8532 // Move on window.
8533 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8534 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8535 ADISPLAY_ID_DEFAULT, {50, 50}))
8536 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8537 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8538 mWindow->consumeDragEvent(false, 50, 50);
8539 mSecondWindow->assertNoEvents();
8540
8541 // Move to another window.
8542 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8543 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8544 ADISPLAY_ID_DEFAULT, {150, 50}))
8545 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8546 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8547 mWindow->consumeDragEvent(true, 150, 50);
8548 mSecondWindow->assertNoEvents();
8549
8550 // drop to another window.
8551 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8552 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8553 {150, 50}))
8554 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8555 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -07008556 mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr);
Arthur Hung6d0571e2021-04-09 20:18:16 +08008557 mWindow->assertNoEvents();
8558 mSecondWindow->assertNoEvents();
8559}
8560
Arthur Hung54745652022-04-20 07:17:41 +00008561TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008562 // Ensure window could track pointerIds if it didn't support split touch.
8563 mWindow->setPreventSplitting(true);
8564
Arthur Hung54745652022-04-20 07:17:41 +00008565 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8566 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8567 {50, 50}))
8568 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8569 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8570
8571 const MotionEvent secondFingerDownEvent =
8572 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8573 .displayId(ADISPLAY_ID_DEFAULT)
8574 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008575 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8576 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008577 .build();
8578 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8579 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8580 InputEventInjectionSync::WAIT_FOR_RESULT))
8581 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00008582 mWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
Arthur Hung54745652022-04-20 07:17:41 +00008583
8584 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008585 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008586}
8587
8588TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
8589 // First down on second window.
8590 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8591 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8592 {150, 50}))
8593 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8594
8595 mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8596
8597 // Second down on first window.
8598 const MotionEvent secondFingerDownEvent =
8599 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8600 .displayId(ADISPLAY_ID_DEFAULT)
8601 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008602 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8603 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008604 .build();
8605 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8606 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8607 InputEventInjectionSync::WAIT_FOR_RESULT))
8608 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8609 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8610
8611 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008612 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008613
8614 // Move on window.
8615 const MotionEvent secondFingerMoveEvent =
8616 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
8617 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008618 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8619 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008620 .build();
8621 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8622 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
8623 InputEventInjectionSync::WAIT_FOR_RESULT));
8624 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8625 mWindow->consumeDragEvent(false, 50, 50);
8626 mSecondWindow->consumeMotionMove();
8627
8628 // Release the drag pointer should perform drop.
8629 const MotionEvent secondFingerUpEvent =
8630 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
8631 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008632 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8633 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008634 .build();
8635 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8636 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
8637 InputEventInjectionSync::WAIT_FOR_RESULT));
8638 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -07008639 mFakePolicy->assertDropTargetEquals(*mDispatcher, mWindow->getToken());
Arthur Hung54745652022-04-20 07:17:41 +00008640 mWindow->assertNoEvents();
8641 mSecondWindow->consumeMotionMove();
8642}
8643
Arthur Hung3915c1f2022-05-31 07:17:17 +00008644TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008645 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +00008646
8647 // Update window of second display.
8648 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008649 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008650 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8651
8652 // Let second display has a touch state.
8653 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8654 injectMotionEvent(mDispatcher,
8655 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8656 AINPUT_SOURCE_TOUCHSCREEN)
8657 .displayId(SECOND_DISPLAY_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008658 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Arthur Hung3915c1f2022-05-31 07:17:17 +00008659 .build()));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07008660 windowInSecondary->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
Harry Cutts33476232023-01-30 19:57:29 +00008661 SECOND_DISPLAY_ID, /*expectedFlag=*/0);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008662 // Update window again.
8663 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8664
8665 // Move on window.
8666 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8667 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8668 ADISPLAY_ID_DEFAULT, {50, 50}))
8669 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8670 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8671 mWindow->consumeDragEvent(false, 50, 50);
8672 mSecondWindow->assertNoEvents();
8673
8674 // Move to another window.
8675 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8676 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8677 ADISPLAY_ID_DEFAULT, {150, 50}))
8678 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8679 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8680 mWindow->consumeDragEvent(true, 150, 50);
8681 mSecondWindow->consumeDragEvent(false, 50, 50);
8682
8683 // drop to another window.
8684 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8685 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8686 {150, 50}))
8687 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8688 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -07008689 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
Arthur Hung3915c1f2022-05-31 07:17:17 +00008690 mWindow->assertNoEvents();
8691 mSecondWindow->assertNoEvents();
8692}
8693
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008694TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
8695 startDrag(true, AINPUT_SOURCE_MOUSE);
8696 // Move on window.
8697 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8698 injectMotionEvent(mDispatcher,
8699 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8700 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8701 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008702 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008703 .x(50)
8704 .y(50))
8705 .build()))
8706 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8707 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8708 mWindow->consumeDragEvent(false, 50, 50);
8709 mSecondWindow->assertNoEvents();
8710
8711 // Move to another window.
8712 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8713 injectMotionEvent(mDispatcher,
8714 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8715 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8716 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008717 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008718 .x(150)
8719 .y(50))
8720 .build()))
8721 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8722 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8723 mWindow->consumeDragEvent(true, 150, 50);
8724 mSecondWindow->consumeDragEvent(false, 50, 50);
8725
8726 // drop to another window.
8727 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8728 injectMotionEvent(mDispatcher,
8729 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
8730 .buttonState(0)
8731 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008732 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008733 .x(150)
8734 .y(50))
8735 .build()))
8736 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8737 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -07008738 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008739 mWindow->assertNoEvents();
8740 mSecondWindow->assertNoEvents();
8741}
8742
Vishnu Nair062a8672021-09-03 16:07:44 -07008743class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
8744
8745TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
8746 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008747 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8748 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008749 window->setDropInput(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008750 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8751 window->setFocusable(true);
8752 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8753 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008754 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008755
8756 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008757 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008758 window->assertNoEvents();
8759
Prabir Pradhan678438e2023-04-13 19:32:51 +00008760 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8761 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -07008762 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
8763 ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008764 window->assertNoEvents();
8765
8766 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008767 window->setDropInput(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008768 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8769
Prabir Pradhan678438e2023-04-13 19:32:51 +00008770 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008771 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8772
Prabir Pradhan678438e2023-04-13 19:32:51 +00008773 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8774 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008775 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8776 window->assertNoEvents();
8777}
8778
8779TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
8780 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8781 std::make_shared<FakeApplicationHandle>();
8782 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008783 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8784 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008785 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00008786 obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008787 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008788 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008789 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8790 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008791 window->setDropInputIfObscured(true);
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00008792 window->setOwnerInfo(gui::Pid{222}, gui::Uid{222});
Vishnu Nair062a8672021-09-03 16:07:44 -07008793 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8794 window->setFocusable(true);
8795 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8796 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008797 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008798
8799 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008800 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008801 window->assertNoEvents();
8802
Prabir Pradhan678438e2023-04-13 19:32:51 +00008803 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8804 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -07008805 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
8806 ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008807 window->assertNoEvents();
8808
8809 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008810 window->setDropInputIfObscured(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008811 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8812
Prabir Pradhan678438e2023-04-13 19:32:51 +00008813 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008814 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8815
Prabir Pradhan678438e2023-04-13 19:32:51 +00008816 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8817 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008818 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
8819 window->assertNoEvents();
8820}
8821
8822TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
8823 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8824 std::make_shared<FakeApplicationHandle>();
8825 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008826 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8827 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008828 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00008829 obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008830 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008831 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008832 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8833 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008834 window->setDropInputIfObscured(true);
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00008835 window->setOwnerInfo(gui::Pid{222}, gui::Uid{222});
Vishnu Nair062a8672021-09-03 16:07:44 -07008836 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8837 window->setFocusable(true);
8838 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8839 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008840 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008841
8842 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008843 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008844 window->assertNoEvents();
8845
Prabir Pradhan678438e2023-04-13 19:32:51 +00008846 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8847 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -07008848 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
8849 ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008850 window->assertNoEvents();
8851
8852 // When the window is no longer obscured because it went on top, it should get input
8853 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}});
8854
Prabir Pradhan678438e2023-04-13 19:32:51 +00008855 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008856 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8857
Prabir Pradhan678438e2023-04-13 19:32:51 +00008858 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8859 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008860 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8861 window->assertNoEvents();
8862}
8863
Antonio Kantekf16f2832021-09-28 04:39:20 +00008864class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
8865protected:
8866 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +00008867 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008868 sp<FakeWindowHandle> mWindow;
8869 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +00008870 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008871
8872 void SetUp() override {
8873 InputDispatcherTest::SetUp();
8874
8875 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +00008876 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008877 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008878 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008879 setFocusedWindow(mWindow);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008880 mSecondWindow =
8881 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008882 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +00008883 mThirdWindow =
8884 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
8885 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
8886 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008887
8888 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Antonio Kantek15beb512022-06-13 22:35:41 +00008889 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}},
8890 {SECOND_DISPLAY_ID, {mThirdWindow}}});
8891 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008892 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008893
Antonio Kantek15beb512022-06-13 22:35:41 +00008894 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +00008895 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008896 WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -07008897 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
8898 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008899 mThirdWindow->assertNoEvents();
8900 }
8901
8902 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
8903 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008904 SECONDARY_WINDOW_UID, /*hasPermission=*/true,
Antonio Kantek15beb512022-06-13 22:35:41 +00008905 SECOND_DISPLAY_ID)) {
8906 mWindow->assertNoEvents();
8907 mSecondWindow->assertNoEvents();
8908 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -07008909 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00008910 }
8911
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00008912 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, gui::Pid pid, gui::Uid uid,
Antonio Kantek15beb512022-06-13 22:35:41 +00008913 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -07008914 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
8915 ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008916 mWindow->consumeTouchModeEvent(inTouchMode);
8917 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008918 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +00008919 }
8920};
8921
Antonio Kantek26defcf2022-02-08 01:12:27 +00008922TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008923 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +00008924 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
8925 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008926 /* hasPermission=*/false);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008927}
8928
Antonio Kantek26defcf2022-02-08 01:12:27 +00008929TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
8930 const WindowInfo& windowInfo = *mWindow->getInfo();
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00008931 gui::Pid ownerPid = windowInfo.ownerPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00008932 gui::Uid ownerUid = windowInfo.ownerUid;
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00008933 mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008934 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008935 ownerUid, /*hasPermission=*/false,
Antonio Kanteka042c022022-07-06 16:51:07 -07008936 ADISPLAY_ID_DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +00008937 mWindow->assertNoEvents();
8938 mSecondWindow->assertNoEvents();
8939}
8940
8941TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
8942 const WindowInfo& windowInfo = *mWindow->getInfo();
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00008943 gui::Pid ownerPid = windowInfo.ownerPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00008944 gui::Uid ownerUid = windowInfo.ownerUid;
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00008945 mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID);
Antonio Kantek15beb512022-06-13 22:35:41 +00008946 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008947 ownerUid, /*hasPermission=*/true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008948}
8949
Antonio Kantekf16f2832021-09-28 04:39:20 +00008950TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008951 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +00008952 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
8953 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008954 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008955 mWindow->assertNoEvents();
8956 mSecondWindow->assertNoEvents();
8957}
8958
Antonio Kantek15beb512022-06-13 22:35:41 +00008959TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
8960 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
8961 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8962 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008963 /*hasPermission=*/true, SECOND_DISPLAY_ID));
Antonio Kantek15beb512022-06-13 22:35:41 +00008964 mWindow->assertNoEvents();
8965 mSecondWindow->assertNoEvents();
8966 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
8967}
8968
Antonio Kantek48710e42022-03-24 14:19:30 -07008969TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
8970 // Interact with the window first.
8971 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
8972 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8973 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
8974
8975 // Then remove focus.
8976 mWindow->setFocusable(false);
8977 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
8978
8979 // Assert that caller can switch touch mode by owning one of the last interacted window.
8980 const WindowInfo& windowInfo = *mWindow->getInfo();
8981 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8982 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008983 /*hasPermission=*/false, ADISPLAY_ID_DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -07008984}
8985
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008986class InputDispatcherSpyWindowTest : public InputDispatcherTest {
8987public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008988 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008989 std::shared_ptr<FakeApplicationHandle> application =
8990 std::make_shared<FakeApplicationHandle>();
8991 std::string name = "Fake Spy ";
8992 name += std::to_string(mSpyCount++);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008993 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher,
8994 name.c_str(), ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008995 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008996 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008997 return spy;
8998 }
8999
9000 sp<FakeWindowHandle> createForeground() {
9001 std::shared_ptr<FakeApplicationHandle> application =
9002 std::make_shared<FakeApplicationHandle>();
9003 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009004 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
9005 ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009006 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009007 return window;
9008 }
9009
9010private:
9011 int mSpyCount{0};
9012};
9013
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08009014using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009015/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -08009016 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
9017 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08009018TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
9019 ScopedSilentDeath _silentDeath;
9020
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009021 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -08009022 spy->setTrustedOverlay(false);
9023 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}),
9024 ".* not a trusted overlay");
9025}
9026
9027/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009028 * Input injection into a display with a spy window but no foreground windows should succeed.
9029 */
9030TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009031 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009032 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}});
9033
9034 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9035 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9036 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9037 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9038}
9039
9040/**
9041 * Verify the order in which different input windows receive events. The touched foreground window
9042 * (if there is one) should always receive the event first. When there are multiple spy windows, the
9043 * spy windows will receive the event according to their Z-order, where the top-most spy window will
9044 * receive events before ones belows it.
9045 *
9046 * Here, we set up a scenario with four windows in the following Z order from the top:
9047 * spy1, spy2, window, spy3.
9048 * We then inject an event and verify that the foreground "window" receives it first, followed by
9049 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
9050 * window.
9051 */
9052TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
9053 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009054 auto spy1 = createSpy();
9055 auto spy2 = createSpy();
9056 auto spy3 = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009057 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}});
9058 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
9059 const size_t numChannels = channels.size();
9060
Michael Wright8e9a8562022-02-09 13:44:29 +00009061 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009062 if (!epollFd.ok()) {
9063 FAIL() << "Failed to create epoll fd";
9064 }
9065
9066 for (size_t i = 0; i < numChannels; i++) {
9067 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
9068 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
9069 FAIL() << "Failed to add fd to epoll";
9070 }
9071 }
9072
9073 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9074 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9075 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9076
9077 std::vector<size_t> eventOrder;
9078 std::vector<struct epoll_event> events(numChannels);
9079 for (;;) {
9080 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
9081 (100ms).count());
9082 if (nFds < 0) {
9083 FAIL() << "Failed to call epoll_wait";
9084 }
9085 if (nFds == 0) {
9086 break; // epoll_wait timed out
9087 }
9088 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -07009089 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -07009090 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009091 channels[i]->consumeMotionDown();
9092 }
9093 }
9094
9095 // Verify the order in which the events were received.
9096 EXPECT_EQ(3u, eventOrder.size());
9097 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
9098 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
9099 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
9100}
9101
9102/**
9103 * A spy window using the NOT_TOUCHABLE flag does not receive events.
9104 */
9105TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
9106 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009107 auto spy = createSpy();
9108 spy->setTouchable(false);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009109 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9110
9111 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9112 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9113 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9114 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9115 spy->assertNoEvents();
9116}
9117
9118/**
9119 * A spy window will only receive gestures that originate within its touchable region. Gestures that
9120 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
9121 * to the window.
9122 */
9123TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
9124 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009125 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009126 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
9127 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9128
9129 // Inject an event outside the spy window's touchable region.
9130 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9131 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9132 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9133 window->consumeMotionDown();
9134 spy->assertNoEvents();
9135 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9136 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9137 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9138 window->consumeMotionUp();
9139 spy->assertNoEvents();
9140
9141 // Inject an event inside the spy window's touchable region.
9142 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9143 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9144 {5, 10}))
9145 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9146 window->consumeMotionDown();
9147 spy->consumeMotionDown();
9148}
9149
9150/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009151 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009152 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009153 */
9154TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
9155 auto window = createForeground();
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00009156 window->setOwnerInfo(gui::Pid{12}, gui::Uid{34});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009157 auto spy = createSpy();
9158 spy->setWatchOutsideTouch(true);
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00009159 spy->setOwnerInfo(gui::Pid{56}, gui::Uid{78});
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009160 spy->setFrame(Rect{0, 0, 20, 20});
9161 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9162
9163 // Inject an event outside the spy window's frame and touchable region.
9164 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009165 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9166 {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009167 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9168 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009169 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009170}
9171
9172/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009173 * Even when a spy window spans over multiple foreground windows, the spy should receive all
9174 * pointers that are down within its bounds.
9175 */
9176TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
9177 auto windowLeft = createForeground();
9178 windowLeft->setFrame({0, 0, 100, 200});
9179 auto windowRight = createForeground();
9180 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009181 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009182 spy->setFrame({0, 0, 200, 200});
9183 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}});
9184
9185 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9186 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9187 {50, 50}))
9188 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9189 windowLeft->consumeMotionDown();
9190 spy->consumeMotionDown();
9191
9192 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08009193 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009194 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009195 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
9196 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009197 .build();
9198 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9199 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9200 InputEventInjectionSync::WAIT_FOR_RESULT))
9201 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9202 windowRight->consumeMotionDown();
Harry Cutts33476232023-01-30 19:57:29 +00009203 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009204}
9205
9206/**
9207 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
9208 * the spy should receive the second pointer with ACTION_DOWN.
9209 */
9210TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
9211 auto window = createForeground();
9212 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009213 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009214 spyRight->setFrame({100, 0, 200, 200});
9215 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}});
9216
9217 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9218 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9219 {50, 50}))
9220 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9221 window->consumeMotionDown();
9222 spyRight->assertNoEvents();
9223
9224 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08009225 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009226 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009227 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
9228 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009229 .build();
9230 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9231 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9232 InputEventInjectionSync::WAIT_FOR_RESULT))
9233 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00009234 window->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009235 spyRight->consumeMotionDown();
9236}
9237
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009238/**
9239 * The spy window should not be able to affect whether or not touches are split. Only the foreground
9240 * windows should be allowed to control split touch.
9241 */
9242TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08009243 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009244 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009245 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08009246 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009247
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009248 auto window = createForeground();
9249 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009250
9251 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9252
9253 // First finger down, no window touched.
9254 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9255 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9256 {100, 200}))
9257 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9258 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9259 window->assertNoEvents();
9260
9261 // Second finger down on window, the window should receive touch down.
9262 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08009263 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009264 .displayId(ADISPLAY_ID_DEFAULT)
9265 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009266 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9267 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009268 .build();
9269 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9270 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9271 InputEventInjectionSync::WAIT_FOR_RESULT))
9272 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9273
9274 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00009275 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009276}
9277
9278/**
9279 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
9280 * do not receive key events.
9281 */
9282TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009283 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009284 spy->setFocusable(false);
9285
9286 auto window = createForeground();
9287 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9288 setFocusedWindow(window);
9289 window->consumeFocusEvent(true);
9290
9291 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
9292 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
9293 window->consumeKeyDown(ADISPLAY_ID_NONE);
9294
9295 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
9296 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
9297 window->consumeKeyUp(ADISPLAY_ID_NONE);
9298
9299 spy->assertNoEvents();
9300}
9301
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009302using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
9303
9304/**
9305 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
9306 * are currently sent to any other windows - including other spy windows - will also be cancelled.
9307 */
9308TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
9309 auto window = createForeground();
9310 auto spy1 = createSpy();
9311 auto spy2 = createSpy();
9312 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}});
9313
9314 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9315 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9316 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9317 window->consumeMotionDown();
9318 spy1->consumeMotionDown();
9319 spy2->consumeMotionDown();
9320
9321 // Pilfer pointers from the second spy window.
9322 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
9323 spy2->assertNoEvents();
9324 spy1->consumeMotionCancel();
9325 window->consumeMotionCancel();
9326
9327 // The rest of the gesture should only be sent to the second spy window.
9328 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9329 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
9330 ADISPLAY_ID_DEFAULT))
9331 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9332 spy2->consumeMotionMove();
9333 spy1->assertNoEvents();
9334 window->assertNoEvents();
9335}
9336
9337/**
9338 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
9339 * in the middle of the gesture.
9340 */
9341TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
9342 auto window = createForeground();
9343 auto spy = createSpy();
9344 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9345
9346 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9347 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9348 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9349 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9350 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9351
9352 window->releaseChannel();
9353
9354 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9355
9356 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9357 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9358 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9359 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
9360}
9361
9362/**
9363 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
9364 * the spy, but not to any other windows.
9365 */
9366TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
9367 auto spy = createSpy();
9368 auto window = createForeground();
9369
9370 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9371
9372 // First finger down on the window and the spy.
9373 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9374 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9375 {100, 200}))
9376 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9377 spy->consumeMotionDown();
9378 window->consumeMotionDown();
9379
9380 // Spy window pilfers the pointers.
9381 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9382 window->consumeMotionCancel();
9383
9384 // Second finger down on the window and spy, but the window should not receive the pointer down.
9385 const MotionEvent secondFingerDownEvent =
9386 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9387 .displayId(ADISPLAY_ID_DEFAULT)
9388 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009389 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9390 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009391 .build();
9392 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9393 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9394 InputEventInjectionSync::WAIT_FOR_RESULT))
9395 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9396
Harry Cutts33476232023-01-30 19:57:29 +00009397 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009398
9399 // Third finger goes down outside all windows, so injection should fail.
9400 const MotionEvent thirdFingerDownEvent =
9401 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9402 .displayId(ADISPLAY_ID_DEFAULT)
9403 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009404 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9405 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
9406 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009407 .build();
9408 ASSERT_EQ(InputEventInjectionResult::FAILED,
9409 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
9410 InputEventInjectionSync::WAIT_FOR_RESULT))
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08009411 << "Inject motion event should return InputEventInjectionResult::FAILED";
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009412
9413 spy->assertNoEvents();
9414 window->assertNoEvents();
9415}
9416
9417/**
9418 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
9419 */
9420TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
9421 auto spy = createSpy();
9422 spy->setFrame(Rect(0, 0, 100, 100));
9423 auto window = createForeground();
9424 window->setFrame(Rect(0, 0, 200, 200));
9425
9426 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9427
9428 // First finger down on the window only
9429 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9430 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9431 {150, 150}))
9432 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9433 window->consumeMotionDown();
9434
9435 // Second finger down on the spy and window
9436 const MotionEvent secondFingerDownEvent =
9437 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9438 .displayId(ADISPLAY_ID_DEFAULT)
9439 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009440 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
9441 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009442 .build();
9443 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9444 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9445 InputEventInjectionSync::WAIT_FOR_RESULT))
9446 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9447 spy->consumeMotionDown();
9448 window->consumeMotionPointerDown(1);
9449
9450 // Third finger down on the spy and window
9451 const MotionEvent thirdFingerDownEvent =
9452 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9453 .displayId(ADISPLAY_ID_DEFAULT)
9454 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009455 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
9456 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
9457 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009458 .build();
9459 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9460 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
9461 InputEventInjectionSync::WAIT_FOR_RESULT))
9462 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9463 spy->consumeMotionPointerDown(1);
9464 window->consumeMotionPointerDown(2);
9465
9466 // Spy window pilfers the pointers.
9467 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9468 window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
9469 window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
9470
9471 spy->assertNoEvents();
9472 window->assertNoEvents();
9473}
9474
9475/**
9476 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
9477 * other windows should be canceled. If this results in the cancellation of all pointers for some
9478 * window, then that window should receive ACTION_CANCEL.
9479 */
9480TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
9481 auto spy = createSpy();
9482 spy->setFrame(Rect(0, 0, 100, 100));
9483 auto window = createForeground();
9484 window->setFrame(Rect(0, 0, 200, 200));
9485
9486 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9487
9488 // First finger down on both spy and window
9489 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9490 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9491 {10, 10}))
9492 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9493 window->consumeMotionDown();
9494 spy->consumeMotionDown();
9495
9496 // Second finger down on the spy and window
9497 const MotionEvent secondFingerDownEvent =
9498 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9499 .displayId(ADISPLAY_ID_DEFAULT)
9500 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009501 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9502 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009503 .build();
9504 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9505 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9506 InputEventInjectionSync::WAIT_FOR_RESULT))
9507 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9508 spy->consumeMotionPointerDown(1);
9509 window->consumeMotionPointerDown(1);
9510
9511 // Spy window pilfers the pointers.
9512 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9513 window->consumeMotionCancel();
9514
9515 spy->assertNoEvents();
9516 window->assertNoEvents();
9517}
9518
9519/**
9520 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
9521 * be sent to other windows
9522 */
9523TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
9524 auto spy = createSpy();
9525 spy->setFrame(Rect(0, 0, 100, 100));
9526 auto window = createForeground();
9527 window->setFrame(Rect(0, 0, 200, 200));
9528
9529 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9530
9531 // First finger down on both window and spy
9532 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9533 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9534 {10, 10}))
9535 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9536 window->consumeMotionDown();
9537 spy->consumeMotionDown();
9538
9539 // Spy window pilfers the pointers.
9540 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9541 window->consumeMotionCancel();
9542
9543 // Second finger down on the window only
9544 const MotionEvent secondFingerDownEvent =
9545 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9546 .displayId(ADISPLAY_ID_DEFAULT)
9547 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009548 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9549 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009550 .build();
9551 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9552 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9553 InputEventInjectionSync::WAIT_FOR_RESULT))
9554 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9555 window->consumeMotionDown();
9556 window->assertNoEvents();
9557
9558 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
9559 spy->consumeMotionMove();
9560 spy->assertNoEvents();
9561}
9562
Prabir Pradhand65552b2021-10-07 11:23:50 -07009563class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
9564public:
9565 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
9566 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9567 std::make_shared<FakeApplicationHandle>();
9568 sp<FakeWindowHandle> overlay =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009569 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
9570 "Stylus interceptor window", ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009571 overlay->setFocusable(false);
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00009572 overlay->setOwnerInfo(gui::Pid{111}, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009573 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009574 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009575 overlay->setTrustedOverlay(true);
9576
9577 std::shared_ptr<FakeApplicationHandle> application =
9578 std::make_shared<FakeApplicationHandle>();
9579 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009580 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
9581 ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009582 window->setFocusable(true);
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00009583 window->setOwnerInfo(gui::Pid{222}, gui::Uid{222});
Prabir Pradhand65552b2021-10-07 11:23:50 -07009584
9585 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
9586 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9587 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00009588 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009589 return {std::move(overlay), std::move(window)};
9590 }
9591
9592 void sendFingerEvent(int32_t action) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00009593 mDispatcher->notifyMotion(
Prabir Pradhand65552b2021-10-07 11:23:50 -07009594 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
Prabir Pradhan678438e2023-04-13 19:32:51 +00009595 ADISPLAY_ID_DEFAULT, {PointF{20, 20}}));
Prabir Pradhand65552b2021-10-07 11:23:50 -07009596 }
9597
9598 void sendStylusEvent(int32_t action) {
9599 NotifyMotionArgs motionArgs =
9600 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
9601 ADISPLAY_ID_DEFAULT, {PointF{30, 40}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009602 motionArgs.pointerProperties[0].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +00009603 mDispatcher->notifyMotion(motionArgs);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009604 }
9605};
9606
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08009607using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
9608
9609TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
Siarhei Vishniakouad3b6822023-06-22 14:17:35 -07009610 testing::GTEST_FLAG(death_test_style) = "threadsafe";
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08009611 ScopedSilentDeath _silentDeath;
9612
Prabir Pradhand65552b2021-10-07 11:23:50 -07009613 auto [overlay, window] = setupStylusOverlayScenario();
9614 overlay->setTrustedOverlay(false);
9615 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
9616 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}),
9617 ".* not a trusted overlay");
9618}
9619
9620TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
9621 auto [overlay, window] = setupStylusOverlayScenario();
9622 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9623
9624 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9625 overlay->consumeMotionDown();
9626 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9627 overlay->consumeMotionUp();
9628
9629 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9630 window->consumeMotionDown();
9631 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9632 window->consumeMotionUp();
9633
9634 overlay->assertNoEvents();
9635 window->assertNoEvents();
9636}
9637
9638TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
9639 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009640 overlay->setSpy(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009641 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9642
9643 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9644 overlay->consumeMotionDown();
9645 window->consumeMotionDown();
9646 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9647 overlay->consumeMotionUp();
9648 window->consumeMotionUp();
9649
9650 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9651 window->consumeMotionDown();
9652 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9653 window->consumeMotionUp();
9654
9655 overlay->assertNoEvents();
9656 window->assertNoEvents();
9657}
9658
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00009659/**
9660 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
9661 * The scenario is as follows:
9662 * - The stylus interceptor overlay is configured as a spy window.
9663 * - The stylus interceptor spy receives the start of a new stylus gesture.
9664 * - It pilfers pointers and then configures itself to no longer be a spy.
9665 * - The stylus interceptor continues to receive the rest of the gesture.
9666 */
9667TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
9668 auto [overlay, window] = setupStylusOverlayScenario();
9669 overlay->setSpy(true);
9670 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9671
9672 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9673 overlay->consumeMotionDown();
9674 window->consumeMotionDown();
9675
9676 // The interceptor pilfers the pointers.
9677 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
9678 window->consumeMotionCancel();
9679
9680 // The interceptor configures itself so that it is no longer a spy.
9681 overlay->setSpy(false);
9682 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9683
9684 // It continues to receive the rest of the stylus gesture.
9685 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
9686 overlay->consumeMotionMove();
9687 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9688 overlay->consumeMotionUp();
9689
9690 window->assertNoEvents();
9691}
9692
Prabir Pradhan5735a322022-04-11 17:23:34 +00009693struct User {
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00009694 gui::Pid mPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00009695 gui::Uid mUid;
Prabir Pradhan5735a322022-04-11 17:23:34 +00009696 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
9697 std::unique_ptr<InputDispatcher>& mDispatcher;
9698
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00009699 User(std::unique_ptr<InputDispatcher>& dispatcher, gui::Pid pid, gui::Uid uid)
Prabir Pradhan5735a322022-04-11 17:23:34 +00009700 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
9701
9702 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
9703 return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
9704 ADISPLAY_ID_DEFAULT, {100, 200},
9705 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
9706 AMOTION_EVENT_INVALID_CURSOR_POSITION},
9707 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
9708 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
9709 }
9710
9711 InputEventInjectionResult injectTargetedKey(int32_t action) const {
Harry Cutts33476232023-01-30 19:57:29 +00009712 return inputdispatcher::injectKey(mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +00009713 InputEventInjectionSync::WAIT_FOR_RESULT,
Harry Cutts33476232023-01-30 19:57:29 +00009714 INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid},
Prabir Pradhan5735a322022-04-11 17:23:34 +00009715 mPolicyFlags);
9716 }
9717
9718 sp<FakeWindowHandle> createWindow() const {
9719 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9720 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009721 sp<FakeWindowHandle> window =
9722 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window",
9723 ADISPLAY_ID_DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +00009724 window->setOwnerInfo(mPid, mUid);
9725 return window;
9726 }
9727};
9728
9729using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
9730
9731TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00009732 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009733 auto window = owner.createWindow();
9734 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9735
9736 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9737 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9738 window->consumeMotionDown();
9739
9740 setFocusedWindow(window);
9741 window->consumeFocusEvent(true);
9742
9743 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9744 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9745 window->consumeKeyDown(ADISPLAY_ID_NONE);
9746}
9747
9748TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00009749 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009750 auto window = owner.createWindow();
9751 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9752
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00009753 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009754 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9755 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9756
9757 setFocusedWindow(window);
9758 window->consumeFocusEvent(true);
9759
9760 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9761 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9762 window->assertNoEvents();
9763}
9764
9765TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00009766 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009767 auto window = owner.createWindow();
9768 auto spy = owner.createWindow();
9769 spy->setSpy(true);
9770 spy->setTrustedOverlay(true);
9771 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9772
9773 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9774 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9775 spy->consumeMotionDown();
9776 window->consumeMotionDown();
9777}
9778
9779TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00009780 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009781 auto window = owner.createWindow();
9782
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00009783 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009784 auto randosSpy = rando.createWindow();
9785 randosSpy->setSpy(true);
9786 randosSpy->setTrustedOverlay(true);
9787 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9788
9789 // The event is targeted at owner's window, so injection should succeed, but the spy should
9790 // not receive the event.
9791 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9792 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9793 randosSpy->assertNoEvents();
9794 window->consumeMotionDown();
9795}
9796
9797TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00009798 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009799 auto window = owner.createWindow();
9800
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00009801 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009802 auto randosSpy = rando.createWindow();
9803 randosSpy->setSpy(true);
9804 randosSpy->setTrustedOverlay(true);
9805 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9806
9807 // A user that has injection permission can inject into any window.
9808 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9809 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
9810 ADISPLAY_ID_DEFAULT));
9811 randosSpy->consumeMotionDown();
9812 window->consumeMotionDown();
9813
9814 setFocusedWindow(randosSpy);
9815 randosSpy->consumeFocusEvent(true);
9816
9817 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
9818 randosSpy->consumeKeyDown(ADISPLAY_ID_NONE);
9819 window->assertNoEvents();
9820}
9821
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -07009822TEST_F(InputDispatcherTargetedInjectionTest, CannotGenerateActionOutsideToOtherUids) {
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00009823 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009824 auto window = owner.createWindow();
9825
Prabir Pradhane59c6dc2023-06-13 19:53:03 +00009826 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009827 auto randosWindow = rando.createWindow();
9828 randosWindow->setFrame(Rect{-10, -10, -5, -5});
9829 randosWindow->setWatchOutsideTouch(true);
9830 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}});
9831
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -07009832 // Do not allow generation of ACTION_OUTSIDE events into windows owned by different uids.
Prabir Pradhan5735a322022-04-11 17:23:34 +00009833 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9834 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9835 window->consumeMotionDown();
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -07009836 randosWindow->assertNoEvents();
Prabir Pradhan5735a322022-04-11 17:23:34 +00009837}
9838
Garfield Tane84e6f92019-08-29 17:28:41 -07009839} // namespace android::inputdispatcher