blob: c0da721264f83f942f89ac19755f0086dd0065b3 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Garfield Tan0fc2fa72019-08-29 17:22:15 -070017#include "../dispatcher/InputDispatcher.h"
Prabir Pradhan8ede1d12023-05-08 19:37:44 +000018#include "../BlockingQueue.h"
Prabir Pradhan9205e422023-05-16 20:06:13 +000019#include "EventBuilders.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080020
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -070021#include <android-base/properties.h>
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080022#include <android-base/silent_death_test.h>
Garfield Tan1c7bc862020-01-28 13:24:04 -080023#include <android-base/stringprintf.h>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070024#include <android-base/thread_annotations.h>
Robert Carr803535b2018-08-02 16:38:15 -070025#include <binder/Binder.h>
Michael Wright8e9a8562022-02-09 13:44:29 +000026#include <fcntl.h>
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -080027#include <gmock/gmock.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080028#include <gtest/gtest.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100029#include <input/Input.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080030#include <linux/input.h>
Prabir Pradhan07e05b62021-11-19 03:57:24 -080031#include <sys/epoll.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100032
Garfield Tan1c7bc862020-01-28 13:24:04 -080033#include <cinttypes>
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080034#include <compare>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070035#include <thread>
Garfield Tan1c7bc862020-01-28 13:24:04 -080036#include <unordered_set>
chaviwd1c23182019-12-20 18:44:56 -080037#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080038
Garfield Tan1c7bc862020-01-28 13:24:04 -080039using android::base::StringPrintf;
chaviw3277faf2021-05-19 16:45:23 -050040using android::gui::FocusRequest;
41using android::gui::TouchOcclusionMode;
42using android::gui::WindowInfo;
43using android::gui::WindowInfoHandle;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080044using android::os::InputEventInjectionResult;
45using android::os::InputEventInjectionSync;
Garfield Tan1c7bc862020-01-28 13:24:04 -080046
Garfield Tane84e6f92019-08-29 17:28:41 -070047namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080048
Dominik Laskowski2f01d772022-03-23 16:01:29 -070049using namespace ftl::flag_operators;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -080050using testing::AllOf;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070051
Michael Wrightd02c5b62014-02-10 15:10:22 -080052// An arbitrary time value.
Prabir Pradhan5735a322022-04-11 17:23:34 +000053static constexpr nsecs_t ARBITRARY_TIME = 1234;
Michael Wrightd02c5b62014-02-10 15:10:22 -080054
55// An arbitrary device id.
Prabir Pradhan9205e422023-05-16 20:06:13 +000056static constexpr int32_t DEVICE_ID = DEFAULT_DEVICE_ID;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -080057static constexpr int32_t SECOND_DEVICE_ID = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -080058
Jeff Brownf086ddb2014-02-11 14:28:48 -080059// An arbitrary display id.
Arthur Hungabbb9d82021-09-01 14:52:30 +000060static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
61static constexpr int32_t SECOND_DISPLAY_ID = 1;
Jeff Brownf086ddb2014-02-11 14:28:48 -080062
Prabir Pradhan8ede1d12023-05-08 19:37:44 +000063// Ensure common actions are interchangeable between keys and motions for convenience.
64static_assert(AMOTION_EVENT_ACTION_DOWN == AKEY_EVENT_ACTION_DOWN);
65static_assert(AMOTION_EVENT_ACTION_UP == AKEY_EVENT_ACTION_UP);
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080066static constexpr int32_t ACTION_DOWN = AMOTION_EVENT_ACTION_DOWN;
67static constexpr int32_t ACTION_MOVE = AMOTION_EVENT_ACTION_MOVE;
68static constexpr int32_t ACTION_UP = AMOTION_EVENT_ACTION_UP;
69static constexpr int32_t ACTION_HOVER_ENTER = AMOTION_EVENT_ACTION_HOVER_ENTER;
Siarhei Vishniakoua235c042023-05-02 09:59:09 -070070static constexpr int32_t ACTION_HOVER_MOVE = AMOTION_EVENT_ACTION_HOVER_MOVE;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080071static constexpr int32_t ACTION_HOVER_EXIT = AMOTION_EVENT_ACTION_HOVER_EXIT;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080072static constexpr int32_t ACTION_OUTSIDE = AMOTION_EVENT_ACTION_OUTSIDE;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -080073static constexpr int32_t ACTION_CANCEL = AMOTION_EVENT_ACTION_CANCEL;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080074/**
75 * The POINTER_DOWN(0) is an unusual, but valid, action. It just means that the new pointer in the
76 * MotionEvent is at the index 0 rather than 1 (or later). That is, the pointer id=0 (which is at
77 * index 0) is the new pointer going down. The same pointer could have been placed at a different
78 * index, and the action would become POINTER_1_DOWN, 2, etc..; these would all be valid. In
79 * general, we try to place pointer id = 0 at the index 0. Of course, this is not possible if
80 * pointer id=0 leaves but the pointer id=1 remains.
81 */
82static constexpr int32_t POINTER_0_DOWN =
83 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080084static constexpr int32_t POINTER_1_DOWN =
85 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +000086static constexpr int32_t POINTER_2_DOWN =
87 AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +000088static constexpr int32_t POINTER_3_DOWN =
89 AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Arthur Hungc539dbb2022-12-08 07:45:36 +000090static constexpr int32_t POINTER_0_UP =
91 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080092static constexpr int32_t POINTER_1_UP =
93 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Harry Cuttsb166c002023-05-09 13:06:05 +000094static constexpr int32_t POINTER_2_UP =
95 AMOTION_EVENT_ACTION_POINTER_UP | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080096
Antonio Kantek15beb512022-06-13 22:35:41 +000097// The default pid and uid for windows created on the primary display by the test.
Prabir Pradhan5735a322022-04-11 17:23:34 +000098static constexpr int32_t WINDOW_PID = 999;
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.
102static constexpr int32_t 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
106static constexpr int32_t MONITOR_PID = 2001;
107
Siarhei Vishniakou289e9242022-02-15 14:50:16 -0800108static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms;
109
Arthur Hungc539dbb2022-12-08 07:45:36 +0000110static constexpr int expectedWallpaperFlags =
111 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
112
Siarhei Vishniakou56e79092023-02-21 19:13:16 -0800113using ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID;
114
chaviwd1c23182019-12-20 18:44:56 -0800115struct PointF {
116 float x;
117 float y;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800118 auto operator<=>(const PointF&) const = default;
chaviwd1c23182019-12-20 18:44:56 -0800119};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800120
Gang Wang342c9272020-01-13 13:15:04 -0500121/**
122 * Return a DOWN key event with KEYCODE_A.
123 */
124static KeyEvent getTestKeyEvent() {
125 KeyEvent event;
126
Garfield Tanfbe732e2020-01-24 11:26:14 -0800127 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
128 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
129 ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -0500130 return event;
131}
132
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000133static void assertMotionAction(int32_t expectedAction, int32_t receivedAction) {
134 ASSERT_EQ(expectedAction, receivedAction)
135 << "expected " << MotionEvent::actionToString(expectedAction) << ", got "
136 << MotionEvent::actionToString(receivedAction);
137}
138
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800139MATCHER_P(WithMotionAction, action, "MotionEvent with specified action") {
140 bool matches = action == arg.getAction();
141 if (!matches) {
142 *result_listener << "expected action " << MotionEvent::actionToString(action)
143 << ", but got " << MotionEvent::actionToString(arg.getAction());
144 }
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -0800145 if (action == AMOTION_EVENT_ACTION_DOWN) {
146 if (!matches) {
147 *result_listener << "; ";
148 }
149 *result_listener << "downTime should match eventTime for ACTION_DOWN events";
150 matches &= arg.getDownTime() == arg.getEventTime();
151 }
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800152 if (action == AMOTION_EVENT_ACTION_CANCEL) {
153 if (!matches) {
154 *result_listener << "; ";
155 }
156 *result_listener << "expected FLAG_CANCELED to be set with ACTION_CANCEL, but was not set";
157 matches &= (arg.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0;
158 }
159 return matches;
160}
161
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -0800162MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") {
163 return arg.getDownTime() == downTime;
164}
165
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800166MATCHER_P(WithDisplayId, displayId, "InputEvent with specified displayId") {
167 return arg.getDisplayId() == displayId;
168}
169
Siarhei Vishniakouf372b812023-02-14 18:06:51 -0800170MATCHER_P(WithDeviceId, deviceId, "InputEvent with specified deviceId") {
171 return arg.getDeviceId() == deviceId;
172}
173
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800174MATCHER_P(WithSource, source, "InputEvent with specified source") {
175 *result_listener << "expected source " << inputEventSourceToString(source) << ", but got "
176 << inputEventSourceToString(arg.getSource());
177 return arg.getSource() == source;
178}
179
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800180MATCHER_P(WithFlags, flags, "InputEvent with specified flags") {
181 return arg.getFlags() == flags;
182}
183
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800184MATCHER_P2(WithCoords, x, y, "MotionEvent with specified coordinates") {
185 if (arg.getPointerCount() != 1) {
186 *result_listener << "Expected 1 pointer, got " << arg.getPointerCount();
187 return false;
188 }
Harry Cutts33476232023-01-30 19:57:29 +0000189 return arg.getX(/*pointerIndex=*/0) == x && arg.getY(/*pointerIndex=*/0) == y;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800190}
191
Siarhei Vishniakouf372b812023-02-14 18:06:51 -0800192MATCHER_P(WithPointerCount, pointerCount, "MotionEvent with specified number of pointers") {
193 return arg.getPointerCount() == pointerCount;
194}
195
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800196MATCHER_P(WithPointers, pointers, "MotionEvent with specified pointers") {
197 // Build a map for the received pointers, by pointer id
198 std::map<int32_t /*pointerId*/, PointF> actualPointers;
199 for (size_t pointerIndex = 0; pointerIndex < arg.getPointerCount(); pointerIndex++) {
200 const int32_t pointerId = arg.getPointerId(pointerIndex);
201 actualPointers[pointerId] = {arg.getX(pointerIndex), arg.getY(pointerIndex)};
202 }
203 return pointers == actualPointers;
204}
205
Michael Wrightd02c5b62014-02-10 15:10:22 -0800206// --- FakeInputDispatcherPolicy ---
207
208class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800209 using AnrResult = std::pair<sp<IBinder>, int32_t /*pid*/>;
210
Michael Wrightd02c5b62014-02-10 15:10:22 -0800211public:
Prabir Pradhana41d2442023-04-20 21:30:40 +0000212 FakeInputDispatcherPolicy() = default;
213 virtual ~FakeInputDispatcherPolicy() = default;
Jackal Guof9696682018-10-05 12:23:23 +0800214
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800215 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700216 assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700217 ASSERT_EQ(event.getType(), InputEventType::KEY);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700218 EXPECT_EQ(event.getDisplayId(), args.displayId);
219
220 const auto& keyEvent = static_cast<const KeyEvent&>(event);
221 EXPECT_EQ(keyEvent.getEventTime(), args.eventTime);
222 EXPECT_EQ(keyEvent.getAction(), args.action);
223 });
Jackal Guof9696682018-10-05 12:23:23 +0800224 }
225
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700226 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) {
227 assertFilterInputEventWasCalledInternal([&](const InputEvent& event) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700228 ASSERT_EQ(event.getType(), InputEventType::MOTION);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700229 EXPECT_EQ(event.getDisplayId(), args.displayId);
230
231 const auto& motionEvent = static_cast<const MotionEvent&>(event);
232 EXPECT_EQ(motionEvent.getEventTime(), args.eventTime);
233 EXPECT_EQ(motionEvent.getAction(), args.action);
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000234 EXPECT_NEAR(motionEvent.getX(0), point.x, MotionEvent::ROUNDING_PRECISION);
235 EXPECT_NEAR(motionEvent.getY(0), point.y, MotionEvent::ROUNDING_PRECISION);
236 EXPECT_NEAR(motionEvent.getRawX(0), point.x, MotionEvent::ROUNDING_PRECISION);
237 EXPECT_NEAR(motionEvent.getRawY(0), point.y, MotionEvent::ROUNDING_PRECISION);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700238 });
Jackal Guof9696682018-10-05 12:23:23 +0800239 }
240
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700241 void assertFilterInputEventWasNotCalled() {
242 std::scoped_lock lock(mLock);
243 ASSERT_EQ(nullptr, mFilteredEvent);
244 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800245
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800246 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700247 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800248 ASSERT_TRUE(mConfigurationChangedTime)
249 << "Timed out waiting for configuration changed call";
250 ASSERT_EQ(*mConfigurationChangedTime, when);
251 mConfigurationChangedTime = std::nullopt;
252 }
253
254 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700255 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800256 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800257 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800258 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
259 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
260 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
261 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
262 mLastNotifySwitch = std::nullopt;
263 }
264
chaviwfd6d3512019-03-25 13:23:49 -0700265 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700266 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800267 ASSERT_EQ(touchedToken, mOnPointerDownToken);
268 mOnPointerDownToken.clear();
269 }
270
271 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700272 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800273 ASSERT_TRUE(mOnPointerDownToken == nullptr)
274 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700275 }
276
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700277 // This function must be called soon after the expected ANR timer starts,
278 // because we are also checking how much time has passed.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500279 void assertNotifyNoFocusedWindowAnrWasCalled(
Chris Yea209fde2020-07-22 13:54:51 -0700280 std::chrono::nanoseconds timeout,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500281 const std::shared_ptr<InputApplicationHandle>& expectedApplication) {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800282 std::unique_lock lock(mLock);
283 android::base::ScopedLockAssertion assumeLocked(mLock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500284 std::shared_ptr<InputApplicationHandle> application;
Prabir Pradhanedd96402022-02-15 01:46:16 -0800285 ASSERT_NO_FATAL_FAILURE(
286 application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock));
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500287 ASSERT_EQ(expectedApplication, application);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700288 }
289
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000290 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
Prabir Pradhanedd96402022-02-15 01:46:16 -0800291 const sp<WindowInfoHandle>& window) {
292 LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null");
293 assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(),
294 window->getInfo()->ownerPid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500295 }
296
Prabir Pradhanedd96402022-02-15 01:46:16 -0800297 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
298 const sp<IBinder>& expectedToken,
299 int32_t expectedPid) {
300 std::unique_lock lock(mLock);
301 android::base::ScopedLockAssertion assumeLocked(mLock);
302 AnrResult result;
303 ASSERT_NO_FATAL_FAILURE(result =
304 getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock));
305 const auto& [token, pid] = result;
306 ASSERT_EQ(expectedToken, token);
307 ASSERT_EQ(expectedPid, pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500308 }
309
Prabir Pradhanedd96402022-02-15 01:46:16 -0800310 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000311 sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500312 std::unique_lock lock(mLock);
313 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800314 AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock);
315 const auto& [token, _] = result;
316 return token;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000317 }
318
Prabir Pradhanedd96402022-02-15 01:46:16 -0800319 void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken,
320 int32_t expectedPid) {
321 std::unique_lock lock(mLock);
322 android::base::ScopedLockAssertion assumeLocked(mLock);
323 AnrResult result;
324 ASSERT_NO_FATAL_FAILURE(
325 result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock));
326 const auto& [token, pid] = result;
327 ASSERT_EQ(expectedToken, token);
328 ASSERT_EQ(expectedPid, pid);
329 }
330
331 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000332 sp<IBinder> getResponsiveWindowToken() {
333 std::unique_lock lock(mLock);
334 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800335 AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock);
336 const auto& [token, _] = result;
337 return token;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700338 }
339
340 void assertNotifyAnrWasNotCalled() {
341 std::scoped_lock lock(mLock);
342 ASSERT_TRUE(mAnrApplications.empty());
Prabir Pradhanedd96402022-02-15 01:46:16 -0800343 ASSERT_TRUE(mAnrWindows.empty());
344 ASSERT_TRUE(mResponsiveWindows.empty())
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500345 << "ANR was not called, but please also consume the 'connection is responsive' "
346 "signal";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700347 }
348
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000349 PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -0800350 std::unique_lock lock(mLock);
351 base::ScopedLockAssertion assumeLocked(mLock);
352
353 if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms,
354 [this, enabled]() REQUIRES(mLock) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000355 return mPointerCaptureRequest->enable ==
Prabir Pradhan99987712020-11-10 18:43:05 -0800356 enabled;
357 })) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000358 ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled
359 << ") to be called.";
360 return {};
Prabir Pradhan99987712020-11-10 18:43:05 -0800361 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000362 auto request = *mPointerCaptureRequest;
363 mPointerCaptureRequest.reset();
364 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -0800365 }
366
367 void assertSetPointerCaptureNotCalled() {
368 std::unique_lock lock(mLock);
369 base::ScopedLockAssertion assumeLocked(mLock);
370
371 if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000372 FAIL() << "Expected setPointerCapture(request) to not be called, but was called. "
Prabir Pradhan99987712020-11-10 18:43:05 -0800373 "enabled = "
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000374 << std::to_string(mPointerCaptureRequest->enable);
Prabir Pradhan99987712020-11-10 18:43:05 -0800375 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000376 mPointerCaptureRequest.reset();
Prabir Pradhan99987712020-11-10 18:43:05 -0800377 }
378
arthurhungf452d0b2021-01-06 00:19:52 +0800379 void assertDropTargetEquals(const sp<IBinder>& targetToken) {
380 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800381 ASSERT_TRUE(mNotifyDropWindowWasCalled);
arthurhungf452d0b2021-01-06 00:19:52 +0800382 ASSERT_EQ(targetToken, mDropTargetWindowToken);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800383 mNotifyDropWindowWasCalled = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800384 }
385
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800386 void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) {
387 std::unique_lock lock(mLock);
388 base::ScopedLockAssertion assumeLocked(mLock);
389 std::optional<sp<IBinder>> receivedToken =
390 getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock,
391 mNotifyInputChannelBroken);
392 ASSERT_TRUE(receivedToken.has_value());
393 ASSERT_EQ(token, *receivedToken);
394 }
395
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800396 /**
397 * Set policy timeout. A value of zero means next key will not be intercepted.
398 */
399 void setInterceptKeyTimeout(std::chrono::milliseconds timeout) {
400 mInterceptKeyTimeout = timeout;
401 }
402
Josep del Riob3981622023-04-18 15:49:45 +0000403 void assertUserActivityPoked() {
404 std::scoped_lock lock(mLock);
405 ASSERT_TRUE(mPokedUserActivity) << "Expected user activity to have been poked";
406 }
407
408 void assertUserActivityNotPoked() {
409 std::scoped_lock lock(mLock);
410 ASSERT_FALSE(mPokedUserActivity) << "Expected user activity not to have been poked";
411 }
412
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000413 void assertNotifyDeviceInteractionWasCalled(int32_t deviceId, std::set<gui::Uid> uids) {
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000414 ASSERT_EQ(std::make_pair(deviceId, uids), mNotifiedInteractions.popWithTimeout(100ms));
415 }
416
417 void assertNotifyDeviceInteractionWasNotCalled() {
418 ASSERT_FALSE(mNotifiedInteractions.popWithTimeout(10ms));
419 }
420
Michael Wrightd02c5b62014-02-10 15:10:22 -0800421private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700422 std::mutex mLock;
423 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
424 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
425 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
426 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800427
Prabir Pradhan99987712020-11-10 18:43:05 -0800428 std::condition_variable mPointerCaptureChangedCondition;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000429
430 std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock);
Prabir Pradhan99987712020-11-10 18:43:05 -0800431
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700432 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700433 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800434 std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock);
435 std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700436 std::condition_variable mNotifyAnr;
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800437 std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock);
438 std::condition_variable mNotifyInputChannelBroken;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700439
arthurhungf452d0b2021-01-06 00:19:52 +0800440 sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800441 bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false;
Josep del Riob3981622023-04-18 15:49:45 +0000442 bool mPokedUserActivity GUARDED_BY(mLock) = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800443
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800444 std::chrono::milliseconds mInterceptKeyTimeout = 0ms;
445
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000446 BlockingQueue<std::pair<int32_t /*deviceId*/, std::set<gui::Uid>>> mNotifiedInteractions;
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000447
Prabir Pradhanedd96402022-02-15 01:46:16 -0800448 // All three ANR-related callbacks behave the same way, so we use this generic function to wait
449 // for a specific container to become non-empty. When the container is non-empty, return the
450 // first entry from the container and erase it.
451 template <class T>
452 T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage,
453 std::unique_lock<std::mutex>& lock) REQUIRES(mLock) {
454 // If there is an ANR, Dispatcher won't be idle because there are still events
455 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
456 // before checking if ANR was called.
457 // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need
458 // to provide it some time to act. 100ms seems reasonable.
459 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
460 const std::chrono::time_point start = std::chrono::steady_clock::now();
461 std::optional<T> token =
462 getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr);
463 if (!token.has_value()) {
464 ADD_FAILURE() << "Did not receive the ANR callback";
465 return {};
466 }
467
468 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
469 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
470 // the dispatcher started counting before this function was called
471 if (std::chrono::abs(timeout - waited) > 100ms) {
472 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
473 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
474 << "ms, but waited "
475 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
476 << "ms instead";
477 }
478 return *token;
479 }
480
481 template <class T>
482 std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout,
483 std::queue<T>& storage,
484 std::unique_lock<std::mutex>& lock,
485 std::condition_variable& condition)
486 REQUIRES(mLock) {
487 condition.wait_for(lock, timeout,
488 [&storage]() REQUIRES(mLock) { return !storage.empty(); });
489 if (storage.empty()) {
490 ADD_FAILURE() << "Did not receive the expected callback";
491 return std::nullopt;
492 }
493 T item = storage.front();
494 storage.pop();
495 return std::make_optional(item);
496 }
497
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600498 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700499 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800500 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800501 }
502
Prabir Pradhanedd96402022-02-15 01:46:16 -0800503 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<int32_t> pid,
504 const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700505 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800506 ASSERT_TRUE(pid.has_value());
507 mAnrWindows.push({connectionToken, *pid});
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700508 mNotifyAnr.notify_all();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500509 }
510
Prabir Pradhanedd96402022-02-15 01:46:16 -0800511 void notifyWindowResponsive(const sp<IBinder>& connectionToken,
512 std::optional<int32_t> pid) override {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500513 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800514 ASSERT_TRUE(pid.has_value());
515 mResponsiveWindows.push({connectionToken, *pid});
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500516 mNotifyAnr.notify_all();
517 }
518
519 void notifyNoFocusedWindowAnr(
520 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
521 std::scoped_lock lock(mLock);
522 mAnrApplications.push(applicationHandle);
523 mNotifyAnr.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800524 }
525
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800526 void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override {
527 std::scoped_lock lock(mLock);
528 mBrokenInputChannels.push(connectionToken);
529 mNotifyInputChannelBroken.notify_all();
530 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800531
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600532 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700533
Chris Yef59a2f42020-10-16 12:55:26 -0700534 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
535 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
536 const std::vector<float>& values) override {}
537
538 void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType,
539 InputDeviceSensorAccuracy accuracy) override {}
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000540
Chris Yefb552902021-02-03 17:18:37 -0800541 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
542
Prabir Pradhana41d2442023-04-20 21:30:40 +0000543 bool filterInputEvent(const InputEvent& inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700544 std::scoped_lock lock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +0000545 switch (inputEvent.getType()) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700546 case InputEventType::KEY: {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000547 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(inputEvent);
548 mFilteredEvent = std::make_unique<KeyEvent>(keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800549 break;
550 }
551
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700552 case InputEventType::MOTION: {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000553 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(inputEvent);
554 mFilteredEvent = std::make_unique<MotionEvent>(motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800555 break;
556 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700557 default: {
558 ADD_FAILURE() << "Should only filter keys or motions";
559 break;
560 }
Jackal Guof9696682018-10-05 12:23:23 +0800561 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800562 return true;
563 }
564
Prabir Pradhana41d2442023-04-20 21:30:40 +0000565 void interceptKeyBeforeQueueing(const KeyEvent& inputEvent, uint32_t&) override {
566 if (inputEvent.getAction() == AKEY_EVENT_ACTION_UP) {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800567 // Clear intercept state when we handled the event.
568 mInterceptKeyTimeout = 0ms;
569 }
570 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800571
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600572 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800573
Prabir Pradhana41d2442023-04-20 21:30:40 +0000574 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&, uint32_t) override {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800575 nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count();
576 // Clear intercept state so we could dispatch the event in next wake.
577 mInterceptKeyTimeout = 0ms;
578 return delay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800579 }
580
Prabir Pradhana41d2442023-04-20 21:30:40 +0000581 std::optional<KeyEvent> dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent&,
582 uint32_t) override {
583 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800584 }
585
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600586 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
587 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700588 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800589 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
590 * essentially a passthrough for notifySwitch.
591 */
Harry Cutts33476232023-01-30 19:57:29 +0000592 mLastNotifySwitch = NotifySwitchArgs(/*id=*/1, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800593 }
594
Josep del Riob3981622023-04-18 15:49:45 +0000595 void pokeUserActivity(nsecs_t, int32_t, int32_t) override {
596 std::scoped_lock lock(mLock);
597 mPokedUserActivity = true;
598 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800599
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600600 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700601 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700602 mOnPointerDownToken = newToken;
603 }
604
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000605 void setPointerCapture(const PointerCaptureRequest& request) override {
Prabir Pradhan99987712020-11-10 18:43:05 -0800606 std::scoped_lock lock(mLock);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000607 mPointerCaptureRequest = {request};
Prabir Pradhan99987712020-11-10 18:43:05 -0800608 mPointerCaptureChangedCondition.notify_all();
609 }
610
arthurhungf452d0b2021-01-06 00:19:52 +0800611 void notifyDropWindow(const sp<IBinder>& token, float x, float y) override {
612 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800613 mNotifyDropWindowWasCalled = true;
arthurhungf452d0b2021-01-06 00:19:52 +0800614 mDropTargetWindowToken = token;
615 }
616
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000617 void notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000618 const std::set<gui::Uid>& uids) override {
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000619 ASSERT_TRUE(mNotifiedInteractions.emplace(deviceId, uids));
620 }
621
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700622 void assertFilterInputEventWasCalledInternal(
623 const std::function<void(const InputEvent&)>& verify) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700624 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800625 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700626 verify(*mFilteredEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800627 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800628 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800629};
630
Michael Wrightd02c5b62014-02-10 15:10:22 -0800631// --- InputDispatcherTest ---
632
633class InputDispatcherTest : public testing::Test {
634protected:
Prabir Pradhana41d2442023-04-20 21:30:40 +0000635 std::unique_ptr<FakeInputDispatcherPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700636 std::unique_ptr<InputDispatcher> mDispatcher;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800637
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000638 void SetUp() override {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000639 mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>();
640 mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy, STALE_EVENT_TIMEOUT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800641 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000642 // Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700643 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800644 }
645
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000646 void TearDown() override {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700647 ASSERT_EQ(OK, mDispatcher->stop());
Prabir Pradhana41d2442023-04-20 21:30:40 +0000648 mFakePolicy.reset();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700649 mDispatcher.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800650 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700651
652 /**
653 * Used for debugging when writing the test
654 */
655 void dumpDispatcherState() {
656 std::string dump;
657 mDispatcher->dump(dump);
658 std::stringstream ss(dump);
659 std::string to;
660
661 while (std::getline(ss, to, '\n')) {
662 ALOGE("%s", to.c_str());
663 }
664 }
Vishnu Nair958da932020-08-21 17:12:37 -0700665
Chavi Weingarten847e8512023-03-29 00:26:09 +0000666 void setFocusedWindow(const sp<WindowInfoHandle>& window) {
Vishnu Nair958da932020-08-21 17:12:37 -0700667 FocusRequest request;
668 request.token = window->getToken();
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +0000669 request.windowName = window->getName();
Vishnu Nair958da932020-08-21 17:12:37 -0700670 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
671 request.displayId = window->getInfo()->displayId;
672 mDispatcher->setFocusedWindow(request);
673 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800674};
675
Michael Wrightd02c5b62014-02-10 15:10:22 -0800676TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
677 KeyEvent event;
678
679 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800680 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
681 INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600682 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
683 ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800684 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000685 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000686 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800687 << "Should reject key events with undefined action.";
688
689 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800690 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
691 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600692 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800693 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000694 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000695 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800696 << "Should reject key events with ACTION_MULTIPLE.";
697}
698
699TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
700 MotionEvent event;
701 PointerProperties pointerProperties[MAX_POINTERS + 1];
702 PointerCoords pointerCoords[MAX_POINTERS + 1];
Siarhei Vishniakou01747382022-01-20 13:23:27 -0800703 for (size_t i = 0; i <= MAX_POINTERS; i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800704 pointerProperties[i].clear();
705 pointerProperties[i].id = i;
706 pointerCoords[i].clear();
707 }
708
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800709 // Some constants commonly used below
710 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
711 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
712 constexpr int32_t metaState = AMETA_NONE;
713 constexpr MotionClassification classification = MotionClassification::NONE;
714
chaviw9eaa22c2020-07-01 16:21:27 -0700715 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800716 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800717 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
chaviw9eaa22c2020-07-01 16:21:27 -0700718 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification,
719 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700720 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
721 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700722 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800723 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000724 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000725 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800726 << "Should reject motion events with undefined action.";
727
728 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800729 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800730 POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
731 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
732 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
733 ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500734 /*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 pointer down index too large.";
739
Garfield Tanfbe732e2020-01-24 11:26:14 -0800740 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700741 AMOTION_EVENT_ACTION_POINTER_DOWN |
742 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700743 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
744 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700745 identityTransform, ARBITRARY_TIME, 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 small.";
751
752 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800753 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800754 POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform,
755 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
756 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
757 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 up index too large.";
763
Garfield Tanfbe732e2020-01-24 11:26:14 -0800764 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700765 AMOTION_EVENT_ACTION_POINTER_UP |
766 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700767 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
768 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700769 identityTransform, ARBITRARY_TIME, 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 small.";
775
776 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800777 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
778 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700779 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700780 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
781 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700782 /*pointerCount*/ 0, 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 0 pointers.";
787
Garfield Tanfbe732e2020-01-24 11:26:14 -0800788 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
789 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700790 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700791 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
792 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700793 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800794 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000795 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000796 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800797 << "Should reject motion events with more than MAX_POINTERS pointers.";
798
799 // Rejects motion events with invalid pointer ids.
800 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800801 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
802 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700803 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700804 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
805 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700806 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800807 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000808 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000809 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800810 << "Should reject motion events with pointer ids less than 0.";
811
812 pointerProperties[0].id = MAX_POINTER_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 greater than MAX_POINTER_ID.";
823
824 // Rejects motion events with duplicate pointer ids.
825 pointerProperties[0].id = 1;
826 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800827 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
828 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700829 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700830 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
831 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700832 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800833 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000834 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000835 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800836 << "Should reject motion events with duplicate pointer ids.";
837}
838
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800839/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
840
841TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
842 constexpr nsecs_t eventTime = 20;
Prabir Pradhan678438e2023-04-13 19:32:51 +0000843 mDispatcher->notifyConfigurationChanged({/*id=*/10, eventTime});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800844 ASSERT_TRUE(mDispatcher->waitForIdle());
845
846 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
847}
848
849TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Harry Cutts33476232023-01-30 19:57:29 +0000850 NotifySwitchArgs args(/*id=*/10, /*eventTime=*/20, /*policyFlags=*/0, /*switchValues=*/1,
851 /*switchMask=*/2);
Prabir Pradhan678438e2023-04-13 19:32:51 +0000852 mDispatcher->notifySwitch(args);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800853
854 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
855 args.policyFlags |= POLICY_FLAG_TRUSTED;
856 mFakePolicy->assertNotifySwitchWasCalled(args);
857}
858
Siarhei Vishniakouadeb6fa2023-05-26 09:11:06 -0700859namespace {
860
Arthur Hungb92218b2018-08-14 12:00:21 +0800861// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700862static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -0700863// Default input dispatching timeout if there is no focused application or paused window
864// from which to determine an appropriate dispatching timeout.
865static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds(
866 android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS *
867 android::base::HwTimeoutMultiplier());
Arthur Hungb92218b2018-08-14 12:00:21 +0800868
869class FakeApplicationHandle : public InputApplicationHandle {
870public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700871 FakeApplicationHandle() {
872 mInfo.name = "Fake Application";
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700873 mInfo.token = sp<BBinder>::make();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500874 mInfo.dispatchingTimeoutMillis =
875 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700876 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800877 virtual ~FakeApplicationHandle() {}
878
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000879 virtual bool updateInfo() override { return true; }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700880
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500881 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
882 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700883 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800884};
885
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800886class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800887public:
Garfield Tan15601662020-09-22 15:32:38 -0700888 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800889 : mName(name) {
Garfield Tan15601662020-09-22 15:32:38 -0700890 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800891 }
892
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800893 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700894 InputEvent* event;
895 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
896 if (!consumeSeq) {
897 return nullptr;
898 }
899 finishEvent(*consumeSeq);
900 return event;
901 }
902
903 /**
904 * Receive an event without acknowledging it.
905 * Return the sequence number that could later be used to send finished signal.
906 */
907 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800908 uint32_t consumeSeq;
909 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800910
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800911 std::chrono::time_point start = std::chrono::steady_clock::now();
912 status_t status = WOULD_BLOCK;
913 while (status == WOULD_BLOCK) {
Harry Cutts33476232023-01-30 19:57:29 +0000914 status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800915 &event);
916 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
917 if (elapsed > 100ms) {
918 break;
919 }
920 }
921
922 if (status == WOULD_BLOCK) {
923 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700924 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800925 }
926
927 if (status != OK) {
928 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700929 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800930 }
931 if (event == nullptr) {
932 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700933 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800934 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700935 if (outEvent != nullptr) {
936 *outEvent = event;
937 }
938 return consumeSeq;
939 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800940
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700941 /**
942 * To be used together with "receiveEvent" to complete the consumption of an event.
943 */
944 void finishEvent(uint32_t consumeSeq) {
945 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
946 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800947 }
948
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000949 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
950 const status_t status = mConsumer->sendTimeline(inputEventId, timeline);
951 ASSERT_EQ(OK, status);
952 }
953
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700954 void consumeEvent(InputEventType expectedEventType, int32_t expectedAction,
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000955 std::optional<int32_t> expectedDisplayId,
956 std::optional<int32_t> expectedFlags) {
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800957 InputEvent* event = consume();
958
959 ASSERT_NE(nullptr, event) << mName.c_str()
960 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800961 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700962 << mName.c_str() << " expected " << ftl::enum_string(expectedEventType)
963 << " event, got " << *event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800964
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000965 if (expectedDisplayId.has_value()) {
966 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
967 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800968
Tiger Huang8664f8c2018-10-11 19:14:35 +0800969 switch (expectedEventType) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700970 case InputEventType::KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800971 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
972 EXPECT_EQ(expectedAction, keyEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000973 if (expectedFlags.has_value()) {
974 EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags());
975 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800976 break;
977 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700978 case InputEventType::MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800979 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000980 assertMotionAction(expectedAction, motionEvent.getAction());
981
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000982 if (expectedFlags.has_value()) {
983 EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags());
984 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800985 break;
986 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700987 case InputEventType::FOCUS: {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100988 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
989 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700990 case InputEventType::CAPTURE: {
Prabir Pradhan99987712020-11-10 18:43:05 -0800991 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
992 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700993 case InputEventType::TOUCH_MODE: {
Antonio Kantekf16f2832021-09-28 04:39:20 +0000994 FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events";
995 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700996 case InputEventType::DRAG: {
arthurhungb89ccb02020-12-30 16:19:01 +0800997 FAIL() << "Use 'consumeDragEvent' for DRAG events";
998 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800999 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001000 }
1001
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001002 MotionEvent* consumeMotion() {
1003 InputEvent* event = consume();
1004
1005 if (event == nullptr) {
1006 ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one.";
1007 return nullptr;
1008 }
1009
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001010 if (event->getType() != InputEventType::MOTION) {
1011 ADD_FAILURE() << mName << " expected a MotionEvent, got " << *event;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001012 return nullptr;
1013 }
1014 return static_cast<MotionEvent*>(event);
1015 }
1016
1017 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
1018 MotionEvent* motionEvent = consumeMotion();
1019 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
1020 ASSERT_THAT(*motionEvent, matcher);
1021 }
1022
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001023 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
1024 InputEvent* event = consume();
1025 ASSERT_NE(nullptr, event) << mName.c_str()
1026 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001027 ASSERT_EQ(InputEventType::FOCUS, event->getType())
1028 << "Instead of FocusEvent, got " << *event;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001029
1030 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1031 << mName.c_str() << ": event displayId should always be NONE.";
1032
1033 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
1034 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001035 }
1036
Prabir Pradhan99987712020-11-10 18:43:05 -08001037 void consumeCaptureEvent(bool hasCapture) {
1038 const InputEvent* event = consume();
1039 ASSERT_NE(nullptr, event) << mName.c_str()
1040 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001041 ASSERT_EQ(InputEventType::CAPTURE, event->getType())
1042 << "Instead of CaptureEvent, got " << *event;
Prabir Pradhan99987712020-11-10 18:43:05 -08001043
1044 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1045 << mName.c_str() << ": event displayId should always be NONE.";
1046
1047 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
1048 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
1049 }
1050
arthurhungb89ccb02020-12-30 16:19:01 +08001051 void consumeDragEvent(bool isExiting, float x, float y) {
1052 const InputEvent* event = consume();
1053 ASSERT_NE(nullptr, event) << mName.c_str()
1054 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001055 ASSERT_EQ(InputEventType::DRAG, event->getType()) << "Instead of DragEvent, got " << *event;
arthurhungb89ccb02020-12-30 16:19:01 +08001056
1057 EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1058 << mName.c_str() << ": event displayId should always be NONE.";
1059
1060 const auto& dragEvent = static_cast<const DragEvent&>(*event);
1061 EXPECT_EQ(isExiting, dragEvent.isExiting());
1062 EXPECT_EQ(x, dragEvent.getX());
1063 EXPECT_EQ(y, dragEvent.getY());
1064 }
1065
Antonio Kantekf16f2832021-09-28 04:39:20 +00001066 void consumeTouchModeEvent(bool inTouchMode) {
1067 const InputEvent* event = consume();
1068 ASSERT_NE(nullptr, event) << mName.c_str()
1069 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001070 ASSERT_EQ(InputEventType::TOUCH_MODE, event->getType())
1071 << "Instead of TouchModeEvent, got " << *event;
Antonio Kantekf16f2832021-09-28 04:39:20 +00001072
1073 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1074 << mName.c_str() << ": event displayId should always be NONE.";
1075 const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event);
1076 EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode());
1077 }
1078
chaviwd1c23182019-12-20 18:44:56 -08001079 void assertNoEvents() {
1080 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001081 if (event == nullptr) {
1082 return;
1083 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001084 if (event->getType() == InputEventType::KEY) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001085 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
1086 ADD_FAILURE() << "Received key event "
1087 << KeyEvent::actionToString(keyEvent.getAction());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001088 } else if (event->getType() == InputEventType::MOTION) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001089 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1090 ADD_FAILURE() << "Received motion event "
1091 << MotionEvent::actionToString(motionEvent.getAction());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001092 } else if (event->getType() == InputEventType::FOCUS) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001093 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
1094 ADD_FAILURE() << "Received focus event, hasFocus = "
1095 << (focusEvent.getHasFocus() ? "true" : "false");
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001096 } else if (event->getType() == InputEventType::CAPTURE) {
Prabir Pradhan99987712020-11-10 18:43:05 -08001097 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
1098 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
1099 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001100 } else if (event->getType() == InputEventType::TOUCH_MODE) {
Antonio Kantekf16f2832021-09-28 04:39:20 +00001101 const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event);
1102 ADD_FAILURE() << "Received touch mode event, inTouchMode = "
1103 << (touchModeEvent.isInTouchMode() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001104 }
1105 FAIL() << mName.c_str()
1106 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -08001107 }
1108
1109 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
1110
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001111 int getChannelFd() { return mConsumer->getChannel()->getFd().get(); }
1112
chaviwd1c23182019-12-20 18:44:56 -08001113protected:
1114 std::unique_ptr<InputConsumer> mConsumer;
1115 PreallocatedInputEventFactory mEventFactory;
1116
1117 std::string mName;
1118};
1119
chaviw3277faf2021-05-19 16:45:23 -05001120class FakeWindowHandle : public WindowInfoHandle {
chaviwd1c23182019-12-20 18:44:56 -08001121public:
1122 static const int32_t WIDTH = 600;
1123 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -08001124
Chris Yea209fde2020-07-22 13:54:51 -07001125 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001126 const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001127 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -08001128 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001129 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -07001130 base::Result<std::unique_ptr<InputChannel>> channel =
1131 dispatcher->createInputChannel(name);
1132 token = (*channel)->getConnectionToken();
1133 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -08001134 }
1135
1136 inputApplicationHandle->updateInfo();
1137 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
1138
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001139 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001140 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -08001141 mInfo.name = name;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001142 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001143 mInfo.alpha = 1.0;
chaviwd1c23182019-12-20 18:44:56 -08001144 mInfo.frameLeft = 0;
1145 mInfo.frameTop = 0;
1146 mInfo.frameRight = WIDTH;
1147 mInfo.frameBottom = HEIGHT;
chaviw1ff3d1e2020-07-01 15:53:47 -07001148 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -08001149 mInfo.globalScaleFactor = 1.0;
1150 mInfo.touchableRegion.clear();
1151 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
Prabir Pradhan5735a322022-04-11 17:23:34 +00001152 mInfo.ownerPid = WINDOW_PID;
1153 mInfo.ownerUid = WINDOW_UID;
chaviwd1c23182019-12-20 18:44:56 -08001154 mInfo.displayId = displayId;
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001155 mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT;
chaviwd1c23182019-12-20 18:44:56 -08001156 }
1157
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07001158 sp<FakeWindowHandle> clone(int32_t displayId) {
1159 sp<FakeWindowHandle> handle = sp<FakeWindowHandle>::make(mInfo.name + "(Mirror)");
1160 handle->mInfo = mInfo;
1161 handle->mInfo.displayId = displayId;
1162 handle->mInfo.id = sId++;
1163 handle->mInputReceiver = mInputReceiver;
1164 return handle;
1165 }
1166
1167 /**
1168 * This is different from clone, because clone will make a "mirror" window - a window with the
1169 * same token, but a different ID. The original window and the clone window are allowed to be
1170 * sent to the dispatcher at the same time - they can coexist inside the dispatcher.
1171 * This function will create a different object of WindowInfoHandle, but with the same
1172 * properties as the original object - including the ID.
1173 * You can use either the old or the new object to consume the events.
1174 * IMPORTANT: The duplicated object is supposed to replace the original object, and not appear
1175 * at the same time inside dispatcher.
1176 */
1177 sp<FakeWindowHandle> duplicate() {
1178 sp<FakeWindowHandle> handle = sp<FakeWindowHandle>::make(mName);
1179 handle->mInfo = mInfo;
1180 handle->mInputReceiver = mInputReceiver;
Arthur Hungabbb9d82021-09-01 14:52:30 +00001181 return handle;
1182 }
1183
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001184 void setTouchable(bool touchable) {
1185 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable);
1186 }
chaviwd1c23182019-12-20 18:44:56 -08001187
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001188 void setFocusable(bool focusable) {
1189 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable);
1190 }
1191
1192 void setVisible(bool visible) {
1193 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible);
1194 }
Vishnu Nair958da932020-08-21 17:12:37 -07001195
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001196 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001197 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001198 }
1199
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001200 void setPaused(bool paused) {
1201 mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused);
1202 }
1203
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001204 void setPreventSplitting(bool preventSplitting) {
1205 mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001206 }
1207
1208 void setSlippery(bool slippery) {
1209 mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery);
1210 }
1211
1212 void setWatchOutsideTouch(bool watchOutside) {
1213 mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside);
1214 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001215
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001216 void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); }
1217
1218 void setInterceptsStylus(bool interceptsStylus) {
1219 mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus);
1220 }
1221
1222 void setDropInput(bool dropInput) {
1223 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput);
1224 }
1225
1226 void setDropInputIfObscured(bool dropInputIfObscured) {
1227 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured);
1228 }
1229
1230 void setNoInputChannel(bool noInputChannel) {
1231 mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel);
1232 }
1233
Josep del Riob3981622023-04-18 15:49:45 +00001234 void setDisableUserActivity(bool disableUserActivity) {
1235 mInfo.setInputConfig(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY, disableUserActivity);
1236 }
1237
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001238 void setAlpha(float alpha) { mInfo.alpha = alpha; }
1239
chaviw3277faf2021-05-19 16:45:23 -05001240 void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; }
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001241
Bernardo Rufino7393d172021-02-26 13:56:11 +00001242 void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
1243
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001244 void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) {
chaviwd1c23182019-12-20 18:44:56 -08001245 mInfo.frameLeft = frame.left;
1246 mInfo.frameTop = frame.top;
1247 mInfo.frameRight = frame.right;
1248 mInfo.frameBottom = frame.bottom;
1249 mInfo.touchableRegion.clear();
1250 mInfo.addTouchableRegion(frame);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001251
1252 const Rect logicalDisplayFrame = displayTransform.transform(frame);
1253 ui::Transform translate;
1254 translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top);
1255 mInfo.transform = translate * displayTransform;
chaviwd1c23182019-12-20 18:44:56 -08001256 }
1257
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001258 void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; }
1259
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001260 void setIsWallpaper(bool isWallpaper) {
1261 mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper);
1262 }
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001263
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001264 void setDupTouchToWallpaper(bool hasWallpaper) {
1265 mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper);
1266 }
chaviwd1c23182019-12-20 18:44:56 -08001267
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001268 void setTrustedOverlay(bool trustedOverlay) {
1269 mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay);
1270 }
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001271
chaviw9eaa22c2020-07-01 16:21:27 -07001272 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
1273 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
1274 }
1275
1276 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -07001277
yunho.shinf4a80b82020-11-16 21:13:57 +09001278 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
1279
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001280 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001281 consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, expectedFlags);
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001282 }
1283
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001284 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001285 consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001286 }
1287
Svet Ganov5d3bc372020-01-26 23:11:07 -08001288 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001289 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001290 consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId),
1291 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001292 }
1293
1294 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001295 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001296 consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
1297 WithDisplayId(expectedDisplayId), WithFlags(expectedFlags)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001298 }
1299
1300 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001301 int32_t expectedFlags = 0) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001302 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
1303 }
1304
1305 void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
1306 std::optional<int32_t> expectedFlags = std::nullopt) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001307 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001308 expectedFlags);
1309 }
1310
Svet Ganov5d3bc372020-01-26 23:11:07 -08001311 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001312 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1313 int32_t expectedFlags = 0) {
1314 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
1315 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001316 consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001317 }
1318
1319 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001320 int32_t expectedFlags = 0) {
1321 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
1322 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001323 consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001324 }
1325
1326 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001327 int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001328 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
Michael Wright3a240c42019-12-10 20:53:41 +00001329 expectedFlags);
1330 }
1331
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001332 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1333 int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001334 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001335 expectedFlags);
1336 }
1337
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001338 void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1339 int32_t expectedFlags = 0) {
1340 InputEvent* event = consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08001341 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001342 ASSERT_EQ(InputEventType::MOTION, event->getType());
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001343 const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1344 EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked());
1345 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getX());
1346 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getY());
1347 }
1348
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001349 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
1350 ASSERT_NE(mInputReceiver, nullptr)
1351 << "Cannot consume events from a window with no receiver";
1352 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
1353 }
1354
Prabir Pradhan99987712020-11-10 18:43:05 -08001355 void consumeCaptureEvent(bool hasCapture) {
1356 ASSERT_NE(mInputReceiver, nullptr)
1357 << "Cannot consume events from a window with no receiver";
1358 mInputReceiver->consumeCaptureEvent(hasCapture);
1359 }
1360
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001361 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
1362 MotionEvent* motionEvent = consumeMotion();
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08001363 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001364 ASSERT_THAT(*motionEvent, matcher);
1365 }
1366
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001367 void consumeEvent(InputEventType expectedEventType, int32_t expectedAction,
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001368 std::optional<int32_t> expectedDisplayId,
1369 std::optional<int32_t> expectedFlags) {
chaviwd1c23182019-12-20 18:44:56 -08001370 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
1371 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
1372 expectedFlags);
1373 }
1374
arthurhungb89ccb02020-12-30 16:19:01 +08001375 void consumeDragEvent(bool isExiting, float x, float y) {
1376 mInputReceiver->consumeDragEvent(isExiting, x, y);
1377 }
1378
Antonio Kantekf16f2832021-09-28 04:39:20 +00001379 void consumeTouchModeEvent(bool inTouchMode) {
1380 ASSERT_NE(mInputReceiver, nullptr)
1381 << "Cannot consume events from a window with no receiver";
1382 mInputReceiver->consumeTouchModeEvent(inTouchMode);
1383 }
1384
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001385 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001386 if (mInputReceiver == nullptr) {
1387 ADD_FAILURE() << "Invalid receive event on window with no receiver";
1388 return std::nullopt;
1389 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001390 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001391 }
1392
1393 void finishEvent(uint32_t sequenceNum) {
1394 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1395 mInputReceiver->finishEvent(sequenceNum);
1396 }
1397
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001398 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
1399 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1400 mInputReceiver->sendTimeline(inputEventId, timeline);
1401 }
1402
chaviwaf87b3e2019-10-01 16:59:28 -07001403 InputEvent* consume() {
1404 if (mInputReceiver == nullptr) {
1405 return nullptr;
1406 }
1407 return mInputReceiver->consume();
1408 }
1409
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001410 MotionEvent* consumeMotion() {
1411 InputEvent* event = consume();
1412 if (event == nullptr) {
1413 ADD_FAILURE() << "Consume failed : no event";
1414 return nullptr;
1415 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001416 if (event->getType() != InputEventType::MOTION) {
1417 ADD_FAILURE() << "Instead of motion event, got " << *event;
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001418 return nullptr;
1419 }
1420 return static_cast<MotionEvent*>(event);
1421 }
1422
Arthur Hungb92218b2018-08-14 12:00:21 +08001423 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001424 if (mInputReceiver == nullptr &&
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001425 mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001426 return; // Can't receive events if the window does not have input channel
1427 }
1428 ASSERT_NE(nullptr, mInputReceiver)
1429 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -08001430 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +08001431 }
1432
chaviwaf87b3e2019-10-01 16:59:28 -07001433 sp<IBinder> getToken() { return mInfo.token; }
1434
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001435 const std::string& getName() { return mName; }
1436
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00001437 void setOwnerInfo(int32_t ownerPid, gui::Uid ownerUid) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001438 mInfo.ownerPid = ownerPid;
1439 mInfo.ownerUid = ownerUid;
1440 }
1441
Prabir Pradhanedd96402022-02-15 01:46:16 -08001442 int32_t getPid() const { return mInfo.ownerPid; }
1443
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001444 void destroyReceiver() { mInputReceiver = nullptr; }
1445
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001446 int getChannelFd() { return mInputReceiver->getChannelFd(); }
1447
chaviwd1c23182019-12-20 18:44:56 -08001448private:
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07001449 FakeWindowHandle(std::string name) : mName(name){};
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001450 const std::string mName;
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07001451 std::shared_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001452 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07001453 friend class sp<FakeWindowHandle>;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001454};
1455
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001456std::atomic<int32_t> FakeWindowHandle::sId{1};
1457
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001458static InputEventInjectionResult injectKey(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001459 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001460 int32_t displayId = ADISPLAY_ID_NONE,
1461 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001462 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00001463 bool allowKeyRepeat = true, std::optional<gui::Uid> targetUid = {},
Prabir Pradhan5735a322022-04-11 17:23:34 +00001464 uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001465 KeyEvent event;
1466 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1467
1468 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001469 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Harry Cutts33476232023-01-30 19:57:29 +00001470 INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount,
1471 currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001472
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001473 if (!allowKeyRepeat) {
1474 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
1475 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001476 // Inject event until dispatch out.
Prabir Pradhan5735a322022-04-11 17:23:34 +00001477 return dispatcher->injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001478}
1479
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001480static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001481 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001482 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001483}
1484
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001485// Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without
1486// sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it
1487// has to be woken up to process the repeating key.
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001488static InputEventInjectionResult injectKeyDownNoRepeat(
1489 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001490 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001491 InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT,
Harry Cutts33476232023-01-30 19:57:29 +00001492 /*allowKeyRepeat=*/false);
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001493}
1494
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001495static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001496 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001497 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001498}
1499
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001500static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001501 const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event,
Garfield Tandf26e862020-07-01 20:18:19 -07001502 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001503 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00001504 std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Prabir Pradhan5735a322022-04-11 17:23:34 +00001505 return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout,
1506 policyFlags);
Garfield Tandf26e862020-07-01 20:18:19 -07001507}
1508
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001509static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001510 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source,
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001511 int32_t displayId, const PointF& position = {100, 200},
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001512 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001513 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1514 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001515 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001516 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC),
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00001517 std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07001518 MotionEventBuilder motionBuilder =
1519 MotionEventBuilder(action, source)
1520 .displayId(displayId)
1521 .eventTime(eventTime)
1522 .rawXCursorPosition(cursorPosition.x)
1523 .rawYCursorPosition(cursorPosition.y)
1524 .pointer(
1525 PointerBuilder(/*id=*/0, ToolType::FINGER).x(position.x).y(position.y));
1526 if (MotionEvent::getActionMasked(action) == ACTION_DOWN) {
1527 motionBuilder.downTime(eventTime);
1528 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001529
1530 // Inject event until dispatch out.
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07001531 return injectMotionEvent(dispatcher, motionBuilder.build(), injectionTimeout, injectionMode,
1532 targetUid, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001533}
1534
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001535static InputEventInjectionResult injectMotionDown(
1536 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId,
1537 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001538 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001539}
1540
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001541static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001542 int32_t source, int32_t displayId,
1543 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001544 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001545}
1546
Jackal Guof9696682018-10-05 12:23:23 +08001547static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1548 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1549 // Define a valid key event.
Harry Cutts33476232023-01-30 19:57:29 +00001550 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001551 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A,
1552 KEY_A, AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001553
1554 return args;
1555}
1556
Josep del Riob3981622023-04-18 15:49:45 +00001557static NotifyKeyArgs generateSystemShortcutArgs(int32_t action,
1558 int32_t displayId = ADISPLAY_ID_NONE) {
1559 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1560 // Define a valid key event.
1561 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1562 displayId, 0, action, /* flags */ 0, AKEYCODE_C, KEY_C, AMETA_META_ON,
1563 currentTime);
1564
1565 return args;
1566}
1567
1568static NotifyKeyArgs generateAssistantKeyArgs(int32_t action,
1569 int32_t displayId = ADISPLAY_ID_NONE) {
1570 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1571 // Define a valid key event.
1572 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1573 displayId, 0, action, /* flags */ 0, AKEYCODE_ASSIST, KEY_ASSISTANT,
1574 AMETA_NONE, currentTime);
1575
1576 return args;
1577}
1578
Prabir Pradhan678438e2023-04-13 19:32:51 +00001579[[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source,
1580 int32_t displayId,
1581 const std::vector<PointF>& points) {
chaviwd1c23182019-12-20 18:44:56 -08001582 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001583 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1584 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1585 }
1586
chaviwd1c23182019-12-20 18:44:56 -08001587 PointerProperties pointerProperties[pointerCount];
1588 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001589
chaviwd1c23182019-12-20 18:44:56 -08001590 for (size_t i = 0; i < pointerCount; i++) {
1591 pointerProperties[i].clear();
1592 pointerProperties[i].id = i;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001593 pointerProperties[i].toolType = ToolType::FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001594
chaviwd1c23182019-12-20 18:44:56 -08001595 pointerCoords[i].clear();
1596 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1597 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1598 }
Jackal Guof9696682018-10-05 12:23:23 +08001599
1600 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1601 // Define a valid motion event.
Harry Cutts33476232023-01-30 19:57:29 +00001602 NotifyMotionArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001603 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1604 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001605 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1606 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001607 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1608 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001609
1610 return args;
1611}
1612
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001613static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) {
1614 return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points);
1615}
1616
chaviwd1c23182019-12-20 18:44:56 -08001617static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1618 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1619}
1620
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001621static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(
1622 const PointerCaptureRequest& request) {
Harry Cutts33476232023-01-30 19:57:29 +00001623 return NotifyPointerCaptureChangedArgs(/*id=*/0, systemTime(SYSTEM_TIME_MONOTONIC), request);
Prabir Pradhan99987712020-11-10 18:43:05 -08001624}
1625
Siarhei Vishniakouadeb6fa2023-05-26 09:11:06 -07001626} // namespace
1627
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001628/**
1629 * When a window unexpectedly disposes of its input channel, policy should be notified about the
1630 * broken channel.
1631 */
1632TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) {
1633 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1634 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001635 sp<FakeWindowHandle>::make(application, mDispatcher,
1636 "Window that breaks its input channel", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001637
1638 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1639
1640 // Window closes its channel, but the window remains.
1641 window->destroyReceiver();
1642 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token);
1643}
1644
Arthur Hungb92218b2018-08-14 12:00:21 +08001645TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001646 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001647 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1648 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001649
Arthur Hung72d8dc32020-03-28 00:48:39 +00001650 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001651 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1652 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1653 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001654
1655 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001656 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001657}
1658
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001659TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) {
1660 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001661 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1662 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001663
1664 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1665 // Inject a MotionEvent to an unknown display.
1666 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1667 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE))
1668 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1669
1670 // Window should receive motion event.
1671 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1672}
1673
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001674/**
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001675 * Calling setInputWindows once should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001676 * This test serves as a sanity check for the next test, where setInputWindows is
1677 * called twice.
1678 */
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001679TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) {
Chris Yea209fde2020-07-22 13:54:51 -07001680 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001681 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1682 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001683 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001684
1685 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001686 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001687 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1688 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001689 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001690
1691 // Window should receive motion event.
1692 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1693}
1694
1695/**
1696 * Calling setInputWindows twice, with the same info, should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001697 */
1698TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001699 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001700 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1701 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001702 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001703
1704 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1705 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001706 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001707 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1708 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001709 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001710
1711 // Window should receive motion event.
1712 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1713}
1714
Arthur Hungb92218b2018-08-14 12:00:21 +08001715// The foreground window should receive the first touch down event.
1716TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001717 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001718 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001719 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001720 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001721 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001722
Arthur Hung72d8dc32020-03-28 00:48:39 +00001723 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001724 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1725 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1726 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001727
1728 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001729 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001730 windowSecond->assertNoEvents();
1731}
1732
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001733/**
1734 * Two windows: A top window, and a wallpaper behind the window.
1735 * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window
1736 * gets ACTION_CANCEL.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001737 * 1. foregroundWindow <-- dup touch to wallpaper
1738 * 2. wallpaperWindow <-- is wallpaper
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001739 */
1740TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) {
1741 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1742 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001743 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001744 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001745 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001746 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001747 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001748
1749 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1750 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1751 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1752 {100, 200}))
1753 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1754
1755 // Both foreground window and its wallpaper should receive the touch down
1756 foregroundWindow->consumeMotionDown();
1757 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1758
1759 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1760 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1761 ADISPLAY_ID_DEFAULT, {110, 200}))
1762 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1763
1764 foregroundWindow->consumeMotionMove();
1765 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1766
1767 // Now the foreground window goes away, but the wallpaper stays
1768 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
1769 foregroundWindow->consumeMotionCancel();
1770 // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1771 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1772}
1773
1774/**
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001775 * Two fingers down on the window, and lift off the first finger.
1776 * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event
1777 * contains a single pointer.
1778 */
1779TEST_F(InputDispatcherTest, CancelAfterPointer0Up) {
1780 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1781 sp<FakeWindowHandle> window =
1782 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1783
1784 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001785 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00001786 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1787 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1788 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001789 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00001790 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1791 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1792 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
1793 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001794 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00001795 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
1796 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1797 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
1798 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001799 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
1800 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
1801 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
1802
1803 // Remove the window. The gesture should be canceled
1804 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
1805 const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}};
1806 window->consumeMotionEvent(
1807 AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers)));
1808}
1809
1810/**
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001811 * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above,
1812 * with the following differences:
1813 * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to
1814 * clean up the connection.
1815 * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful.
1816 * Ensure that there's no crash in the dispatcher.
1817 */
1818TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) {
1819 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1820 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001821 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001822 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001823 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001824 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001825 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001826
1827 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1828 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1829 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1830 {100, 200}))
1831 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1832
1833 // Both foreground window and its wallpaper should receive the touch down
1834 foregroundWindow->consumeMotionDown();
1835 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1836
1837 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1838 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1839 ADISPLAY_ID_DEFAULT, {110, 200}))
1840 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1841
1842 foregroundWindow->consumeMotionMove();
1843 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1844
1845 // Wallpaper closes its channel, but the window remains.
1846 wallpaperWindow->destroyReceiver();
1847 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token);
1848
1849 // Now the foreground window goes away, but the wallpaper stays, even though its channel
1850 // is no longer valid.
1851 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
1852 foregroundWindow->consumeMotionCancel();
1853}
1854
Arthur Hungc539dbb2022-12-08 07:45:36 +00001855class ShouldSplitTouchFixture : public InputDispatcherTest,
1856 public ::testing::WithParamInterface<bool> {};
1857INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture,
1858 ::testing::Values(true, false));
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001859/**
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001860 * A single window that receives touch (on top), and a wallpaper window underneath it.
1861 * The top window gets a multitouch gesture.
1862 * Ensure that wallpaper gets the same gesture.
1863 */
Arthur Hungc539dbb2022-12-08 07:45:36 +00001864TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001865 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00001866 sp<FakeWindowHandle> foregroundWindow =
1867 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
1868 foregroundWindow->setDupTouchToWallpaper(true);
1869 foregroundWindow->setPreventSplitting(GetParam());
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001870
1871 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001872 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001873 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001874
Arthur Hungc539dbb2022-12-08 07:45:36 +00001875 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001876
1877 // Touch down on top window
1878 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1879 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1880 {100, 100}))
1881 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1882
1883 // Both top window and its wallpaper should receive the touch down
Arthur Hungc539dbb2022-12-08 07:45:36 +00001884 foregroundWindow->consumeMotionDown();
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001885 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1886
1887 // Second finger down on the top window
1888 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001889 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001890 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001891 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1892 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001893 .build();
1894 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1895 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
1896 InputEventInjectionSync::WAIT_FOR_RESULT))
1897 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1898
Harry Cutts33476232023-01-30 19:57:29 +00001899 foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
1900 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001901 expectedWallpaperFlags);
Arthur Hungc539dbb2022-12-08 07:45:36 +00001902
1903 const MotionEvent secondFingerUpEvent =
1904 MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
1905 .displayId(ADISPLAY_ID_DEFAULT)
1906 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001907 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1908 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Arthur Hungc539dbb2022-12-08 07:45:36 +00001909 .build();
1910 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1911 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
1912 InputEventInjectionSync::WAIT_FOR_RESULT))
1913 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1914 foregroundWindow->consumeMotionPointerUp(0);
1915 wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1916
1917 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08001918 injectMotionEvent(mDispatcher,
1919 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
1920 AINPUT_SOURCE_TOUCHSCREEN)
1921 .displayId(ADISPLAY_ID_DEFAULT)
1922 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
1923 .pointer(PointerBuilder(/* id */ 1,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001924 ToolType::FINGER)
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08001925 .x(100)
1926 .y(100))
1927 .build(),
1928 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT))
Arthur Hungc539dbb2022-12-08 07:45:36 +00001929 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1930 foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1931 wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001932}
1933
1934/**
1935 * Two windows: a window on the left and window on the right.
1936 * A third window, wallpaper, is behind both windows, and spans both top windows.
1937 * The first touch down goes to the left window. A second pointer touches down on the right window.
1938 * The touch is split, so both left and right windows should receive ACTION_DOWN.
1939 * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by
1940 * ACTION_POINTER_DOWN(1).
1941 */
1942TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) {
1943 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1944 sp<FakeWindowHandle> leftWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001945 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001946 leftWindow->setFrame(Rect(0, 0, 200, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001947 leftWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001948
1949 sp<FakeWindowHandle> rightWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001950 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001951 rightWindow->setFrame(Rect(200, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001952 rightWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001953
1954 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001955 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001956 wallpaperWindow->setFrame(Rect(0, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001957 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001958
1959 mDispatcher->setInputWindows(
1960 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
1961
1962 // Touch down on left window
1963 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1964 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1965 {100, 100}))
1966 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1967
1968 // Both foreground window and its wallpaper should receive the touch down
1969 leftWindow->consumeMotionDown();
1970 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1971
1972 // Second finger down on the right window
1973 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001974 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001975 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001976 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1977 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001978 .build();
1979 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1980 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
1981 InputEventInjectionSync::WAIT_FOR_RESULT))
1982 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1983
1984 leftWindow->consumeMotionMove();
1985 // Since the touch is split, right window gets ACTION_DOWN
1986 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00001987 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001988 expectedWallpaperFlags);
1989
1990 // Now, leftWindow, which received the first finger, disappears.
1991 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}});
1992 leftWindow->consumeMotionCancel();
1993 // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1994 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1995
1996 // The pointer that's still down on the right window moves, and goes to the right window only.
1997 // As far as the dispatcher's concerned though, both pointers are still present.
1998 const MotionEvent secondFingerMoveEvent =
1999 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2000 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002001 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2002 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002003 .build();
2004 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2005 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
2006 InputEventInjectionSync::WAIT_FOR_RESULT));
2007 rightWindow->consumeMotionMove();
2008
2009 leftWindow->assertNoEvents();
2010 rightWindow->assertNoEvents();
2011 wallpaperWindow->assertNoEvents();
2012}
2013
Arthur Hungc539dbb2022-12-08 07:45:36 +00002014/**
2015 * Two windows: a window on the left with dup touch to wallpaper and window on the right without it.
2016 * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL
2017 * The right window should receive ACTION_DOWN.
2018 */
2019TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) {
Arthur Hung74c248d2022-11-23 07:09:59 +00002020 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00002021 sp<FakeWindowHandle> leftWindow =
2022 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2023 leftWindow->setFrame(Rect(0, 0, 200, 200));
2024 leftWindow->setDupTouchToWallpaper(true);
2025 leftWindow->setSlippery(true);
2026
2027 sp<FakeWindowHandle> rightWindow =
2028 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2029 rightWindow->setFrame(Rect(200, 0, 400, 200));
Arthur Hung74c248d2022-11-23 07:09:59 +00002030
2031 sp<FakeWindowHandle> wallpaperWindow =
2032 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
2033 wallpaperWindow->setIsWallpaper(true);
Arthur Hung74c248d2022-11-23 07:09:59 +00002034
Arthur Hungc539dbb2022-12-08 07:45:36 +00002035 mDispatcher->setInputWindows(
2036 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
Arthur Hung74c248d2022-11-23 07:09:59 +00002037
Arthur Hungc539dbb2022-12-08 07:45:36 +00002038 // Touch down on left window
Arthur Hung74c248d2022-11-23 07:09:59 +00002039 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2040 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002041 {100, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002042 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungc539dbb2022-12-08 07:45:36 +00002043
2044 // Both foreground window and its wallpaper should receive the touch down
2045 leftWindow->consumeMotionDown();
Arthur Hung74c248d2022-11-23 07:09:59 +00002046 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2047
Arthur Hungc539dbb2022-12-08 07:45:36 +00002048 // Move to right window, the left window should receive cancel.
Arthur Hung74c248d2022-11-23 07:09:59 +00002049 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002050 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2051 ADISPLAY_ID_DEFAULT, {201, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002052 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2053
Arthur Hungc539dbb2022-12-08 07:45:36 +00002054 leftWindow->consumeMotionCancel();
2055 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2056 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Arthur Hung74c248d2022-11-23 07:09:59 +00002057}
2058
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002059/**
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002060 * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not
2061 * interactive, it might stop sending this flag.
2062 * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure
2063 * to have a consistent input stream.
2064 *
2065 * Test procedure:
2066 * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL.
2067 * DOWN (new gesture).
2068 *
2069 * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent
2070 * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app.
2071 *
2072 * We technically just need a single window here, but we are using two windows (spy on top and a
2073 * regular window below) to emulate the actual situation where it happens on the device.
2074 */
2075TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) {
2076 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2077 sp<FakeWindowHandle> spyWindow =
2078 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2079 spyWindow->setFrame(Rect(0, 0, 200, 200));
2080 spyWindow->setTrustedOverlay(true);
2081 spyWindow->setSpy(true);
2082
2083 sp<FakeWindowHandle> window =
2084 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2085 window->setFrame(Rect(0, 0, 200, 200));
2086
2087 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2088 const int32_t touchDeviceId = 4;
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002089
2090 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002091 mDispatcher->notifyMotion(
2092 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2093 .deviceId(touchDeviceId)
2094 .policyFlags(DEFAULT_POLICY_FLAGS)
2095 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2096 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002097
Prabir Pradhan678438e2023-04-13 19:32:51 +00002098 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2099 .deviceId(touchDeviceId)
2100 .policyFlags(DEFAULT_POLICY_FLAGS)
2101 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2102 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2103 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002104 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2105 spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2106 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2107 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2108
2109 // Cancel the current gesture. Send the cancel without the default policy flags.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002110 mDispatcher->notifyMotion(
2111 MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN)
2112 .deviceId(touchDeviceId)
2113 .policyFlags(0)
2114 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2115 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2116 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002117 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2118 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2119
2120 // We don't need to reset the device to reproduce the issue, but the reset event typically
2121 // follows, so we keep it here to model the actual listener behaviour more closely.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002122 mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId});
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002123
2124 // Start new gesture
Prabir Pradhan678438e2023-04-13 19:32:51 +00002125 mDispatcher->notifyMotion(
2126 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2127 .deviceId(touchDeviceId)
2128 .policyFlags(DEFAULT_POLICY_FLAGS)
2129 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2130 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002131 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2132 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2133
2134 // No more events
2135 spyWindow->assertNoEvents();
2136 window->assertNoEvents();
2137}
2138
2139/**
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002140 * Two windows: a window on the left and a window on the right.
2141 * Mouse is hovered from the right window into the left window.
2142 * Next, we tap on the left window, where the cursor was last seen.
2143 * The second tap is done onto the right window.
2144 * The mouse and tap are from two different devices.
2145 * We technically don't need to set the downtime / eventtime for these events, but setting these
2146 * explicitly helps during debugging.
2147 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2148 * In the buggy implementation, a tap on the right window would cause a crash.
2149 */
2150TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) {
2151 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2152 sp<FakeWindowHandle> leftWindow =
2153 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2154 leftWindow->setFrame(Rect(0, 0, 200, 200));
2155
2156 sp<FakeWindowHandle> rightWindow =
2157 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2158 rightWindow->setFrame(Rect(200, 0, 400, 200));
2159
2160 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2161 // All times need to start at the current time, otherwise the dispatcher will drop the events as
2162 // stale.
2163 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
2164 const int32_t mouseDeviceId = 6;
2165 const int32_t touchDeviceId = 4;
2166 // Move the cursor from right
2167 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2168 injectMotionEvent(mDispatcher,
2169 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2170 AINPUT_SOURCE_MOUSE)
2171 .deviceId(mouseDeviceId)
2172 .downTime(baseTime + 10)
2173 .eventTime(baseTime + 20)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002174 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002175 .x(300)
2176 .y(100))
2177 .build()));
2178 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2179
2180 // .. to the left window
2181 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2182 injectMotionEvent(mDispatcher,
2183 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2184 AINPUT_SOURCE_MOUSE)
2185 .deviceId(mouseDeviceId)
2186 .downTime(baseTime + 10)
2187 .eventTime(baseTime + 30)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002188 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002189 .x(110)
2190 .y(100))
2191 .build()));
2192 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2193 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2194 // Now tap the left window
2195 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2196 injectMotionEvent(mDispatcher,
2197 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2198 AINPUT_SOURCE_TOUCHSCREEN)
2199 .deviceId(touchDeviceId)
2200 .downTime(baseTime + 40)
2201 .eventTime(baseTime + 40)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002202 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002203 .x(100)
2204 .y(100))
2205 .build()));
2206 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2207 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2208
2209 // release tap
2210 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2211 injectMotionEvent(mDispatcher,
2212 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2213 AINPUT_SOURCE_TOUCHSCREEN)
2214 .deviceId(touchDeviceId)
2215 .downTime(baseTime + 40)
2216 .eventTime(baseTime + 50)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002217 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002218 .x(100)
2219 .y(100))
2220 .build()));
2221 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2222
2223 // Tap the window on the right
2224 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2225 injectMotionEvent(mDispatcher,
2226 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2227 AINPUT_SOURCE_TOUCHSCREEN)
2228 .deviceId(touchDeviceId)
2229 .downTime(baseTime + 60)
2230 .eventTime(baseTime + 60)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002231 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002232 .x(300)
2233 .y(100))
2234 .build()));
2235 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2236
2237 // release tap
2238 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2239 injectMotionEvent(mDispatcher,
2240 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2241 AINPUT_SOURCE_TOUCHSCREEN)
2242 .deviceId(touchDeviceId)
2243 .downTime(baseTime + 60)
2244 .eventTime(baseTime + 70)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002245 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002246 .x(300)
2247 .y(100))
2248 .build()));
2249 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2250
2251 // No more events
2252 leftWindow->assertNoEvents();
2253 rightWindow->assertNoEvents();
2254}
2255
2256/**
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002257 * Start hovering in a window. While this hover is still active, make another window appear on top.
2258 * The top, obstructing window has no input channel, so it's not supposed to receive input.
2259 * While the top window is present, the hovering is stopped.
2260 * Later, hovering gets resumed again.
2261 * Ensure that new hover gesture is handled correctly.
2262 * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly
2263 * to the window that's currently being hovered over.
2264 */
2265TEST_F(InputDispatcherTest, HoverWhileWindowAppears) {
2266 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2267 sp<FakeWindowHandle> window =
2268 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2269 window->setFrame(Rect(0, 0, 200, 200));
2270
2271 // Only a single window is present at first
2272 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2273
2274 // Start hovering in the window
2275 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2276 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2277 .build());
2278 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2279
2280 // Now, an obscuring window appears!
2281 sp<FakeWindowHandle> obscuringWindow =
2282 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
2283 ADISPLAY_ID_DEFAULT,
2284 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
2285 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
2286 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
2287 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
2288 obscuringWindow->setNoInputChannel(true);
2289 obscuringWindow->setFocusable(false);
2290 obscuringWindow->setAlpha(1.0);
2291 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
2292
2293 // While this new obscuring window is present, the hovering is stopped
2294 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2295 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2296 .build());
2297 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2298
2299 // Now the obscuring window goes away.
2300 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2301
2302 // And a new hover gesture starts.
2303 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2304 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2305 .build());
2306 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2307}
2308
2309/**
2310 * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to
2311 * the obscuring window.
2312 */
2313TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) {
2314 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2315 sp<FakeWindowHandle> window =
2316 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2317 window->setFrame(Rect(0, 0, 200, 200));
2318
2319 // Only a single window is present at first
2320 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2321
2322 // Start hovering in the window
2323 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2324 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2325 .build());
2326 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2327
2328 // Now, an obscuring window appears!
2329 sp<FakeWindowHandle> obscuringWindow =
2330 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
2331 ADISPLAY_ID_DEFAULT,
2332 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
2333 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
2334 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
2335 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
2336 obscuringWindow->setNoInputChannel(true);
2337 obscuringWindow->setFocusable(false);
2338 obscuringWindow->setAlpha(1.0);
2339 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
2340
2341 // While this new obscuring window is present, the hovering continues. The event can't go to the
2342 // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window.
2343 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2344 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2345 .build());
2346 obscuringWindow->assertNoEvents();
2347 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2348
2349 // Now the obscuring window goes away.
2350 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2351
2352 // Hovering continues in the same position. The hovering pointer re-enters the bottom window,
2353 // so it should generate a HOVER_ENTER
2354 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2355 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2356 .build());
2357 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2358
2359 // Now the MOVE should be getting dispatched normally
2360 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2361 .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110))
2362 .build());
2363 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
2364}
2365
2366/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002367 * Two windows: a window on the left and a window on the right.
2368 * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains
2369 * down. Then, on the left window, also place second touch pointer down.
2370 * This test tries to reproduce a crash.
2371 * In the buggy implementation, second pointer down on the left window would cause a crash.
2372 */
2373TEST_F(InputDispatcherTest, MultiDeviceSplitTouch) {
2374 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2375 sp<FakeWindowHandle> leftWindow =
2376 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2377 leftWindow->setFrame(Rect(0, 0, 200, 200));
2378
2379 sp<FakeWindowHandle> rightWindow =
2380 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2381 rightWindow->setFrame(Rect(200, 0, 400, 200));
2382
2383 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2384
2385 const int32_t touchDeviceId = 4;
2386 const int32_t mouseDeviceId = 6;
2387 NotifyMotionArgs args;
2388
2389 // Start hovering over the left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002390 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2391 .deviceId(mouseDeviceId)
2392 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2393 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002394 leftWindow->consumeMotionEvent(
2395 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2396
2397 // Mouse down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002398 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2399 .deviceId(mouseDeviceId)
2400 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2401 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2402 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002403
2404 leftWindow->consumeMotionEvent(
2405 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
2406 leftWindow->consumeMotionEvent(
2407 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2408
Prabir Pradhan678438e2023-04-13 19:32:51 +00002409 mDispatcher->notifyMotion(
2410 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2411 .deviceId(mouseDeviceId)
2412 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2413 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2414 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2415 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002416 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2417
2418 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002419 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2420 .deviceId(touchDeviceId)
2421 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2422 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002423 leftWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
2424
2425 rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2426
2427 // Second touch pointer down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002428 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2429 .deviceId(touchDeviceId)
2430 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2431 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
2432 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002433 leftWindow->consumeMotionEvent(
2434 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2435 // This MOVE event is not necessary (doesn't carry any new information), but it's there in the
2436 // current implementation.
2437 const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}};
2438 rightWindow->consumeMotionEvent(
2439 AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers)));
2440
2441 leftWindow->assertNoEvents();
2442 rightWindow->assertNoEvents();
2443}
2444
2445/**
2446 * On a single window, use two different devices: mouse and touch.
2447 * Touch happens first, with two pointers going down, and then the first pointer leaving.
2448 * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL.
2449 * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored,
2450 * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not
2451 * represent a new gesture.
2452 */
2453TEST_F(InputDispatcherTest, MixedTouchAndMouseWithPointerDown) {
2454 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2455 sp<FakeWindowHandle> window =
2456 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2457 window->setFrame(Rect(0, 0, 400, 400));
2458
2459 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2460
2461 const int32_t touchDeviceId = 4;
2462 const int32_t mouseDeviceId = 6;
2463 NotifyMotionArgs args;
2464
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08002465 // First touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002466 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2467 .deviceId(touchDeviceId)
2468 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2469 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002470 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002471 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2472 .deviceId(touchDeviceId)
2473 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2474 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2475 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002476 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002477 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
2478 .deviceId(touchDeviceId)
2479 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2480 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2481 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002482 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2483 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2484 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
2485
2486 // Mouse down. The touch should be canceled
Prabir Pradhan678438e2023-04-13 19:32:51 +00002487 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2488 .deviceId(mouseDeviceId)
2489 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2490 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
2491 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002492
2493 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08002494 WithPointerCount(1u)));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002495 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2496
Prabir Pradhan678438e2023-04-13 19:32:51 +00002497 mDispatcher->notifyMotion(
2498 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2499 .deviceId(mouseDeviceId)
2500 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2501 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2502 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
2503 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002504 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2505
2506 // Second touch pointer down.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002507 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2508 .deviceId(touchDeviceId)
2509 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2510 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2511 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002512 // The pointer_down event should be ignored
2513 window->assertNoEvents();
2514}
2515
2516/**
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002517 * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels
2518 * the injected event.
2519 */
2520TEST_F(InputDispatcherTest, UnfinishedInjectedEvent) {
2521 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2522 sp<FakeWindowHandle> window =
2523 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2524 window->setFrame(Rect(0, 0, 400, 400));
2525
2526 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2527
2528 const int32_t touchDeviceId = 4;
2529 NotifyMotionArgs args;
2530 // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after
2531 // completion.
2532 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2533 injectMotionEvent(mDispatcher,
2534 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2535 .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002536 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002537 .x(50)
2538 .y(50))
2539 .build()));
2540 window->consumeMotionEvent(
2541 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
2542
2543 // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer
2544 // should be canceled and the new gesture should take over.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002545 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2546 .deviceId(touchDeviceId)
2547 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2548 .build());
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002549
2550 window->consumeMotionEvent(
2551 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
2552 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2553}
2554
2555/**
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002556 * This test is similar to the test above, but the sequence of injected events is different.
2557 *
2558 * Two windows: a window on the left and a window on the right.
2559 * Mouse is hovered over the left window.
2560 * Next, we tap on the left window, where the cursor was last seen.
2561 *
2562 * After that, we inject one finger down onto the right window, and then a second finger down onto
2563 * the left window.
2564 * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right
2565 * window (first), and then another on the left window (second).
2566 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2567 * In the buggy implementation, second finger down on the left window would cause a crash.
2568 */
2569TEST_F(InputDispatcherTest, HoverTapAndSplitTouch) {
2570 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2571 sp<FakeWindowHandle> leftWindow =
2572 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2573 leftWindow->setFrame(Rect(0, 0, 200, 200));
2574
2575 sp<FakeWindowHandle> rightWindow =
2576 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2577 rightWindow->setFrame(Rect(200, 0, 400, 200));
2578
2579 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2580
2581 const int32_t mouseDeviceId = 6;
2582 const int32_t touchDeviceId = 4;
2583 // Hover over the left window. Keep the cursor there.
2584 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2585 injectMotionEvent(mDispatcher,
2586 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2587 AINPUT_SOURCE_MOUSE)
2588 .deviceId(mouseDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002589 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002590 .x(50)
2591 .y(50))
2592 .build()));
2593 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2594
2595 // Tap on left window
2596 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2597 injectMotionEvent(mDispatcher,
2598 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2599 AINPUT_SOURCE_TOUCHSCREEN)
2600 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002601 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002602 .x(100)
2603 .y(100))
2604 .build()));
2605
2606 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2607 injectMotionEvent(mDispatcher,
2608 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2609 AINPUT_SOURCE_TOUCHSCREEN)
2610 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002611 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002612 .x(100)
2613 .y(100))
2614 .build()));
2615 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2616 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2617 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2618
2619 // First finger down on right window
2620 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2621 injectMotionEvent(mDispatcher,
2622 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2623 AINPUT_SOURCE_TOUCHSCREEN)
2624 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002625 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002626 .x(300)
2627 .y(100))
2628 .build()));
2629 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2630
2631 // Second finger down on the left window
2632 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2633 injectMotionEvent(mDispatcher,
2634 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2635 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002636 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002637 .x(300)
2638 .y(100))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002639 .pointer(PointerBuilder(1, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002640 .x(100)
2641 .y(100))
2642 .build()));
2643 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2644 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE));
2645
2646 // No more events
2647 leftWindow->assertNoEvents();
2648 rightWindow->assertNoEvents();
2649}
2650
2651/**
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002652 * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs.
2653 * While the touch is down, new hover events from the stylus device should be ignored. After the
2654 * touch is gone, stylus hovering should start working again.
2655 */
2656TEST_F(InputDispatcherTest, StylusHoverAndTouchTap) {
2657 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2658 sp<FakeWindowHandle> window =
2659 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2660 window->setFrame(Rect(0, 0, 200, 200));
2661
2662 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2663
2664 const int32_t stylusDeviceId = 5;
2665 const int32_t touchDeviceId = 4;
2666 // Start hovering with stylus
2667 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2668 injectMotionEvent(mDispatcher,
2669 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2670 AINPUT_SOURCE_STYLUS)
2671 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002672 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002673 .x(50)
2674 .y(50))
2675 .build()));
2676 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2677
2678 // Finger down on the window
2679 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2680 injectMotionEvent(mDispatcher,
2681 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2682 AINPUT_SOURCE_TOUCHSCREEN)
2683 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002684 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002685 .x(100)
2686 .y(100))
2687 .build()));
2688 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2689 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2690
2691 // Try to continue hovering with stylus. Since we are already down, injection should fail
2692 ASSERT_EQ(InputEventInjectionResult::FAILED,
2693 injectMotionEvent(mDispatcher,
2694 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2695 AINPUT_SOURCE_STYLUS)
2696 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002697 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002698 .x(50)
2699 .y(50))
2700 .build()));
2701 // No event should be sent. This event should be ignored because a pointer from another device
2702 // is already down.
2703
2704 // Lift up the finger
2705 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2706 injectMotionEvent(mDispatcher,
2707 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2708 AINPUT_SOURCE_TOUCHSCREEN)
2709 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002710 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002711 .x(100)
2712 .y(100))
2713 .build()));
2714 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2715
2716 // Now that the touch is gone, stylus hovering should start working again
2717 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2718 injectMotionEvent(mDispatcher,
2719 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2720 AINPUT_SOURCE_STYLUS)
2721 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002722 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002723 .x(50)
2724 .y(50))
2725 .build()));
2726 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2727 // No more events
2728 window->assertNoEvents();
2729}
2730
2731/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002732 * A spy window above a window with no input channel.
2733 * Start hovering with a stylus device, and then tap with it.
2734 * Ensure spy window receives the entire sequence.
2735 */
2736TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) {
2737 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2738 sp<FakeWindowHandle> spyWindow =
2739 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2740 spyWindow->setFrame(Rect(0, 0, 200, 200));
2741 spyWindow->setTrustedOverlay(true);
2742 spyWindow->setSpy(true);
2743 sp<FakeWindowHandle> window =
2744 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2745 window->setNoInputChannel(true);
2746 window->setFrame(Rect(0, 0, 200, 200));
2747
2748 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2749
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002750 // Start hovering with stylus
Prabir Pradhan678438e2023-04-13 19:32:51 +00002751 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2752 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2753 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002754 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2755 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00002756 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2757 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2758 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002759 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2760
2761 // Stylus touches down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002762 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2763 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2764 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002765 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2766
2767 // Stylus goes up
Prabir Pradhan678438e2023-04-13 19:32:51 +00002768 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, 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_UP));
2772
2773 // Again hover
Prabir Pradhan678438e2023-04-13 19:32:51 +00002774 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, 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_HOVER_ENTER));
2778 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00002779 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2780 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2781 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002782 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2783
2784 // No more events
2785 spyWindow->assertNoEvents();
2786 window->assertNoEvents();
2787}
2788
2789/**
2790 * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream.
2791 * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse
2792 * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active.
2793 * While the mouse is down, new move events from the touch device should be ignored.
2794 */
2795TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) {
2796 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2797 sp<FakeWindowHandle> spyWindow =
2798 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2799 spyWindow->setFrame(Rect(0, 0, 200, 200));
2800 spyWindow->setTrustedOverlay(true);
2801 spyWindow->setSpy(true);
2802 sp<FakeWindowHandle> window =
2803 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2804 window->setFrame(Rect(0, 0, 200, 200));
2805
2806 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2807
2808 const int32_t mouseDeviceId = 7;
2809 const int32_t touchDeviceId = 4;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002810
2811 // Hover a bit with mouse first
Prabir Pradhan678438e2023-04-13 19:32:51 +00002812 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2813 .deviceId(mouseDeviceId)
2814 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2815 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002816 spyWindow->consumeMotionEvent(
2817 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2818 window->consumeMotionEvent(
2819 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2820
2821 // Start touching
Prabir Pradhan678438e2023-04-13 19:32:51 +00002822 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2823 .deviceId(touchDeviceId)
2824 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
2825 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002826 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2827 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2828 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2829 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2830
Prabir Pradhan678438e2023-04-13 19:32:51 +00002831 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2832 .deviceId(touchDeviceId)
2833 .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55))
2834 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002835 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2836 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2837
2838 // Pilfer the stream
2839 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
2840 window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
2841
Prabir Pradhan678438e2023-04-13 19:32:51 +00002842 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2843 .deviceId(touchDeviceId)
2844 .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60))
2845 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002846 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2847
2848 // Mouse down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002849 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2850 .deviceId(mouseDeviceId)
2851 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2852 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2853 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002854
2855 spyWindow->consumeMotionEvent(
2856 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
2857 spyWindow->consumeMotionEvent(
2858 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2859 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2860
Prabir Pradhan678438e2023-04-13 19:32:51 +00002861 mDispatcher->notifyMotion(
2862 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2863 .deviceId(mouseDeviceId)
2864 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2865 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2866 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2867 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002868 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2869 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2870
2871 // Mouse move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00002872 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
2873 .deviceId(mouseDeviceId)
2874 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2875 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110))
2876 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002877 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2878 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2879
2880 // Touch move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00002881 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2882 .deviceId(touchDeviceId)
2883 .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65))
2884 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002885
2886 // No more events
2887 spyWindow->assertNoEvents();
2888 window->assertNoEvents();
2889}
2890
2891/**
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002892 * On the display, have a single window, and also an area where there's no window.
2893 * First pointer touches the "no window" area of the screen. Second pointer touches the window.
2894 * Make sure that the window receives the second pointer, and first pointer is simply ignored.
2895 */
2896TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) {
2897 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2898 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002899 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002900
2901 mDispatcher->setInputWindows({{DISPLAY_ID, {window}}});
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002902
2903 // Touch down on the empty space
Prabir Pradhan678438e2023-04-13 19:32:51 +00002904 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002905
2906 mDispatcher->waitForIdle();
2907 window->assertNoEvents();
2908
2909 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00002910 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002911 mDispatcher->waitForIdle();
2912 window->consumeMotionDown();
2913}
2914
2915/**
2916 * Same test as above, but instead of touching the empty space, the first touch goes to
2917 * non-touchable window.
2918 */
2919TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) {
2920 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2921 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002922 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002923 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
2924 window1->setTouchable(false);
2925 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002926 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002927 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
2928
2929 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
2930
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002931 // Touch down on the non-touchable window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002932 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002933
2934 mDispatcher->waitForIdle();
2935 window1->assertNoEvents();
2936 window2->assertNoEvents();
2937
2938 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00002939 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002940 mDispatcher->waitForIdle();
2941 window2->consumeMotionDown();
2942}
2943
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002944/**
2945 * When splitting touch events the downTime should be adjusted such that the downTime corresponds
2946 * to the event time of the first ACTION_DOWN sent to the particular window.
2947 */
2948TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) {
2949 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2950 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002951 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002952 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
2953 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002954 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002955 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
2956
2957 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
2958
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002959 // Touch down on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002960 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002961
2962 mDispatcher->waitForIdle();
2963 InputEvent* inputEvent1 = window1->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002964 ASSERT_NE(inputEvent1, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002965 window2->assertNoEvents();
2966 MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1);
2967 nsecs_t downTimeForWindow1 = motionEvent1.getDownTime();
2968 ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime());
2969
2970 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00002971 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002972 mDispatcher->waitForIdle();
2973 InputEvent* inputEvent2 = window2->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002974 ASSERT_NE(inputEvent2, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002975 MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2);
2976 nsecs_t downTimeForWindow2 = motionEvent2.getDownTime();
2977 ASSERT_NE(downTimeForWindow1, downTimeForWindow2);
2978 ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime());
2979
2980 // Now move the pointer on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002981 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002982 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002983 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002984
2985 // Now add new touch down on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002986 mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002987 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002988 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002989
2990 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
2991 window1->consumeMotionMove();
2992 window1->assertNoEvents();
2993
2994 // Now move the pointer on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002995 mDispatcher->notifyMotion(
2996 generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002997 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002998 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002999
Prabir Pradhan678438e2023-04-13 19:32:51 +00003000 mDispatcher->notifyMotion(
3001 generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003002 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003003 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003004}
3005
Garfield Tandf26e862020-07-01 20:18:19 -07003006TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07003007 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07003008 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003009 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003010 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003011 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003012 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003013 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003014
3015 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3016
3017 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
3018
3019 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003020 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003021 injectMotionEvent(mDispatcher,
3022 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3023 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003024 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003025 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003026 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003027
3028 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003029 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003030 injectMotionEvent(mDispatcher,
3031 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3032 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003033 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003034 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003035 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3036 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003037
3038 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003039 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003040 injectMotionEvent(mDispatcher,
3041 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3042 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003043 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003044 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003045 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3046 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003047
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003048 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003049 injectMotionEvent(mDispatcher,
3050 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3051 AINPUT_SOURCE_MOUSE)
3052 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3053 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003054 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003055 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003056 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003057
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003058 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003059 injectMotionEvent(mDispatcher,
3060 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3061 AINPUT_SOURCE_MOUSE)
3062 .buttonState(0)
3063 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003064 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003065 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003066 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003067
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003068 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003069 injectMotionEvent(mDispatcher,
3070 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3071 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003072 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003073 .build()));
3074 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3075
3076 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003077 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003078 injectMotionEvent(mDispatcher,
3079 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3080 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003081 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003082 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003083 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003084
3085 // No more events
3086 windowLeft->assertNoEvents();
3087 windowRight->assertNoEvents();
3088}
3089
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003090/**
3091 * Put two fingers down (and don't release them) and click the mouse button.
3092 * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the
3093 * currently active gesture should be canceled, and the new one should proceed.
3094 */
3095TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) {
3096 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3097 sp<FakeWindowHandle> window =
3098 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3099 window->setFrame(Rect(0, 0, 600, 800));
3100
3101 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3102
3103 const int32_t touchDeviceId = 4;
3104 const int32_t mouseDeviceId = 6;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003105
3106 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003107 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3108 .deviceId(touchDeviceId)
3109 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3110 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003111
Prabir Pradhan678438e2023-04-13 19:32:51 +00003112 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3113 .deviceId(touchDeviceId)
3114 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3115 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
3116 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003117 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3118 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
3119
3120 // Inject a series of mouse events for a mouse click
Prabir Pradhan678438e2023-04-13 19:32:51 +00003121 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3122 .deviceId(mouseDeviceId)
3123 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3124 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
3125 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003126 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
3127 WithPointerCount(2u)));
3128 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3129
Prabir Pradhan678438e2023-04-13 19:32:51 +00003130 mDispatcher->notifyMotion(
3131 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
3132 .deviceId(mouseDeviceId)
3133 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3134 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
3135 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
3136 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003137 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3138
3139 // Try to send more touch events while the mouse is down. Since it's a continuation of an
3140 // already canceled gesture, it should be ignored.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003141 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3142 .deviceId(touchDeviceId)
3143 .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101))
3144 .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121))
3145 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003146 window->assertNoEvents();
3147}
3148
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003149TEST_F(InputDispatcherTest, HoverWithSpyWindows) {
3150 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3151
3152 sp<FakeWindowHandle> spyWindow =
3153 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3154 spyWindow->setFrame(Rect(0, 0, 600, 800));
3155 spyWindow->setTrustedOverlay(true);
3156 spyWindow->setSpy(true);
3157 sp<FakeWindowHandle> window =
3158 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3159 window->setFrame(Rect(0, 0, 600, 800));
3160
3161 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3162 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
3163
3164 // Send mouse cursor to the window
3165 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3166 injectMotionEvent(mDispatcher,
3167 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3168 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003169 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003170 .x(100)
3171 .y(100))
3172 .build()));
3173
3174 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3175 WithSource(AINPUT_SOURCE_MOUSE)));
3176 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3177 WithSource(AINPUT_SOURCE_MOUSE)));
3178
3179 window->assertNoEvents();
3180 spyWindow->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07003181}
3182
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003183TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) {
3184 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3185
3186 sp<FakeWindowHandle> spyWindow =
3187 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3188 spyWindow->setFrame(Rect(0, 0, 600, 800));
3189 spyWindow->setTrustedOverlay(true);
3190 spyWindow->setSpy(true);
3191 sp<FakeWindowHandle> window =
3192 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3193 window->setFrame(Rect(0, 0, 600, 800));
3194
3195 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3196 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
3197
3198 // Send mouse cursor to the window
3199 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3200 injectMotionEvent(mDispatcher,
3201 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3202 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003203 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003204 .x(100)
3205 .y(100))
3206 .build()));
3207
3208 // Move mouse cursor
3209 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3210 injectMotionEvent(mDispatcher,
3211 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3212 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003213 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003214 .x(110)
3215 .y(110))
3216 .build()));
3217
3218 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3219 WithSource(AINPUT_SOURCE_MOUSE)));
3220 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3221 WithSource(AINPUT_SOURCE_MOUSE)));
3222 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3223 WithSource(AINPUT_SOURCE_MOUSE)));
3224 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3225 WithSource(AINPUT_SOURCE_MOUSE)));
3226 // Touch down on the window
3227 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3228 injectMotionEvent(mDispatcher,
3229 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3230 AINPUT_SOURCE_TOUCHSCREEN)
3231 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003232 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003233 .x(200)
3234 .y(200))
3235 .build()));
3236 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3237 WithSource(AINPUT_SOURCE_MOUSE)));
3238 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3239 WithSource(AINPUT_SOURCE_MOUSE)));
3240 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3241 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3242 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3243 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3244
3245 // pilfer the motion, retaining the gesture on the spy window.
3246 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
3247 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
3248 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3249
3250 // Touch UP on the window
3251 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3252 injectMotionEvent(mDispatcher,
3253 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3254 AINPUT_SOURCE_TOUCHSCREEN)
3255 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003256 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003257 .x(200)
3258 .y(200))
3259 .build()));
3260 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3261 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3262
3263 // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going
3264 // to send a new gesture. It should again go to both windows (spy and the window below), just
3265 // like the first gesture did, before pilfering. The window configuration has not changed.
3266
3267 // One more tap - DOWN
3268 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3269 injectMotionEvent(mDispatcher,
3270 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3271 AINPUT_SOURCE_TOUCHSCREEN)
3272 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003273 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003274 .x(250)
3275 .y(250))
3276 .build()));
3277 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3278 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3279 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3280 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3281
3282 // Touch UP on the window
3283 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3284 injectMotionEvent(mDispatcher,
3285 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3286 AINPUT_SOURCE_TOUCHSCREEN)
3287 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003288 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003289 .x(250)
3290 .y(250))
3291 .build()));
3292 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3293 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3294 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3295 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3296
3297 window->assertNoEvents();
3298 spyWindow->assertNoEvents();
3299}
3300
Garfield Tandf26e862020-07-01 20:18:19 -07003301// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
3302// directly in this test.
3303TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07003304 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07003305 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003306 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003307 window->setFrame(Rect(0, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003308
3309 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3310
3311 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3312
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003313 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003314 injectMotionEvent(mDispatcher,
3315 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3316 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003317 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003318 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003319 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003320 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003321 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003322 injectMotionEvent(mDispatcher,
3323 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3324 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003325 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003326 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003327 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3328 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003329
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003330 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003331 injectMotionEvent(mDispatcher,
3332 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3333 AINPUT_SOURCE_MOUSE)
3334 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3335 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003336 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003337 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003338 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003339
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003340 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003341 injectMotionEvent(mDispatcher,
3342 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3343 AINPUT_SOURCE_MOUSE)
3344 .buttonState(0)
3345 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003346 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003347 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003348 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003349
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003350 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003351 injectMotionEvent(mDispatcher,
3352 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3353 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003354 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003355 .build()));
3356 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3357
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07003358 // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the
3359 // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail.
3360 ASSERT_EQ(InputEventInjectionResult::FAILED,
Garfield Tandf26e862020-07-01 20:18:19 -07003361 injectMotionEvent(mDispatcher,
3362 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
3363 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003364 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003365 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003366 window->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07003367}
3368
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003369/**
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003370 * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event
3371 * is generated.
3372 */
3373TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) {
3374 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3375 sp<FakeWindowHandle> window =
3376 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3377 window->setFrame(Rect(0, 0, 1200, 800));
3378
3379 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3380
3381 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3382
3383 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3384 injectMotionEvent(mDispatcher,
3385 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3386 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003387 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003388 .x(300)
3389 .y(400))
3390 .build()));
3391 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
3392
3393 // Remove the window, but keep the channel.
3394 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
3395 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3396}
3397
3398/**
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003399 * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT.
3400 */
3401TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) {
3402 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3403 sp<FakeWindowHandle> window =
3404 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3405 window->setFrame(Rect(0, 0, 100, 100));
3406
3407 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3408
3409 const int32_t mouseDeviceId = 7;
3410 const int32_t touchDeviceId = 4;
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003411
3412 // Start hovering with the mouse
Prabir Pradhan678438e2023-04-13 19:32:51 +00003413 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
3414 .deviceId(mouseDeviceId)
3415 .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10))
3416 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003417 window->consumeMotionEvent(
3418 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3419
3420 // Touch goes down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003421 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3422 .deviceId(touchDeviceId)
3423 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
3424 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003425
3426 window->consumeMotionEvent(
3427 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
3428 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3429}
3430
3431/**
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003432 * Inject a mouse hover event followed by a tap from touchscreen.
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003433 * The tap causes a HOVER_EXIT event to be generated because the current event
3434 * stream's source has been switched.
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003435 */
3436TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) {
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 // Inject a hover_move from mouse.
3445 NotifyMotionArgs motionArgs =
3446 generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE,
3447 ADISPLAY_ID_DEFAULT, {{50, 50}});
3448 motionArgs.xCursorPosition = 50;
3449 motionArgs.yCursorPosition = 50;
Prabir Pradhan678438e2023-04-13 19:32:51 +00003450 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003451 ASSERT_NO_FATAL_FAILURE(
3452 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3453 WithSource(AINPUT_SOURCE_MOUSE))));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003454
3455 // Tap on the window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003456 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3457 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3458 {{10, 10}}));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003459 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003460 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3461 WithSource(AINPUT_SOURCE_MOUSE))));
3462
3463 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003464 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3465 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
3466
Prabir Pradhan678438e2023-04-13 19:32:51 +00003467 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3468 ADISPLAY_ID_DEFAULT, {{10, 10}}));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003469 ASSERT_NO_FATAL_FAILURE(
3470 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3471 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
3472}
3473
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003474TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) {
3475 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3476 sp<FakeWindowHandle> windowDefaultDisplay =
3477 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
3478 ADISPLAY_ID_DEFAULT);
3479 windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800));
3480 sp<FakeWindowHandle> windowSecondDisplay =
3481 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay",
3482 SECOND_DISPLAY_ID);
3483 windowSecondDisplay->setFrame(Rect(0, 0, 600, 800));
3484
3485 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
3486 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
3487
3488 // Set cursor position in window in default display and check that hover enter and move
3489 // events are generated.
3490 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3491 injectMotionEvent(mDispatcher,
3492 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3493 AINPUT_SOURCE_MOUSE)
3494 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003495 .pointer(PointerBuilder(0, ToolType::MOUSE)
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003496 .x(300)
3497 .y(600))
3498 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003499 windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003500
3501 // Remove all windows in secondary display and check that no event happens on window in
3502 // primary display.
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003503 mDispatcher->setInputWindows(
3504 {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003505 windowDefaultDisplay->assertNoEvents();
3506
3507 // Move cursor position in window in default display and check that only hover move
3508 // event is generated and not hover enter event.
3509 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
3510 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
3511 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3512 injectMotionEvent(mDispatcher,
3513 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3514 AINPUT_SOURCE_MOUSE)
3515 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003516 .pointer(PointerBuilder(0, ToolType::MOUSE)
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003517 .x(400)
3518 .y(700))
3519 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003520 windowDefaultDisplay->consumeMotionEvent(
3521 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3522 WithSource(AINPUT_SOURCE_MOUSE)));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003523 windowDefaultDisplay->assertNoEvents();
3524}
3525
Garfield Tan00f511d2019-06-12 16:55:40 -07003526TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07003527 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07003528
3529 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003530 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003531 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07003532 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003533 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003534 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07003535
3536 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3537
Arthur Hung72d8dc32020-03-28 00:48:39 +00003538 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07003539
3540 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
3541 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003542 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07003543 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003544 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003545 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003546 windowRight->assertNoEvents();
3547}
3548
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003549TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07003550 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003551 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3552 "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07003553 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003554
Arthur Hung72d8dc32020-03-28 00:48:39 +00003555 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003556 setFocusedWindow(window);
3557
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003558 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003559
Prabir Pradhan678438e2023-04-13 19:32:51 +00003560 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003561
3562 // Window should receive key down event.
3563 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3564
3565 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
3566 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003567 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07003568 window->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003569 AKEY_EVENT_FLAG_CANCELED);
3570}
3571
3572TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07003573 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003574 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3575 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003576
Arthur Hung72d8dc32020-03-28 00:48:39 +00003577 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003578
Prabir Pradhan678438e2023-04-13 19:32:51 +00003579 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3580 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003581
3582 // Window should receive motion down event.
3583 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3584
3585 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
3586 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003587 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08003588 window->consumeMotionEvent(
3589 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003590}
3591
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -07003592TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) {
3593 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3594 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3595 "Fake Window", ADISPLAY_ID_DEFAULT);
3596
3597 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3598
3599 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3600 .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10))
3601 .build());
3602
3603 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3604
3605 // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT
3606 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
3607 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3608
3609 // After the device has been reset, a new hovering stream can be sent to the window
3610 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3611 .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15))
3612 .build());
3613 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3614}
3615
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003616TEST_F(InputDispatcherTest, InterceptKeyByPolicy) {
3617 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003618 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3619 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003620 window->setFocusable(true);
3621
3622 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3623 setFocusedWindow(window);
3624
3625 window->consumeFocusEvent(true);
3626
Prabir Pradhan678438e2023-04-13 19:32:51 +00003627 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003628 const std::chrono::milliseconds interceptKeyTimeout = 50ms;
3629 const nsecs_t injectTime = keyArgs.eventTime;
3630 mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout);
Prabir Pradhan678438e2023-04-13 19:32:51 +00003631 mDispatcher->notifyKey(keyArgs);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003632 // The dispatching time should be always greater than or equal to intercept key timeout.
3633 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3634 ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >=
3635 std::chrono::nanoseconds(interceptKeyTimeout).count());
3636}
3637
Siarhei Vishniakouf54d2ab2023-06-09 13:23:53 -07003638/**
3639 * Keys with ACTION_UP are delivered immediately, even if a long 'intercept key timeout' is set.
3640 */
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003641TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) {
3642 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003643 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3644 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003645 window->setFocusable(true);
3646
3647 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3648 setFocusedWindow(window);
3649
3650 window->consumeFocusEvent(true);
3651
Prabir Pradhan678438e2023-04-13 19:32:51 +00003652 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003653 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf54d2ab2023-06-09 13:23:53 -07003654
3655 // Set a value that's significantly larger than the default consumption timeout. If the
3656 // implementation is correct, the actual value doesn't matter; it won't slow down the test.
3657 mFakePolicy->setInterceptKeyTimeout(600ms);
3658 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
3659 // Window should receive key event immediately when same key up.
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003660 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3661}
3662
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003663/**
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003664 * Two windows. First is a regular window. Second does not overlap with the first, and has
3665 * WATCH_OUTSIDE_TOUCH.
3666 * Both windows are owned by the same UID.
3667 * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero
3668 * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID.
3669 */
3670TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) {
3671 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3672 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3673 "First Window", ADISPLAY_ID_DEFAULT);
3674 window->setFrame(Rect{0, 0, 100, 100});
3675
3676 sp<FakeWindowHandle> outsideWindow =
3677 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3678 ADISPLAY_ID_DEFAULT);
3679 outsideWindow->setFrame(Rect{100, 100, 200, 200});
3680 outsideWindow->setWatchOutsideTouch(true);
3681 // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped
3682 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {outsideWindow, window}}});
3683
3684 // Tap on first window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003685 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3686 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3687 {PointF{50, 50}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003688 window->consumeMotionDown();
3689 // The coordinates of the tap in 'outsideWindow' are relative to its top left corner.
3690 // Therefore, we should offset them by (100, 100) relative to the screen's top left corner.
3691 outsideWindow->consumeMotionEvent(
3692 AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50)));
3693}
3694
3695/**
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003696 * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when
3697 * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one
3698 * ACTION_OUTSIDE event is sent per gesture.
3699 */
3700TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) {
3701 // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH.
3702 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003703 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3704 "First Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003705 window->setWatchOutsideTouch(true);
3706 window->setFrame(Rect{0, 0, 100, 100});
3707 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003708 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3709 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003710 secondWindow->setFrame(Rect{100, 100, 200, 200});
3711 sp<FakeWindowHandle> thirdWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003712 sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window",
3713 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003714 thirdWindow->setFrame(Rect{200, 200, 300, 300});
3715 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}});
3716
3717 // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003718 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3719 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3720 {PointF{-10, -10}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003721 window->assertNoEvents();
3722 secondWindow->assertNoEvents();
3723
3724 // The second pointer lands inside `secondWindow`, which should receive a DOWN event.
3725 // Now, `window` should get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003726 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3727 ADISPLAY_ID_DEFAULT,
3728 {PointF{-10, -10}, PointF{105, 105}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003729 const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}};
3730 window->consumeMotionEvent(
3731 AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers)));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003732 secondWindow->consumeMotionDown();
3733 thirdWindow->assertNoEvents();
3734
3735 // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is
3736 // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003737 mDispatcher->notifyMotion(
3738 generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3739 {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003740 window->assertNoEvents();
3741 secondWindow->consumeMotionMove();
3742 thirdWindow->consumeMotionDown();
3743}
3744
Prabir Pradhan814fe082022-07-22 20:22:18 +00003745TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) {
3746 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003747 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3748 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003749 window->setFocusable(true);
3750
Patrick Williamsd828f302023-04-28 17:52:08 -05003751 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00003752 setFocusedWindow(window);
3753
3754 window->consumeFocusEvent(true);
3755
Prabir Pradhan678438e2023-04-13 19:32:51 +00003756 const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3757 const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
3758 mDispatcher->notifyKey(keyDown);
3759 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003760
3761 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3762 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3763
3764 // All windows are removed from the display. Ensure that we can no longer dispatch to it.
Patrick Williamsd828f302023-04-28 17:52:08 -05003765 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00003766
3767 window->consumeFocusEvent(false);
3768
Prabir Pradhan678438e2023-04-13 19:32:51 +00003769 mDispatcher->notifyKey(keyDown);
3770 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003771 window->assertNoEvents();
3772}
3773
Arthur Hung96483742022-11-15 03:30:48 +00003774TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) {
3775 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3776 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3777 "Fake Window", ADISPLAY_ID_DEFAULT);
3778 // Ensure window is non-split and have some transform.
3779 window->setPreventSplitting(true);
3780 window->setWindowOffset(20, 40);
Patrick Williamsd828f302023-04-28 17:52:08 -05003781 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung96483742022-11-15 03:30:48 +00003782
3783 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3784 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3785 {50, 50}))
3786 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3787 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3788
3789 const MotionEvent secondFingerDownEvent =
3790 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3791 .displayId(ADISPLAY_ID_DEFAULT)
3792 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003793 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
3794 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50))
Arthur Hung96483742022-11-15 03:30:48 +00003795 .build();
3796 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3797 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
3798 InputEventInjectionSync::WAIT_FOR_RESULT))
3799 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3800
3801 const MotionEvent* event = window->consumeMotion();
3802 EXPECT_EQ(POINTER_1_DOWN, event->getAction());
3803 EXPECT_EQ(70, event->getX(0)); // 50 + 20
3804 EXPECT_EQ(90, event->getY(0)); // 50 + 40
3805 EXPECT_EQ(-10, event->getX(1)); // -30 + 20
3806 EXPECT_EQ(-10, event->getY(1)); // -50 + 40
3807}
3808
Harry Cuttsb166c002023-05-09 13:06:05 +00003809TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) {
3810 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3811 sp<FakeWindowHandle> window =
3812 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3813 window->setFrame(Rect(0, 0, 400, 400));
3814 sp<FakeWindowHandle> trustedOverlay =
3815 sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay",
3816 ADISPLAY_ID_DEFAULT);
3817 trustedOverlay->setSpy(true);
3818 trustedOverlay->setTrustedOverlay(true);
3819
3820 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {trustedOverlay, window}}});
3821
3822 // Start a three-finger touchpad swipe
3823 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3824 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3825 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3826 .build());
3827 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
3828 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3829 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3830 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3831 .build());
3832 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
3833 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3834 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3835 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
3836 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3837 .build());
3838
3839 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3840 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
3841 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN));
3842
3843 // Move the swipe a bit
3844 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
3845 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3846 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3847 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3848 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3849 .build());
3850
3851 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3852
3853 // End the swipe
3854 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
3855 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3856 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3857 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3858 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3859 .build());
3860 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
3861 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3862 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3863 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3864 .build());
3865 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
3866 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3867 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3868 .build());
3869
3870 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP));
3871 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
3872 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP));
3873
3874 window->assertNoEvents();
3875}
3876
3877TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) {
3878 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3879 sp<FakeWindowHandle> window =
3880 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3881 window->setFrame(Rect(0, 0, 400, 400));
3882 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3883
3884 // Start a three-finger touchpad swipe
3885 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3886 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3887 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3888 .build());
3889 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
3890 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3891 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3892 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3893 .build());
3894 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
3895 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3896 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3897 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
3898 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3899 .build());
3900
3901 // Move the swipe a bit
3902 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
3903 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3904 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3905 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3906 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3907 .build());
3908
3909 // End the swipe
3910 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
3911 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3912 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3913 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3914 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3915 .build());
3916 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
3917 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3918 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3919 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3920 .build());
3921 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
3922 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3923 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3924 .build());
3925
3926 window->assertNoEvents();
3927}
3928
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003929/**
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07003930 * Send a two-pointer gesture to a single window. The window's orientation changes in response to
3931 * the first pointer.
3932 * Ensure that the second pointer is not sent to the window.
3933 *
3934 * The subsequent gesture should be correctly delivered to the window.
3935 */
3936TEST_F(InputDispatcherTest, MultiplePointersWithRotatingWindow) {
3937 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3938 sp<FakeWindowHandle> window =
3939 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3940 window->setFrame(Rect(0, 0, 400, 400));
3941 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3942
3943 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
3944 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3945 .downTime(baseTime + 10)
3946 .eventTime(baseTime + 10)
3947 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3948 .build());
3949
3950 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3951
3952 // We need a new window object for the same window, because dispatcher will store objects by
3953 // reference. That means that the testing code and the dispatcher will refer to the same shared
3954 // object. Calling window->setTransform here would affect dispatcher's comparison
3955 // of the old window to the new window, since both the old window and the new window would be
3956 // updated to the same value.
3957 sp<FakeWindowHandle> windowDup = window->duplicate();
3958
3959 // Change the transform so that the orientation is now different from original.
3960 windowDup->setWindowTransform(0, -1, 1, 0);
3961
3962 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDup}}});
3963
3964 window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
3965
3966 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3967 .downTime(baseTime + 10)
3968 .eventTime(baseTime + 30)
3969 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3970 .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200))
3971 .build());
3972
3973 // Finish the gesture and start a new one. Ensure the new gesture is sent to the window
3974 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
3975 .downTime(baseTime + 10)
3976 .eventTime(baseTime + 40)
3977 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3978 .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200))
3979 .build());
3980 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
3981 .downTime(baseTime + 10)
3982 .eventTime(baseTime + 50)
3983 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3984 .build());
3985
3986 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3987 .downTime(baseTime + 60)
3988 .eventTime(baseTime + 60)
3989 .pointer(PointerBuilder(0, ToolType::FINGER).x(40).y(40))
3990 .build());
3991
3992 windowDup->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3993}
3994
3995/**
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003996 * Ensure the correct coordinate spaces are used by InputDispatcher.
3997 *
3998 * InputDispatcher works in the display space, so its coordinate system is relative to the display
3999 * panel. Windows get events in the window space, and get raw coordinates in the logical display
4000 * space.
4001 */
4002class InputDispatcherDisplayProjectionTest : public InputDispatcherTest {
4003public:
4004 void SetUp() override {
4005 InputDispatcherTest::SetUp();
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004006 removeAllWindowsAndDisplays();
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004007 }
4008
4009 void addDisplayInfo(int displayId, const ui::Transform& transform) {
4010 gui::DisplayInfo info;
4011 info.displayId = displayId;
4012 info.transform = transform;
4013 mDisplayInfos.push_back(std::move(info));
Patrick Williamsd828f302023-04-28 17:52:08 -05004014 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004015 }
4016
4017 void addWindow(const sp<WindowInfoHandle>& windowHandle) {
4018 mWindowInfos.push_back(*windowHandle->getInfo());
Patrick Williamsd828f302023-04-28 17:52:08 -05004019 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004020 }
4021
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004022 void removeAllWindowsAndDisplays() {
4023 mDisplayInfos.clear();
4024 mWindowInfos.clear();
4025 }
4026
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004027 // Set up a test scenario where the display has a scaled projection and there are two windows
4028 // on the display.
4029 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() {
4030 // The display has a projection that has a scale factor of 2 and 4 in the x and y directions
4031 // respectively.
4032 ui::Transform displayTransform;
4033 displayTransform.set(2, 0, 0, 4);
4034 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
4035
4036 std::shared_ptr<FakeApplicationHandle> application =
4037 std::make_shared<FakeApplicationHandle>();
4038
4039 // Add two windows to the display. Their frames are represented in the display space.
4040 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004041 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4042 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004043 firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform);
4044 addWindow(firstWindow);
4045
4046 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004047 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4048 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004049 secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform);
4050 addWindow(secondWindow);
4051 return {std::move(firstWindow), std::move(secondWindow)};
4052 }
4053
4054private:
4055 std::vector<gui::DisplayInfo> mDisplayInfos;
4056 std::vector<gui::WindowInfo> mWindowInfos;
4057};
4058
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004059TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) {
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004060 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4061 // Send down to the first window. The point is represented in the display space. The point is
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004062 // selected so that if the hit test was performed with the point and the bounds being in
4063 // different coordinate spaces, the event would end up in the incorrect window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004064 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4065 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4066 {PointF{75, 55}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004067
4068 firstWindow->consumeMotionDown();
4069 secondWindow->assertNoEvents();
4070}
4071
4072// Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API,
4073// the event should be treated as being in the logical display space.
4074TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) {
4075 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4076 // Send down to the first window. The point is represented in the logical display space. The
4077 // point is selected so that if the hit test was done in logical display space, then it would
4078 // end up in the incorrect window.
4079 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4080 PointF{75 * 2, 55 * 4});
4081
4082 firstWindow->consumeMotionDown();
4083 secondWindow->assertNoEvents();
4084}
4085
Prabir Pradhandaa2f142021-12-10 09:30:08 +00004086// Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed
4087// event should be treated as being in the logical display space.
4088TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) {
4089 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4090
4091 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
4092 ui::Transform injectedEventTransform;
4093 injectedEventTransform.set(matrix);
4094 const vec2 expectedPoint{75, 55}; // The injected point in the logical display space.
4095 const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint);
4096
4097 MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4098 .displayId(ADISPLAY_ID_DEFAULT)
4099 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004100 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER)
Prabir Pradhandaa2f142021-12-10 09:30:08 +00004101 .x(untransformedPoint.x)
4102 .y(untransformedPoint.y))
4103 .build();
4104 event.transform(matrix);
4105
4106 injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT,
4107 InputEventInjectionSync::WAIT_FOR_RESULT);
4108
4109 firstWindow->consumeMotionDown();
4110 secondWindow->assertNoEvents();
4111}
4112
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004113TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) {
4114 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4115
4116 // Send down to the second window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004117 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4118 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4119 {PointF{150, 220}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004120
4121 firstWindow->assertNoEvents();
4122 const MotionEvent* event = secondWindow->consumeMotion();
Siarhei Vishniakoub681c202023-05-01 11:22:33 -07004123 ASSERT_NE(nullptr, event);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004124 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction());
4125
4126 // Ensure that the events from the "getRaw" API are in logical display coordinates.
4127 EXPECT_EQ(300, event->getRawX(0));
4128 EXPECT_EQ(880, event->getRawY(0));
4129
4130 // Ensure that the x and y values are in the window's coordinate space.
4131 // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in
4132 // the logical display space. This will be the origin of the window space.
4133 EXPECT_EQ(100, event->getX(0));
4134 EXPECT_EQ(80, event->getY(0));
4135}
4136
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004137/** Ensure consistent behavior of InputDispatcher in all orientations. */
4138class InputDispatcherDisplayOrientationFixture
4139 : public InputDispatcherDisplayProjectionTest,
4140 public ::testing::WithParamInterface<ui::Rotation> {};
4141
4142// This test verifies the touchable region of a window for all rotations of the display by tapping
4143// in different locations on the display, specifically points close to the four corners of a
4144// window.
4145TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) {
4146 constexpr static int32_t displayWidth = 400;
4147 constexpr static int32_t displayHeight = 800;
4148
4149 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4150
4151 const auto rotation = GetParam();
4152
4153 // Set up the display with the specified rotation.
4154 const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270;
4155 const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth;
4156 const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight;
4157 const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation),
4158 logicalDisplayWidth, logicalDisplayHeight);
4159 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
4160
4161 // Create a window with its bounds determined in the logical display.
4162 const Rect frameInLogicalDisplay(100, 100, 200, 300);
4163 const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay);
4164 sp<FakeWindowHandle> window =
4165 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4166 window->setFrame(frameInDisplay, displayTransform);
4167 addWindow(window);
4168
4169 // The following points in logical display space should be inside the window.
4170 static const std::array<vec2, 4> insidePoints{
4171 {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}};
4172 for (const auto pointInsideWindow : insidePoints) {
4173 const vec2 p = displayTransform.inverse().transform(pointInsideWindow);
4174 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004175 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4176 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4177 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004178 window->consumeMotionDown();
4179
Prabir Pradhan678438e2023-04-13 19:32:51 +00004180 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4181 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4182 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004183 window->consumeMotionUp();
4184 }
4185
4186 // The following points in logical display space should be outside the window.
4187 static const std::array<vec2, 5> outsidePoints{
4188 {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}};
4189 for (const auto pointOutsideWindow : outsidePoints) {
4190 const vec2 p = displayTransform.inverse().transform(pointOutsideWindow);
4191 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004192 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4193 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4194 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004195
Prabir Pradhan678438e2023-04-13 19:32:51 +00004196 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4197 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4198 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004199 }
4200 window->assertNoEvents();
4201}
4202
4203// Run the precision tests for all rotations.
4204INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests,
4205 InputDispatcherDisplayOrientationFixture,
4206 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
4207 ui::ROTATION_270),
4208 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
4209 return ftl::enum_string(testParamInfo.param);
4210 });
4211
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004212using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher,
4213 sp<IBinder>, sp<IBinder>)>;
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004214
4215class TransferTouchFixture : public InputDispatcherTest,
4216 public ::testing::WithParamInterface<TransferFunction> {};
4217
4218TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07004219 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004220
4221 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004222 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004223 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4224 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004225 firstWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004226 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004227 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4228 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004229 sp<FakeWindowHandle> wallpaper =
4230 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
4231 wallpaper->setIsWallpaper(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004232 // Add the windows to the dispatcher
Arthur Hungc539dbb2022-12-08 07:45:36 +00004233 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004234
4235 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004236 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4237 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004238
Svet Ganov5d3bc372020-01-26 23:11:07 -08004239 // Only the first window should get the down event
4240 firstWindow->consumeMotionDown();
4241 secondWindow->assertNoEvents();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004242 wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004243
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004244 // Transfer touch to the second window
4245 TransferFunction f = GetParam();
4246 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4247 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004248 // The first window gets cancel and the second gets down
4249 firstWindow->consumeMotionCancel();
4250 secondWindow->consumeMotionDown();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004251 wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004252
4253 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004254 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4255 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004256 // The first window gets no events and the second gets up
4257 firstWindow->assertNoEvents();
4258 secondWindow->consumeMotionUp();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004259 wallpaper->assertNoEvents();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004260}
4261
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004262/**
4263 * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch
4264 * from. When we have spy windows, there are several windows to choose from: either spy, or the
4265 * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most
4266 * natural to the user.
4267 * In this test, we are sending a pointer to both spy window and first window. We then try to
4268 * transfer touch to the second window. The dispatcher should identify the first window as the
4269 * one that should lose the gesture, and therefore the action should be to move the gesture from
4270 * the first window to the second.
4271 * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test
4272 * the other API, as well.
4273 */
4274TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) {
4275 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4276
4277 // Create a couple of windows + a spy window
4278 sp<FakeWindowHandle> spyWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004279 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004280 spyWindow->setTrustedOverlay(true);
4281 spyWindow->setSpy(true);
4282 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004283 sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004284 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004285 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004286
4287 // Add the windows to the dispatcher
4288 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}});
4289
4290 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004291 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4292 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004293 // Only the first window and spy should get the down event
4294 spyWindow->consumeMotionDown();
4295 firstWindow->consumeMotionDown();
4296
4297 // Transfer touch to the second window. Non-spy window should be preferred over the spy window
4298 // if f === 'transferTouch'.
4299 TransferFunction f = GetParam();
4300 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4301 ASSERT_TRUE(success);
4302 // The first window gets cancel and the second gets down
4303 firstWindow->consumeMotionCancel();
4304 secondWindow->consumeMotionDown();
4305
4306 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004307 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4308 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004309 // The first window gets no events and the second+spy get up
4310 firstWindow->assertNoEvents();
4311 spyWindow->consumeMotionUp();
4312 secondWindow->consumeMotionUp();
4313}
4314
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004315TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004316 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004317
4318 PointF touchPoint = {10, 10};
4319
4320 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004321 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004322 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4323 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004324 firstWindow->setPreventSplitting(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004325 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004326 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4327 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004328 secondWindow->setPreventSplitting(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004329
4330 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004331 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004332
4333 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004334 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4335 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4336 {touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004337 // Only the first window should get the down event
4338 firstWindow->consumeMotionDown();
4339 secondWindow->assertNoEvents();
4340
4341 // Send pointer down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004342 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4343 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004344 // Only the first window should get the pointer down event
4345 firstWindow->consumeMotionPointerDown(1);
4346 secondWindow->assertNoEvents();
4347
4348 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004349 TransferFunction f = GetParam();
4350 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4351 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004352 // The first window gets cancel and the second gets down and pointer down
4353 firstWindow->consumeMotionCancel();
4354 secondWindow->consumeMotionDown();
4355 secondWindow->consumeMotionPointerDown(1);
4356
4357 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004358 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4359 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004360 // The first window gets nothing and the second gets pointer up
4361 firstWindow->assertNoEvents();
4362 secondWindow->consumeMotionPointerUp(1);
4363
4364 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004365 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4366 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004367 // The first window gets nothing and the second gets up
4368 firstWindow->assertNoEvents();
4369 secondWindow->consumeMotionUp();
4370}
4371
Arthur Hungc539dbb2022-12-08 07:45:36 +00004372TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) {
4373 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4374
4375 // Create a couple of windows
4376 sp<FakeWindowHandle> firstWindow =
4377 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4378 ADISPLAY_ID_DEFAULT);
4379 firstWindow->setDupTouchToWallpaper(true);
4380 sp<FakeWindowHandle> secondWindow =
4381 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4382 ADISPLAY_ID_DEFAULT);
4383 secondWindow->setDupTouchToWallpaper(true);
4384
4385 sp<FakeWindowHandle> wallpaper1 =
4386 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT);
4387 wallpaper1->setIsWallpaper(true);
4388
4389 sp<FakeWindowHandle> wallpaper2 =
4390 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT);
4391 wallpaper2->setIsWallpaper(true);
4392 // Add the windows to the dispatcher
4393 mDispatcher->setInputWindows(
4394 {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}});
4395
4396 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004397 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4398 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004399
4400 // Only the first window should get the down event
4401 firstWindow->consumeMotionDown();
4402 secondWindow->assertNoEvents();
4403 wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4404 wallpaper2->assertNoEvents();
4405
4406 // Transfer touch focus to the second window
4407 TransferFunction f = GetParam();
4408 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4409 ASSERT_TRUE(success);
4410
4411 // The first window gets cancel and the second gets down
4412 firstWindow->consumeMotionCancel();
4413 secondWindow->consumeMotionDown();
4414 wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4415 wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4416
4417 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004418 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4419 ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004420 // The first window gets no events and the second gets up
4421 firstWindow->assertNoEvents();
4422 secondWindow->consumeMotionUp();
4423 wallpaper1->assertNoEvents();
4424 wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4425}
4426
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004427// For the cases of single pointer touch and two pointers non-split touch, the api's
4428// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
4429// for the case where there are multiple pointers split across several windows.
4430INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
4431 ::testing::Values(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004432 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4433 sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004434 return dispatcher->transferTouch(destChannelToken,
4435 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004436 },
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004437 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4438 sp<IBinder> from, sp<IBinder> to) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004439 return dispatcher->transferTouchFocus(from, to,
Harry Cutts33476232023-01-30 19:57:29 +00004440 /*isDragAndDrop=*/false);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004441 }));
4442
Svet Ganov5d3bc372020-01-26 23:11:07 -08004443TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004444 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004445
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004446 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004447 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4448 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004449 firstWindow->setFrame(Rect(0, 0, 600, 400));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004450
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004451 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004452 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4453 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004454 secondWindow->setFrame(Rect(0, 400, 600, 800));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004455
4456 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004457 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004458
4459 PointF pointInFirst = {300, 200};
4460 PointF pointInSecond = {300, 600};
4461
4462 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004463 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4464 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4465 {pointInFirst}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004466 // Only the first window should get the down event
4467 firstWindow->consumeMotionDown();
4468 secondWindow->assertNoEvents();
4469
4470 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004471 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4472 ADISPLAY_ID_DEFAULT,
4473 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004474 // The first window gets a move and the second a down
4475 firstWindow->consumeMotionMove();
4476 secondWindow->consumeMotionDown();
4477
4478 // Transfer touch focus to the second window
4479 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
4480 // The first window gets cancel and the new gets pointer down (it already saw down)
4481 firstWindow->consumeMotionCancel();
4482 secondWindow->consumeMotionPointerDown(1);
4483
4484 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004485 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4486 ADISPLAY_ID_DEFAULT,
4487 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004488 // The first window gets nothing and the second gets pointer up
4489 firstWindow->assertNoEvents();
4490 secondWindow->consumeMotionPointerUp(1);
4491
4492 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004493 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4494 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004495 // The first window gets nothing and the second gets up
4496 firstWindow->assertNoEvents();
4497 secondWindow->consumeMotionUp();
4498}
4499
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004500// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
4501// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
4502// touch is not supported, so the touch should continue on those windows and the transferred-to
4503// window should get nothing.
4504TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
4505 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4506
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004507 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004508 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4509 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004510 firstWindow->setFrame(Rect(0, 0, 600, 400));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004511
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004512 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004513 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4514 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004515 secondWindow->setFrame(Rect(0, 400, 600, 800));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004516
4517 // Add the windows to the dispatcher
4518 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4519
4520 PointF pointInFirst = {300, 200};
4521 PointF pointInSecond = {300, 600};
4522
4523 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004524 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4525 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4526 {pointInFirst}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004527 // Only the first window should get the down event
4528 firstWindow->consumeMotionDown();
4529 secondWindow->assertNoEvents();
4530
4531 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004532 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4533 ADISPLAY_ID_DEFAULT,
4534 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004535 // The first window gets a move and the second a down
4536 firstWindow->consumeMotionMove();
4537 secondWindow->consumeMotionDown();
4538
4539 // Transfer touch focus to the second window
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004540 const bool transferred =
4541 mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004542 // The 'transferTouch' call should not succeed, because there are 2 touched windows
4543 ASSERT_FALSE(transferred);
4544 firstWindow->assertNoEvents();
4545 secondWindow->assertNoEvents();
4546
4547 // The rest of the dispatch should proceed as normal
4548 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004549 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4550 ADISPLAY_ID_DEFAULT,
4551 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004552 // The first window gets MOVE and the second gets pointer up
4553 firstWindow->consumeMotionMove();
4554 secondWindow->consumeMotionUp();
4555
4556 // Send up event to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004557 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4558 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004559 // The first window gets nothing and the second gets up
4560 firstWindow->consumeMotionUp();
4561 secondWindow->assertNoEvents();
4562}
4563
Arthur Hungabbb9d82021-09-01 14:52:30 +00004564// This case will create two windows and one mirrored window on the default display and mirror
4565// two windows on the second display. It will test if 'transferTouchFocus' works fine if we put
4566// the windows info of second display before default display.
4567TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) {
4568 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4569 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004570 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004571 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004572 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004573 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004574 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004575
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004576 sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004577 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004578
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004579 sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004580 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004581
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004582 sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004583 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004584
4585 // Update window info, let it find window handle of second display first.
4586 mDispatcher->setInputWindows(
4587 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4588 {ADISPLAY_ID_DEFAULT,
4589 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4590
4591 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4592 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4593 {50, 50}))
4594 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4595
4596 // Window should receive motion event.
4597 firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4598
4599 // Transfer touch focus
4600 ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(),
4601 secondWindowInPrimary->getToken()));
4602 // The first window gets cancel.
4603 firstWindowInPrimary->consumeMotionCancel();
4604 secondWindowInPrimary->consumeMotionDown();
4605
4606 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4607 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4608 ADISPLAY_ID_DEFAULT, {150, 50}))
4609 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4610 firstWindowInPrimary->assertNoEvents();
4611 secondWindowInPrimary->consumeMotionMove();
4612
4613 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4614 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4615 {150, 50}))
4616 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4617 firstWindowInPrimary->assertNoEvents();
4618 secondWindowInPrimary->consumeMotionUp();
4619}
4620
4621// Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use
4622// 'transferTouch' api.
4623TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
4624 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4625 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004626 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004627 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004628 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004629 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004630 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004631
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004632 sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004633 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004634
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004635 sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004636 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004637
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004638 sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004639 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004640
4641 // Update window info, let it find window handle of second display first.
4642 mDispatcher->setInputWindows(
4643 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4644 {ADISPLAY_ID_DEFAULT,
4645 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4646
4647 // Touch on second display.
4648 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4649 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50}))
4650 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4651
4652 // Window should receive motion event.
4653 firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4654
4655 // Transfer touch focus
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004656 ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004657
4658 // The first window gets cancel.
4659 firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID);
4660 secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4661
4662 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4663 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4664 SECOND_DISPLAY_ID, {150, 50}))
4665 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4666 firstWindowInPrimary->assertNoEvents();
4667 secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID);
4668
4669 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4670 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
4671 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4672 firstWindowInPrimary->assertNoEvents();
4673 secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID);
4674}
4675
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004676TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004677 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004678 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4679 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004680
Vishnu Nair47074b82020-08-14 11:54:47 -07004681 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004682 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004683 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004684
4685 window->consumeFocusEvent(true);
4686
Prabir Pradhan678438e2023-04-13 19:32:51 +00004687 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004688
4689 // Window should receive key down event.
4690 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Josep del Riob3981622023-04-18 15:49:45 +00004691
4692 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00004693 mDispatcher->waitForIdle();
Josep del Riob3981622023-04-18 15:49:45 +00004694 mFakePolicy->assertUserActivityPoked();
4695}
4696
4697TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) {
4698 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4699 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4700 "Fake Window", ADISPLAY_ID_DEFAULT);
4701
4702 window->setDisableUserActivity(true);
4703 window->setFocusable(true);
4704 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4705 setFocusedWindow(window);
4706
4707 window->consumeFocusEvent(true);
4708
4709 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4710
4711 // Window should receive key down event.
4712 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4713
4714 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00004715 mDispatcher->waitForIdle();
Josep del Riob3981622023-04-18 15:49:45 +00004716 mFakePolicy->assertUserActivityNotPoked();
4717}
4718
4719TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) {
4720 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4721 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4722 "Fake Window", ADISPLAY_ID_DEFAULT);
4723
4724 window->setFocusable(true);
4725 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4726 setFocusedWindow(window);
4727
4728 window->consumeFocusEvent(true);
4729
4730 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4731 mDispatcher->waitForIdle();
4732
4733 // System key is not passed down
4734 window->assertNoEvents();
4735
4736 // Should have poked user activity
4737 mFakePolicy->assertUserActivityPoked();
4738}
4739
4740TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) {
4741 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4742 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4743 "Fake Window", ADISPLAY_ID_DEFAULT);
4744
4745 window->setFocusable(true);
4746 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4747 setFocusedWindow(window);
4748
4749 window->consumeFocusEvent(true);
4750
4751 mDispatcher->notifyKey(generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4752 mDispatcher->waitForIdle();
4753
4754 // System key is not passed down
4755 window->assertNoEvents();
4756
4757 // Should have poked user activity
4758 mFakePolicy->assertUserActivityPoked();
4759}
4760
4761TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) {
4762 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4763 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4764 "Fake Window", ADISPLAY_ID_DEFAULT);
4765
4766 window->setDisableUserActivity(true);
4767 window->setFocusable(true);
4768 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4769 setFocusedWindow(window);
4770
4771 window->consumeFocusEvent(true);
4772
4773 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4774 mDispatcher->waitForIdle();
4775
4776 // System key is not passed down
4777 window->assertNoEvents();
4778
4779 // Should have poked user activity
4780 mFakePolicy->assertUserActivityPoked();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004781}
4782
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07004783TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) {
4784 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4785 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4786 "Fake Window", ADISPLAY_ID_DEFAULT);
4787
4788 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4789
4790 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4791 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4792 ADISPLAY_ID_DEFAULT, {100, 100}))
4793 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4794
4795 window->consumeMotionEvent(
4796 AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT)));
4797
4798 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00004799 mDispatcher->waitForIdle();
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07004800 mFakePolicy->assertUserActivityPoked();
4801}
4802
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004803TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004804 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004805 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4806 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004807
Arthur Hung72d8dc32020-03-28 00:48:39 +00004808 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004809
Prabir Pradhan678438e2023-04-13 19:32:51 +00004810 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004811 mDispatcher->waitForIdle();
4812
4813 window->assertNoEvents();
4814}
4815
4816// If a window is touchable, but does not have focus, it should receive motion events, but not keys
4817TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07004818 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004819 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4820 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004821
Arthur Hung72d8dc32020-03-28 00:48:39 +00004822 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004823
4824 // Send key
Prabir Pradhan678438e2023-04-13 19:32:51 +00004825 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004826 // Send motion
Prabir Pradhan678438e2023-04-13 19:32:51 +00004827 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4828 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004829
4830 // Window should receive only the motion event
4831 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4832 window->assertNoEvents(); // Key event or focus event will not be received
4833}
4834
arthurhungea3f4fc2020-12-21 23:18:53 +08004835TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
4836 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4837
arthurhungea3f4fc2020-12-21 23:18:53 +08004838 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004839 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4840 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004841 firstWindow->setFrame(Rect(0, 0, 600, 400));
arthurhungea3f4fc2020-12-21 23:18:53 +08004842
arthurhungea3f4fc2020-12-21 23:18:53 +08004843 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004844 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4845 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004846 secondWindow->setFrame(Rect(0, 400, 600, 800));
arthurhungea3f4fc2020-12-21 23:18:53 +08004847
4848 // Add the windows to the dispatcher
4849 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4850
4851 PointF pointInFirst = {300, 200};
4852 PointF pointInSecond = {300, 600};
4853
4854 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004855 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4856 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4857 {pointInFirst}));
arthurhungea3f4fc2020-12-21 23:18:53 +08004858 // Only the first window should get the down event
4859 firstWindow->consumeMotionDown();
4860 secondWindow->assertNoEvents();
4861
4862 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004863 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4864 ADISPLAY_ID_DEFAULT,
4865 {pointInFirst, pointInSecond}));
arthurhungea3f4fc2020-12-21 23:18:53 +08004866 // The first window gets a move and the second a down
4867 firstWindow->consumeMotionMove();
4868 secondWindow->consumeMotionDown();
4869
4870 // Send pointer cancel to the second window
4871 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004872 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08004873 {pointInFirst, pointInSecond});
4874 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
Prabir Pradhan678438e2023-04-13 19:32:51 +00004875 mDispatcher->notifyMotion(pointerUpMotionArgs);
arthurhungea3f4fc2020-12-21 23:18:53 +08004876 // The first window gets move and the second gets cancel.
4877 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
4878 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
4879
4880 // Send up event.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004881 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4882 ADISPLAY_ID_DEFAULT));
arthurhungea3f4fc2020-12-21 23:18:53 +08004883 // The first window gets up and the second gets nothing.
4884 firstWindow->consumeMotionUp();
4885 secondWindow->assertNoEvents();
4886}
4887
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004888TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
4889 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4890
4891 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004892 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004893 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4894 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
4895 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
4896 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
4897
Harry Cutts33476232023-01-30 19:57:29 +00004898 window->sendTimeline(/*inputEventId=*/1, graphicsTimeline);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004899 window->assertNoEvents();
4900 mDispatcher->waitForIdle();
4901}
4902
chaviwd1c23182019-12-20 18:44:56 -08004903class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00004904public:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004905 FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004906 int32_t displayId) {
Garfield Tan15601662020-09-22 15:32:38 -07004907 base::Result<std::unique_ptr<InputChannel>> channel =
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08004908 dispatcher->createInputMonitor(displayId, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07004909 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00004910 }
4911
chaviwd1c23182019-12-20 18:44:56 -08004912 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
4913
4914 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004915 mInputReceiver->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
4916 expectedFlags);
chaviwd1c23182019-12-20 18:44:56 -08004917 }
4918
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004919 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
4920
4921 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
4922
chaviwd1c23182019-12-20 18:44:56 -08004923 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004924 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
chaviwd1c23182019-12-20 18:44:56 -08004925 expectedDisplayId, expectedFlags);
4926 }
4927
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004928 void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004929 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004930 expectedDisplayId, expectedFlags);
4931 }
4932
chaviwd1c23182019-12-20 18:44:56 -08004933 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004934 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP,
chaviwd1c23182019-12-20 18:44:56 -08004935 expectedDisplayId, expectedFlags);
4936 }
4937
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004938 void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08004939 mInputReceiver->consumeMotionEvent(
4940 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
4941 WithDisplayId(expectedDisplayId),
4942 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004943 }
4944
Arthur Hungfbfa5722021-11-16 02:45:54 +00004945 void consumeMotionPointerDown(int32_t pointerIdx) {
4946 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
4947 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004948 mInputReceiver->consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00004949 /*expectedFlags=*/0);
Arthur Hungfbfa5722021-11-16 02:45:54 +00004950 }
4951
Evan Rosky84f07f02021-04-16 10:42:42 -07004952 MotionEvent* consumeMotion() {
4953 InputEvent* event = mInputReceiver->consume();
4954 if (!event) {
4955 ADD_FAILURE() << "No event was produced";
4956 return nullptr;
4957 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004958 if (event->getType() != InputEventType::MOTION) {
4959 ADD_FAILURE() << "Expected MotionEvent, got " << *event;
Evan Rosky84f07f02021-04-16 10:42:42 -07004960 return nullptr;
4961 }
4962 return static_cast<MotionEvent*>(event);
4963 }
4964
chaviwd1c23182019-12-20 18:44:56 -08004965 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
4966
4967private:
4968 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00004969};
4970
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004971using InputDispatcherMonitorTest = InputDispatcherTest;
4972
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004973/**
4974 * Two entities that receive touch: A window, and a global monitor.
4975 * The touch goes to the window, and then the window disappears.
4976 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
4977 * for the monitor, as well.
4978 * 1. foregroundWindow
4979 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
4980 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004981TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004982 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4983 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004984 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004985
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004986 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004987
4988 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4989 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4990 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4991 {100, 200}))
4992 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4993
4994 // Both the foreground window and the global monitor should receive the touch down
4995 window->consumeMotionDown();
4996 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
4997
4998 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4999 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5000 ADISPLAY_ID_DEFAULT, {110, 200}))
5001 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5002
5003 window->consumeMotionMove();
5004 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
5005
5006 // Now the foreground window goes away
5007 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
5008 window->consumeMotionCancel();
5009 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
5010
5011 // If more events come in, there will be no more foreground window to send them to. This will
5012 // cause a cancel for the monitor, as well.
5013 ASSERT_EQ(InputEventInjectionResult::FAILED,
5014 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5015 ADISPLAY_ID_DEFAULT, {120, 200}))
5016 << "Injection should fail because the window was removed";
5017 window->assertNoEvents();
5018 // Global monitor now gets the cancel
5019 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5020}
5021
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005022TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07005023 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005024 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5025 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005026 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00005027
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005028 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005029
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005030 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00005031 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005032 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00005033 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08005034 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005035}
5036
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005037TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
5038 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005039
Chris Yea209fde2020-07-22 13:54:51 -07005040 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005041 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5042 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005043 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00005044
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005045 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00005046 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005047 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08005048 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005049 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005050
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005051 // Pilfer pointers from the monitor.
5052 // This should not do anything and the window should continue to receive events.
5053 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00005054
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005055 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005056 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5057 ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005058 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005059
5060 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
5061 window->consumeMotionMove(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005062}
5063
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005064TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07005065 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005066 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5067 "Fake Window", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07005068 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5069 window->setWindowOffset(20, 40);
5070 window->setWindowTransform(0, 1, -1, 0);
5071
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005072 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07005073
5074 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5075 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5076 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5077 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5078 MotionEvent* event = monitor.consumeMotion();
5079 // Even though window has transform, gesture monitor must not.
5080 ASSERT_EQ(ui::Transform(), event->getTransform());
5081}
5082
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005083TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00005084 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005085 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00005086
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005087 ASSERT_EQ(InputEventInjectionResult::FAILED,
Arthur Hungb3307ee2021-10-14 10:57:37 +00005088 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005089 << "Injection should fail if there is a monitor, but no touchable window";
5090 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00005091}
5092
chaviw81e2bb92019-12-18 15:03:51 -08005093TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005094 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005095 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5096 "Fake Window", ADISPLAY_ID_DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08005097
Arthur Hung72d8dc32020-03-28 00:48:39 +00005098 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08005099
5100 NotifyMotionArgs motionArgs =
5101 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5102 ADISPLAY_ID_DEFAULT);
5103
Prabir Pradhan678438e2023-04-13 19:32:51 +00005104 mDispatcher->notifyMotion(motionArgs);
chaviw81e2bb92019-12-18 15:03:51 -08005105 // Window should receive motion down event.
5106 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5107
5108 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08005109 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08005110 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5111 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
5112 motionArgs.pointerCoords[0].getX() - 10);
5113
Prabir Pradhan678438e2023-04-13 19:32:51 +00005114 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005115 window->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005116 /*expectedFlags=*/0);
chaviw81e2bb92019-12-18 15:03:51 -08005117}
5118
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005119/**
5120 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
5121 * the device default right away. In the test scenario, we check both the default value,
5122 * and the action of enabling / disabling.
5123 */
5124TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07005125 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005126 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5127 "Test window", ADISPLAY_ID_DEFAULT);
Antonio Kantekea47acb2021-12-23 12:41:25 -08005128 const WindowInfo& windowInfo = *window->getInfo();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005129
5130 // Set focused application.
5131 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005132 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005133
5134 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00005135 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005136 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005137 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005138
5139 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07005140 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005141 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00005142 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005143
5144 SCOPED_TRACE("Disable touch mode");
Antonio Kantekea47acb2021-12-23 12:41:25 -08005145 mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00005146 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005147 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07005148 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005149 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005150 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005151 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005152
5153 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07005154 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005155 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00005156 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005157
5158 SCOPED_TRACE("Enable touch mode again");
Antonio Kantekea47acb2021-12-23 12:41:25 -08005159 mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00005160 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005161 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07005162 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005163 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005164 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005165 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005166
5167 window->assertNoEvents();
5168}
5169
Gang Wange9087892020-01-07 12:17:14 -05005170TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005171 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005172 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5173 "Test window", ADISPLAY_ID_DEFAULT);
Gang Wange9087892020-01-07 12:17:14 -05005174
5175 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005176 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05005177
Arthur Hung72d8dc32020-03-28 00:48:39 +00005178 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005179 setFocusedWindow(window);
5180
Harry Cutts33476232023-01-30 19:57:29 +00005181 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Gang Wange9087892020-01-07 12:17:14 -05005182
Prabir Pradhan678438e2023-04-13 19:32:51 +00005183 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
5184 mDispatcher->notifyKey(keyArgs);
Gang Wange9087892020-01-07 12:17:14 -05005185
5186 InputEvent* event = window->consume();
5187 ASSERT_NE(event, nullptr);
5188
5189 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
5190 ASSERT_NE(verified, nullptr);
5191 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
5192
5193 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
5194 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
5195 ASSERT_EQ(keyArgs.source, verified->source);
5196 ASSERT_EQ(keyArgs.displayId, verified->displayId);
5197
5198 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
5199
5200 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05005201 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005202 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05005203 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
5204 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
5205 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
5206 ASSERT_EQ(0, verifiedKey.repeatCount);
5207}
5208
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005209TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005210 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005211 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5212 "Test window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005213
5214 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5215
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07005216 ui::Transform transform;
5217 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
5218
5219 gui::DisplayInfo displayInfo;
5220 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
5221 displayInfo.transform = transform;
5222
Patrick Williamsd828f302023-04-28 17:52:08 -05005223 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005224
Prabir Pradhan678438e2023-04-13 19:32:51 +00005225 const NotifyMotionArgs motionArgs =
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005226 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5227 ADISPLAY_ID_DEFAULT);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005228 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005229
5230 InputEvent* event = window->consume();
5231 ASSERT_NE(event, nullptr);
5232
5233 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
5234 ASSERT_NE(verified, nullptr);
5235 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
5236
5237 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
5238 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
5239 EXPECT_EQ(motionArgs.source, verified->source);
5240 EXPECT_EQ(motionArgs.displayId, verified->displayId);
5241
5242 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
5243
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07005244 const vec2 rawXY =
5245 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
5246 motionArgs.pointerCoords[0].getXYValue());
5247 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
5248 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005249 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005250 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005251 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005252 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
5253 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
5254}
5255
chaviw09c8d2d2020-08-24 15:48:26 -07005256/**
5257 * Ensure that separate calls to sign the same data are generating the same key.
5258 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
5259 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
5260 * tests.
5261 */
5262TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
5263 KeyEvent event = getTestKeyEvent();
5264 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5265
5266 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
5267 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
5268 ASSERT_EQ(hmac1, hmac2);
5269}
5270
5271/**
5272 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
5273 */
5274TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
5275 KeyEvent event = getTestKeyEvent();
5276 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5277 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
5278
5279 verifiedEvent.deviceId += 1;
5280 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5281
5282 verifiedEvent.source += 1;
5283 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5284
5285 verifiedEvent.eventTimeNanos += 1;
5286 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5287
5288 verifiedEvent.displayId += 1;
5289 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5290
5291 verifiedEvent.action += 1;
5292 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5293
5294 verifiedEvent.downTimeNanos += 1;
5295 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5296
5297 verifiedEvent.flags += 1;
5298 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5299
5300 verifiedEvent.keyCode += 1;
5301 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5302
5303 verifiedEvent.scanCode += 1;
5304 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5305
5306 verifiedEvent.metaState += 1;
5307 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5308
5309 verifiedEvent.repeatCount += 1;
5310 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5311}
5312
Vishnu Nair958da932020-08-21 17:12:37 -07005313TEST_F(InputDispatcherTest, SetFocusedWindow) {
5314 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5315 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005316 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005317 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005318 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005319 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5320
5321 // Top window is also focusable but is not granted focus.
5322 windowTop->setFocusable(true);
5323 windowSecond->setFocusable(true);
5324 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5325 setFocusedWindow(windowSecond);
5326
5327 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005328 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5329 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005330
5331 // Focused window should receive event.
5332 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5333 windowTop->assertNoEvents();
5334}
5335
5336TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
5337 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5338 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005339 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005340 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5341
5342 window->setFocusable(true);
5343 // Release channel for window is no longer valid.
5344 window->releaseChannel();
5345 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5346 setFocusedWindow(window);
5347
5348 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005349 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5350 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005351
5352 // window channel is invalid, so it should not receive any input event.
5353 window->assertNoEvents();
5354}
5355
5356TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
5357 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5358 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005359 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005360 window->setFocusable(false);
Vishnu Nair958da932020-08-21 17:12:37 -07005361 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5362
Vishnu Nair958da932020-08-21 17:12:37 -07005363 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5364 setFocusedWindow(window);
5365
5366 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005367 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5368 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005369
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005370 // window is not focusable, so it should not receive any input event.
Vishnu Nair958da932020-08-21 17:12:37 -07005371 window->assertNoEvents();
5372}
5373
5374TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
5375 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5376 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005377 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005378 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005379 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005380 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5381
5382 windowTop->setFocusable(true);
5383 windowSecond->setFocusable(true);
5384 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5385 setFocusedWindow(windowTop);
5386 windowTop->consumeFocusEvent(true);
5387
Chavi Weingarten847e8512023-03-29 00:26:09 +00005388 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
5389 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005390 windowSecond->consumeFocusEvent(true);
5391 windowTop->consumeFocusEvent(false);
5392
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005393 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5394 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005395
5396 // Focused window should receive event.
5397 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5398}
5399
Chavi Weingarten847e8512023-03-29 00:26:09 +00005400TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) {
Vishnu Nair958da932020-08-21 17:12:37 -07005401 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5402 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005403 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005404 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005405 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005406 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5407
5408 windowTop->setFocusable(true);
Chavi Weingarten847e8512023-03-29 00:26:09 +00005409 windowSecond->setFocusable(false);
5410 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
Vishnu Nair958da932020-08-21 17:12:37 -07005411 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Chavi Weingarten847e8512023-03-29 00:26:09 +00005412 setFocusedWindow(windowTop);
5413 windowTop->consumeFocusEvent(true);
Vishnu Nair958da932020-08-21 17:12:37 -07005414
Chavi Weingarten847e8512023-03-29 00:26:09 +00005415 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5416 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005417
5418 // Event should be dropped.
Chavi Weingarten847e8512023-03-29 00:26:09 +00005419 windowTop->consumeKeyDown(ADISPLAY_ID_NONE);
Vishnu Nair958da932020-08-21 17:12:37 -07005420 windowSecond->assertNoEvents();
5421}
5422
5423TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
5424 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5425 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005426 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005427 sp<FakeWindowHandle> previousFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005428 sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow",
5429 ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005430 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5431
5432 window->setFocusable(true);
5433 previousFocusedWindow->setFocusable(true);
5434 window->setVisible(false);
5435 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
5436 setFocusedWindow(previousFocusedWindow);
5437 previousFocusedWindow->consumeFocusEvent(true);
5438
5439 // Requesting focus on invisible window takes focus from currently focused window.
5440 setFocusedWindow(window);
5441 previousFocusedWindow->consumeFocusEvent(false);
5442
5443 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005444 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005445 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
5446 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07005447
5448 // Window does not get focus event or key down.
5449 window->assertNoEvents();
5450
5451 // Window becomes visible.
5452 window->setVisible(true);
5453 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5454
5455 // Window receives focus event.
5456 window->consumeFocusEvent(true);
5457 // Focused window receives key down.
5458 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5459}
5460
Vishnu Nair599f1412021-06-21 10:39:58 -07005461TEST_F(InputDispatcherTest, DisplayRemoved) {
5462 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5463 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005464 sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
Vishnu Nair599f1412021-06-21 10:39:58 -07005465 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5466
5467 // window is granted focus.
5468 window->setFocusable(true);
5469 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5470 setFocusedWindow(window);
5471 window->consumeFocusEvent(true);
5472
5473 // When a display is removed window loses focus.
5474 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
5475 window->consumeFocusEvent(false);
5476}
5477
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005478/**
5479 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
5480 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
5481 * of the 'slipperyEnterWindow'.
5482 *
5483 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
5484 * a way so that the touched location is no longer covered by the top window.
5485 *
5486 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
5487 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
5488 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
5489 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
5490 * with ACTION_DOWN).
5491 * Thus, the touch has been transferred from the top window into the bottom window, because the top
5492 * window moved itself away from the touched location and had Flag::SLIPPERY.
5493 *
5494 * Even though the top window moved away from the touched location, it is still obscuring the bottom
5495 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
5496 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
5497 *
5498 * In this test, we ensure that the event received by the bottom window has
5499 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
5500 */
5501TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
Prabir Pradhan5735a322022-04-11 17:23:34 +00005502 constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00005503 constexpr gui::Uid SLIPPERY_UID{WINDOW_UID.val() + 1};
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005504
5505 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5506 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5507
5508 sp<FakeWindowHandle> slipperyExitWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005509 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005510 slipperyExitWindow->setSlippery(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005511 // Make sure this one overlaps the bottom window
5512 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
5513 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
5514 // one. Windows with the same owner are not considered to be occluding each other.
5515 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
5516
5517 sp<FakeWindowHandle> slipperyEnterWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005518 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005519 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
5520
5521 mDispatcher->setInputWindows(
5522 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5523
5524 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
Prabir Pradhan678438e2023-04-13 19:32:51 +00005525 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5526 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5527 {{50, 50}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005528 slipperyExitWindow->consumeMotionDown();
5529 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
5530 mDispatcher->setInputWindows(
5531 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5532
Prabir Pradhan678438e2023-04-13 19:32:51 +00005533 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE,
5534 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5535 {{51, 51}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005536
5537 slipperyExitWindow->consumeMotionCancel();
5538
5539 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
5540 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
5541}
5542
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07005543/**
5544 * Two windows, one on the left and another on the right. The left window is slippery. The right
5545 * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the
5546 * touch moves from the left window into the right window, the gesture should continue to go to the
5547 * left window. Touch shouldn't slip because the right window can't receive touches. This test
5548 * reproduces a crash.
5549 */
5550TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) {
5551 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5552
5553 sp<FakeWindowHandle> leftSlipperyWindow =
5554 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
5555 leftSlipperyWindow->setSlippery(true);
5556 leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100));
5557
5558 sp<FakeWindowHandle> rightDropTouchesWindow =
5559 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
5560 rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100));
5561 rightDropTouchesWindow->setDropInput(true);
5562
5563 mDispatcher->setInputWindows(
5564 {{ADISPLAY_ID_DEFAULT, {leftSlipperyWindow, rightDropTouchesWindow}}});
5565
5566 // Start touch in the left window
5567 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5568 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
5569 .build());
5570 leftSlipperyWindow->consumeMotionDown();
5571
5572 // And move it into the right window
5573 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
5574 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
5575 .build());
5576
5577 // Since the right window isn't eligible to receive input, touch does not slip.
5578 // The left window continues to receive the gesture.
5579 leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
5580 rightDropTouchesWindow->assertNoEvents();
5581}
5582
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005583TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithMotions) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00005584 using Uid = gui::Uid;
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005585 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5586
5587 sp<FakeWindowHandle> leftWindow =
5588 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
5589 leftWindow->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00005590 leftWindow->setOwnerInfo(1, Uid{101});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005591
5592 sp<FakeWindowHandle> rightSpy =
5593 sp<FakeWindowHandle>::make(application, mDispatcher, "Right spy", ADISPLAY_ID_DEFAULT);
5594 rightSpy->setFrame(Rect(100, 0, 200, 100));
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00005595 rightSpy->setOwnerInfo(2, Uid{102});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005596 rightSpy->setSpy(true);
5597 rightSpy->setTrustedOverlay(true);
5598
5599 sp<FakeWindowHandle> rightWindow =
5600 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
5601 rightWindow->setFrame(Rect(100, 0, 200, 100));
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00005602 rightWindow->setOwnerInfo(3, Uid{103});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005603
5604 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightSpy, rightWindow, leftWindow}}});
5605
5606 // Touch in the left window
5607 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5608 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
5609 .build());
5610 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionDown());
5611 mDispatcher->waitForIdle();
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00005612 ASSERT_NO_FATAL_FAILURE(
5613 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{101}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005614
5615 // Touch another finger over the right windows
5616 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5617 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
5618 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
5619 .build());
5620 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionDown());
5621 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionDown());
5622 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionMove());
5623 mDispatcher->waitForIdle();
5624 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00005625 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID,
5626 {Uid{101}, Uid{102}, Uid{103}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005627
5628 // Release finger over left window. The UP actions are not treated as device interaction.
5629 // The windows that did not receive the UP pointer will receive MOVE events, but since this
5630 // is part of the UP action, we do not treat this as device interaction.
5631 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
5632 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
5633 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
5634 .build());
5635 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionUp());
5636 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove());
5637 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove());
5638 mDispatcher->waitForIdle();
5639 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
5640
5641 // Move remaining finger
5642 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
5643 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
5644 .build());
5645 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove());
5646 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove());
5647 mDispatcher->waitForIdle();
5648 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00005649 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{102}, Uid{103}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005650
5651 // Release all fingers
5652 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
5653 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
5654 .build());
5655 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionUp());
5656 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionUp());
5657 mDispatcher->waitForIdle();
5658 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
5659}
5660
5661TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithKeys) {
5662 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5663
5664 sp<FakeWindowHandle> window =
5665 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
5666 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00005667 window->setOwnerInfo(1, gui::Uid{101});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005668
5669 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5670 setFocusedWindow(window);
5671 ASSERT_NO_FATAL_FAILURE(window->consumeFocusEvent(true));
5672
5673 mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).build());
5674 ASSERT_NO_FATAL_FAILURE(window->consumeKeyDown(ADISPLAY_ID_DEFAULT));
5675 mDispatcher->waitForIdle();
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00005676 ASSERT_NO_FATAL_FAILURE(
5677 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {gui::Uid{101}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005678
5679 // The UP actions are not treated as device interaction.
5680 mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).build());
5681 ASSERT_NO_FATAL_FAILURE(window->consumeKeyUp(ADISPLAY_ID_DEFAULT));
5682 mDispatcher->waitForIdle();
5683 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
5684}
5685
Garfield Tan1c7bc862020-01-28 13:24:04 -08005686class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
5687protected:
5688 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
5689 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
5690
Chris Yea209fde2020-07-22 13:54:51 -07005691 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005692 sp<FakeWindowHandle> mWindow;
5693
5694 virtual void SetUp() override {
Prabir Pradhana41d2442023-04-20 21:30:40 +00005695 mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>();
Prabir Pradhana41d2442023-04-20 21:30:40 +00005696 mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005697 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
Nergi Rahardi730cf3c2023-04-13 12:41:17 +09005698 mDispatcher->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005699 ASSERT_EQ(OK, mDispatcher->start());
5700
5701 setUpWindow();
5702 }
5703
5704 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07005705 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005706 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005707
Vishnu Nair47074b82020-08-14 11:54:47 -07005708 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005709 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005710 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005711 mWindow->consumeFocusEvent(true);
5712 }
5713
Chris Ye2ad95392020-09-01 13:44:44 -07005714 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005715 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005716 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005717 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005718 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005719
5720 // Window should receive key down event.
5721 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5722 }
5723
5724 void expectKeyRepeatOnce(int32_t repeatCount) {
5725 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
5726 InputEvent* repeatEvent = mWindow->consume();
5727 ASSERT_NE(nullptr, repeatEvent);
5728
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005729 ASSERT_EQ(InputEventType::KEY, repeatEvent->getType());
Garfield Tan1c7bc862020-01-28 13:24:04 -08005730
5731 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
5732 uint32_t eventAction = repeatKeyEvent->getAction();
5733 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
5734 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
5735 }
5736
Chris Ye2ad95392020-09-01 13:44:44 -07005737 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005738 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005739 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005740 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005741 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005742
5743 // Window should receive key down event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005744 mWindow->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005745 /*expectedFlags=*/0);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005746 }
5747};
5748
5749TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Harry Cutts33476232023-01-30 19:57:29 +00005750 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005751 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5752 expectKeyRepeatOnce(repeatCount);
5753 }
5754}
5755
5756TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
Harry Cutts33476232023-01-30 19:57:29 +00005757 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005758 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5759 expectKeyRepeatOnce(repeatCount);
5760 }
Harry Cutts33476232023-01-30 19:57:29 +00005761 sendAndConsumeKeyDown(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005762 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08005763 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5764 expectKeyRepeatOnce(repeatCount);
5765 }
5766}
5767
5768TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005769 sendAndConsumeKeyDown(/*deviceId=*/1);
5770 expectKeyRepeatOnce(/*repeatCount=*/1);
5771 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005772 mWindow->assertNoEvents();
5773}
5774
5775TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005776 sendAndConsumeKeyDown(/*deviceId=*/1);
5777 expectKeyRepeatOnce(/*repeatCount=*/1);
5778 sendAndConsumeKeyDown(/*deviceId=*/2);
5779 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005780 // Stale key up from device 1.
Harry Cutts33476232023-01-30 19:57:29 +00005781 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005782 // Device 2 is still down, keep repeating
Harry Cutts33476232023-01-30 19:57:29 +00005783 expectKeyRepeatOnce(/*repeatCount=*/2);
5784 expectKeyRepeatOnce(/*repeatCount=*/3);
Chris Ye2ad95392020-09-01 13:44:44 -07005785 // Device 2 key up
Harry Cutts33476232023-01-30 19:57:29 +00005786 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005787 mWindow->assertNoEvents();
5788}
5789
5790TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005791 sendAndConsumeKeyDown(/*deviceId=*/1);
5792 expectKeyRepeatOnce(/*repeatCount=*/1);
5793 sendAndConsumeKeyDown(/*deviceId=*/2);
5794 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005795 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
Harry Cutts33476232023-01-30 19:57:29 +00005796 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005797 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08005798 mWindow->assertNoEvents();
5799}
5800
liushenxiang42232912021-05-21 20:24:09 +08005801TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
5802 sendAndConsumeKeyDown(DEVICE_ID);
Harry Cutts33476232023-01-30 19:57:29 +00005803 expectKeyRepeatOnce(/*repeatCount=*/1);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005804 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
liushenxiang42232912021-05-21 20:24:09 +08005805 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
5806 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
5807 mWindow->assertNoEvents();
5808}
5809
Garfield Tan1c7bc862020-01-28 13:24:04 -08005810TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005811 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005812 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005813 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5814 InputEvent* repeatEvent = mWindow->consume();
5815 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5816 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
5817 IdGenerator::getSource(repeatEvent->getId()));
5818 }
5819}
5820
5821TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005822 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005823 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005824
5825 std::unordered_set<int32_t> idSet;
5826 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5827 InputEvent* repeatEvent = mWindow->consume();
5828 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5829 int32_t id = repeatEvent->getId();
5830 EXPECT_EQ(idSet.end(), idSet.find(id));
5831 idSet.insert(id);
5832 }
5833}
5834
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005835/* Test InputDispatcher for MultiDisplay */
5836class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
5837public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005838 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005839 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08005840
Chris Yea209fde2020-07-22 13:54:51 -07005841 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005842 windowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005843 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005844
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005845 // Set focus window for primary display, but focused display would be second one.
5846 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07005847 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005848 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005849 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005850 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08005851
Chris Yea209fde2020-07-22 13:54:51 -07005852 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005853 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005854 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005855 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005856 // Set focus display to second one.
5857 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
5858 // Set focus window for second display.
5859 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07005860 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005861 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005862 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005863 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005864 }
5865
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005866 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005867 InputDispatcherTest::TearDown();
5868
Chris Yea209fde2020-07-22 13:54:51 -07005869 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005870 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07005871 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005872 windowInSecondary.clear();
5873 }
5874
5875protected:
Chris Yea209fde2020-07-22 13:54:51 -07005876 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005877 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07005878 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005879 sp<FakeWindowHandle> windowInSecondary;
5880};
5881
5882TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
5883 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005884 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5885 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5886 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005887 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08005888 windowInSecondary->assertNoEvents();
5889
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005890 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005891 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5892 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5893 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005894 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005895 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08005896}
5897
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005898TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08005899 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005900 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5901 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005902 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005903 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08005904 windowInSecondary->assertNoEvents();
5905
5906 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005907 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005908 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005909 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005910 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08005911
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005912 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00005913 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08005914
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005915 // Old focus should receive a cancel event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005916 windowInSecondary->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005917 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08005918
5919 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005920 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005921 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08005922 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005923 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08005924 windowInSecondary->assertNoEvents();
5925}
5926
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005927// Test per-display input monitors for motion event.
5928TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08005929 FakeMonitorReceiver monitorInPrimary =
5930 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5931 FakeMonitorReceiver monitorInSecondary =
5932 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005933
5934 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005935 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5936 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5937 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005938 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08005939 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005940 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005941 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005942
5943 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005944 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5945 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5946 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005947 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005948 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005949 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08005950 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005951
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08005952 // Lift up the touch from the second display
5953 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5954 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5955 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5956 windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID);
5957 monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID);
5958
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005959 // Test inject a non-pointer motion event.
5960 // If specific a display, it will dispatch to the focused window of particular display,
5961 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005962 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5963 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
5964 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005965 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005966 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005967 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08005968 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005969}
5970
5971// Test per-display input monitors for key event.
5972TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005973 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08005974 FakeMonitorReceiver monitorInPrimary =
5975 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5976 FakeMonitorReceiver monitorInSecondary =
5977 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005978
5979 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005980 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5981 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005982 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005983 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005984 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08005985 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005986}
5987
Vishnu Nair958da932020-08-21 17:12:37 -07005988TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
5989 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005990 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005991 secondWindowInPrimary->setFocusable(true);
5992 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
5993 setFocusedWindow(secondWindowInPrimary);
5994 windowInPrimary->consumeFocusEvent(false);
5995 secondWindowInPrimary->consumeFocusEvent(true);
5996
5997 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005998 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
5999 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006000 windowInPrimary->assertNoEvents();
6001 windowInSecondary->assertNoEvents();
6002 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6003}
6004
Arthur Hungdfd528e2021-12-08 13:23:04 +00006005TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
6006 FakeMonitorReceiver monitorInPrimary =
6007 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6008 FakeMonitorReceiver monitorInSecondary =
6009 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
6010
6011 // Test touch down on primary display.
6012 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6013 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
6014 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6015 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6016 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
6017
6018 // Test touch down on second display.
6019 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6020 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
6021 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6022 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
6023 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
6024
6025 // Trigger cancel touch.
6026 mDispatcher->cancelCurrentTouch();
6027 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6028 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6029 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
6030 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
6031
6032 // Test inject a move motion event, no window/monitor should receive the event.
6033 ASSERT_EQ(InputEventInjectionResult::FAILED,
6034 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6035 ADISPLAY_ID_DEFAULT, {110, 200}))
6036 << "Inject motion event should return InputEventInjectionResult::FAILED";
6037 windowInPrimary->assertNoEvents();
6038 monitorInPrimary.assertNoEvents();
6039
6040 ASSERT_EQ(InputEventInjectionResult::FAILED,
6041 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6042 SECOND_DISPLAY_ID, {110, 200}))
6043 << "Inject motion event should return InputEventInjectionResult::FAILED";
6044 windowInSecondary->assertNoEvents();
6045 monitorInSecondary.assertNoEvents();
6046}
6047
Jackal Guof9696682018-10-05 12:23:23 +08006048class InputFilterTest : public InputDispatcherTest {
6049protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006050 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
6051 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08006052 NotifyMotionArgs motionArgs;
6053
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006054 motionArgs =
6055 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006056 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006057 motionArgs =
6058 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006059 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006060 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08006061 if (expectToBeFiltered) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006062 const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue());
6063 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08006064 } else {
6065 mFakePolicy->assertFilterInputEventWasNotCalled();
6066 }
6067 }
6068
6069 void testNotifyKey(bool expectToBeFiltered) {
6070 NotifyKeyArgs keyArgs;
6071
6072 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006073 mDispatcher->notifyKey(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08006074 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006075 mDispatcher->notifyKey(keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006076 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08006077
6078 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08006079 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08006080 } else {
6081 mFakePolicy->assertFilterInputEventWasNotCalled();
6082 }
6083 }
6084};
6085
6086// Test InputFilter for MotionEvent
6087TEST_F(InputFilterTest, MotionEvent_InputFilter) {
6088 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
6089 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
6090 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
6091
6092 // Enable InputFilter
6093 mDispatcher->setInputFilterEnabled(true);
6094 // Test touch on both primary and second display, and check if both events are filtered.
6095 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
6096 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
6097
6098 // Disable InputFilter
6099 mDispatcher->setInputFilterEnabled(false);
6100 // Test touch on both primary and second display, and check if both events aren't filtered.
6101 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
6102 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
6103}
6104
6105// Test InputFilter for KeyEvent
6106TEST_F(InputFilterTest, KeyEvent_InputFilter) {
6107 // Since the InputFilter is disabled by default, check if key event aren't filtered.
6108 testNotifyKey(/*expectToBeFiltered*/ false);
6109
6110 // Enable InputFilter
6111 mDispatcher->setInputFilterEnabled(true);
6112 // Send a key event, and check if it is filtered.
6113 testNotifyKey(/*expectToBeFiltered*/ true);
6114
6115 // Disable InputFilter
6116 mDispatcher->setInputFilterEnabled(false);
6117 // Send a key event, and check if it isn't filtered.
6118 testNotifyKey(/*expectToBeFiltered*/ false);
6119}
6120
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006121// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
6122// logical display coordinate space.
6123TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
6124 ui::Transform firstDisplayTransform;
6125 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
6126 ui::Transform secondDisplayTransform;
6127 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
6128
6129 std::vector<gui::DisplayInfo> displayInfos(2);
6130 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
6131 displayInfos[0].transform = firstDisplayTransform;
6132 displayInfos[1].displayId = SECOND_DISPLAY_ID;
6133 displayInfos[1].transform = secondDisplayTransform;
6134
Patrick Williamsd828f302023-04-28 17:52:08 -05006135 mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0});
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006136
6137 // Enable InputFilter
6138 mDispatcher->setInputFilterEnabled(true);
6139
6140 // Ensure the correct transforms are used for the displays.
6141 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform);
6142 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform);
6143}
6144
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006145class InputFilterInjectionPolicyTest : public InputDispatcherTest {
6146protected:
6147 virtual void SetUp() override {
6148 InputDispatcherTest::SetUp();
6149
6150 /**
6151 * We don't need to enable input filter to test the injected event policy, but we enabled it
6152 * here to make the tests more realistic, since this policy only matters when inputfilter is
6153 * on.
6154 */
6155 mDispatcher->setInputFilterEnabled(true);
6156
6157 std::shared_ptr<InputApplicationHandle> application =
6158 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006159 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
6160 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006161
6162 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6163 mWindow->setFocusable(true);
6164 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6165 setFocusedWindow(mWindow);
6166 mWindow->consumeFocusEvent(true);
6167 }
6168
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006169 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
6170 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006171 KeyEvent event;
6172
6173 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
6174 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
6175 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
Harry Cutts33476232023-01-30 19:57:29 +00006176 KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006177 const int32_t additionalPolicyFlags =
6178 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
6179 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006180 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006181 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
6182 policyFlags | additionalPolicyFlags));
6183
6184 InputEvent* received = mWindow->consume();
6185 ASSERT_NE(nullptr, received);
6186 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006187 ASSERT_EQ(received->getType(), InputEventType::KEY);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006188 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
6189 ASSERT_EQ(flags, keyEvent.getFlags());
6190 }
6191
6192 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
6193 int32_t flags) {
6194 MotionEvent event;
6195 PointerProperties pointerProperties[1];
6196 PointerCoords pointerCoords[1];
6197 pointerProperties[0].clear();
6198 pointerProperties[0].id = 0;
6199 pointerCoords[0].clear();
6200 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
6201 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
6202
6203 ui::Transform identityTransform;
6204 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
6205 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
6206 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
6207 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
6208 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07006209 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07006210 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006211 /*pointerCount*/ 1, pointerProperties, pointerCoords);
6212
6213 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
6214 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006215 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006216 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
6217 policyFlags | additionalPolicyFlags));
6218
6219 InputEvent* received = mWindow->consume();
6220 ASSERT_NE(nullptr, received);
6221 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006222 ASSERT_EQ(received->getType(), InputEventType::MOTION);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006223 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
6224 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006225 }
6226
6227private:
6228 sp<FakeWindowHandle> mWindow;
6229};
6230
6231TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006232 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
6233 // filter. Without it, the event will no different from a regularly injected event, and the
6234 // injected device id will be overwritten.
Harry Cutts33476232023-01-30 19:57:29 +00006235 testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
6236 /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006237}
6238
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006239TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006240 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00006241 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006242 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
6243}
6244
6245TEST_F(InputFilterInjectionPolicyTest,
6246 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
6247 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00006248 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006249 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006250}
6251
6252TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
Harry Cutts33476232023-01-30 19:57:29 +00006253 testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3,
6254 /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006255}
6256
chaviwfd6d3512019-03-25 13:23:49 -07006257class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006258 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07006259 InputDispatcherTest::SetUp();
6260
Chris Yea209fde2020-07-22 13:54:51 -07006261 std::shared_ptr<FakeApplicationHandle> application =
6262 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006263 mUnfocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006264 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07006265 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07006266
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006267 mFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006268 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006269 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07006270
6271 // Set focused application.
6272 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07006273 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07006274
6275 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00006276 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006277 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006278 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07006279 }
6280
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006281 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07006282 InputDispatcherTest::TearDown();
6283
6284 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006285 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07006286 }
6287
6288protected:
6289 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006290 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006291 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07006292};
6293
6294// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
6295// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
6296// the onPointerDownOutsideFocus callback.
6297TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006298 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006299 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6300 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006301 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006302 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006303
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006304 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07006305 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
6306}
6307
6308// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
6309// DOWN on the window that doesn't have focus. Ensure no window received the
6310// onPointerDownOutsideFocus callback.
6311TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006312 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006313 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006314 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006315 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006316
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006317 ASSERT_TRUE(mDispatcher->waitForIdle());
6318 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006319}
6320
6321// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
6322// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
6323TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006324 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6325 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006326 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006327 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07006328
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006329 ASSERT_TRUE(mDispatcher->waitForIdle());
6330 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006331}
6332
6333// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
6334// DOWN on the window that already has focus. Ensure no window received the
6335// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006336TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006337 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006338 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006339 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006340 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006341 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006342
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006343 ASSERT_TRUE(mDispatcher->waitForIdle());
6344 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006345}
6346
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006347// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
6348// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
6349TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
6350 const MotionEvent event =
6351 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
6352 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006353 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006354 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
6355 .build();
6356 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
6357 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6358 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
6359
6360 ASSERT_TRUE(mDispatcher->waitForIdle());
6361 mFakePolicy->assertOnPointerDownWasNotCalled();
6362 // Ensure that the unfocused window did not receive any FOCUS events.
6363 mUnfocusedWindow->assertNoEvents();
6364}
6365
chaviwaf87b3e2019-10-01 16:59:28 -07006366// These tests ensures we can send touch events to a single client when there are multiple input
6367// windows that point to the same client token.
6368class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
6369 virtual void SetUp() override {
6370 InputDispatcherTest::SetUp();
6371
Chris Yea209fde2020-07-22 13:54:51 -07006372 std::shared_ptr<FakeApplicationHandle> application =
6373 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006374 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
6375 ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07006376 mWindow1->setFrame(Rect(0, 0, 100, 100));
6377
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006378 mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2",
6379 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviwaf87b3e2019-10-01 16:59:28 -07006380 mWindow2->setFrame(Rect(100, 100, 200, 200));
6381
Arthur Hung72d8dc32020-03-28 00:48:39 +00006382 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07006383 }
6384
6385protected:
6386 sp<FakeWindowHandle> mWindow1;
6387 sp<FakeWindowHandle> mWindow2;
6388
6389 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05006390 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07006391 vec2 vals = windowInfo->transform.transform(point.x, point.y);
6392 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07006393 }
6394
6395 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
6396 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006397 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07006398 InputEvent* event = window->consume();
6399
6400 ASSERT_NE(nullptr, event) << name.c_str()
6401 << ": consumer should have returned non-NULL event.";
6402
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006403 ASSERT_EQ(InputEventType::MOTION, event->getType())
6404 << name.c_str() << ": expected MotionEvent, got " << *event;
chaviwaf87b3e2019-10-01 16:59:28 -07006405
6406 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006407 assertMotionAction(expectedAction, motionEvent.getAction());
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08006408 ASSERT_EQ(points.size(), motionEvent.getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07006409
6410 for (size_t i = 0; i < points.size(); i++) {
6411 float expectedX = points[i].x;
6412 float expectedY = points[i].y;
6413
6414 EXPECT_EQ(expectedX, motionEvent.getX(i))
6415 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
6416 << ", got " << motionEvent.getX(i);
6417 EXPECT_EQ(expectedY, motionEvent.getY(i))
6418 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
6419 << ", got " << motionEvent.getY(i);
6420 }
6421 }
chaviw9eaa22c2020-07-01 16:21:27 -07006422
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08006423 void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07006424 std::vector<PointF> expectedPoints) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00006425 mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
6426 ADISPLAY_ID_DEFAULT, touchedPoints));
chaviw9eaa22c2020-07-01 16:21:27 -07006427
6428 // Always consume from window1 since it's the window that has the InputReceiver
6429 consumeMotionEvent(mWindow1, action, expectedPoints);
6430 }
chaviwaf87b3e2019-10-01 16:59:28 -07006431};
6432
6433TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
6434 // Touch Window 1
6435 PointF touchedPoint = {10, 10};
6436 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006437 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006438
6439 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006440 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006441
6442 // Touch Window 2
6443 touchedPoint = {150, 150};
6444 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006445 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006446}
6447
chaviw9eaa22c2020-07-01 16:21:27 -07006448TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
6449 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07006450 mWindow2->setWindowScale(0.5f, 0.5f);
6451
6452 // Touch Window 1
6453 PointF touchedPoint = {10, 10};
6454 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006455 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006456 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006457 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006458
6459 // Touch Window 2
6460 touchedPoint = {150, 150};
6461 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006462 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
6463 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006464
chaviw9eaa22c2020-07-01 16:21:27 -07006465 // Update the transform so rotation is set
6466 mWindow2->setWindowTransform(0, -1, 1, 0);
6467 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
6468 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006469}
6470
chaviw9eaa22c2020-07-01 16:21:27 -07006471TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006472 mWindow2->setWindowScale(0.5f, 0.5f);
6473
6474 // Touch Window 1
6475 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6476 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006477 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006478
6479 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006480 touchedPoints.push_back(PointF{150, 150});
6481 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006482 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006483
chaviw9eaa22c2020-07-01 16:21:27 -07006484 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006485 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006486 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006487
chaviw9eaa22c2020-07-01 16:21:27 -07006488 // Update the transform so rotation is set for Window 2
6489 mWindow2->setWindowTransform(0, -1, 1, 0);
6490 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006491 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006492}
6493
chaviw9eaa22c2020-07-01 16:21:27 -07006494TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006495 mWindow2->setWindowScale(0.5f, 0.5f);
6496
6497 // Touch Window 1
6498 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6499 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006500 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006501
6502 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006503 touchedPoints.push_back(PointF{150, 150});
6504 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006505
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006506 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006507
6508 // Move both windows
6509 touchedPoints = {{20, 20}, {175, 175}};
6510 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6511 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6512
chaviw9eaa22c2020-07-01 16:21:27 -07006513 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006514
chaviw9eaa22c2020-07-01 16:21:27 -07006515 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006516 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006517 expectedPoints.pop_back();
6518
6519 // Touch Window 2
6520 mWindow2->setWindowTransform(0, -1, 1, 0);
6521 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006522 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006523
6524 // Move both windows
6525 touchedPoints = {{20, 20}, {175, 175}};
6526 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6527 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6528
6529 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006530}
6531
6532TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
6533 mWindow1->setWindowScale(0.5f, 0.5f);
6534
6535 // Touch Window 1
6536 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6537 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006538 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006539
6540 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006541 touchedPoints.push_back(PointF{150, 150});
6542 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006543
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006544 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006545
6546 // Move both windows
6547 touchedPoints = {{20, 20}, {175, 175}};
6548 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6549 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6550
chaviw9eaa22c2020-07-01 16:21:27 -07006551 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006552}
6553
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07006554/**
6555 * When one of the windows is slippery, the touch should not slip into the other window with the
6556 * same input channel.
6557 */
6558TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) {
6559 mWindow1->setSlippery(true);
6560 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
6561
6562 // Touch down in window 1
6563 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6564 ADISPLAY_ID_DEFAULT, {{50, 50}}));
6565 consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}});
6566
6567 // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip.
6568 // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN
6569 // getting generated.
6570 mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6571 ADISPLAY_ID_DEFAULT, {{150, 150}}));
6572
6573 consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}});
6574}
6575
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07006576/**
6577 * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and
6578 * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window
6579 * that the pointer is hovering over may have a different transform.
6580 */
6581TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) {
6582 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
6583
6584 // Start hover in window 1
6585 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN,
6586 ADISPLAY_ID_DEFAULT, {{50, 50}}));
6587 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
6588 {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})});
6589
6590 // Move hover to window 2.
6591 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6592 ADISPLAY_ID_DEFAULT, {{150, 150}}));
6593
6594 consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}});
6595 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
6596 {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})});
6597}
6598
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006599class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
6600 virtual void SetUp() override {
6601 InputDispatcherTest::SetUp();
6602
Chris Yea209fde2020-07-22 13:54:51 -07006603 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006604 mApplication->setDispatchingTimeout(20ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006605 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
6606 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006607 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05006608 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07006609 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006610
6611 // Set focused application.
6612 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
6613
6614 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006615 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006616 mWindow->consumeFocusEvent(true);
6617 }
6618
6619 virtual void TearDown() override {
6620 InputDispatcherTest::TearDown();
6621 mWindow.clear();
6622 }
6623
6624protected:
Chris Yea209fde2020-07-22 13:54:51 -07006625 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006626 sp<FakeWindowHandle> mWindow;
6627 static constexpr PointF WINDOW_LOCATION = {20, 20};
6628
6629 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006630 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006631 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6632 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006633 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006634 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6635 WINDOW_LOCATION));
6636 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006637
6638 sp<FakeWindowHandle> addSpyWindow() {
6639 sp<FakeWindowHandle> spy =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006640 sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006641 spy->setTrustedOverlay(true);
6642 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006643 spy->setSpy(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006644 spy->setDispatchingTimeout(30ms);
6645 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}});
6646 return spy;
6647 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006648};
6649
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006650// Send a tap and respond, which should not cause an ANR.
6651TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
6652 tapOnWindow();
6653 mWindow->consumeMotionDown();
6654 mWindow->consumeMotionUp();
6655 ASSERT_TRUE(mDispatcher->waitForIdle());
6656 mFakePolicy->assertNotifyAnrWasNotCalled();
6657}
6658
6659// Send a regular key and respond, which should not cause an ANR.
6660TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006661 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006662 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
6663 ASSERT_TRUE(mDispatcher->waitForIdle());
6664 mFakePolicy->assertNotifyAnrWasNotCalled();
6665}
6666
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006667TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
6668 mWindow->setFocusable(false);
6669 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6670 mWindow->consumeFocusEvent(false);
6671
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006672 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006673 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6674 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
6675 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006676 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006677 // Key will not go to window because we have no focused window.
6678 // The 'no focused window' ANR timer should start instead.
6679
6680 // Now, the focused application goes away.
6681 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
6682 // The key should get dropped and there should be no ANR.
6683
6684 ASSERT_TRUE(mDispatcher->waitForIdle());
6685 mFakePolicy->assertNotifyAnrWasNotCalled();
6686}
6687
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006688// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006689// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
6690// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006691TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006692 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006693 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6694 WINDOW_LOCATION));
6695
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006696 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
6697 ASSERT_TRUE(sequenceNum);
6698 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006699 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006700
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006701 mWindow->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006702 mWindow->consumeMotionEvent(
6703 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006704 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006705 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006706}
6707
6708// Send a key to the app and have the app not respond right away.
6709TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
6710 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006711 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006712 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
6713 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006714 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006715 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006716 ASSERT_TRUE(mDispatcher->waitForIdle());
6717}
6718
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006719// We have a focused application, but no focused window
6720TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006721 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006722 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6723 mWindow->consumeFocusEvent(false);
6724
6725 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006726 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006727 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6728 WINDOW_LOCATION));
6729 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
6730 mDispatcher->waitForIdle();
6731 mFakePolicy->assertNotifyAnrWasNotCalled();
6732
6733 // Once a focused event arrives, we get an ANR for this application
6734 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6735 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006736 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006737 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6738 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006739 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006740 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07006741 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006742 ASSERT_TRUE(mDispatcher->waitForIdle());
6743}
6744
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006745/**
6746 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
6747 * there will not be an ANR.
6748 */
6749TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
6750 mWindow->setFocusable(false);
6751 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6752 mWindow->consumeFocusEvent(false);
6753
6754 KeyEvent event;
6755 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
6756 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
6757
6758 // Define a valid key down event that is stale (too old).
6759 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
6760 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A,
Harry Cutts33476232023-01-30 19:57:29 +00006761 AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006762
6763 const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
6764
6765 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006766 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006767 InputEventInjectionSync::WAIT_FOR_RESULT,
6768 INJECT_EVENT_TIMEOUT, policyFlags);
6769 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
6770 << "Injection should fail because the event is stale";
6771
6772 ASSERT_TRUE(mDispatcher->waitForIdle());
6773 mFakePolicy->assertNotifyAnrWasNotCalled();
6774 mWindow->assertNoEvents();
6775}
6776
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006777// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006778// Make sure that we don't notify policy twice about the same ANR.
6779TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006780 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006781 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6782 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006783
6784 // Once a focused event arrives, we get an ANR for this application
6785 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6786 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006787 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006788 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6789 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006790 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Vishnu Naire4df8752022-09-08 09:17:55 -07006791 const std::chrono::duration appTimeout =
6792 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6793 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006794
Vishnu Naire4df8752022-09-08 09:17:55 -07006795 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006796 // ANR should not be raised again. It is up to policy to do that if it desires.
6797 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006798
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006799 // If we now get a focused window, the ANR should stop, but the policy handles that via
6800 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006801 ASSERT_TRUE(mDispatcher->waitForIdle());
6802}
6803
6804// We have a focused application, but no focused window
6805TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006806 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006807 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6808 mWindow->consumeFocusEvent(false);
6809
6810 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006811 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006812 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006813 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
6814 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006815
Vishnu Naire4df8752022-09-08 09:17:55 -07006816 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6817 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006818
6819 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006820 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006821 ASSERT_TRUE(mDispatcher->waitForIdle());
6822 mWindow->assertNoEvents();
6823}
6824
6825/**
6826 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
6827 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
6828 * If we process 1 of the events, but ANR on the second event with the same timestamp,
6829 * the ANR mechanism should still work.
6830 *
6831 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
6832 * DOWN event, while not responding on the second one.
6833 */
6834TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
6835 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
6836 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6837 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6838 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6839 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006840 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006841
6842 // Now send ACTION_UP, with identical timestamp
6843 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
6844 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6845 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6846 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006847 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006848
6849 // We have now sent down and up. Let's consume first event and then ANR on the second.
6850 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6851 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006852 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006853}
6854
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006855// A spy window can receive an ANR
6856TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
6857 sp<FakeWindowHandle> spy = addSpyWindow();
6858
6859 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6860 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6861 WINDOW_LOCATION));
6862 mWindow->consumeMotionDown();
6863
6864 std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN
6865 ASSERT_TRUE(sequenceNum);
6866 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006867 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006868
6869 spy->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006870 spy->consumeMotionEvent(
6871 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006872 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006873 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006874}
6875
6876// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006877// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006878TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
6879 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006880
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006881 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6882 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006883 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006884 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006885
6886 // Stuck on the ACTION_UP
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
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006890 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006891 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006892 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6893 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006894
6895 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6896 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006897 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006898 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006899 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006900}
6901
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006902// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006903// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006904TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
6905 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006906
6907 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006908 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6909 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006910
6911 mWindow->consumeMotionDown();
6912 // Stuck on the ACTION_UP
6913 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006914 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006915
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006916 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006917 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006918 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6919 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006920
6921 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6922 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006923 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006924 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006925 spy->assertNoEvents();
6926}
6927
6928TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
6929 mDispatcher->setMonitorDispatchingTimeoutForTest(30ms);
6930
6931 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6932
6933 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6934 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6935 WINDOW_LOCATION));
6936
6937 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6938 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
6939 ASSERT_TRUE(consumeSeq);
6940
Prabir Pradhanedd96402022-02-15 01:46:16 -08006941 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006942
6943 monitor.finishEvent(*consumeSeq);
6944 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6945
6946 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006947 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006948}
6949
6950// If a window is unresponsive, then you get anr. if the window later catches up and starts to
6951// process events, you don't get an anr. When the window later becomes unresponsive again, you
6952// get an ANR again.
6953// 1. tap -> block on ACTION_UP -> receive ANR
6954// 2. consume all pending events (= queue becomes healthy again)
6955// 3. tap again -> block on ACTION_UP again -> receive ANR second time
6956TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
6957 tapOnWindow();
6958
6959 mWindow->consumeMotionDown();
6960 // Block on ACTION_UP
6961 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006962 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006963 mWindow->consumeMotionUp(); // Now the connection should be healthy again
6964 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006965 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006966 mWindow->assertNoEvents();
6967
6968 tapOnWindow();
6969 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006970 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006971 mWindow->consumeMotionUp();
6972
6973 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006974 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006975 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006976 mWindow->assertNoEvents();
6977}
6978
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006979// If a connection remains unresponsive for a while, make sure policy is only notified once about
6980// it.
6981TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006982 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006983 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6984 WINDOW_LOCATION));
6985
6986 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006987 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006988 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006989 // 'notifyConnectionUnresponsive' should only be called once per connection
6990 mFakePolicy->assertNotifyAnrWasNotCalled();
6991 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006992 mWindow->consumeMotionDown();
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006993 mWindow->consumeMotionEvent(
6994 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006995 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006996 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006997 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006998 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006999}
7000
7001/**
7002 * If a window is processing a motion event, and then a key event comes in, the key event should
7003 * not to to the focused window until the motion is processed.
7004 *
7005 * Warning!!!
7006 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
7007 * and the injection timeout that we specify when injecting the key.
7008 * We must have the injection timeout (10ms) be smaller than
7009 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
7010 *
7011 * If that value changes, this test should also change.
7012 */
7013TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
7014 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
7015 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
7016
7017 tapOnWindow();
7018 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
7019 ASSERT_TRUE(downSequenceNum);
7020 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
7021 ASSERT_TRUE(upSequenceNum);
7022 // Don't finish the events yet, and send a key
7023 // Injection will "succeed" because we will eventually give up and send the key to the focused
7024 // window even if motions are still being processed. But because the injection timeout is short,
7025 // we will receive INJECTION_TIMED_OUT as the result.
7026
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007027 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007028 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007029 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
7030 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007031 // Key will not be sent to the window, yet, because the window is still processing events
7032 // and the key remains pending, waiting for the touch events to be processed
7033 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
7034 ASSERT_FALSE(keySequenceNum);
7035
7036 std::this_thread::sleep_for(500ms);
7037 // if we wait long enough though, dispatcher will give up, and still send the key
7038 // to the focused window, even though we have not yet finished the motion event
7039 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7040 mWindow->finishEvent(*downSequenceNum);
7041 mWindow->finishEvent(*upSequenceNum);
7042}
7043
7044/**
7045 * If a window is processing a motion event, and then a key event comes in, the key event should
7046 * not go to the focused window until the motion is processed.
7047 * If then a new motion comes in, then the pending key event should be going to the currently
7048 * focused window right away.
7049 */
7050TEST_F(InputDispatcherSingleWindowAnr,
7051 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
7052 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
7053 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
7054
7055 tapOnWindow();
7056 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
7057 ASSERT_TRUE(downSequenceNum);
7058 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
7059 ASSERT_TRUE(upSequenceNum);
7060 // Don't finish the events yet, and send a key
7061 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007062 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007063 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7064 InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007065 // At this point, key is still pending, and should not be sent to the application yet.
7066 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
7067 ASSERT_FALSE(keySequenceNum);
7068
7069 // Now tap down again. It should cause the pending key to go to the focused window right away.
7070 tapOnWindow();
7071 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
7072 // the other events yet. We can finish events in any order.
7073 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
7074 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
7075 mWindow->consumeMotionDown();
7076 mWindow->consumeMotionUp();
7077 mWindow->assertNoEvents();
7078}
7079
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07007080/**
7081 * Send an event to the app and have the app not respond right away.
7082 * When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
7083 * So InputDispatcher will enqueue ACTION_CANCEL event as well.
7084 * At some point, the window becomes responsive again.
7085 * Ensure that subsequent events get dropped, and the next gesture is delivered.
7086 */
7087TEST_F(InputDispatcherSingleWindowAnr, TwoGesturesWithAnr) {
7088 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7089 .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10))
7090 .build());
7091
7092 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
7093 ASSERT_TRUE(sequenceNum);
7094 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
7095 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
7096
7097 mWindow->finishEvent(*sequenceNum);
7098 mWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
7099 ASSERT_TRUE(mDispatcher->waitForIdle());
7100 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
7101
7102 // Now that the window is responsive, let's continue the gesture.
7103 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
7104 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
7105 .build());
7106
7107 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7108 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
7109 .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3))
7110 .build());
7111
7112 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
7113 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
7114 .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3))
7115 .build());
7116 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
7117 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
7118 .build());
7119 // We already canceled this pointer, so the window shouldn't get any new events.
7120 mWindow->assertNoEvents();
7121
7122 // Start another one.
7123 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7124 .pointer(PointerBuilder(0, ToolType::FINGER).x(15).y(15))
7125 .build());
7126 mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
7127}
7128
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007129class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
7130 virtual void SetUp() override {
7131 InputDispatcherTest::SetUp();
7132
Chris Yea209fde2020-07-22 13:54:51 -07007133 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007134 mApplication->setDispatchingTimeout(10ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007135 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
7136 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007137 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007138 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007139 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007140
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007141 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
7142 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05007143 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007144 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007145
7146 // Set focused application.
7147 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07007148 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007149
7150 // Expect one focus window exist in display.
7151 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07007152 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007153 mFocusedWindow->consumeFocusEvent(true);
7154 }
7155
7156 virtual void TearDown() override {
7157 InputDispatcherTest::TearDown();
7158
7159 mUnfocusedWindow.clear();
7160 mFocusedWindow.clear();
7161 }
7162
7163protected:
Chris Yea209fde2020-07-22 13:54:51 -07007164 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007165 sp<FakeWindowHandle> mUnfocusedWindow;
7166 sp<FakeWindowHandle> mFocusedWindow;
7167 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
7168 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
7169 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
7170
7171 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
7172
7173 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
7174
7175private:
7176 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007177 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007178 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7179 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007180 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007181 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7182 location));
7183 }
7184};
7185
7186// If we have 2 windows that are both unresponsive, the one with the shortest timeout
7187// should be ANR'd first.
7188TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007189 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007190 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7191 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007192 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007193 mFocusedWindow->consumeMotionDown();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007194 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007195 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007196 // We consumed all events, so no ANR
7197 ASSERT_TRUE(mDispatcher->waitForIdle());
7198 mFakePolicy->assertNotifyAnrWasNotCalled();
7199
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007200 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007201 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7202 FOCUSED_WINDOW_LOCATION));
7203 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
7204 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007205
7206 const std::chrono::duration timeout =
7207 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007208 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007209 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
7210 // sequence to make it consistent
7211 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007212 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007213 mFocusedWindow->consumeMotionDown();
7214 // This cancel is generated because the connection was unresponsive
7215 mFocusedWindow->consumeMotionCancel();
7216 mFocusedWindow->assertNoEvents();
7217 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007218 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08007219 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7220 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007221 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007222}
7223
7224// If we have 2 windows with identical timeouts that are both unresponsive,
7225// it doesn't matter which order they should have ANR.
7226// But we should receive ANR for both.
7227TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
7228 // Set the timeout for unfocused window to match the focused window
7229 mUnfocusedWindow->setDispatchingTimeout(10ms);
7230 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
7231
7232 tapOnFocusedWindow();
7233 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Prabir Pradhanedd96402022-02-15 01:46:16 -08007234 sp<IBinder> anrConnectionToken1, anrConnectionToken2;
7235 ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms));
7236 ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007237
7238 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007239 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
7240 mFocusedWindow->getToken() == anrConnectionToken2);
7241 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
7242 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007243
7244 ASSERT_TRUE(mDispatcher->waitForIdle());
7245 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007246
7247 mFocusedWindow->consumeMotionDown();
7248 mFocusedWindow->consumeMotionUp();
7249 mUnfocusedWindow->consumeMotionOutside();
7250
Prabir Pradhanedd96402022-02-15 01:46:16 -08007251 sp<IBinder> responsiveToken1, responsiveToken2;
7252 ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken());
7253 ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007254
7255 // Both applications should be marked as responsive, in any order
7256 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
7257 mFocusedWindow->getToken() == responsiveToken2);
7258 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
7259 mUnfocusedWindow->getToken() == responsiveToken2);
7260 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007261}
7262
7263// If a window is already not responding, the second tap on the same window should be ignored.
7264// We should also log an error to account for the dropped event (not tested here).
7265// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
7266TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
7267 tapOnFocusedWindow();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007268 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007269 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007270 // Receive the events, but don't respond
7271 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
7272 ASSERT_TRUE(downEventSequenceNum);
7273 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
7274 ASSERT_TRUE(upEventSequenceNum);
7275 const std::chrono::duration timeout =
7276 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007277 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007278
7279 // Tap once again
7280 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007281 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007282 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7283 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007284 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007285 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7286 FOCUSED_WINDOW_LOCATION));
7287 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
7288 // valid touch target
7289 mUnfocusedWindow->assertNoEvents();
7290
7291 // Consume the first tap
7292 mFocusedWindow->finishEvent(*downEventSequenceNum);
7293 mFocusedWindow->finishEvent(*upEventSequenceNum);
7294 ASSERT_TRUE(mDispatcher->waitForIdle());
7295 // The second tap did not go to the focused window
7296 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007297 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -08007298 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7299 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007300 mFakePolicy->assertNotifyAnrWasNotCalled();
7301}
7302
7303// If you tap outside of all windows, there will not be ANR
7304TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007305 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007306 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7307 LOCATION_OUTSIDE_ALL_WINDOWS));
7308 ASSERT_TRUE(mDispatcher->waitForIdle());
7309 mFakePolicy->assertNotifyAnrWasNotCalled();
7310}
7311
7312// Since the focused window is paused, tapping on it should not produce any events
7313TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
7314 mFocusedWindow->setPaused(true);
7315 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
7316
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));
7320
7321 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
7322 ASSERT_TRUE(mDispatcher->waitForIdle());
7323 // Should not ANR because the window is paused, and touches shouldn't go to it
7324 mFakePolicy->assertNotifyAnrWasNotCalled();
7325
7326 mFocusedWindow->assertNoEvents();
7327 mUnfocusedWindow->assertNoEvents();
7328}
7329
7330/**
7331 * If a window is processing a motion event, and then a key event comes in, the key event should
7332 * not to to the focused window until the motion is processed.
7333 * If a different window becomes focused at this time, the key should go to that window instead.
7334 *
7335 * Warning!!!
7336 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
7337 * and the injection timeout that we specify when injecting the key.
7338 * We must have the injection timeout (10ms) be smaller than
7339 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
7340 *
7341 * If that value changes, this test should also change.
7342 */
7343TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
7344 // Set a long ANR timeout to prevent it from triggering
7345 mFocusedWindow->setDispatchingTimeout(2s);
7346 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7347
7348 tapOnUnfocusedWindow();
7349 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
7350 ASSERT_TRUE(downSequenceNum);
7351 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
7352 ASSERT_TRUE(upSequenceNum);
7353 // Don't finish the events yet, and send a key
7354 // Injection will succeed because we will eventually give up and send the key to the focused
7355 // window even if motions are still being processed.
7356
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007357 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007358 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7359 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007360 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007361 // Key will not be sent to the window, yet, because the window is still processing events
7362 // and the key remains pending, waiting for the touch events to be processed
7363 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
7364 ASSERT_FALSE(keySequenceNum);
7365
7366 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07007367 mFocusedWindow->setFocusable(false);
7368 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007369 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07007370 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007371
7372 // Focus events should precede the key events
7373 mUnfocusedWindow->consumeFocusEvent(true);
7374 mFocusedWindow->consumeFocusEvent(false);
7375
7376 // Finish the tap events, which should unblock dispatcher
7377 mUnfocusedWindow->finishEvent(*downSequenceNum);
7378 mUnfocusedWindow->finishEvent(*upSequenceNum);
7379
7380 // Now that all queues are cleared and no backlog in the connections, the key event
7381 // can finally go to the newly focused "mUnfocusedWindow".
7382 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7383 mFocusedWindow->assertNoEvents();
7384 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007385 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007386}
7387
7388// When the touch stream is split across 2 windows, and one of them does not respond,
7389// then ANR should be raised and the touch should be canceled for the unresponsive window.
7390// The other window should not be affected by that.
7391TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
7392 // Touch Window 1
Prabir Pradhan678438e2023-04-13 19:32:51 +00007393 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7394 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7395 {FOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007396 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007397 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007398
7399 // Touch Window 2
Prabir Pradhan678438e2023-04-13 19:32:51 +00007400 mDispatcher->notifyMotion(
7401 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7402 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007403
7404 const std::chrono::duration timeout =
7405 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007406 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007407
7408 mUnfocusedWindow->consumeMotionDown();
7409 mFocusedWindow->consumeMotionDown();
7410 // Focused window may or may not receive ACTION_MOVE
7411 // But it should definitely receive ACTION_CANCEL due to the ANR
7412 InputEvent* event;
7413 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
7414 ASSERT_TRUE(moveOrCancelSequenceNum);
7415 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
7416 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007417 ASSERT_EQ(event->getType(), InputEventType::MOTION);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007418 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
7419 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
7420 mFocusedWindow->consumeMotionCancel();
7421 } else {
7422 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
7423 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007424 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08007425 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7426 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007427
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007428 mUnfocusedWindow->assertNoEvents();
7429 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007430 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007431}
7432
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007433/**
7434 * If we have no focused window, and a key comes in, we start the ANR timer.
7435 * The focused application should add a focused window before the timer runs out to prevent ANR.
7436 *
7437 * If the user touches another application during this time, the key should be dropped.
7438 * Next, if a new focused window comes in, without toggling the focused application,
7439 * then no ANR should occur.
7440 *
7441 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
7442 * but in some cases the policy may not update the focused application.
7443 */
7444TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
7445 std::shared_ptr<FakeApplicationHandle> focusedApplication =
7446 std::make_shared<FakeApplicationHandle>();
7447 focusedApplication->setDispatchingTimeout(60ms);
7448 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
7449 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
7450 mFocusedWindow->setFocusable(false);
7451
7452 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7453 mFocusedWindow->consumeFocusEvent(false);
7454
7455 // Send a key. The ANR timer should start because there is no focused window.
7456 // 'focusedApplication' will get blamed if this timer completes.
7457 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007458 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007459 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7460 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
7461 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007462 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007463
7464 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
7465 // then the injected touches won't cause the focused event to get dropped.
7466 // The dispatcher only checks for whether the queue should be pruned upon queueing.
7467 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
7468 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
7469 // For this test, it means that the key would get delivered to the window once it becomes
7470 // focused.
7471 std::this_thread::sleep_for(10ms);
7472
7473 // Touch unfocused window. This should force the pending key to get dropped.
Prabir Pradhan678438e2023-04-13 19:32:51 +00007474 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7475 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7476 {UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007477
7478 // We do not consume the motion right away, because that would require dispatcher to first
7479 // process (== drop) the key event, and by that time, ANR will be raised.
7480 // Set the focused window first.
7481 mFocusedWindow->setFocusable(true);
7482 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7483 setFocusedWindow(mFocusedWindow);
7484 mFocusedWindow->consumeFocusEvent(true);
7485 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
7486 // to another application. This could be a bug / behaviour in the policy.
7487
7488 mUnfocusedWindow->consumeMotionDown();
7489
7490 ASSERT_TRUE(mDispatcher->waitForIdle());
7491 // Should not ANR because we actually have a focused window. It was just added too slowly.
7492 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
7493}
7494
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007495// These tests ensure we cannot send touch events to a window that's positioned behind a window
7496// that has feature NO_INPUT_CHANNEL.
7497// Layout:
7498// Top (closest to user)
7499// mNoInputWindow (above all windows)
7500// mBottomWindow
7501// Bottom (furthest from user)
7502class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
7503 virtual void SetUp() override {
7504 InputDispatcherTest::SetUp();
7505
7506 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007507 mNoInputWindow =
7508 sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7509 "Window without input channel", ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00007510 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007511 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007512 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7513 // It's perfectly valid for this window to not have an associated input channel
7514
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007515 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
7516 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007517 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
7518
7519 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7520 }
7521
7522protected:
7523 std::shared_ptr<FakeApplicationHandle> mApplication;
7524 sp<FakeWindowHandle> mNoInputWindow;
7525 sp<FakeWindowHandle> mBottomWindow;
7526};
7527
7528TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
7529 PointF touchedPoint = {10, 10};
7530
Prabir Pradhan678438e2023-04-13 19:32:51 +00007531 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7532 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7533 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007534
7535 mNoInputWindow->assertNoEvents();
7536 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
7537 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
7538 // and therefore should prevent mBottomWindow from receiving touches
7539 mBottomWindow->assertNoEvents();
7540}
7541
7542/**
7543 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
7544 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
7545 */
7546TEST_F(InputDispatcherMultiWindowOcclusionTests,
7547 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007548 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7549 "Window with input channel and NO_INPUT_CHANNEL",
7550 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007551
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007552 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007553 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7554 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7555
7556 PointF touchedPoint = {10, 10};
7557
Prabir Pradhan678438e2023-04-13 19:32:51 +00007558 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7559 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7560 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007561
7562 mNoInputWindow->assertNoEvents();
7563 mBottomWindow->assertNoEvents();
7564}
7565
Vishnu Nair958da932020-08-21 17:12:37 -07007566class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
7567protected:
7568 std::shared_ptr<FakeApplicationHandle> mApp;
7569 sp<FakeWindowHandle> mWindow;
7570 sp<FakeWindowHandle> mMirror;
7571
7572 virtual void SetUp() override {
7573 InputDispatcherTest::SetUp();
7574 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007575 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
7576 mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror",
7577 ADISPLAY_ID_DEFAULT, mWindow->getToken());
Vishnu Nair958da932020-08-21 17:12:37 -07007578 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7579 mWindow->setFocusable(true);
7580 mMirror->setFocusable(true);
7581 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7582 }
7583};
7584
7585TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
7586 // Request focus on a mirrored window
7587 setFocusedWindow(mMirror);
7588
7589 // window gets focused
7590 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007591 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7592 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007593 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
7594}
7595
7596// A focused & mirrored window remains focused only if the window and its mirror are both
7597// focusable.
7598TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
7599 setFocusedWindow(mMirror);
7600
7601 // window gets focused
7602 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007603 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7604 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007605 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007606 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7607 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007608 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7609
7610 mMirror->setFocusable(false);
7611 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7612
7613 // window loses focus since one of the windows associated with the token in not focusable
7614 mWindow->consumeFocusEvent(false);
7615
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007616 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7617 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007618 mWindow->assertNoEvents();
7619}
7620
7621// A focused & mirrored window remains focused until the window and its mirror both become
7622// invisible.
7623TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
7624 setFocusedWindow(mMirror);
7625
7626 // window gets focused
7627 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007628 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7629 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007630 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007631 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7632 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007633 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7634
7635 mMirror->setVisible(false);
7636 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7637
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007638 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7639 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007640 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007641 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7642 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007643 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7644
7645 mWindow->setVisible(false);
7646 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7647
7648 // window loses focus only after all windows associated with the token become invisible.
7649 mWindow->consumeFocusEvent(false);
7650
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007651 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7652 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007653 mWindow->assertNoEvents();
7654}
7655
7656// A focused & mirrored window remains focused until both windows are removed.
7657TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
7658 setFocusedWindow(mMirror);
7659
7660 // window gets focused
7661 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007662 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7663 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007664 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007665 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7666 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007667 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7668
7669 // single window is removed but the window token remains focused
7670 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
7671
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007672 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7673 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007674 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007675 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7676 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007677 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7678
7679 // Both windows are removed
7680 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
7681 mWindow->consumeFocusEvent(false);
7682
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007683 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7684 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007685 mWindow->assertNoEvents();
7686}
7687
7688// Focus request can be pending until one window becomes visible.
7689TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
7690 // Request focus on an invisible mirror.
7691 mWindow->setVisible(false);
7692 mMirror->setVisible(false);
7693 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7694 setFocusedWindow(mMirror);
7695
7696 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007697 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007698 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7699 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07007700
7701 mMirror->setVisible(true);
7702 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7703
7704 // window gets focused
7705 mWindow->consumeFocusEvent(true);
7706 // window gets the pending key event
7707 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7708}
Prabir Pradhan99987712020-11-10 18:43:05 -08007709
7710class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
7711protected:
7712 std::shared_ptr<FakeApplicationHandle> mApp;
7713 sp<FakeWindowHandle> mWindow;
7714 sp<FakeWindowHandle> mSecondWindow;
7715
7716 void SetUp() override {
7717 InputDispatcherTest::SetUp();
7718 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007719 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007720 mWindow->setFocusable(true);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007721 mSecondWindow =
7722 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007723 mSecondWindow->setFocusable(true);
7724
7725 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7726 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
7727
7728 setFocusedWindow(mWindow);
7729 mWindow->consumeFocusEvent(true);
7730 }
7731
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007732 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007733 mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request));
Prabir Pradhan99987712020-11-10 18:43:05 -08007734 }
7735
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007736 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
7737 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08007738 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007739 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
7740 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007741 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007742 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08007743 }
7744};
7745
7746TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
7747 // Ensure that capture cannot be obtained for unfocused windows.
7748 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
7749 mFakePolicy->assertSetPointerCaptureNotCalled();
7750 mSecondWindow->assertNoEvents();
7751
7752 // Ensure that capture can be enabled from the focus window.
7753 requestAndVerifyPointerCapture(mWindow, true);
7754
7755 // Ensure that capture cannot be disabled from a window that does not have capture.
7756 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
7757 mFakePolicy->assertSetPointerCaptureNotCalled();
7758
7759 // Ensure that capture can be disabled from the window with capture.
7760 requestAndVerifyPointerCapture(mWindow, false);
7761}
7762
7763TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007764 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007765
7766 setFocusedWindow(mSecondWindow);
7767
7768 // Ensure that the capture disabled event was sent first.
7769 mWindow->consumeCaptureEvent(false);
7770 mWindow->consumeFocusEvent(false);
7771 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007772 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007773
7774 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007775 notifyPointerCaptureChanged({});
7776 notifyPointerCaptureChanged(request);
7777 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08007778 mWindow->assertNoEvents();
7779 mSecondWindow->assertNoEvents();
7780 mFakePolicy->assertSetPointerCaptureNotCalled();
7781}
7782
7783TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007784 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007785
7786 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007787 notifyPointerCaptureChanged({});
7788 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007789
7790 // Ensure that Pointer Capture is disabled.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007791 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007792 mWindow->consumeCaptureEvent(false);
7793 mWindow->assertNoEvents();
7794}
7795
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007796TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
7797 requestAndVerifyPointerCapture(mWindow, true);
7798
7799 // The first window loses focus.
7800 setFocusedWindow(mSecondWindow);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007801 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007802 mWindow->consumeCaptureEvent(false);
7803
7804 // Request Pointer Capture from the second window before the notification from InputReader
7805 // arrives.
7806 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007807 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007808
7809 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007810 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007811
7812 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007813 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007814
7815 mSecondWindow->consumeFocusEvent(true);
7816 mSecondWindow->consumeCaptureEvent(true);
7817}
7818
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007819TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
7820 // App repeatedly enables and disables capture.
7821 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7822 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7823 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7824 mFakePolicy->assertSetPointerCaptureCalled(false);
7825 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7826 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7827
7828 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
7829 // first request is now stale, this should do nothing.
7830 notifyPointerCaptureChanged(firstRequest);
7831 mWindow->assertNoEvents();
7832
7833 // InputReader notifies that the second request was enabled.
7834 notifyPointerCaptureChanged(secondRequest);
7835 mWindow->consumeCaptureEvent(true);
7836}
7837
Prabir Pradhan7092e262022-05-03 16:51:09 +00007838TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
7839 requestAndVerifyPointerCapture(mWindow, true);
7840
7841 // App toggles pointer capture off and on.
7842 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7843 mFakePolicy->assertSetPointerCaptureCalled(false);
7844
7845 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7846 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7847
7848 // InputReader notifies that the latest "enable" request was processed, while skipping over the
7849 // preceding "disable" request.
7850 notifyPointerCaptureChanged(enableRequest);
7851
7852 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
7853 // any notifications.
7854 mWindow->assertNoEvents();
7855}
7856
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007857class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
7858protected:
7859 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00007860
7861 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
7862 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
7863
7864 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
7865 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7866
7867 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
7868 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
7869 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7870 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
7871 MAXIMUM_OBSCURING_OPACITY);
7872
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00007873 static constexpr gui::Uid TOUCHED_APP_UID{10001};
7874 static constexpr gui::Uid APP_B_UID{10002};
7875 static constexpr gui::Uid APP_C_UID{10003};
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007876
7877 sp<FakeWindowHandle> mTouchWindow;
7878
7879 virtual void SetUp() override {
7880 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007881 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007882 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
7883 }
7884
7885 virtual void TearDown() override {
7886 InputDispatcherTest::TearDown();
7887 mTouchWindow.clear();
7888 }
7889
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00007890 sp<FakeWindowHandle> getOccludingWindow(gui::Uid uid, std::string name, TouchOcclusionMode mode,
chaviw3277faf2021-05-19 16:45:23 -05007891 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007892 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007893 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007894 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007895 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007896 return window;
7897 }
7898
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00007899 sp<FakeWindowHandle> getWindow(gui::Uid uid, std::string name) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007900 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
7901 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007902 sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007903 // Generate an arbitrary PID based on the UID
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00007904 window->setOwnerInfo(1777 + (uid.val() % 10000), uid);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007905 return window;
7906 }
7907
7908 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007909 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7910 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7911 points));
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007912 }
7913};
7914
7915TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007916 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007917 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007918 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007919
7920 touch();
7921
7922 mTouchWindow->assertNoEvents();
7923}
7924
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007925TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00007926 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
7927 const sp<FakeWindowHandle>& w =
7928 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
7929 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7930
7931 touch();
7932
7933 mTouchWindow->assertNoEvents();
7934}
7935
7936TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007937 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
7938 const sp<FakeWindowHandle>& w =
7939 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
7940 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7941
7942 touch();
7943
7944 w->assertNoEvents();
7945}
7946
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007947TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007948 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
7949 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007950
7951 touch();
7952
7953 mTouchWindow->consumeAnyMotionDown();
7954}
7955
7956TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007957 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007958 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007959 w->setFrame(Rect(0, 0, 50, 50));
7960 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007961
7962 touch({PointF{100, 100}});
7963
7964 mTouchWindow->consumeAnyMotionDown();
7965}
7966
7967TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007968 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007969 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007970 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7971
7972 touch();
7973
7974 mTouchWindow->consumeAnyMotionDown();
7975}
7976
7977TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
7978 const sp<FakeWindowHandle>& w =
7979 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
7980 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007981
7982 touch();
7983
7984 mTouchWindow->consumeAnyMotionDown();
7985}
7986
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007987TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
7988 const sp<FakeWindowHandle>& w =
7989 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
7990 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7991
7992 touch();
7993
7994 w->assertNoEvents();
7995}
7996
7997/**
7998 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
7999 * inside) while letting them pass-through. Note that even though touch passes through the occluding
8000 * window, the occluding window will still receive ACTION_OUTSIDE event.
8001 */
8002TEST_F(InputDispatcherUntrustedTouchesTest,
8003 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
8004 const sp<FakeWindowHandle>& w =
8005 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008006 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00008007 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8008
8009 touch();
8010
8011 w->consumeMotionOutside();
8012}
8013
8014TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
8015 const sp<FakeWindowHandle>& w =
8016 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008017 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00008018 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8019
8020 touch();
8021
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008022 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00008023}
8024
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00008025TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008026 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008027 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8028 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008029 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8030
8031 touch();
8032
8033 mTouchWindow->consumeAnyMotionDown();
8034}
8035
8036TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
8037 const sp<FakeWindowHandle>& w =
8038 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8039 MAXIMUM_OBSCURING_OPACITY);
8040 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00008041
8042 touch();
8043
8044 mTouchWindow->consumeAnyMotionDown();
8045}
8046
8047TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008048 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008049 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8050 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008051 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8052
8053 touch();
8054
8055 mTouchWindow->assertNoEvents();
8056}
8057
8058TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
8059 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
8060 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008061 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
8062 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008063 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008064 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
8065 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008066 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
8067
8068 touch();
8069
8070 mTouchWindow->assertNoEvents();
8071}
8072
8073TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
8074 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
8075 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008076 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
8077 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008078 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008079 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
8080 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008081 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
8082
8083 touch();
8084
8085 mTouchWindow->consumeAnyMotionDown();
8086}
8087
8088TEST_F(InputDispatcherUntrustedTouchesTest,
8089 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
8090 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008091 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8092 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008093 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008094 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
8095 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008096 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
8097
8098 touch();
8099
8100 mTouchWindow->consumeAnyMotionDown();
8101}
8102
8103TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
8104 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008105 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8106 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008107 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008108 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
8109 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008110 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00008111
8112 touch();
8113
8114 mTouchWindow->assertNoEvents();
8115}
8116
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008117TEST_F(InputDispatcherUntrustedTouchesTest,
8118 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
8119 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008120 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
8121 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008122 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008123 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8124 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008125 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
8126
8127 touch();
8128
8129 mTouchWindow->assertNoEvents();
8130}
8131
8132TEST_F(InputDispatcherUntrustedTouchesTest,
8133 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
8134 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008135 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
8136 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008137 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008138 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8139 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008140 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
8141
8142 touch();
8143
8144 mTouchWindow->consumeAnyMotionDown();
8145}
8146
8147TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
8148 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008149 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
8150 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008151 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8152
8153 touch();
8154
8155 mTouchWindow->consumeAnyMotionDown();
8156}
8157
8158TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
8159 const sp<FakeWindowHandle>& w =
8160 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
8161 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8162
8163 touch();
8164
8165 mTouchWindow->consumeAnyMotionDown();
8166}
8167
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00008168TEST_F(InputDispatcherUntrustedTouchesTest,
8169 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
8170 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
8171 const sp<FakeWindowHandle>& w =
8172 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
8173 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8174
8175 touch();
8176
8177 mTouchWindow->assertNoEvents();
8178}
8179
8180TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
8181 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
8182 const sp<FakeWindowHandle>& w =
8183 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
8184 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8185
8186 touch();
8187
8188 mTouchWindow->consumeAnyMotionDown();
8189}
8190
8191TEST_F(InputDispatcherUntrustedTouchesTest,
8192 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
8193 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
8194 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008195 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8196 OPACITY_ABOVE_THRESHOLD);
8197 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8198
8199 touch();
8200
8201 mTouchWindow->consumeAnyMotionDown();
8202}
8203
8204TEST_F(InputDispatcherUntrustedTouchesTest,
8205 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
8206 const sp<FakeWindowHandle>& w1 =
8207 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
8208 OPACITY_BELOW_THRESHOLD);
8209 const sp<FakeWindowHandle>& w2 =
8210 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8211 OPACITY_BELOW_THRESHOLD);
8212 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
8213
8214 touch();
8215
8216 mTouchWindow->assertNoEvents();
8217}
8218
8219/**
8220 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
8221 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
8222 * (which alone would result in allowing touches) does not affect the blocking behavior.
8223 */
8224TEST_F(InputDispatcherUntrustedTouchesTest,
8225 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
8226 const sp<FakeWindowHandle>& wB =
8227 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
8228 OPACITY_BELOW_THRESHOLD);
8229 const sp<FakeWindowHandle>& wC =
8230 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
8231 OPACITY_BELOW_THRESHOLD);
8232 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
8233
8234 touch();
8235
8236 mTouchWindow->assertNoEvents();
8237}
8238
8239/**
8240 * This test is testing that a window from a different UID but with same application token doesn't
8241 * block the touch. Apps can share the application token for close UI collaboration for example.
8242 */
8243TEST_F(InputDispatcherUntrustedTouchesTest,
8244 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
8245 const sp<FakeWindowHandle>& w =
8246 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
8247 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00008248 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8249
8250 touch();
8251
8252 mTouchWindow->consumeAnyMotionDown();
8253}
8254
arthurhungb89ccb02020-12-30 16:19:01 +08008255class InputDispatcherDragTests : public InputDispatcherTest {
8256protected:
8257 std::shared_ptr<FakeApplicationHandle> mApp;
8258 sp<FakeWindowHandle> mWindow;
8259 sp<FakeWindowHandle> mSecondWindow;
8260 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008261 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008262 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
8263 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +08008264
8265 void SetUp() override {
8266 InputDispatcherTest::SetUp();
8267 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008268 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08008269 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08008270
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008271 mSecondWindow =
8272 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08008273 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08008274
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008275 mSpyWindow =
8276 sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008277 mSpyWindow->setSpy(true);
8278 mSpyWindow->setTrustedOverlay(true);
8279 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
8280
arthurhungb89ccb02020-12-30 16:19:01 +08008281 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008282 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08008283 }
8284
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008285 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
8286 switch (fromSource) {
8287 case AINPUT_SOURCE_TOUCHSCREEN:
8288 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8289 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
8290 ADISPLAY_ID_DEFAULT, {50, 50}))
8291 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8292 break;
8293 case AINPUT_SOURCE_STYLUS:
8294 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8295 injectMotionEvent(
8296 mDispatcher,
8297 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8298 AINPUT_SOURCE_STYLUS)
8299 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008300 .pointer(PointerBuilder(0, ToolType::STYLUS)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008301 .x(50)
8302 .y(50))
8303 .build()));
8304 break;
8305 case AINPUT_SOURCE_MOUSE:
8306 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8307 injectMotionEvent(
8308 mDispatcher,
8309 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
8310 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8311 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008312 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008313 .x(50)
8314 .y(50))
8315 .build()));
8316 break;
8317 default:
8318 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
8319 }
arthurhungb89ccb02020-12-30 16:19:01 +08008320
8321 // Window should receive motion event.
8322 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008323 // Spy window should also receive motion event
8324 mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +00008325 }
8326
8327 // Start performing drag, we will create a drag window and transfer touch to it.
8328 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
8329 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008330 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +00008331 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008332 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +00008333 }
arthurhungb89ccb02020-12-30 16:19:01 +08008334
8335 // The drag window covers the entire display
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008336 mDragWindow =
8337 sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008338 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
arthurhungb89ccb02020-12-30 16:19:01 +08008339 mDispatcher->setInputWindows(
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008340 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08008341
8342 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +00008343 bool transferred =
8344 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
Harry Cutts33476232023-01-30 19:57:29 +00008345 /*isDragDrop=*/true);
Arthur Hung54745652022-04-20 07:17:41 +00008346 if (transferred) {
8347 mWindow->consumeMotionCancel();
8348 mDragWindow->consumeMotionDown();
8349 }
8350 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +08008351 }
8352};
8353
8354TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008355 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +08008356
8357 // Move on window.
8358 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8359 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8360 ADISPLAY_ID_DEFAULT, {50, 50}))
8361 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8362 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8363 mWindow->consumeDragEvent(false, 50, 50);
8364 mSecondWindow->assertNoEvents();
8365
8366 // Move to another window.
8367 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8368 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8369 ADISPLAY_ID_DEFAULT, {150, 50}))
8370 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8371 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8372 mWindow->consumeDragEvent(true, 150, 50);
8373 mSecondWindow->consumeDragEvent(false, 50, 50);
8374
8375 // Move back to original window.
8376 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8377 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8378 ADISPLAY_ID_DEFAULT, {50, 50}))
8379 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8380 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8381 mWindow->consumeDragEvent(false, 50, 50);
8382 mSecondWindow->consumeDragEvent(true, -50, 50);
8383
8384 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8385 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
8386 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8387 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8388 mWindow->assertNoEvents();
8389 mSecondWindow->assertNoEvents();
8390}
8391
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008392TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008393 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008394
8395 // No cancel event after drag start
8396 mSpyWindow->assertNoEvents();
8397
8398 const MotionEvent secondFingerDownEvent =
8399 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8400 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008401 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8402 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008403 .build();
8404 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8405 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8406 InputEventInjectionSync::WAIT_FOR_RESULT))
8407 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8408
8409 // Receives cancel for first pointer after next pointer down
8410 mSpyWindow->consumeMotionCancel();
8411 mSpyWindow->consumeMotionDown();
8412
8413 mSpyWindow->assertNoEvents();
8414}
8415
arthurhungf452d0b2021-01-06 00:19:52 +08008416TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008417 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +08008418
8419 // Move on window.
8420 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8421 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8422 ADISPLAY_ID_DEFAULT, {50, 50}))
8423 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8424 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8425 mWindow->consumeDragEvent(false, 50, 50);
8426 mSecondWindow->assertNoEvents();
8427
8428 // Move to another window.
8429 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8430 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8431 ADISPLAY_ID_DEFAULT, {150, 50}))
8432 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8433 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8434 mWindow->consumeDragEvent(true, 150, 50);
8435 mSecondWindow->consumeDragEvent(false, 50, 50);
8436
8437 // drop to another window.
8438 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8439 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8440 {150, 50}))
8441 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8442 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8443 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8444 mWindow->assertNoEvents();
8445 mSecondWindow->assertNoEvents();
8446}
8447
arthurhung6d4bed92021-03-17 11:59:33 +08008448TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008449 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +08008450
8451 // Move on window and keep button pressed.
8452 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8453 injectMotionEvent(mDispatcher,
8454 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
8455 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008456 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008457 .build()))
8458 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8459 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8460 mWindow->consumeDragEvent(false, 50, 50);
8461 mSecondWindow->assertNoEvents();
8462
8463 // Move to another window and release button, expect to drop item.
8464 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8465 injectMotionEvent(mDispatcher,
8466 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
8467 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008468 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008469 .build()))
8470 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8471 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8472 mWindow->assertNoEvents();
8473 mSecondWindow->assertNoEvents();
8474 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8475
8476 // nothing to the window.
8477 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8478 injectMotionEvent(mDispatcher,
8479 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
8480 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008481 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008482 .build()))
8483 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8484 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8485 mWindow->assertNoEvents();
8486 mSecondWindow->assertNoEvents();
8487}
8488
Arthur Hung54745652022-04-20 07:17:41 +00008489TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008490 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +08008491
8492 // Set second window invisible.
8493 mSecondWindow->setVisible(false);
8494 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
8495
8496 // Move on window.
8497 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8498 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8499 ADISPLAY_ID_DEFAULT, {50, 50}))
8500 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8501 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8502 mWindow->consumeDragEvent(false, 50, 50);
8503 mSecondWindow->assertNoEvents();
8504
8505 // Move to another window.
8506 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8507 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8508 ADISPLAY_ID_DEFAULT, {150, 50}))
8509 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8510 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8511 mWindow->consumeDragEvent(true, 150, 50);
8512 mSecondWindow->assertNoEvents();
8513
8514 // drop to another window.
8515 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8516 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8517 {150, 50}))
8518 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8519 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8520 mFakePolicy->assertDropTargetEquals(nullptr);
8521 mWindow->assertNoEvents();
8522 mSecondWindow->assertNoEvents();
8523}
8524
Arthur Hung54745652022-04-20 07:17:41 +00008525TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008526 // Ensure window could track pointerIds if it didn't support split touch.
8527 mWindow->setPreventSplitting(true);
8528
Arthur Hung54745652022-04-20 07:17:41 +00008529 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8530 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8531 {50, 50}))
8532 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8533 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8534
8535 const MotionEvent secondFingerDownEvent =
8536 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8537 .displayId(ADISPLAY_ID_DEFAULT)
8538 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008539 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8540 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008541 .build();
8542 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8543 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8544 InputEventInjectionSync::WAIT_FOR_RESULT))
8545 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00008546 mWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
Arthur Hung54745652022-04-20 07:17:41 +00008547
8548 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008549 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008550}
8551
8552TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
8553 // First down on second window.
8554 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8555 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8556 {150, 50}))
8557 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8558
8559 mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8560
8561 // Second down on first window.
8562 const MotionEvent secondFingerDownEvent =
8563 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8564 .displayId(ADISPLAY_ID_DEFAULT)
8565 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008566 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8567 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008568 .build();
8569 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8570 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8571 InputEventInjectionSync::WAIT_FOR_RESULT))
8572 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8573 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8574
8575 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008576 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008577
8578 // Move on window.
8579 const MotionEvent secondFingerMoveEvent =
8580 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
8581 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008582 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8583 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008584 .build();
8585 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8586 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
8587 InputEventInjectionSync::WAIT_FOR_RESULT));
8588 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8589 mWindow->consumeDragEvent(false, 50, 50);
8590 mSecondWindow->consumeMotionMove();
8591
8592 // Release the drag pointer should perform drop.
8593 const MotionEvent secondFingerUpEvent =
8594 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
8595 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008596 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8597 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008598 .build();
8599 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8600 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
8601 InputEventInjectionSync::WAIT_FOR_RESULT));
8602 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8603 mFakePolicy->assertDropTargetEquals(mWindow->getToken());
8604 mWindow->assertNoEvents();
8605 mSecondWindow->consumeMotionMove();
8606}
8607
Arthur Hung3915c1f2022-05-31 07:17:17 +00008608TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008609 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +00008610
8611 // Update window of second display.
8612 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008613 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008614 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8615
8616 // Let second display has a touch state.
8617 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8618 injectMotionEvent(mDispatcher,
8619 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8620 AINPUT_SOURCE_TOUCHSCREEN)
8621 .displayId(SECOND_DISPLAY_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008622 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Arthur Hung3915c1f2022-05-31 07:17:17 +00008623 .build()));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07008624 windowInSecondary->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
Harry Cutts33476232023-01-30 19:57:29 +00008625 SECOND_DISPLAY_ID, /*expectedFlag=*/0);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008626 // Update window again.
8627 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8628
8629 // Move on window.
8630 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8631 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8632 ADISPLAY_ID_DEFAULT, {50, 50}))
8633 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8634 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8635 mWindow->consumeDragEvent(false, 50, 50);
8636 mSecondWindow->assertNoEvents();
8637
8638 // Move to another window.
8639 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8640 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8641 ADISPLAY_ID_DEFAULT, {150, 50}))
8642 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8643 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8644 mWindow->consumeDragEvent(true, 150, 50);
8645 mSecondWindow->consumeDragEvent(false, 50, 50);
8646
8647 // drop to another window.
8648 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8649 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8650 {150, 50}))
8651 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8652 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8653 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8654 mWindow->assertNoEvents();
8655 mSecondWindow->assertNoEvents();
8656}
8657
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008658TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
8659 startDrag(true, AINPUT_SOURCE_MOUSE);
8660 // Move on window.
8661 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8662 injectMotionEvent(mDispatcher,
8663 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8664 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8665 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008666 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008667 .x(50)
8668 .y(50))
8669 .build()))
8670 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8671 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8672 mWindow->consumeDragEvent(false, 50, 50);
8673 mSecondWindow->assertNoEvents();
8674
8675 // Move to another window.
8676 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8677 injectMotionEvent(mDispatcher,
8678 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8679 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8680 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008681 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008682 .x(150)
8683 .y(50))
8684 .build()))
8685 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8686 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8687 mWindow->consumeDragEvent(true, 150, 50);
8688 mSecondWindow->consumeDragEvent(false, 50, 50);
8689
8690 // drop to another window.
8691 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8692 injectMotionEvent(mDispatcher,
8693 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
8694 .buttonState(0)
8695 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008696 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008697 .x(150)
8698 .y(50))
8699 .build()))
8700 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8701 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8702 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8703 mWindow->assertNoEvents();
8704 mSecondWindow->assertNoEvents();
8705}
8706
Vishnu Nair062a8672021-09-03 16:07:44 -07008707class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
8708
8709TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
8710 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008711 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8712 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008713 window->setDropInput(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008714 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8715 window->setFocusable(true);
8716 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8717 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008718 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008719
8720 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008721 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008722 window->assertNoEvents();
8723
Prabir Pradhan678438e2023-04-13 19:32:51 +00008724 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8725 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -07008726 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
8727 ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008728 window->assertNoEvents();
8729
8730 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008731 window->setDropInput(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008732 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8733
Prabir Pradhan678438e2023-04-13 19:32:51 +00008734 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008735 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8736
Prabir Pradhan678438e2023-04-13 19:32:51 +00008737 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8738 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008739 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8740 window->assertNoEvents();
8741}
8742
8743TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
8744 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8745 std::make_shared<FakeApplicationHandle>();
8746 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008747 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8748 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008749 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00008750 obscuringWindow->setOwnerInfo(111, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008751 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008752 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008753 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8754 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008755 window->setDropInputIfObscured(true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00008756 window->setOwnerInfo(222, gui::Uid{222});
Vishnu Nair062a8672021-09-03 16:07:44 -07008757 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8758 window->setFocusable(true);
8759 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8760 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008761 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008762
8763 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008764 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008765 window->assertNoEvents();
8766
Prabir Pradhan678438e2023-04-13 19:32:51 +00008767 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8768 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -07008769 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
8770 ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008771 window->assertNoEvents();
8772
8773 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008774 window->setDropInputIfObscured(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008775 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8776
Prabir Pradhan678438e2023-04-13 19:32:51 +00008777 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008778 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8779
Prabir Pradhan678438e2023-04-13 19:32:51 +00008780 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8781 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008782 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
8783 window->assertNoEvents();
8784}
8785
8786TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
8787 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8788 std::make_shared<FakeApplicationHandle>();
8789 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008790 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8791 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008792 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00008793 obscuringWindow->setOwnerInfo(111, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008794 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008795 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008796 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8797 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008798 window->setDropInputIfObscured(true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00008799 window->setOwnerInfo(222, gui::Uid{222});
Vishnu Nair062a8672021-09-03 16:07:44 -07008800 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8801 window->setFocusable(true);
8802 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8803 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008804 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008805
8806 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008807 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008808 window->assertNoEvents();
8809
Prabir Pradhan678438e2023-04-13 19:32:51 +00008810 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8811 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -07008812 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
8813 ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008814 window->assertNoEvents();
8815
8816 // When the window is no longer obscured because it went on top, it should get input
8817 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}});
8818
Prabir Pradhan678438e2023-04-13 19:32:51 +00008819 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008820 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8821
Prabir Pradhan678438e2023-04-13 19:32:51 +00008822 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8823 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008824 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8825 window->assertNoEvents();
8826}
8827
Antonio Kantekf16f2832021-09-28 04:39:20 +00008828class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
8829protected:
8830 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +00008831 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008832 sp<FakeWindowHandle> mWindow;
8833 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +00008834 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008835
8836 void SetUp() override {
8837 InputDispatcherTest::SetUp();
8838
8839 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +00008840 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008841 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008842 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008843 setFocusedWindow(mWindow);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008844 mSecondWindow =
8845 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008846 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +00008847 mThirdWindow =
8848 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
8849 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
8850 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008851
8852 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Antonio Kantek15beb512022-06-13 22:35:41 +00008853 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}},
8854 {SECOND_DISPLAY_ID, {mThirdWindow}}});
8855 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008856 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008857
Antonio Kantek15beb512022-06-13 22:35:41 +00008858 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +00008859 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008860 WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -07008861 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
8862 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008863 mThirdWindow->assertNoEvents();
8864 }
8865
8866 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
8867 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008868 SECONDARY_WINDOW_UID, /*hasPermission=*/true,
Antonio Kantek15beb512022-06-13 22:35:41 +00008869 SECOND_DISPLAY_ID)) {
8870 mWindow->assertNoEvents();
8871 mSecondWindow->assertNoEvents();
8872 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -07008873 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00008874 }
8875
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00008876 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, gui::Uid uid,
Antonio Kantek15beb512022-06-13 22:35:41 +00008877 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -07008878 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
8879 ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008880 mWindow->consumeTouchModeEvent(inTouchMode);
8881 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008882 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +00008883 }
8884};
8885
Antonio Kantek26defcf2022-02-08 01:12:27 +00008886TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008887 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +00008888 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
8889 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008890 /* hasPermission=*/false);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008891}
8892
Antonio Kantek26defcf2022-02-08 01:12:27 +00008893TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
8894 const WindowInfo& windowInfo = *mWindow->getInfo();
8895 int32_t ownerPid = windowInfo.ownerPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00008896 gui::Uid ownerUid = windowInfo.ownerUid;
8897 mWindow->setOwnerInfo(/*pid=*/-1, gui::Uid::INVALID);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008898 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008899 ownerUid, /*hasPermission=*/false,
Antonio Kanteka042c022022-07-06 16:51:07 -07008900 ADISPLAY_ID_DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +00008901 mWindow->assertNoEvents();
8902 mSecondWindow->assertNoEvents();
8903}
8904
8905TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
8906 const WindowInfo& windowInfo = *mWindow->getInfo();
8907 int32_t ownerPid = windowInfo.ownerPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00008908 gui::Uid ownerUid = windowInfo.ownerUid;
8909 mWindow->setOwnerInfo(/*pid=*/-1, gui::Uid::INVALID);
Antonio Kantek15beb512022-06-13 22:35:41 +00008910 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008911 ownerUid, /*hasPermission=*/true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008912}
8913
Antonio Kantekf16f2832021-09-28 04:39:20 +00008914TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008915 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +00008916 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
8917 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008918 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008919 mWindow->assertNoEvents();
8920 mSecondWindow->assertNoEvents();
8921}
8922
Antonio Kantek15beb512022-06-13 22:35:41 +00008923TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
8924 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
8925 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8926 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008927 /*hasPermission=*/true, SECOND_DISPLAY_ID));
Antonio Kantek15beb512022-06-13 22:35:41 +00008928 mWindow->assertNoEvents();
8929 mSecondWindow->assertNoEvents();
8930 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
8931}
8932
Antonio Kantek48710e42022-03-24 14:19:30 -07008933TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
8934 // Interact with the window first.
8935 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
8936 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8937 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
8938
8939 // Then remove focus.
8940 mWindow->setFocusable(false);
8941 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
8942
8943 // Assert that caller can switch touch mode by owning one of the last interacted window.
8944 const WindowInfo& windowInfo = *mWindow->getInfo();
8945 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8946 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008947 /*hasPermission=*/false, ADISPLAY_ID_DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -07008948}
8949
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008950class InputDispatcherSpyWindowTest : public InputDispatcherTest {
8951public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008952 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008953 std::shared_ptr<FakeApplicationHandle> application =
8954 std::make_shared<FakeApplicationHandle>();
8955 std::string name = "Fake Spy ";
8956 name += std::to_string(mSpyCount++);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008957 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher,
8958 name.c_str(), ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008959 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008960 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008961 return spy;
8962 }
8963
8964 sp<FakeWindowHandle> createForeground() {
8965 std::shared_ptr<FakeApplicationHandle> application =
8966 std::make_shared<FakeApplicationHandle>();
8967 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008968 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
8969 ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008970 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008971 return window;
8972 }
8973
8974private:
8975 int mSpyCount{0};
8976};
8977
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008978using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008979/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008980 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
8981 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008982TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
8983 ScopedSilentDeath _silentDeath;
8984
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008985 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008986 spy->setTrustedOverlay(false);
8987 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}),
8988 ".* not a trusted overlay");
8989}
8990
8991/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008992 * Input injection into a display with a spy window but no foreground windows should succeed.
8993 */
8994TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008995 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008996 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}});
8997
8998 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8999 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9000 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9001 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9002}
9003
9004/**
9005 * Verify the order in which different input windows receive events. The touched foreground window
9006 * (if there is one) should always receive the event first. When there are multiple spy windows, the
9007 * spy windows will receive the event according to their Z-order, where the top-most spy window will
9008 * receive events before ones belows it.
9009 *
9010 * Here, we set up a scenario with four windows in the following Z order from the top:
9011 * spy1, spy2, window, spy3.
9012 * We then inject an event and verify that the foreground "window" receives it first, followed by
9013 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
9014 * window.
9015 */
9016TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
9017 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009018 auto spy1 = createSpy();
9019 auto spy2 = createSpy();
9020 auto spy3 = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009021 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}});
9022 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
9023 const size_t numChannels = channels.size();
9024
Michael Wright8e9a8562022-02-09 13:44:29 +00009025 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009026 if (!epollFd.ok()) {
9027 FAIL() << "Failed to create epoll fd";
9028 }
9029
9030 for (size_t i = 0; i < numChannels; i++) {
9031 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
9032 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
9033 FAIL() << "Failed to add fd to epoll";
9034 }
9035 }
9036
9037 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9038 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9039 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9040
9041 std::vector<size_t> eventOrder;
9042 std::vector<struct epoll_event> events(numChannels);
9043 for (;;) {
9044 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
9045 (100ms).count());
9046 if (nFds < 0) {
9047 FAIL() << "Failed to call epoll_wait";
9048 }
9049 if (nFds == 0) {
9050 break; // epoll_wait timed out
9051 }
9052 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -07009053 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -07009054 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009055 channels[i]->consumeMotionDown();
9056 }
9057 }
9058
9059 // Verify the order in which the events were received.
9060 EXPECT_EQ(3u, eventOrder.size());
9061 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
9062 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
9063 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
9064}
9065
9066/**
9067 * A spy window using the NOT_TOUCHABLE flag does not receive events.
9068 */
9069TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
9070 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009071 auto spy = createSpy();
9072 spy->setTouchable(false);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009073 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9074
9075 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9076 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9077 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9078 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9079 spy->assertNoEvents();
9080}
9081
9082/**
9083 * A spy window will only receive gestures that originate within its touchable region. Gestures that
9084 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
9085 * to the window.
9086 */
9087TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
9088 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009089 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009090 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
9091 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9092
9093 // Inject an event outside the spy window's touchable region.
9094 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9095 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9096 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9097 window->consumeMotionDown();
9098 spy->assertNoEvents();
9099 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9100 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9101 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9102 window->consumeMotionUp();
9103 spy->assertNoEvents();
9104
9105 // Inject an event inside the spy window's touchable region.
9106 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9107 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9108 {5, 10}))
9109 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9110 window->consumeMotionDown();
9111 spy->consumeMotionDown();
9112}
9113
9114/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009115 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009116 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009117 */
9118TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
9119 auto window = createForeground();
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00009120 window->setOwnerInfo(12, gui::Uid{34});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009121 auto spy = createSpy();
9122 spy->setWatchOutsideTouch(true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00009123 spy->setOwnerInfo(56, gui::Uid{78});
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009124 spy->setFrame(Rect{0, 0, 20, 20});
9125 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9126
9127 // Inject an event outside the spy window's frame and touchable region.
9128 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009129 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9130 {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009131 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9132 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009133 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009134}
9135
9136/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009137 * Even when a spy window spans over multiple foreground windows, the spy should receive all
9138 * pointers that are down within its bounds.
9139 */
9140TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
9141 auto windowLeft = createForeground();
9142 windowLeft->setFrame({0, 0, 100, 200});
9143 auto windowRight = createForeground();
9144 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009145 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009146 spy->setFrame({0, 0, 200, 200});
9147 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}});
9148
9149 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9150 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9151 {50, 50}))
9152 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9153 windowLeft->consumeMotionDown();
9154 spy->consumeMotionDown();
9155
9156 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08009157 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009158 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009159 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
9160 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009161 .build();
9162 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9163 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9164 InputEventInjectionSync::WAIT_FOR_RESULT))
9165 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9166 windowRight->consumeMotionDown();
Harry Cutts33476232023-01-30 19:57:29 +00009167 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009168}
9169
9170/**
9171 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
9172 * the spy should receive the second pointer with ACTION_DOWN.
9173 */
9174TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
9175 auto window = createForeground();
9176 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009177 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009178 spyRight->setFrame({100, 0, 200, 200});
9179 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}});
9180
9181 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9182 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9183 {50, 50}))
9184 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9185 window->consumeMotionDown();
9186 spyRight->assertNoEvents();
9187
9188 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08009189 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009190 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009191 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
9192 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009193 .build();
9194 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9195 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9196 InputEventInjectionSync::WAIT_FOR_RESULT))
9197 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00009198 window->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009199 spyRight->consumeMotionDown();
9200}
9201
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009202/**
9203 * The spy window should not be able to affect whether or not touches are split. Only the foreground
9204 * windows should be allowed to control split touch.
9205 */
9206TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08009207 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009208 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009209 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08009210 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009211
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009212 auto window = createForeground();
9213 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009214
9215 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9216
9217 // First finger down, no window touched.
9218 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9219 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9220 {100, 200}))
9221 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9222 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9223 window->assertNoEvents();
9224
9225 // Second finger down on window, the window should receive touch down.
9226 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08009227 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009228 .displayId(ADISPLAY_ID_DEFAULT)
9229 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009230 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9231 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009232 .build();
9233 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9234 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9235 InputEventInjectionSync::WAIT_FOR_RESULT))
9236 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9237
9238 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00009239 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009240}
9241
9242/**
9243 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
9244 * do not receive key events.
9245 */
9246TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009247 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009248 spy->setFocusable(false);
9249
9250 auto window = createForeground();
9251 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9252 setFocusedWindow(window);
9253 window->consumeFocusEvent(true);
9254
9255 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
9256 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
9257 window->consumeKeyDown(ADISPLAY_ID_NONE);
9258
9259 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
9260 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
9261 window->consumeKeyUp(ADISPLAY_ID_NONE);
9262
9263 spy->assertNoEvents();
9264}
9265
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009266using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
9267
9268/**
9269 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
9270 * are currently sent to any other windows - including other spy windows - will also be cancelled.
9271 */
9272TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
9273 auto window = createForeground();
9274 auto spy1 = createSpy();
9275 auto spy2 = createSpy();
9276 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}});
9277
9278 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9279 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9280 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9281 window->consumeMotionDown();
9282 spy1->consumeMotionDown();
9283 spy2->consumeMotionDown();
9284
9285 // Pilfer pointers from the second spy window.
9286 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
9287 spy2->assertNoEvents();
9288 spy1->consumeMotionCancel();
9289 window->consumeMotionCancel();
9290
9291 // The rest of the gesture should only be sent to the second spy window.
9292 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9293 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
9294 ADISPLAY_ID_DEFAULT))
9295 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9296 spy2->consumeMotionMove();
9297 spy1->assertNoEvents();
9298 window->assertNoEvents();
9299}
9300
9301/**
9302 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
9303 * in the middle of the gesture.
9304 */
9305TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
9306 auto window = createForeground();
9307 auto spy = createSpy();
9308 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9309
9310 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9311 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9312 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9313 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9314 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9315
9316 window->releaseChannel();
9317
9318 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9319
9320 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9321 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9322 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9323 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
9324}
9325
9326/**
9327 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
9328 * the spy, but not to any other windows.
9329 */
9330TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
9331 auto spy = createSpy();
9332 auto window = createForeground();
9333
9334 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9335
9336 // First finger down on the window and the spy.
9337 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9338 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9339 {100, 200}))
9340 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9341 spy->consumeMotionDown();
9342 window->consumeMotionDown();
9343
9344 // Spy window pilfers the pointers.
9345 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9346 window->consumeMotionCancel();
9347
9348 // Second finger down on the window and spy, but the window should not receive the pointer down.
9349 const MotionEvent secondFingerDownEvent =
9350 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9351 .displayId(ADISPLAY_ID_DEFAULT)
9352 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009353 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9354 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009355 .build();
9356 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9357 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9358 InputEventInjectionSync::WAIT_FOR_RESULT))
9359 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9360
Harry Cutts33476232023-01-30 19:57:29 +00009361 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009362
9363 // Third finger goes down outside all windows, so injection should fail.
9364 const MotionEvent thirdFingerDownEvent =
9365 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9366 .displayId(ADISPLAY_ID_DEFAULT)
9367 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009368 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9369 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
9370 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009371 .build();
9372 ASSERT_EQ(InputEventInjectionResult::FAILED,
9373 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
9374 InputEventInjectionSync::WAIT_FOR_RESULT))
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08009375 << "Inject motion event should return InputEventInjectionResult::FAILED";
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009376
9377 spy->assertNoEvents();
9378 window->assertNoEvents();
9379}
9380
9381/**
9382 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
9383 */
9384TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
9385 auto spy = createSpy();
9386 spy->setFrame(Rect(0, 0, 100, 100));
9387 auto window = createForeground();
9388 window->setFrame(Rect(0, 0, 200, 200));
9389
9390 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9391
9392 // First finger down on the window only
9393 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9394 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9395 {150, 150}))
9396 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9397 window->consumeMotionDown();
9398
9399 // Second finger down on the spy and window
9400 const MotionEvent secondFingerDownEvent =
9401 MotionEventBuilder(POINTER_1_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(150).y(150))
9405 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009406 .build();
9407 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9408 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9409 InputEventInjectionSync::WAIT_FOR_RESULT))
9410 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9411 spy->consumeMotionDown();
9412 window->consumeMotionPointerDown(1);
9413
9414 // Third finger down on the spy and window
9415 const MotionEvent thirdFingerDownEvent =
9416 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9417 .displayId(ADISPLAY_ID_DEFAULT)
9418 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009419 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
9420 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
9421 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009422 .build();
9423 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9424 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
9425 InputEventInjectionSync::WAIT_FOR_RESULT))
9426 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9427 spy->consumeMotionPointerDown(1);
9428 window->consumeMotionPointerDown(2);
9429
9430 // Spy window pilfers the pointers.
9431 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9432 window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
9433 window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
9434
9435 spy->assertNoEvents();
9436 window->assertNoEvents();
9437}
9438
9439/**
9440 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
9441 * other windows should be canceled. If this results in the cancellation of all pointers for some
9442 * window, then that window should receive ACTION_CANCEL.
9443 */
9444TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
9445 auto spy = createSpy();
9446 spy->setFrame(Rect(0, 0, 100, 100));
9447 auto window = createForeground();
9448 window->setFrame(Rect(0, 0, 200, 200));
9449
9450 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9451
9452 // First finger down on both spy and window
9453 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9454 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9455 {10, 10}))
9456 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9457 window->consumeMotionDown();
9458 spy->consumeMotionDown();
9459
9460 // Second finger down on the spy and window
9461 const MotionEvent secondFingerDownEvent =
9462 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9463 .displayId(ADISPLAY_ID_DEFAULT)
9464 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009465 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9466 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009467 .build();
9468 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9469 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9470 InputEventInjectionSync::WAIT_FOR_RESULT))
9471 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9472 spy->consumeMotionPointerDown(1);
9473 window->consumeMotionPointerDown(1);
9474
9475 // Spy window pilfers the pointers.
9476 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9477 window->consumeMotionCancel();
9478
9479 spy->assertNoEvents();
9480 window->assertNoEvents();
9481}
9482
9483/**
9484 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
9485 * be sent to other windows
9486 */
9487TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
9488 auto spy = createSpy();
9489 spy->setFrame(Rect(0, 0, 100, 100));
9490 auto window = createForeground();
9491 window->setFrame(Rect(0, 0, 200, 200));
9492
9493 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9494
9495 // First finger down on both window and spy
9496 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9497 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9498 {10, 10}))
9499 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9500 window->consumeMotionDown();
9501 spy->consumeMotionDown();
9502
9503 // Spy window pilfers the pointers.
9504 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9505 window->consumeMotionCancel();
9506
9507 // Second finger down on the window only
9508 const MotionEvent secondFingerDownEvent =
9509 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9510 .displayId(ADISPLAY_ID_DEFAULT)
9511 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009512 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9513 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009514 .build();
9515 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9516 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9517 InputEventInjectionSync::WAIT_FOR_RESULT))
9518 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9519 window->consumeMotionDown();
9520 window->assertNoEvents();
9521
9522 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
9523 spy->consumeMotionMove();
9524 spy->assertNoEvents();
9525}
9526
Prabir Pradhand65552b2021-10-07 11:23:50 -07009527class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
9528public:
9529 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
9530 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9531 std::make_shared<FakeApplicationHandle>();
9532 sp<FakeWindowHandle> overlay =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009533 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
9534 "Stylus interceptor window", ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009535 overlay->setFocusable(false);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00009536 overlay->setOwnerInfo(111, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009537 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009538 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009539 overlay->setTrustedOverlay(true);
9540
9541 std::shared_ptr<FakeApplicationHandle> application =
9542 std::make_shared<FakeApplicationHandle>();
9543 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009544 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
9545 ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009546 window->setFocusable(true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00009547 window->setOwnerInfo(222, gui::Uid{222});
Prabir Pradhand65552b2021-10-07 11:23:50 -07009548
9549 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
9550 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9551 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00009552 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009553 return {std::move(overlay), std::move(window)};
9554 }
9555
9556 void sendFingerEvent(int32_t action) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00009557 mDispatcher->notifyMotion(
Prabir Pradhand65552b2021-10-07 11:23:50 -07009558 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
Prabir Pradhan678438e2023-04-13 19:32:51 +00009559 ADISPLAY_ID_DEFAULT, {PointF{20, 20}}));
Prabir Pradhand65552b2021-10-07 11:23:50 -07009560 }
9561
9562 void sendStylusEvent(int32_t action) {
9563 NotifyMotionArgs motionArgs =
9564 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
9565 ADISPLAY_ID_DEFAULT, {PointF{30, 40}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009566 motionArgs.pointerProperties[0].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +00009567 mDispatcher->notifyMotion(motionArgs);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009568 }
9569};
9570
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08009571using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
9572
9573TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
9574 ScopedSilentDeath _silentDeath;
9575
Prabir Pradhand65552b2021-10-07 11:23:50 -07009576 auto [overlay, window] = setupStylusOverlayScenario();
9577 overlay->setTrustedOverlay(false);
9578 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
9579 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}),
9580 ".* not a trusted overlay");
9581}
9582
9583TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
9584 auto [overlay, window] = setupStylusOverlayScenario();
9585 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9586
9587 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9588 overlay->consumeMotionDown();
9589 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9590 overlay->consumeMotionUp();
9591
9592 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9593 window->consumeMotionDown();
9594 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9595 window->consumeMotionUp();
9596
9597 overlay->assertNoEvents();
9598 window->assertNoEvents();
9599}
9600
9601TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
9602 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009603 overlay->setSpy(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009604 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9605
9606 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9607 overlay->consumeMotionDown();
9608 window->consumeMotionDown();
9609 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9610 overlay->consumeMotionUp();
9611 window->consumeMotionUp();
9612
9613 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9614 window->consumeMotionDown();
9615 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9616 window->consumeMotionUp();
9617
9618 overlay->assertNoEvents();
9619 window->assertNoEvents();
9620}
9621
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00009622/**
9623 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
9624 * The scenario is as follows:
9625 * - The stylus interceptor overlay is configured as a spy window.
9626 * - The stylus interceptor spy receives the start of a new stylus gesture.
9627 * - It pilfers pointers and then configures itself to no longer be a spy.
9628 * - The stylus interceptor continues to receive the rest of the gesture.
9629 */
9630TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
9631 auto [overlay, window] = setupStylusOverlayScenario();
9632 overlay->setSpy(true);
9633 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9634
9635 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9636 overlay->consumeMotionDown();
9637 window->consumeMotionDown();
9638
9639 // The interceptor pilfers the pointers.
9640 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
9641 window->consumeMotionCancel();
9642
9643 // The interceptor configures itself so that it is no longer a spy.
9644 overlay->setSpy(false);
9645 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9646
9647 // It continues to receive the rest of the stylus gesture.
9648 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
9649 overlay->consumeMotionMove();
9650 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9651 overlay->consumeMotionUp();
9652
9653 window->assertNoEvents();
9654}
9655
Prabir Pradhan5735a322022-04-11 17:23:34 +00009656struct User {
9657 int32_t mPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00009658 gui::Uid mUid;
Prabir Pradhan5735a322022-04-11 17:23:34 +00009659 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
9660 std::unique_ptr<InputDispatcher>& mDispatcher;
9661
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00009662 User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, gui::Uid uid)
Prabir Pradhan5735a322022-04-11 17:23:34 +00009663 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
9664
9665 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
9666 return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
9667 ADISPLAY_ID_DEFAULT, {100, 200},
9668 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
9669 AMOTION_EVENT_INVALID_CURSOR_POSITION},
9670 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
9671 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
9672 }
9673
9674 InputEventInjectionResult injectTargetedKey(int32_t action) const {
Harry Cutts33476232023-01-30 19:57:29 +00009675 return inputdispatcher::injectKey(mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +00009676 InputEventInjectionSync::WAIT_FOR_RESULT,
Harry Cutts33476232023-01-30 19:57:29 +00009677 INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid},
Prabir Pradhan5735a322022-04-11 17:23:34 +00009678 mPolicyFlags);
9679 }
9680
9681 sp<FakeWindowHandle> createWindow() const {
9682 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9683 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009684 sp<FakeWindowHandle> window =
9685 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window",
9686 ADISPLAY_ID_DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +00009687 window->setOwnerInfo(mPid, mUid);
9688 return window;
9689 }
9690};
9691
9692using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
9693
9694TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00009695 auto owner = User(mDispatcher, 10, gui::Uid{11});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009696 auto window = owner.createWindow();
9697 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9698
9699 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9700 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9701 window->consumeMotionDown();
9702
9703 setFocusedWindow(window);
9704 window->consumeFocusEvent(true);
9705
9706 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9707 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9708 window->consumeKeyDown(ADISPLAY_ID_NONE);
9709}
9710
9711TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00009712 auto owner = User(mDispatcher, 10, gui::Uid{11});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009713 auto window = owner.createWindow();
9714 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9715
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00009716 auto rando = User(mDispatcher, 20, gui::Uid{21});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009717 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9718 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9719
9720 setFocusedWindow(window);
9721 window->consumeFocusEvent(true);
9722
9723 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9724 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9725 window->assertNoEvents();
9726}
9727
9728TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00009729 auto owner = User(mDispatcher, 10, gui::Uid{11});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009730 auto window = owner.createWindow();
9731 auto spy = owner.createWindow();
9732 spy->setSpy(true);
9733 spy->setTrustedOverlay(true);
9734 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9735
9736 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9737 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9738 spy->consumeMotionDown();
9739 window->consumeMotionDown();
9740}
9741
9742TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00009743 auto owner = User(mDispatcher, 10, gui::Uid{11});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009744 auto window = owner.createWindow();
9745
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00009746 auto rando = User(mDispatcher, 20, gui::Uid{21});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009747 auto randosSpy = rando.createWindow();
9748 randosSpy->setSpy(true);
9749 randosSpy->setTrustedOverlay(true);
9750 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9751
9752 // The event is targeted at owner's window, so injection should succeed, but the spy should
9753 // not receive the event.
9754 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9755 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9756 randosSpy->assertNoEvents();
9757 window->consumeMotionDown();
9758}
9759
9760TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00009761 auto owner = User(mDispatcher, 10, gui::Uid{11});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009762 auto window = owner.createWindow();
9763
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00009764 auto rando = User(mDispatcher, 20, gui::Uid{21});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009765 auto randosSpy = rando.createWindow();
9766 randosSpy->setSpy(true);
9767 randosSpy->setTrustedOverlay(true);
9768 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9769
9770 // A user that has injection permission can inject into any window.
9771 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9772 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
9773 ADISPLAY_ID_DEFAULT));
9774 randosSpy->consumeMotionDown();
9775 window->consumeMotionDown();
9776
9777 setFocusedWindow(randosSpy);
9778 randosSpy->consumeFocusEvent(true);
9779
9780 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
9781 randosSpy->consumeKeyDown(ADISPLAY_ID_NONE);
9782 window->assertNoEvents();
9783}
9784
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -07009785TEST_F(InputDispatcherTargetedInjectionTest, CannotGenerateActionOutsideToOtherUids) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00009786 auto owner = User(mDispatcher, 10, gui::Uid{11});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009787 auto window = owner.createWindow();
9788
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00009789 auto rando = User(mDispatcher, 20, gui::Uid{21});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009790 auto randosWindow = rando.createWindow();
9791 randosWindow->setFrame(Rect{-10, -10, -5, -5});
9792 randosWindow->setWatchOutsideTouch(true);
9793 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}});
9794
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -07009795 // Do not allow generation of ACTION_OUTSIDE events into windows owned by different uids.
Prabir Pradhan5735a322022-04-11 17:23:34 +00009796 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9797 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9798 window->consumeMotionDown();
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -07009799 randosWindow->assertNoEvents();
Prabir Pradhan5735a322022-04-11 17:23:34 +00009800}
9801
Garfield Tane84e6f92019-08-29 17:28:41 -07009802} // namespace android::inputdispatcher