blob: c0bc68c292f8dde7abdfb587e0d37a0ad13c9543 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Garfield Tan0fc2fa72019-08-29 17:22:15 -070017#include "../dispatcher/InputDispatcher.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080018
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -070019#include <android-base/properties.h>
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080020#include <android-base/silent_death_test.h>
Garfield Tan1c7bc862020-01-28 13:24:04 -080021#include <android-base/stringprintf.h>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070022#include <android-base/thread_annotations.h>
Robert Carr803535b2018-08-02 16:38:15 -070023#include <binder/Binder.h>
Michael Wright8e9a8562022-02-09 13:44:29 +000024#include <fcntl.h>
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -080025#include <gmock/gmock.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080026#include <gtest/gtest.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100027#include <input/Input.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080028#include <linux/input.h>
Prabir Pradhan07e05b62021-11-19 03:57:24 -080029#include <sys/epoll.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100030
Garfield Tan1c7bc862020-01-28 13:24:04 -080031#include <cinttypes>
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080032#include <compare>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070033#include <thread>
Garfield Tan1c7bc862020-01-28 13:24:04 -080034#include <unordered_set>
chaviwd1c23182019-12-20 18:44:56 -080035#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080036
Garfield Tan1c7bc862020-01-28 13:24:04 -080037using android::base::StringPrintf;
chaviw3277faf2021-05-19 16:45:23 -050038using android::gui::FocusRequest;
39using android::gui::TouchOcclusionMode;
40using android::gui::WindowInfo;
41using android::gui::WindowInfoHandle;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080042using android::os::InputEventInjectionResult;
43using android::os::InputEventInjectionSync;
Garfield Tan1c7bc862020-01-28 13:24:04 -080044
Garfield Tane84e6f92019-08-29 17:28:41 -070045namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080046
Dominik Laskowski2f01d772022-03-23 16:01:29 -070047using namespace ftl::flag_operators;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -080048using testing::AllOf;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070049
Michael Wrightd02c5b62014-02-10 15:10:22 -080050// An arbitrary time value.
Prabir Pradhan5735a322022-04-11 17:23:34 +000051static constexpr nsecs_t ARBITRARY_TIME = 1234;
Michael Wrightd02c5b62014-02-10 15:10:22 -080052
53// An arbitrary device id.
Prabir Pradhan5735a322022-04-11 17:23:34 +000054static constexpr int32_t DEVICE_ID = 1;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -080055static constexpr int32_t SECOND_DEVICE_ID = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -080056
Jeff Brownf086ddb2014-02-11 14:28:48 -080057// An arbitrary display id.
Arthur Hungabbb9d82021-09-01 14:52:30 +000058static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
59static constexpr int32_t SECOND_DISPLAY_ID = 1;
Jeff Brownf086ddb2014-02-11 14:28:48 -080060
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080061static constexpr int32_t ACTION_DOWN = AMOTION_EVENT_ACTION_DOWN;
62static constexpr int32_t ACTION_MOVE = AMOTION_EVENT_ACTION_MOVE;
63static constexpr int32_t ACTION_UP = AMOTION_EVENT_ACTION_UP;
64static constexpr int32_t ACTION_HOVER_ENTER = AMOTION_EVENT_ACTION_HOVER_ENTER;
65static constexpr int32_t ACTION_HOVER_EXIT = AMOTION_EVENT_ACTION_HOVER_EXIT;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080066static constexpr int32_t ACTION_OUTSIDE = AMOTION_EVENT_ACTION_OUTSIDE;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -080067static constexpr int32_t ACTION_CANCEL = AMOTION_EVENT_ACTION_CANCEL;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080068/**
69 * The POINTER_DOWN(0) is an unusual, but valid, action. It just means that the new pointer in the
70 * MotionEvent is at the index 0 rather than 1 (or later). That is, the pointer id=0 (which is at
71 * index 0) is the new pointer going down. The same pointer could have been placed at a different
72 * index, and the action would become POINTER_1_DOWN, 2, etc..; these would all be valid. In
73 * general, we try to place pointer id = 0 at the index 0. Of course, this is not possible if
74 * pointer id=0 leaves but the pointer id=1 remains.
75 */
76static constexpr int32_t POINTER_0_DOWN =
77 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080078static constexpr int32_t POINTER_1_DOWN =
79 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +000080static constexpr int32_t POINTER_2_DOWN =
81 AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +000082static constexpr int32_t POINTER_3_DOWN =
83 AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Arthur Hungc539dbb2022-12-08 07:45:36 +000084static constexpr int32_t POINTER_0_UP =
85 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080086static constexpr int32_t POINTER_1_UP =
87 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
88
Antonio Kantek15beb512022-06-13 22:35:41 +000089// The default pid and uid for windows created on the primary display by the test.
Prabir Pradhan5735a322022-04-11 17:23:34 +000090static constexpr int32_t WINDOW_PID = 999;
91static constexpr int32_t WINDOW_UID = 1001;
92
Antonio Kantek15beb512022-06-13 22:35:41 +000093// The default pid and uid for the windows created on the secondary display by the test.
94static constexpr int32_t SECONDARY_WINDOW_PID = 1010;
95static constexpr int32_t SECONDARY_WINDOW_UID = 1012;
96
Prabir Pradhan5735a322022-04-11 17:23:34 +000097// The default policy flags to use for event injection by tests.
98static constexpr uint32_t DEFAULT_POLICY_FLAGS = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
Michael Wrightd02c5b62014-02-10 15:10:22 -080099
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +0000100// An arbitrary pid of the gesture monitor window
101static constexpr int32_t MONITOR_PID = 2001;
102
Siarhei Vishniakou289e9242022-02-15 14:50:16 -0800103static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms;
104
Arthur Hungc539dbb2022-12-08 07:45:36 +0000105static constexpr int expectedWallpaperFlags =
106 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
107
Siarhei Vishniakou56e79092023-02-21 19:13:16 -0800108using ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID;
109
chaviwd1c23182019-12-20 18:44:56 -0800110struct PointF {
111 float x;
112 float y;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800113 auto operator<=>(const PointF&) const = default;
chaviwd1c23182019-12-20 18:44:56 -0800114};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800115
Gang Wang342c9272020-01-13 13:15:04 -0500116/**
117 * Return a DOWN key event with KEYCODE_A.
118 */
119static KeyEvent getTestKeyEvent() {
120 KeyEvent event;
121
Garfield Tanfbe732e2020-01-24 11:26:14 -0800122 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
123 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
124 ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -0500125 return event;
126}
127
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000128static void assertMotionAction(int32_t expectedAction, int32_t receivedAction) {
129 ASSERT_EQ(expectedAction, receivedAction)
130 << "expected " << MotionEvent::actionToString(expectedAction) << ", got "
131 << MotionEvent::actionToString(receivedAction);
132}
133
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800134MATCHER_P(WithMotionAction, action, "MotionEvent with specified action") {
135 bool matches = action == arg.getAction();
136 if (!matches) {
137 *result_listener << "expected action " << MotionEvent::actionToString(action)
138 << ", but got " << MotionEvent::actionToString(arg.getAction());
139 }
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -0800140 if (action == AMOTION_EVENT_ACTION_DOWN) {
141 if (!matches) {
142 *result_listener << "; ";
143 }
144 *result_listener << "downTime should match eventTime for ACTION_DOWN events";
145 matches &= arg.getDownTime() == arg.getEventTime();
146 }
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800147 if (action == AMOTION_EVENT_ACTION_CANCEL) {
148 if (!matches) {
149 *result_listener << "; ";
150 }
151 *result_listener << "expected FLAG_CANCELED to be set with ACTION_CANCEL, but was not set";
152 matches &= (arg.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0;
153 }
154 return matches;
155}
156
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -0800157MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") {
158 return arg.getDownTime() == downTime;
159}
160
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800161MATCHER_P(WithDisplayId, displayId, "InputEvent with specified displayId") {
162 return arg.getDisplayId() == displayId;
163}
164
Siarhei Vishniakouf372b812023-02-14 18:06:51 -0800165MATCHER_P(WithDeviceId, deviceId, "InputEvent with specified deviceId") {
166 return arg.getDeviceId() == deviceId;
167}
168
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800169MATCHER_P(WithSource, source, "InputEvent with specified source") {
170 *result_listener << "expected source " << inputEventSourceToString(source) << ", but got "
171 << inputEventSourceToString(arg.getSource());
172 return arg.getSource() == source;
173}
174
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800175MATCHER_P(WithFlags, flags, "InputEvent with specified flags") {
176 return arg.getFlags() == flags;
177}
178
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800179MATCHER_P2(WithCoords, x, y, "MotionEvent with specified coordinates") {
180 if (arg.getPointerCount() != 1) {
181 *result_listener << "Expected 1 pointer, got " << arg.getPointerCount();
182 return false;
183 }
Harry Cutts33476232023-01-30 19:57:29 +0000184 return arg.getX(/*pointerIndex=*/0) == x && arg.getY(/*pointerIndex=*/0) == y;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800185}
186
Siarhei Vishniakouf372b812023-02-14 18:06:51 -0800187MATCHER_P(WithPointerCount, pointerCount, "MotionEvent with specified number of pointers") {
188 return arg.getPointerCount() == pointerCount;
189}
190
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800191MATCHER_P(WithPointers, pointers, "MotionEvent with specified pointers") {
192 // Build a map for the received pointers, by pointer id
193 std::map<int32_t /*pointerId*/, PointF> actualPointers;
194 for (size_t pointerIndex = 0; pointerIndex < arg.getPointerCount(); pointerIndex++) {
195 const int32_t pointerId = arg.getPointerId(pointerIndex);
196 actualPointers[pointerId] = {arg.getX(pointerIndex), arg.getY(pointerIndex)};
197 }
198 return pointers == actualPointers;
199}
200
Michael Wrightd02c5b62014-02-10 15:10:22 -0800201// --- FakeInputDispatcherPolicy ---
202
203class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
204 InputDispatcherConfiguration mConfig;
205
Prabir Pradhanedd96402022-02-15 01:46:16 -0800206 using AnrResult = std::pair<sp<IBinder>, int32_t /*pid*/>;
207
Michael Wrightd02c5b62014-02-10 15:10:22 -0800208protected:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000209 virtual ~FakeInputDispatcherPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800210
211public:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000212 FakeInputDispatcherPolicy() {}
Jackal Guof9696682018-10-05 12:23:23 +0800213
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800214 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700215 assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700216 ASSERT_EQ(event.getType(), InputEventType::KEY);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700217 EXPECT_EQ(event.getDisplayId(), args.displayId);
218
219 const auto& keyEvent = static_cast<const KeyEvent&>(event);
220 EXPECT_EQ(keyEvent.getEventTime(), args.eventTime);
221 EXPECT_EQ(keyEvent.getAction(), args.action);
222 });
Jackal Guof9696682018-10-05 12:23:23 +0800223 }
224
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700225 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) {
226 assertFilterInputEventWasCalledInternal([&](const InputEvent& event) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700227 ASSERT_EQ(event.getType(), InputEventType::MOTION);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700228 EXPECT_EQ(event.getDisplayId(), args.displayId);
229
230 const auto& motionEvent = static_cast<const MotionEvent&>(event);
231 EXPECT_EQ(motionEvent.getEventTime(), args.eventTime);
232 EXPECT_EQ(motionEvent.getAction(), args.action);
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000233 EXPECT_NEAR(motionEvent.getX(0), point.x, MotionEvent::ROUNDING_PRECISION);
234 EXPECT_NEAR(motionEvent.getY(0), point.y, MotionEvent::ROUNDING_PRECISION);
235 EXPECT_NEAR(motionEvent.getRawX(0), point.x, MotionEvent::ROUNDING_PRECISION);
236 EXPECT_NEAR(motionEvent.getRawY(0), point.y, MotionEvent::ROUNDING_PRECISION);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700237 });
Jackal Guof9696682018-10-05 12:23:23 +0800238 }
239
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700240 void assertFilterInputEventWasNotCalled() {
241 std::scoped_lock lock(mLock);
242 ASSERT_EQ(nullptr, mFilteredEvent);
243 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800244
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800245 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700246 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800247 ASSERT_TRUE(mConfigurationChangedTime)
248 << "Timed out waiting for configuration changed call";
249 ASSERT_EQ(*mConfigurationChangedTime, when);
250 mConfigurationChangedTime = std::nullopt;
251 }
252
253 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700254 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800255 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800256 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800257 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
258 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
259 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
260 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
261 mLastNotifySwitch = std::nullopt;
262 }
263
chaviwfd6d3512019-03-25 13:23:49 -0700264 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700265 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800266 ASSERT_EQ(touchedToken, mOnPointerDownToken);
267 mOnPointerDownToken.clear();
268 }
269
270 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700271 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800272 ASSERT_TRUE(mOnPointerDownToken == nullptr)
273 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700274 }
275
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700276 // This function must be called soon after the expected ANR timer starts,
277 // because we are also checking how much time has passed.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500278 void assertNotifyNoFocusedWindowAnrWasCalled(
Chris Yea209fde2020-07-22 13:54:51 -0700279 std::chrono::nanoseconds timeout,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500280 const std::shared_ptr<InputApplicationHandle>& expectedApplication) {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800281 std::unique_lock lock(mLock);
282 android::base::ScopedLockAssertion assumeLocked(mLock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500283 std::shared_ptr<InputApplicationHandle> application;
Prabir Pradhanedd96402022-02-15 01:46:16 -0800284 ASSERT_NO_FATAL_FAILURE(
285 application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock));
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500286 ASSERT_EQ(expectedApplication, application);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700287 }
288
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000289 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
Prabir Pradhanedd96402022-02-15 01:46:16 -0800290 const sp<WindowInfoHandle>& window) {
291 LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null");
292 assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(),
293 window->getInfo()->ownerPid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500294 }
295
Prabir Pradhanedd96402022-02-15 01:46:16 -0800296 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
297 const sp<IBinder>& expectedToken,
298 int32_t expectedPid) {
299 std::unique_lock lock(mLock);
300 android::base::ScopedLockAssertion assumeLocked(mLock);
301 AnrResult result;
302 ASSERT_NO_FATAL_FAILURE(result =
303 getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock));
304 const auto& [token, pid] = result;
305 ASSERT_EQ(expectedToken, token);
306 ASSERT_EQ(expectedPid, pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500307 }
308
Prabir Pradhanedd96402022-02-15 01:46:16 -0800309 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000310 sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500311 std::unique_lock lock(mLock);
312 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800313 AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock);
314 const auto& [token, _] = result;
315 return token;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000316 }
317
Prabir Pradhanedd96402022-02-15 01:46:16 -0800318 void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken,
319 int32_t expectedPid) {
320 std::unique_lock lock(mLock);
321 android::base::ScopedLockAssertion assumeLocked(mLock);
322 AnrResult result;
323 ASSERT_NO_FATAL_FAILURE(
324 result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock));
325 const auto& [token, pid] = result;
326 ASSERT_EQ(expectedToken, token);
327 ASSERT_EQ(expectedPid, pid);
328 }
329
330 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000331 sp<IBinder> getResponsiveWindowToken() {
332 std::unique_lock lock(mLock);
333 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800334 AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock);
335 const auto& [token, _] = result;
336 return token;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700337 }
338
339 void assertNotifyAnrWasNotCalled() {
340 std::scoped_lock lock(mLock);
341 ASSERT_TRUE(mAnrApplications.empty());
Prabir Pradhanedd96402022-02-15 01:46:16 -0800342 ASSERT_TRUE(mAnrWindows.empty());
343 ASSERT_TRUE(mResponsiveWindows.empty())
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500344 << "ANR was not called, but please also consume the 'connection is responsive' "
345 "signal";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700346 }
347
Garfield Tan1c7bc862020-01-28 13:24:04 -0800348 void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) {
349 mConfig.keyRepeatTimeout = timeout;
350 mConfig.keyRepeatDelay = delay;
351 }
352
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000353 PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -0800354 std::unique_lock lock(mLock);
355 base::ScopedLockAssertion assumeLocked(mLock);
356
357 if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms,
358 [this, enabled]() REQUIRES(mLock) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000359 return mPointerCaptureRequest->enable ==
Prabir Pradhan99987712020-11-10 18:43:05 -0800360 enabled;
361 })) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000362 ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled
363 << ") to be called.";
364 return {};
Prabir Pradhan99987712020-11-10 18:43:05 -0800365 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000366 auto request = *mPointerCaptureRequest;
367 mPointerCaptureRequest.reset();
368 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -0800369 }
370
371 void assertSetPointerCaptureNotCalled() {
372 std::unique_lock lock(mLock);
373 base::ScopedLockAssertion assumeLocked(mLock);
374
375 if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000376 FAIL() << "Expected setPointerCapture(request) to not be called, but was called. "
Prabir Pradhan99987712020-11-10 18:43:05 -0800377 "enabled = "
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000378 << std::to_string(mPointerCaptureRequest->enable);
Prabir Pradhan99987712020-11-10 18:43:05 -0800379 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000380 mPointerCaptureRequest.reset();
Prabir Pradhan99987712020-11-10 18:43:05 -0800381 }
382
arthurhungf452d0b2021-01-06 00:19:52 +0800383 void assertDropTargetEquals(const sp<IBinder>& targetToken) {
384 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800385 ASSERT_TRUE(mNotifyDropWindowWasCalled);
arthurhungf452d0b2021-01-06 00:19:52 +0800386 ASSERT_EQ(targetToken, mDropTargetWindowToken);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800387 mNotifyDropWindowWasCalled = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800388 }
389
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800390 void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) {
391 std::unique_lock lock(mLock);
392 base::ScopedLockAssertion assumeLocked(mLock);
393 std::optional<sp<IBinder>> receivedToken =
394 getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock,
395 mNotifyInputChannelBroken);
396 ASSERT_TRUE(receivedToken.has_value());
397 ASSERT_EQ(token, *receivedToken);
398 }
399
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800400 /**
401 * Set policy timeout. A value of zero means next key will not be intercepted.
402 */
403 void setInterceptKeyTimeout(std::chrono::milliseconds timeout) {
404 mInterceptKeyTimeout = timeout;
405 }
406
Michael Wrightd02c5b62014-02-10 15:10:22 -0800407private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700408 std::mutex mLock;
409 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
410 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
411 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
412 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800413
Prabir Pradhan99987712020-11-10 18:43:05 -0800414 std::condition_variable mPointerCaptureChangedCondition;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000415
416 std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock);
Prabir Pradhan99987712020-11-10 18:43:05 -0800417
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700418 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700419 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800420 std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock);
421 std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700422 std::condition_variable mNotifyAnr;
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800423 std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock);
424 std::condition_variable mNotifyInputChannelBroken;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700425
arthurhungf452d0b2021-01-06 00:19:52 +0800426 sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800427 bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800428
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800429 std::chrono::milliseconds mInterceptKeyTimeout = 0ms;
430
Prabir Pradhanedd96402022-02-15 01:46:16 -0800431 // All three ANR-related callbacks behave the same way, so we use this generic function to wait
432 // for a specific container to become non-empty. When the container is non-empty, return the
433 // first entry from the container and erase it.
434 template <class T>
435 T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage,
436 std::unique_lock<std::mutex>& lock) REQUIRES(mLock) {
437 // If there is an ANR, Dispatcher won't be idle because there are still events
438 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
439 // before checking if ANR was called.
440 // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need
441 // to provide it some time to act. 100ms seems reasonable.
442 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
443 const std::chrono::time_point start = std::chrono::steady_clock::now();
444 std::optional<T> token =
445 getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr);
446 if (!token.has_value()) {
447 ADD_FAILURE() << "Did not receive the ANR callback";
448 return {};
449 }
450
451 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
452 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
453 // the dispatcher started counting before this function was called
454 if (std::chrono::abs(timeout - waited) > 100ms) {
455 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
456 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
457 << "ms, but waited "
458 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
459 << "ms instead";
460 }
461 return *token;
462 }
463
464 template <class T>
465 std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout,
466 std::queue<T>& storage,
467 std::unique_lock<std::mutex>& lock,
468 std::condition_variable& condition)
469 REQUIRES(mLock) {
470 condition.wait_for(lock, timeout,
471 [&storage]() REQUIRES(mLock) { return !storage.empty(); });
472 if (storage.empty()) {
473 ADD_FAILURE() << "Did not receive the expected callback";
474 return std::nullopt;
475 }
476 T item = storage.front();
477 storage.pop();
478 return std::make_optional(item);
479 }
480
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600481 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700482 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800483 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800484 }
485
Prabir Pradhanedd96402022-02-15 01:46:16 -0800486 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<int32_t> pid,
487 const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700488 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800489 ASSERT_TRUE(pid.has_value());
490 mAnrWindows.push({connectionToken, *pid});
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700491 mNotifyAnr.notify_all();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500492 }
493
Prabir Pradhanedd96402022-02-15 01:46:16 -0800494 void notifyWindowResponsive(const sp<IBinder>& connectionToken,
495 std::optional<int32_t> pid) override {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500496 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800497 ASSERT_TRUE(pid.has_value());
498 mResponsiveWindows.push({connectionToken, *pid});
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500499 mNotifyAnr.notify_all();
500 }
501
502 void notifyNoFocusedWindowAnr(
503 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
504 std::scoped_lock lock(mLock);
505 mAnrApplications.push(applicationHandle);
506 mNotifyAnr.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800507 }
508
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800509 void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override {
510 std::scoped_lock lock(mLock);
511 mBrokenInputChannels.push(connectionToken);
512 mNotifyInputChannelBroken.notify_all();
513 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800514
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600515 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700516
Chris Yef59a2f42020-10-16 12:55:26 -0700517 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
518 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
519 const std::vector<float>& values) override {}
520
521 void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType,
522 InputDeviceSensorAccuracy accuracy) override {}
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000523
Chris Yefb552902021-02-03 17:18:37 -0800524 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
525
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600526 void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800527 *outConfig = mConfig;
528 }
529
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600530 bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700531 std::scoped_lock lock(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800532 switch (inputEvent->getType()) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700533 case InputEventType::KEY: {
Jackal Guof9696682018-10-05 12:23:23 +0800534 const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800535 mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800536 break;
537 }
538
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700539 case InputEventType::MOTION: {
Jackal Guof9696682018-10-05 12:23:23 +0800540 const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800541 mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800542 break;
543 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700544 default: {
545 ADD_FAILURE() << "Should only filter keys or motions";
546 break;
547 }
Jackal Guof9696682018-10-05 12:23:23 +0800548 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800549 return true;
550 }
551
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800552 void interceptKeyBeforeQueueing(const KeyEvent* inputEvent, uint32_t&) override {
553 if (inputEvent->getAction() == AKEY_EVENT_ACTION_UP) {
554 // Clear intercept state when we handled the event.
555 mInterceptKeyTimeout = 0ms;
556 }
557 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800558
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600559 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800560
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600561 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800562 nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count();
563 // Clear intercept state so we could dispatch the event in next wake.
564 mInterceptKeyTimeout = 0ms;
565 return delay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800566 }
567
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600568 bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800569 return false;
570 }
571
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600572 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
573 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700574 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800575 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
576 * essentially a passthrough for notifySwitch.
577 */
Harry Cutts33476232023-01-30 19:57:29 +0000578 mLastNotifySwitch = NotifySwitchArgs(/*id=*/1, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800579 }
580
Sean Stoutb4e0a592021-02-23 07:34:53 -0800581 void pokeUserActivity(nsecs_t, int32_t, int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800582
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600583 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700584 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700585 mOnPointerDownToken = newToken;
586 }
587
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000588 void setPointerCapture(const PointerCaptureRequest& request) override {
Prabir Pradhan99987712020-11-10 18:43:05 -0800589 std::scoped_lock lock(mLock);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000590 mPointerCaptureRequest = {request};
Prabir Pradhan99987712020-11-10 18:43:05 -0800591 mPointerCaptureChangedCondition.notify_all();
592 }
593
arthurhungf452d0b2021-01-06 00:19:52 +0800594 void notifyDropWindow(const sp<IBinder>& token, float x, float y) override {
595 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800596 mNotifyDropWindowWasCalled = true;
arthurhungf452d0b2021-01-06 00:19:52 +0800597 mDropTargetWindowToken = token;
598 }
599
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700600 void assertFilterInputEventWasCalledInternal(
601 const std::function<void(const InputEvent&)>& verify) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700602 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800603 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700604 verify(*mFilteredEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800605 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800606 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800607};
608
Michael Wrightd02c5b62014-02-10 15:10:22 -0800609// --- InputDispatcherTest ---
610
611class InputDispatcherTest : public testing::Test {
612protected:
613 sp<FakeInputDispatcherPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700614 std::unique_ptr<InputDispatcher> mDispatcher;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800615
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000616 void SetUp() override {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700617 mFakePolicy = sp<FakeInputDispatcherPolicy>::make();
Siarhei Vishniakou289e9242022-02-15 14:50:16 -0800618 mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy, STALE_EVENT_TIMEOUT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800619 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000620 // Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700621 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800622 }
623
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000624 void TearDown() override {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700625 ASSERT_EQ(OK, mDispatcher->stop());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800626 mFakePolicy.clear();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700627 mDispatcher.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800628 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700629
630 /**
631 * Used for debugging when writing the test
632 */
633 void dumpDispatcherState() {
634 std::string dump;
635 mDispatcher->dump(dump);
636 std::stringstream ss(dump);
637 std::string to;
638
639 while (std::getline(ss, to, '\n')) {
640 ALOGE("%s", to.c_str());
641 }
642 }
Vishnu Nair958da932020-08-21 17:12:37 -0700643
Chavi Weingarten847e8512023-03-29 00:26:09 +0000644 void setFocusedWindow(const sp<WindowInfoHandle>& window) {
Vishnu Nair958da932020-08-21 17:12:37 -0700645 FocusRequest request;
646 request.token = window->getToken();
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +0000647 request.windowName = window->getName();
Vishnu Nair958da932020-08-21 17:12:37 -0700648 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
649 request.displayId = window->getInfo()->displayId;
650 mDispatcher->setFocusedWindow(request);
651 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800652};
653
Michael Wrightd02c5b62014-02-10 15:10:22 -0800654TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
655 KeyEvent event;
656
657 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800658 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
659 INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600660 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
661 ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800662 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000663 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000664 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800665 << "Should reject key events with undefined action.";
666
667 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800668 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
669 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600670 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800671 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000672 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000673 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800674 << "Should reject key events with ACTION_MULTIPLE.";
675}
676
677TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
678 MotionEvent event;
679 PointerProperties pointerProperties[MAX_POINTERS + 1];
680 PointerCoords pointerCoords[MAX_POINTERS + 1];
Siarhei Vishniakou01747382022-01-20 13:23:27 -0800681 for (size_t i = 0; i <= MAX_POINTERS; i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800682 pointerProperties[i].clear();
683 pointerProperties[i].id = i;
684 pointerCoords[i].clear();
685 }
686
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800687 // Some constants commonly used below
688 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
689 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
690 constexpr int32_t metaState = AMETA_NONE;
691 constexpr MotionClassification classification = MotionClassification::NONE;
692
chaviw9eaa22c2020-07-01 16:21:27 -0700693 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800694 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800695 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
chaviw9eaa22c2020-07-01 16:21:27 -0700696 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification,
697 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700698 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
699 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700700 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800701 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000702 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000703 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800704 << "Should reject motion events with undefined action.";
705
706 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800707 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800708 POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
709 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
710 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
711 ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500712 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800713 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000714 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000715 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800716 << "Should reject motion events with pointer down index too large.";
717
Garfield Tanfbe732e2020-01-24 11:26:14 -0800718 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700719 AMOTION_EVENT_ACTION_POINTER_DOWN |
720 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700721 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
722 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700723 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500724 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800725 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000726 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000727 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800728 << "Should reject motion events with pointer down index too small.";
729
730 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800731 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800732 POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform,
733 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
734 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
735 ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500736 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800737 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000738 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000739 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800740 << "Should reject motion events with pointer up index too large.";
741
Garfield Tanfbe732e2020-01-24 11:26:14 -0800742 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700743 AMOTION_EVENT_ACTION_POINTER_UP |
744 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700745 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
746 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700747 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500748 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800749 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000750 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000751 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800752 << "Should reject motion events with pointer up index too small.";
753
754 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800755 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
756 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700757 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700758 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
759 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700760 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800761 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000762 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000763 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800764 << "Should reject motion events with 0 pointers.";
765
Garfield Tanfbe732e2020-01-24 11:26:14 -0800766 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
767 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700768 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700769 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
770 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700771 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800772 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000773 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000774 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800775 << "Should reject motion events with more than MAX_POINTERS pointers.";
776
777 // Rejects motion events with invalid pointer ids.
778 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800779 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
780 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700781 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700782 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
783 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700784 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800785 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000786 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000787 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800788 << "Should reject motion events with pointer ids less than 0.";
789
790 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800791 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
792 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700793 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700794 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
795 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700796 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800797 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000798 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000799 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800800 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
801
802 // Rejects motion events with duplicate pointer ids.
803 pointerProperties[0].id = 1;
804 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800805 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
806 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700807 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700808 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
809 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700810 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800811 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000812 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000813 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800814 << "Should reject motion events with duplicate pointer ids.";
815}
816
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800817/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
818
819TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
820 constexpr nsecs_t eventTime = 20;
Prabir Pradhan678438e2023-04-13 19:32:51 +0000821 mDispatcher->notifyConfigurationChanged({/*id=*/10, eventTime});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800822 ASSERT_TRUE(mDispatcher->waitForIdle());
823
824 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
825}
826
827TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Harry Cutts33476232023-01-30 19:57:29 +0000828 NotifySwitchArgs args(/*id=*/10, /*eventTime=*/20, /*policyFlags=*/0, /*switchValues=*/1,
829 /*switchMask=*/2);
Prabir Pradhan678438e2023-04-13 19:32:51 +0000830 mDispatcher->notifySwitch(args);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800831
832 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
833 args.policyFlags |= POLICY_FLAG_TRUSTED;
834 mFakePolicy->assertNotifySwitchWasCalled(args);
835}
836
Arthur Hungb92218b2018-08-14 12:00:21 +0800837// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700838static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -0700839// Default input dispatching timeout if there is no focused application or paused window
840// from which to determine an appropriate dispatching timeout.
841static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds(
842 android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS *
843 android::base::HwTimeoutMultiplier());
Arthur Hungb92218b2018-08-14 12:00:21 +0800844
845class FakeApplicationHandle : public InputApplicationHandle {
846public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700847 FakeApplicationHandle() {
848 mInfo.name = "Fake Application";
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700849 mInfo.token = sp<BBinder>::make();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500850 mInfo.dispatchingTimeoutMillis =
851 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700852 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800853 virtual ~FakeApplicationHandle() {}
854
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000855 virtual bool updateInfo() override { return true; }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700856
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500857 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
858 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700859 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800860};
861
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800862class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800863public:
Garfield Tan15601662020-09-22 15:32:38 -0700864 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800865 : mName(name) {
Garfield Tan15601662020-09-22 15:32:38 -0700866 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800867 }
868
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800869 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700870 InputEvent* event;
871 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
872 if (!consumeSeq) {
873 return nullptr;
874 }
875 finishEvent(*consumeSeq);
876 return event;
877 }
878
879 /**
880 * Receive an event without acknowledging it.
881 * Return the sequence number that could later be used to send finished signal.
882 */
883 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800884 uint32_t consumeSeq;
885 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800886
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800887 std::chrono::time_point start = std::chrono::steady_clock::now();
888 status_t status = WOULD_BLOCK;
889 while (status == WOULD_BLOCK) {
Harry Cutts33476232023-01-30 19:57:29 +0000890 status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800891 &event);
892 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
893 if (elapsed > 100ms) {
894 break;
895 }
896 }
897
898 if (status == WOULD_BLOCK) {
899 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700900 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800901 }
902
903 if (status != OK) {
904 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700905 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800906 }
907 if (event == nullptr) {
908 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700909 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800910 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700911 if (outEvent != nullptr) {
912 *outEvent = event;
913 }
914 return consumeSeq;
915 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800916
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700917 /**
918 * To be used together with "receiveEvent" to complete the consumption of an event.
919 */
920 void finishEvent(uint32_t consumeSeq) {
921 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
922 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800923 }
924
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000925 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
926 const status_t status = mConsumer->sendTimeline(inputEventId, timeline);
927 ASSERT_EQ(OK, status);
928 }
929
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700930 void consumeEvent(InputEventType expectedEventType, int32_t expectedAction,
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000931 std::optional<int32_t> expectedDisplayId,
932 std::optional<int32_t> expectedFlags) {
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800933 InputEvent* event = consume();
934
935 ASSERT_NE(nullptr, event) << mName.c_str()
936 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800937 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700938 << mName.c_str() << " expected " << ftl::enum_string(expectedEventType)
939 << " event, got " << *event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800940
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000941 if (expectedDisplayId.has_value()) {
942 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
943 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800944
Tiger Huang8664f8c2018-10-11 19:14:35 +0800945 switch (expectedEventType) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700946 case InputEventType::KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800947 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
948 EXPECT_EQ(expectedAction, keyEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000949 if (expectedFlags.has_value()) {
950 EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags());
951 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800952 break;
953 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700954 case InputEventType::MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800955 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000956 assertMotionAction(expectedAction, motionEvent.getAction());
957
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000958 if (expectedFlags.has_value()) {
959 EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags());
960 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800961 break;
962 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700963 case InputEventType::FOCUS: {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100964 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
965 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700966 case InputEventType::CAPTURE: {
Prabir Pradhan99987712020-11-10 18:43:05 -0800967 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
968 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700969 case InputEventType::TOUCH_MODE: {
Antonio Kantekf16f2832021-09-28 04:39:20 +0000970 FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events";
971 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700972 case InputEventType::DRAG: {
arthurhungb89ccb02020-12-30 16:19:01 +0800973 FAIL() << "Use 'consumeDragEvent' for DRAG events";
974 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800975 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800976 }
977
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800978 MotionEvent* consumeMotion() {
979 InputEvent* event = consume();
980
981 if (event == nullptr) {
982 ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one.";
983 return nullptr;
984 }
985
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700986 if (event->getType() != InputEventType::MOTION) {
987 ADD_FAILURE() << mName << " expected a MotionEvent, got " << *event;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800988 return nullptr;
989 }
990 return static_cast<MotionEvent*>(event);
991 }
992
993 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
994 MotionEvent* motionEvent = consumeMotion();
995 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
996 ASSERT_THAT(*motionEvent, matcher);
997 }
998
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100999 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
1000 InputEvent* event = consume();
1001 ASSERT_NE(nullptr, event) << mName.c_str()
1002 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001003 ASSERT_EQ(InputEventType::FOCUS, event->getType())
1004 << "Instead of FocusEvent, got " << *event;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001005
1006 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1007 << mName.c_str() << ": event displayId should always be NONE.";
1008
1009 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
1010 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001011 }
1012
Prabir Pradhan99987712020-11-10 18:43:05 -08001013 void consumeCaptureEvent(bool hasCapture) {
1014 const InputEvent* event = consume();
1015 ASSERT_NE(nullptr, event) << mName.c_str()
1016 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001017 ASSERT_EQ(InputEventType::CAPTURE, event->getType())
1018 << "Instead of CaptureEvent, got " << *event;
Prabir Pradhan99987712020-11-10 18:43:05 -08001019
1020 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1021 << mName.c_str() << ": event displayId should always be NONE.";
1022
1023 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
1024 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
1025 }
1026
arthurhungb89ccb02020-12-30 16:19:01 +08001027 void consumeDragEvent(bool isExiting, float x, float y) {
1028 const InputEvent* event = consume();
1029 ASSERT_NE(nullptr, event) << mName.c_str()
1030 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001031 ASSERT_EQ(InputEventType::DRAG, event->getType()) << "Instead of DragEvent, got " << *event;
arthurhungb89ccb02020-12-30 16:19:01 +08001032
1033 EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1034 << mName.c_str() << ": event displayId should always be NONE.";
1035
1036 const auto& dragEvent = static_cast<const DragEvent&>(*event);
1037 EXPECT_EQ(isExiting, dragEvent.isExiting());
1038 EXPECT_EQ(x, dragEvent.getX());
1039 EXPECT_EQ(y, dragEvent.getY());
1040 }
1041
Antonio Kantekf16f2832021-09-28 04:39:20 +00001042 void consumeTouchModeEvent(bool inTouchMode) {
1043 const InputEvent* event = consume();
1044 ASSERT_NE(nullptr, event) << mName.c_str()
1045 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001046 ASSERT_EQ(InputEventType::TOUCH_MODE, event->getType())
1047 << "Instead of TouchModeEvent, got " << *event;
Antonio Kantekf16f2832021-09-28 04:39:20 +00001048
1049 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1050 << mName.c_str() << ": event displayId should always be NONE.";
1051 const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event);
1052 EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode());
1053 }
1054
chaviwd1c23182019-12-20 18:44:56 -08001055 void assertNoEvents() {
1056 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001057 if (event == nullptr) {
1058 return;
1059 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001060 if (event->getType() == InputEventType::KEY) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001061 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
1062 ADD_FAILURE() << "Received key event "
1063 << KeyEvent::actionToString(keyEvent.getAction());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001064 } else if (event->getType() == InputEventType::MOTION) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001065 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1066 ADD_FAILURE() << "Received motion event "
1067 << MotionEvent::actionToString(motionEvent.getAction());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001068 } else if (event->getType() == InputEventType::FOCUS) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001069 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
1070 ADD_FAILURE() << "Received focus event, hasFocus = "
1071 << (focusEvent.getHasFocus() ? "true" : "false");
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001072 } else if (event->getType() == InputEventType::CAPTURE) {
Prabir Pradhan99987712020-11-10 18:43:05 -08001073 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
1074 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
1075 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001076 } else if (event->getType() == InputEventType::TOUCH_MODE) {
Antonio Kantekf16f2832021-09-28 04:39:20 +00001077 const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event);
1078 ADD_FAILURE() << "Received touch mode event, inTouchMode = "
1079 << (touchModeEvent.isInTouchMode() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001080 }
1081 FAIL() << mName.c_str()
1082 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -08001083 }
1084
1085 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
1086
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001087 int getChannelFd() { return mConsumer->getChannel()->getFd().get(); }
1088
chaviwd1c23182019-12-20 18:44:56 -08001089protected:
1090 std::unique_ptr<InputConsumer> mConsumer;
1091 PreallocatedInputEventFactory mEventFactory;
1092
1093 std::string mName;
1094};
1095
chaviw3277faf2021-05-19 16:45:23 -05001096class FakeWindowHandle : public WindowInfoHandle {
chaviwd1c23182019-12-20 18:44:56 -08001097public:
1098 static const int32_t WIDTH = 600;
1099 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -08001100
Chris Yea209fde2020-07-22 13:54:51 -07001101 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001102 const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001103 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -08001104 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001105 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -07001106 base::Result<std::unique_ptr<InputChannel>> channel =
1107 dispatcher->createInputChannel(name);
1108 token = (*channel)->getConnectionToken();
1109 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -08001110 }
1111
1112 inputApplicationHandle->updateInfo();
1113 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
1114
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001115 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001116 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -08001117 mInfo.name = name;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001118 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001119 mInfo.alpha = 1.0;
chaviwd1c23182019-12-20 18:44:56 -08001120 mInfo.frameLeft = 0;
1121 mInfo.frameTop = 0;
1122 mInfo.frameRight = WIDTH;
1123 mInfo.frameBottom = HEIGHT;
chaviw1ff3d1e2020-07-01 15:53:47 -07001124 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -08001125 mInfo.globalScaleFactor = 1.0;
1126 mInfo.touchableRegion.clear();
1127 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
Prabir Pradhan5735a322022-04-11 17:23:34 +00001128 mInfo.ownerPid = WINDOW_PID;
1129 mInfo.ownerUid = WINDOW_UID;
chaviwd1c23182019-12-20 18:44:56 -08001130 mInfo.displayId = displayId;
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001131 mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT;
chaviwd1c23182019-12-20 18:44:56 -08001132 }
1133
Arthur Hungabbb9d82021-09-01 14:52:30 +00001134 sp<FakeWindowHandle> clone(
1135 const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001136 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId) {
Arthur Hungabbb9d82021-09-01 14:52:30 +00001137 sp<FakeWindowHandle> handle =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001138 sp<FakeWindowHandle>::make(inputApplicationHandle, dispatcher,
1139 mInfo.name + "(Mirror)", displayId, mInfo.token);
Arthur Hungabbb9d82021-09-01 14:52:30 +00001140 return handle;
1141 }
1142
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001143 void setTouchable(bool touchable) {
1144 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable);
1145 }
chaviwd1c23182019-12-20 18:44:56 -08001146
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001147 void setFocusable(bool focusable) {
1148 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable);
1149 }
1150
1151 void setVisible(bool visible) {
1152 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible);
1153 }
Vishnu Nair958da932020-08-21 17:12:37 -07001154
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001155 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001156 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001157 }
1158
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001159 void setPaused(bool paused) {
1160 mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused);
1161 }
1162
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001163 void setPreventSplitting(bool preventSplitting) {
1164 mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001165 }
1166
1167 void setSlippery(bool slippery) {
1168 mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery);
1169 }
1170
1171 void setWatchOutsideTouch(bool watchOutside) {
1172 mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside);
1173 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001174
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001175 void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); }
1176
1177 void setInterceptsStylus(bool interceptsStylus) {
1178 mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus);
1179 }
1180
1181 void setDropInput(bool dropInput) {
1182 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput);
1183 }
1184
1185 void setDropInputIfObscured(bool dropInputIfObscured) {
1186 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured);
1187 }
1188
1189 void setNoInputChannel(bool noInputChannel) {
1190 mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel);
1191 }
1192
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001193 void setAlpha(float alpha) { mInfo.alpha = alpha; }
1194
chaviw3277faf2021-05-19 16:45:23 -05001195 void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; }
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001196
Bernardo Rufino7393d172021-02-26 13:56:11 +00001197 void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
1198
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001199 void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) {
chaviwd1c23182019-12-20 18:44:56 -08001200 mInfo.frameLeft = frame.left;
1201 mInfo.frameTop = frame.top;
1202 mInfo.frameRight = frame.right;
1203 mInfo.frameBottom = frame.bottom;
1204 mInfo.touchableRegion.clear();
1205 mInfo.addTouchableRegion(frame);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001206
1207 const Rect logicalDisplayFrame = displayTransform.transform(frame);
1208 ui::Transform translate;
1209 translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top);
1210 mInfo.transform = translate * displayTransform;
chaviwd1c23182019-12-20 18:44:56 -08001211 }
1212
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001213 void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; }
1214
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001215 void setIsWallpaper(bool isWallpaper) {
1216 mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper);
1217 }
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001218
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001219 void setDupTouchToWallpaper(bool hasWallpaper) {
1220 mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper);
1221 }
chaviwd1c23182019-12-20 18:44:56 -08001222
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001223 void setTrustedOverlay(bool trustedOverlay) {
1224 mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay);
1225 }
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001226
chaviw9eaa22c2020-07-01 16:21:27 -07001227 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
1228 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
1229 }
1230
1231 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -07001232
yunho.shinf4a80b82020-11-16 21:13:57 +09001233 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
1234
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001235 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001236 consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, expectedFlags);
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001237 }
1238
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001239 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001240 consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001241 }
1242
Svet Ganov5d3bc372020-01-26 23:11:07 -08001243 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001244 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001245 consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId),
1246 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001247 }
1248
1249 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001250 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001251 consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
1252 WithDisplayId(expectedDisplayId), WithFlags(expectedFlags)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001253 }
1254
1255 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001256 int32_t expectedFlags = 0) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001257 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
1258 }
1259
1260 void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
1261 std::optional<int32_t> expectedFlags = std::nullopt) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001262 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001263 expectedFlags);
1264 }
1265
Svet Ganov5d3bc372020-01-26 23:11:07 -08001266 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001267 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1268 int32_t expectedFlags = 0) {
1269 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
1270 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001271 consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001272 }
1273
1274 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001275 int32_t expectedFlags = 0) {
1276 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
1277 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001278 consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001279 }
1280
1281 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001282 int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001283 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
Michael Wright3a240c42019-12-10 20:53:41 +00001284 expectedFlags);
1285 }
1286
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001287 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1288 int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001289 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001290 expectedFlags);
1291 }
1292
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001293 void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1294 int32_t expectedFlags = 0) {
1295 InputEvent* event = consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08001296 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001297 ASSERT_EQ(InputEventType::MOTION, event->getType());
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001298 const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1299 EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked());
1300 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getX());
1301 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getY());
1302 }
1303
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001304 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
1305 ASSERT_NE(mInputReceiver, nullptr)
1306 << "Cannot consume events from a window with no receiver";
1307 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
1308 }
1309
Prabir Pradhan99987712020-11-10 18:43:05 -08001310 void consumeCaptureEvent(bool hasCapture) {
1311 ASSERT_NE(mInputReceiver, nullptr)
1312 << "Cannot consume events from a window with no receiver";
1313 mInputReceiver->consumeCaptureEvent(hasCapture);
1314 }
1315
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001316 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
1317 MotionEvent* motionEvent = consumeMotion();
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08001318 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001319 ASSERT_THAT(*motionEvent, matcher);
1320 }
1321
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001322 void consumeEvent(InputEventType expectedEventType, int32_t expectedAction,
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001323 std::optional<int32_t> expectedDisplayId,
1324 std::optional<int32_t> expectedFlags) {
chaviwd1c23182019-12-20 18:44:56 -08001325 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
1326 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
1327 expectedFlags);
1328 }
1329
arthurhungb89ccb02020-12-30 16:19:01 +08001330 void consumeDragEvent(bool isExiting, float x, float y) {
1331 mInputReceiver->consumeDragEvent(isExiting, x, y);
1332 }
1333
Antonio Kantekf16f2832021-09-28 04:39:20 +00001334 void consumeTouchModeEvent(bool inTouchMode) {
1335 ASSERT_NE(mInputReceiver, nullptr)
1336 << "Cannot consume events from a window with no receiver";
1337 mInputReceiver->consumeTouchModeEvent(inTouchMode);
1338 }
1339
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001340 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001341 if (mInputReceiver == nullptr) {
1342 ADD_FAILURE() << "Invalid receive event on window with no receiver";
1343 return std::nullopt;
1344 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001345 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001346 }
1347
1348 void finishEvent(uint32_t sequenceNum) {
1349 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1350 mInputReceiver->finishEvent(sequenceNum);
1351 }
1352
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001353 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
1354 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1355 mInputReceiver->sendTimeline(inputEventId, timeline);
1356 }
1357
chaviwaf87b3e2019-10-01 16:59:28 -07001358 InputEvent* consume() {
1359 if (mInputReceiver == nullptr) {
1360 return nullptr;
1361 }
1362 return mInputReceiver->consume();
1363 }
1364
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001365 MotionEvent* consumeMotion() {
1366 InputEvent* event = consume();
1367 if (event == nullptr) {
1368 ADD_FAILURE() << "Consume failed : no event";
1369 return nullptr;
1370 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001371 if (event->getType() != InputEventType::MOTION) {
1372 ADD_FAILURE() << "Instead of motion event, got " << *event;
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001373 return nullptr;
1374 }
1375 return static_cast<MotionEvent*>(event);
1376 }
1377
Arthur Hungb92218b2018-08-14 12:00:21 +08001378 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001379 if (mInputReceiver == nullptr &&
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001380 mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001381 return; // Can't receive events if the window does not have input channel
1382 }
1383 ASSERT_NE(nullptr, mInputReceiver)
1384 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -08001385 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +08001386 }
1387
chaviwaf87b3e2019-10-01 16:59:28 -07001388 sp<IBinder> getToken() { return mInfo.token; }
1389
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001390 const std::string& getName() { return mName; }
1391
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001392 void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) {
1393 mInfo.ownerPid = ownerPid;
1394 mInfo.ownerUid = ownerUid;
1395 }
1396
Prabir Pradhanedd96402022-02-15 01:46:16 -08001397 int32_t getPid() const { return mInfo.ownerPid; }
1398
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001399 void destroyReceiver() { mInputReceiver = nullptr; }
1400
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001401 int getChannelFd() { return mInputReceiver->getChannelFd(); }
1402
chaviwd1c23182019-12-20 18:44:56 -08001403private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001404 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -08001405 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001406 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001407};
1408
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001409std::atomic<int32_t> FakeWindowHandle::sId{1};
1410
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001411static InputEventInjectionResult injectKey(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001412 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001413 int32_t displayId = ADISPLAY_ID_NONE,
1414 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001415 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001416 bool allowKeyRepeat = true, std::optional<int32_t> targetUid = {},
1417 uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001418 KeyEvent event;
1419 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1420
1421 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001422 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Harry Cutts33476232023-01-30 19:57:29 +00001423 INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount,
1424 currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001425
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001426 if (!allowKeyRepeat) {
1427 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
1428 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001429 // Inject event until dispatch out.
Prabir Pradhan5735a322022-04-11 17:23:34 +00001430 return dispatcher->injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001431}
1432
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001433static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001434 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001435 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001436}
1437
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001438// Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without
1439// sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it
1440// has to be woken up to process the repeating key.
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001441static InputEventInjectionResult injectKeyDownNoRepeat(
1442 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001443 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001444 InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT,
Harry Cutts33476232023-01-30 19:57:29 +00001445 /*allowKeyRepeat=*/false);
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001446}
1447
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001448static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001449 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001450 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001451}
1452
Garfield Tandf26e862020-07-01 20:18:19 -07001453class PointerBuilder {
1454public:
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001455 PointerBuilder(int32_t id, ToolType toolType) {
Garfield Tandf26e862020-07-01 20:18:19 -07001456 mProperties.clear();
1457 mProperties.id = id;
1458 mProperties.toolType = toolType;
1459 mCoords.clear();
1460 }
1461
1462 PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); }
1463
1464 PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); }
1465
1466 PointerBuilder& axis(int32_t axis, float value) {
1467 mCoords.setAxisValue(axis, value);
1468 return *this;
1469 }
1470
1471 PointerProperties buildProperties() const { return mProperties; }
1472
1473 PointerCoords buildCoords() const { return mCoords; }
1474
1475private:
1476 PointerProperties mProperties;
1477 PointerCoords mCoords;
1478};
1479
1480class MotionEventBuilder {
1481public:
1482 MotionEventBuilder(int32_t action, int32_t source) {
1483 mAction = action;
1484 mSource = source;
1485 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001486 mDownTime = mEventTime;
Garfield Tandf26e862020-07-01 20:18:19 -07001487 }
1488
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08001489 MotionEventBuilder& deviceId(int32_t deviceId) {
1490 mDeviceId = deviceId;
1491 return *this;
1492 }
1493
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001494 MotionEventBuilder& downTime(nsecs_t downTime) {
1495 mDownTime = downTime;
1496 return *this;
1497 }
1498
Garfield Tandf26e862020-07-01 20:18:19 -07001499 MotionEventBuilder& eventTime(nsecs_t eventTime) {
1500 mEventTime = eventTime;
1501 return *this;
1502 }
1503
1504 MotionEventBuilder& displayId(int32_t displayId) {
1505 mDisplayId = displayId;
1506 return *this;
1507 }
1508
1509 MotionEventBuilder& actionButton(int32_t actionButton) {
1510 mActionButton = actionButton;
1511 return *this;
1512 }
1513
arthurhung6d4bed92021-03-17 11:59:33 +08001514 MotionEventBuilder& buttonState(int32_t buttonState) {
1515 mButtonState = buttonState;
Garfield Tandf26e862020-07-01 20:18:19 -07001516 return *this;
1517 }
1518
1519 MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) {
1520 mRawXCursorPosition = rawXCursorPosition;
1521 return *this;
1522 }
1523
1524 MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) {
1525 mRawYCursorPosition = rawYCursorPosition;
1526 return *this;
1527 }
1528
1529 MotionEventBuilder& pointer(PointerBuilder pointer) {
1530 mPointers.push_back(pointer);
1531 return *this;
1532 }
1533
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001534 MotionEventBuilder& addFlag(uint32_t flags) {
1535 mFlags |= flags;
1536 return *this;
1537 }
1538
Garfield Tandf26e862020-07-01 20:18:19 -07001539 MotionEvent build() {
1540 std::vector<PointerProperties> pointerProperties;
1541 std::vector<PointerCoords> pointerCoords;
1542 for (const PointerBuilder& pointer : mPointers) {
1543 pointerProperties.push_back(pointer.buildProperties());
1544 pointerCoords.push_back(pointer.buildCoords());
1545 }
1546
1547 // Set mouse cursor position for the most common cases to avoid boilerplate.
1548 if (mSource == AINPUT_SOURCE_MOUSE &&
1549 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) &&
1550 mPointers.size() == 1) {
1551 mRawXCursorPosition = pointerCoords[0].getX();
1552 mRawYCursorPosition = pointerCoords[0].getY();
1553 }
1554
1555 MotionEvent event;
chaviw9eaa22c2020-07-01 16:21:27 -07001556 ui::Transform identityTransform;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08001557 event.initialize(InputEvent::nextId(), mDeviceId, mSource, mDisplayId, INVALID_HMAC,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001558 mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE,
chaviw9eaa22c2020-07-01 16:21:27 -07001559 mButtonState, MotionClassification::NONE, identityTransform,
1560 /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001561 mRawYCursorPosition, identityTransform, mDownTime, mEventTime,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07001562 mPointers.size(), pointerProperties.data(), pointerCoords.data());
Garfield Tandf26e862020-07-01 20:18:19 -07001563
1564 return event;
1565 }
1566
1567private:
1568 int32_t mAction;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08001569 int32_t mDeviceId = DEVICE_ID;
Garfield Tandf26e862020-07-01 20:18:19 -07001570 int32_t mSource;
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001571 nsecs_t mDownTime;
Garfield Tandf26e862020-07-01 20:18:19 -07001572 nsecs_t mEventTime;
1573 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1574 int32_t mActionButton{0};
1575 int32_t mButtonState{0};
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001576 int32_t mFlags{0};
Garfield Tandf26e862020-07-01 20:18:19 -07001577 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1578 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1579
1580 std::vector<PointerBuilder> mPointers;
1581};
1582
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08001583class MotionArgsBuilder {
1584public:
1585 MotionArgsBuilder(int32_t action, int32_t source) {
1586 mAction = action;
1587 mSource = source;
1588 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1589 mDownTime = mEventTime;
1590 }
1591
1592 MotionArgsBuilder& deviceId(int32_t deviceId) {
1593 mDeviceId = deviceId;
1594 return *this;
1595 }
1596
1597 MotionArgsBuilder& downTime(nsecs_t downTime) {
1598 mDownTime = downTime;
1599 return *this;
1600 }
1601
1602 MotionArgsBuilder& eventTime(nsecs_t eventTime) {
1603 mEventTime = eventTime;
1604 return *this;
1605 }
1606
1607 MotionArgsBuilder& displayId(int32_t displayId) {
1608 mDisplayId = displayId;
1609 return *this;
1610 }
1611
1612 MotionArgsBuilder& policyFlags(int32_t policyFlags) {
1613 mPolicyFlags = policyFlags;
1614 return *this;
1615 }
1616
1617 MotionArgsBuilder& actionButton(int32_t actionButton) {
1618 mActionButton = actionButton;
1619 return *this;
1620 }
1621
1622 MotionArgsBuilder& buttonState(int32_t buttonState) {
1623 mButtonState = buttonState;
1624 return *this;
1625 }
1626
1627 MotionArgsBuilder& rawXCursorPosition(float rawXCursorPosition) {
1628 mRawXCursorPosition = rawXCursorPosition;
1629 return *this;
1630 }
1631
1632 MotionArgsBuilder& rawYCursorPosition(float rawYCursorPosition) {
1633 mRawYCursorPosition = rawYCursorPosition;
1634 return *this;
1635 }
1636
1637 MotionArgsBuilder& pointer(PointerBuilder pointer) {
1638 mPointers.push_back(pointer);
1639 return *this;
1640 }
1641
1642 MotionArgsBuilder& addFlag(uint32_t flags) {
1643 mFlags |= flags;
1644 return *this;
1645 }
1646
1647 NotifyMotionArgs build() {
1648 std::vector<PointerProperties> pointerProperties;
1649 std::vector<PointerCoords> pointerCoords;
1650 for (const PointerBuilder& pointer : mPointers) {
1651 pointerProperties.push_back(pointer.buildProperties());
1652 pointerCoords.push_back(pointer.buildCoords());
1653 }
1654
1655 // Set mouse cursor position for the most common cases to avoid boilerplate.
1656 if (mSource == AINPUT_SOURCE_MOUSE &&
1657 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) &&
1658 mPointers.size() == 1) {
1659 mRawXCursorPosition = pointerCoords[0].getX();
1660 mRawYCursorPosition = pointerCoords[0].getY();
1661 }
1662
1663 NotifyMotionArgs args(InputEvent::nextId(), mEventTime, /*readTime=*/mEventTime, mDeviceId,
1664 mSource, mDisplayId, mPolicyFlags, mAction, mActionButton, mFlags,
1665 AMETA_NONE, mButtonState, MotionClassification::NONE, /*edgeFlags=*/0,
1666 mPointers.size(), pointerProperties.data(), pointerCoords.data(),
1667 /*xPrecision=*/0, /*yPrecision=*/0, mRawXCursorPosition,
1668 mRawYCursorPosition, mDownTime, /*videoFrames=*/{});
1669
1670 return args;
1671 }
1672
1673private:
1674 int32_t mAction;
1675 int32_t mDeviceId = DEVICE_ID;
1676 int32_t mSource;
1677 nsecs_t mDownTime;
1678 nsecs_t mEventTime;
1679 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1680 int32_t mPolicyFlags = DEFAULT_POLICY_FLAGS;
1681 int32_t mActionButton{0};
1682 int32_t mButtonState{0};
1683 int32_t mFlags{0};
1684 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1685 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1686
1687 std::vector<PointerBuilder> mPointers;
1688};
1689
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001690static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001691 const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event,
Garfield Tandf26e862020-07-01 20:18:19 -07001692 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001693 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
1694 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
1695 return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout,
1696 policyFlags);
Garfield Tandf26e862020-07-01 20:18:19 -07001697}
1698
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001699static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001700 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source,
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001701 int32_t displayId, const PointF& position = {100, 200},
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001702 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001703 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1704 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001705 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001706 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC),
1707 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Garfield Tandf26e862020-07-01 20:18:19 -07001708 MotionEvent event = MotionEventBuilder(action, source)
1709 .displayId(displayId)
1710 .eventTime(eventTime)
1711 .rawXCursorPosition(cursorPosition.x)
1712 .rawYCursorPosition(cursorPosition.y)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001713 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER)
Garfield Tandf26e862020-07-01 20:18:19 -07001714 .x(position.x)
1715 .y(position.y))
1716 .build();
Arthur Hungb92218b2018-08-14 12:00:21 +08001717
1718 // Inject event until dispatch out.
Prabir Pradhan5735a322022-04-11 17:23:34 +00001719 return injectMotionEvent(dispatcher, event, injectionTimeout, injectionMode, targetUid,
1720 policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001721}
1722
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001723static InputEventInjectionResult injectMotionDown(
1724 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId,
1725 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001726 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001727}
1728
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001729static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001730 int32_t source, int32_t displayId,
1731 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001732 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001733}
1734
Jackal Guof9696682018-10-05 12:23:23 +08001735static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1736 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1737 // Define a valid key event.
Harry Cutts33476232023-01-30 19:57:29 +00001738 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001739 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A,
1740 KEY_A, AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001741
1742 return args;
1743}
1744
Prabir Pradhan678438e2023-04-13 19:32:51 +00001745[[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source,
1746 int32_t displayId,
1747 const std::vector<PointF>& points) {
chaviwd1c23182019-12-20 18:44:56 -08001748 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001749 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1750 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1751 }
1752
chaviwd1c23182019-12-20 18:44:56 -08001753 PointerProperties pointerProperties[pointerCount];
1754 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001755
chaviwd1c23182019-12-20 18:44:56 -08001756 for (size_t i = 0; i < pointerCount; i++) {
1757 pointerProperties[i].clear();
1758 pointerProperties[i].id = i;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001759 pointerProperties[i].toolType = ToolType::FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001760
chaviwd1c23182019-12-20 18:44:56 -08001761 pointerCoords[i].clear();
1762 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1763 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1764 }
Jackal Guof9696682018-10-05 12:23:23 +08001765
1766 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1767 // Define a valid motion event.
Harry Cutts33476232023-01-30 19:57:29 +00001768 NotifyMotionArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001769 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1770 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001771 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1772 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001773 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1774 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001775
1776 return args;
1777}
1778
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001779static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) {
1780 return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points);
1781}
1782
chaviwd1c23182019-12-20 18:44:56 -08001783static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1784 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1785}
1786
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001787static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(
1788 const PointerCaptureRequest& request) {
Harry Cutts33476232023-01-30 19:57:29 +00001789 return NotifyPointerCaptureChangedArgs(/*id=*/0, systemTime(SYSTEM_TIME_MONOTONIC), request);
Prabir Pradhan99987712020-11-10 18:43:05 -08001790}
1791
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001792/**
1793 * When a window unexpectedly disposes of its input channel, policy should be notified about the
1794 * broken channel.
1795 */
1796TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) {
1797 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1798 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001799 sp<FakeWindowHandle>::make(application, mDispatcher,
1800 "Window that breaks its input channel", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001801
1802 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1803
1804 // Window closes its channel, but the window remains.
1805 window->destroyReceiver();
1806 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token);
1807}
1808
Arthur Hungb92218b2018-08-14 12:00:21 +08001809TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001810 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001811 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1812 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001813
Arthur Hung72d8dc32020-03-28 00:48:39 +00001814 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001815 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1816 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1817 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001818
1819 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001820 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001821}
1822
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001823TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) {
1824 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001825 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1826 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001827
1828 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1829 // Inject a MotionEvent to an unknown display.
1830 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1831 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE))
1832 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1833
1834 // Window should receive motion event.
1835 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1836}
1837
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001838/**
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001839 * Calling setInputWindows once should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001840 * This test serves as a sanity check for the next test, where setInputWindows is
1841 * called twice.
1842 */
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001843TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) {
Chris Yea209fde2020-07-22 13:54:51 -07001844 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001845 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1846 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001847 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001848
1849 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001850 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001851 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1852 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001853 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001854
1855 // Window should receive motion event.
1856 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1857}
1858
1859/**
1860 * Calling setInputWindows twice, with the same info, should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001861 */
1862TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001863 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001864 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1865 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001866 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001867
1868 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1869 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001870 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001871 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1872 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001873 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001874
1875 // Window should receive motion event.
1876 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1877}
1878
Arthur Hungb92218b2018-08-14 12:00:21 +08001879// The foreground window should receive the first touch down event.
1880TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001881 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001882 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001883 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001884 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001885 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001886
Arthur Hung72d8dc32020-03-28 00:48:39 +00001887 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001888 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1889 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1890 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001891
1892 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001893 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001894 windowSecond->assertNoEvents();
1895}
1896
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001897/**
1898 * Two windows: A top window, and a wallpaper behind the window.
1899 * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window
1900 * gets ACTION_CANCEL.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001901 * 1. foregroundWindow <-- dup touch to wallpaper
1902 * 2. wallpaperWindow <-- is wallpaper
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001903 */
1904TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) {
1905 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1906 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001907 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001908 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001909 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001910 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001911 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001912
1913 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1914 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1915 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1916 {100, 200}))
1917 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1918
1919 // Both foreground window and its wallpaper should receive the touch down
1920 foregroundWindow->consumeMotionDown();
1921 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1922
1923 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1924 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1925 ADISPLAY_ID_DEFAULT, {110, 200}))
1926 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1927
1928 foregroundWindow->consumeMotionMove();
1929 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1930
1931 // Now the foreground window goes away, but the wallpaper stays
1932 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
1933 foregroundWindow->consumeMotionCancel();
1934 // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1935 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1936}
1937
1938/**
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001939 * Two fingers down on the window, and lift off the first finger.
1940 * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event
1941 * contains a single pointer.
1942 */
1943TEST_F(InputDispatcherTest, CancelAfterPointer0Up) {
1944 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1945 sp<FakeWindowHandle> window =
1946 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1947
1948 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001949 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00001950 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1951 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1952 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001953 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00001954 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1955 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1956 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
1957 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001958 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00001959 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
1960 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1961 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
1962 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001963 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
1964 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
1965 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
1966
1967 // Remove the window. The gesture should be canceled
1968 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
1969 const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}};
1970 window->consumeMotionEvent(
1971 AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers)));
1972}
1973
1974/**
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001975 * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above,
1976 * with the following differences:
1977 * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to
1978 * clean up the connection.
1979 * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful.
1980 * Ensure that there's no crash in the dispatcher.
1981 */
1982TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) {
1983 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1984 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001985 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001986 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001987 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001988 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001989 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001990
1991 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1992 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1993 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1994 {100, 200}))
1995 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1996
1997 // Both foreground window and its wallpaper should receive the touch down
1998 foregroundWindow->consumeMotionDown();
1999 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2000
2001 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2002 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2003 ADISPLAY_ID_DEFAULT, {110, 200}))
2004 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2005
2006 foregroundWindow->consumeMotionMove();
2007 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2008
2009 // Wallpaper closes its channel, but the window remains.
2010 wallpaperWindow->destroyReceiver();
2011 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token);
2012
2013 // Now the foreground window goes away, but the wallpaper stays, even though its channel
2014 // is no longer valid.
2015 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
2016 foregroundWindow->consumeMotionCancel();
2017}
2018
Arthur Hungc539dbb2022-12-08 07:45:36 +00002019class ShouldSplitTouchFixture : public InputDispatcherTest,
2020 public ::testing::WithParamInterface<bool> {};
2021INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture,
2022 ::testing::Values(true, false));
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08002023/**
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002024 * A single window that receives touch (on top), and a wallpaper window underneath it.
2025 * The top window gets a multitouch gesture.
2026 * Ensure that wallpaper gets the same gesture.
2027 */
Arthur Hungc539dbb2022-12-08 07:45:36 +00002028TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002029 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00002030 sp<FakeWindowHandle> foregroundWindow =
2031 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
2032 foregroundWindow->setDupTouchToWallpaper(true);
2033 foregroundWindow->setPreventSplitting(GetParam());
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002034
2035 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002036 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002037 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002038
Arthur Hungc539dbb2022-12-08 07:45:36 +00002039 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002040
2041 // Touch down on top window
2042 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2043 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2044 {100, 100}))
2045 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2046
2047 // Both top window and its wallpaper should receive the touch down
Arthur Hungc539dbb2022-12-08 07:45:36 +00002048 foregroundWindow->consumeMotionDown();
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002049 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2050
2051 // Second finger down on the top window
2052 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002053 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002054 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002055 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2056 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002057 .build();
2058 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2059 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
2060 InputEventInjectionSync::WAIT_FOR_RESULT))
2061 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2062
Harry Cutts33476232023-01-30 19:57:29 +00002063 foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
2064 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002065 expectedWallpaperFlags);
Arthur Hungc539dbb2022-12-08 07:45:36 +00002066
2067 const MotionEvent secondFingerUpEvent =
2068 MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
2069 .displayId(ADISPLAY_ID_DEFAULT)
2070 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002071 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2072 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Arthur Hungc539dbb2022-12-08 07:45:36 +00002073 .build();
2074 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2075 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
2076 InputEventInjectionSync::WAIT_FOR_RESULT))
2077 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2078 foregroundWindow->consumeMotionPointerUp(0);
2079 wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2080
2081 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08002082 injectMotionEvent(mDispatcher,
2083 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2084 AINPUT_SOURCE_TOUCHSCREEN)
2085 .displayId(ADISPLAY_ID_DEFAULT)
2086 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
2087 .pointer(PointerBuilder(/* id */ 1,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002088 ToolType::FINGER)
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08002089 .x(100)
2090 .y(100))
2091 .build(),
2092 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT))
Arthur Hungc539dbb2022-12-08 07:45:36 +00002093 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2094 foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
2095 wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002096}
2097
2098/**
2099 * Two windows: a window on the left and window on the right.
2100 * A third window, wallpaper, is behind both windows, and spans both top windows.
2101 * The first touch down goes to the left window. A second pointer touches down on the right window.
2102 * The touch is split, so both left and right windows should receive ACTION_DOWN.
2103 * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by
2104 * ACTION_POINTER_DOWN(1).
2105 */
2106TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) {
2107 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2108 sp<FakeWindowHandle> leftWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002109 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002110 leftWindow->setFrame(Rect(0, 0, 200, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002111 leftWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002112
2113 sp<FakeWindowHandle> rightWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002114 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002115 rightWindow->setFrame(Rect(200, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002116 rightWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002117
2118 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002119 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002120 wallpaperWindow->setFrame(Rect(0, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002121 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002122
2123 mDispatcher->setInputWindows(
2124 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
2125
2126 // Touch down on left window
2127 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2128 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2129 {100, 100}))
2130 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2131
2132 // Both foreground window and its wallpaper should receive the touch down
2133 leftWindow->consumeMotionDown();
2134 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2135
2136 // Second finger down on the right window
2137 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002138 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002139 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002140 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2141 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002142 .build();
2143 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2144 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
2145 InputEventInjectionSync::WAIT_FOR_RESULT))
2146 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2147
2148 leftWindow->consumeMotionMove();
2149 // Since the touch is split, right window gets ACTION_DOWN
2150 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00002151 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002152 expectedWallpaperFlags);
2153
2154 // Now, leftWindow, which received the first finger, disappears.
2155 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}});
2156 leftWindow->consumeMotionCancel();
2157 // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
2158 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2159
2160 // The pointer that's still down on the right window moves, and goes to the right window only.
2161 // As far as the dispatcher's concerned though, both pointers are still present.
2162 const MotionEvent secondFingerMoveEvent =
2163 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2164 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002165 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2166 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002167 .build();
2168 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2169 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
2170 InputEventInjectionSync::WAIT_FOR_RESULT));
2171 rightWindow->consumeMotionMove();
2172
2173 leftWindow->assertNoEvents();
2174 rightWindow->assertNoEvents();
2175 wallpaperWindow->assertNoEvents();
2176}
2177
Arthur Hungc539dbb2022-12-08 07:45:36 +00002178/**
2179 * Two windows: a window on the left with dup touch to wallpaper and window on the right without it.
2180 * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL
2181 * The right window should receive ACTION_DOWN.
2182 */
2183TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) {
Arthur Hung74c248d2022-11-23 07:09:59 +00002184 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00002185 sp<FakeWindowHandle> leftWindow =
2186 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2187 leftWindow->setFrame(Rect(0, 0, 200, 200));
2188 leftWindow->setDupTouchToWallpaper(true);
2189 leftWindow->setSlippery(true);
2190
2191 sp<FakeWindowHandle> rightWindow =
2192 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2193 rightWindow->setFrame(Rect(200, 0, 400, 200));
Arthur Hung74c248d2022-11-23 07:09:59 +00002194
2195 sp<FakeWindowHandle> wallpaperWindow =
2196 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
2197 wallpaperWindow->setIsWallpaper(true);
Arthur Hung74c248d2022-11-23 07:09:59 +00002198
Arthur Hungc539dbb2022-12-08 07:45:36 +00002199 mDispatcher->setInputWindows(
2200 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
Arthur Hung74c248d2022-11-23 07:09:59 +00002201
Arthur Hungc539dbb2022-12-08 07:45:36 +00002202 // Touch down on left window
Arthur Hung74c248d2022-11-23 07:09:59 +00002203 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2204 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002205 {100, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002206 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungc539dbb2022-12-08 07:45:36 +00002207
2208 // Both foreground window and its wallpaper should receive the touch down
2209 leftWindow->consumeMotionDown();
Arthur Hung74c248d2022-11-23 07:09:59 +00002210 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2211
Arthur Hungc539dbb2022-12-08 07:45:36 +00002212 // Move to right window, the left window should receive cancel.
Arthur Hung74c248d2022-11-23 07:09:59 +00002213 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002214 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2215 ADISPLAY_ID_DEFAULT, {201, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002216 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2217
Arthur Hungc539dbb2022-12-08 07:45:36 +00002218 leftWindow->consumeMotionCancel();
2219 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2220 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Arthur Hung74c248d2022-11-23 07:09:59 +00002221}
2222
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002223/**
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002224 * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not
2225 * interactive, it might stop sending this flag.
2226 * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure
2227 * to have a consistent input stream.
2228 *
2229 * Test procedure:
2230 * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL.
2231 * DOWN (new gesture).
2232 *
2233 * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent
2234 * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app.
2235 *
2236 * We technically just need a single window here, but we are using two windows (spy on top and a
2237 * regular window below) to emulate the actual situation where it happens on the device.
2238 */
2239TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) {
2240 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2241 sp<FakeWindowHandle> spyWindow =
2242 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2243 spyWindow->setFrame(Rect(0, 0, 200, 200));
2244 spyWindow->setTrustedOverlay(true);
2245 spyWindow->setSpy(true);
2246
2247 sp<FakeWindowHandle> window =
2248 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2249 window->setFrame(Rect(0, 0, 200, 200));
2250
2251 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2252 const int32_t touchDeviceId = 4;
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002253
2254 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002255 mDispatcher->notifyMotion(
2256 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2257 .deviceId(touchDeviceId)
2258 .policyFlags(DEFAULT_POLICY_FLAGS)
2259 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2260 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002261
Prabir Pradhan678438e2023-04-13 19:32:51 +00002262 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2263 .deviceId(touchDeviceId)
2264 .policyFlags(DEFAULT_POLICY_FLAGS)
2265 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2266 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2267 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002268 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2269 spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2270 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2271 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2272
2273 // Cancel the current gesture. Send the cancel without the default policy flags.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002274 mDispatcher->notifyMotion(
2275 MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN)
2276 .deviceId(touchDeviceId)
2277 .policyFlags(0)
2278 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2279 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2280 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002281 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2282 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2283
2284 // We don't need to reset the device to reproduce the issue, but the reset event typically
2285 // follows, so we keep it here to model the actual listener behaviour more closely.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002286 mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId});
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002287
2288 // Start new gesture
Prabir Pradhan678438e2023-04-13 19:32:51 +00002289 mDispatcher->notifyMotion(
2290 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2291 .deviceId(touchDeviceId)
2292 .policyFlags(DEFAULT_POLICY_FLAGS)
2293 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2294 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002295 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2296 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2297
2298 // No more events
2299 spyWindow->assertNoEvents();
2300 window->assertNoEvents();
2301}
2302
2303/**
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002304 * Two windows: a window on the left and a window on the right.
2305 * Mouse is hovered from the right window into the left window.
2306 * Next, we tap on the left window, where the cursor was last seen.
2307 * The second tap is done onto the right window.
2308 * The mouse and tap are from two different devices.
2309 * We technically don't need to set the downtime / eventtime for these events, but setting these
2310 * explicitly helps during debugging.
2311 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2312 * In the buggy implementation, a tap on the right window would cause a crash.
2313 */
2314TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) {
2315 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2316 sp<FakeWindowHandle> leftWindow =
2317 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2318 leftWindow->setFrame(Rect(0, 0, 200, 200));
2319
2320 sp<FakeWindowHandle> rightWindow =
2321 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2322 rightWindow->setFrame(Rect(200, 0, 400, 200));
2323
2324 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2325 // All times need to start at the current time, otherwise the dispatcher will drop the events as
2326 // stale.
2327 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
2328 const int32_t mouseDeviceId = 6;
2329 const int32_t touchDeviceId = 4;
2330 // Move the cursor from right
2331 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2332 injectMotionEvent(mDispatcher,
2333 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2334 AINPUT_SOURCE_MOUSE)
2335 .deviceId(mouseDeviceId)
2336 .downTime(baseTime + 10)
2337 .eventTime(baseTime + 20)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002338 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002339 .x(300)
2340 .y(100))
2341 .build()));
2342 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2343
2344 // .. to the left window
2345 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2346 injectMotionEvent(mDispatcher,
2347 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2348 AINPUT_SOURCE_MOUSE)
2349 .deviceId(mouseDeviceId)
2350 .downTime(baseTime + 10)
2351 .eventTime(baseTime + 30)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002352 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002353 .x(110)
2354 .y(100))
2355 .build()));
2356 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2357 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2358 // Now tap the left window
2359 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2360 injectMotionEvent(mDispatcher,
2361 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2362 AINPUT_SOURCE_TOUCHSCREEN)
2363 .deviceId(touchDeviceId)
2364 .downTime(baseTime + 40)
2365 .eventTime(baseTime + 40)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002366 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002367 .x(100)
2368 .y(100))
2369 .build()));
2370 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2371 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2372
2373 // release tap
2374 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2375 injectMotionEvent(mDispatcher,
2376 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2377 AINPUT_SOURCE_TOUCHSCREEN)
2378 .deviceId(touchDeviceId)
2379 .downTime(baseTime + 40)
2380 .eventTime(baseTime + 50)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002381 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002382 .x(100)
2383 .y(100))
2384 .build()));
2385 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2386
2387 // Tap the window on the right
2388 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2389 injectMotionEvent(mDispatcher,
2390 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2391 AINPUT_SOURCE_TOUCHSCREEN)
2392 .deviceId(touchDeviceId)
2393 .downTime(baseTime + 60)
2394 .eventTime(baseTime + 60)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002395 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002396 .x(300)
2397 .y(100))
2398 .build()));
2399 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2400
2401 // release tap
2402 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2403 injectMotionEvent(mDispatcher,
2404 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2405 AINPUT_SOURCE_TOUCHSCREEN)
2406 .deviceId(touchDeviceId)
2407 .downTime(baseTime + 60)
2408 .eventTime(baseTime + 70)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002409 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002410 .x(300)
2411 .y(100))
2412 .build()));
2413 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2414
2415 // No more events
2416 leftWindow->assertNoEvents();
2417 rightWindow->assertNoEvents();
2418}
2419
2420/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002421 * Two windows: a window on the left and a window on the right.
2422 * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains
2423 * down. Then, on the left window, also place second touch pointer down.
2424 * This test tries to reproduce a crash.
2425 * In the buggy implementation, second pointer down on the left window would cause a crash.
2426 */
2427TEST_F(InputDispatcherTest, MultiDeviceSplitTouch) {
2428 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2429 sp<FakeWindowHandle> leftWindow =
2430 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2431 leftWindow->setFrame(Rect(0, 0, 200, 200));
2432
2433 sp<FakeWindowHandle> rightWindow =
2434 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2435 rightWindow->setFrame(Rect(200, 0, 400, 200));
2436
2437 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2438
2439 const int32_t touchDeviceId = 4;
2440 const int32_t mouseDeviceId = 6;
2441 NotifyMotionArgs args;
2442
2443 // Start hovering over the left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002444 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2445 .deviceId(mouseDeviceId)
2446 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2447 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002448 leftWindow->consumeMotionEvent(
2449 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2450
2451 // Mouse down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002452 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2453 .deviceId(mouseDeviceId)
2454 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2455 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2456 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002457
2458 leftWindow->consumeMotionEvent(
2459 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
2460 leftWindow->consumeMotionEvent(
2461 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2462
Prabir Pradhan678438e2023-04-13 19:32:51 +00002463 mDispatcher->notifyMotion(
2464 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2465 .deviceId(mouseDeviceId)
2466 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2467 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2468 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2469 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002470 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2471
2472 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002473 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2474 .deviceId(touchDeviceId)
2475 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2476 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002477 leftWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
2478
2479 rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2480
2481 // Second touch pointer down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002482 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2483 .deviceId(touchDeviceId)
2484 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2485 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
2486 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002487 leftWindow->consumeMotionEvent(
2488 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2489 // This MOVE event is not necessary (doesn't carry any new information), but it's there in the
2490 // current implementation.
2491 const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}};
2492 rightWindow->consumeMotionEvent(
2493 AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers)));
2494
2495 leftWindow->assertNoEvents();
2496 rightWindow->assertNoEvents();
2497}
2498
2499/**
2500 * On a single window, use two different devices: mouse and touch.
2501 * Touch happens first, with two pointers going down, and then the first pointer leaving.
2502 * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL.
2503 * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored,
2504 * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not
2505 * represent a new gesture.
2506 */
2507TEST_F(InputDispatcherTest, MixedTouchAndMouseWithPointerDown) {
2508 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2509 sp<FakeWindowHandle> window =
2510 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2511 window->setFrame(Rect(0, 0, 400, 400));
2512
2513 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2514
2515 const int32_t touchDeviceId = 4;
2516 const int32_t mouseDeviceId = 6;
2517 NotifyMotionArgs args;
2518
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08002519 // First touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002520 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2521 .deviceId(touchDeviceId)
2522 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2523 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002524 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002525 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2526 .deviceId(touchDeviceId)
2527 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2528 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2529 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002530 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002531 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
2532 .deviceId(touchDeviceId)
2533 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2534 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2535 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002536 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2537 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2538 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
2539
2540 // Mouse down. The touch should be canceled
Prabir Pradhan678438e2023-04-13 19:32:51 +00002541 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2542 .deviceId(mouseDeviceId)
2543 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2544 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
2545 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002546
2547 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08002548 WithPointerCount(1u)));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002549 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2550
Prabir Pradhan678438e2023-04-13 19:32:51 +00002551 mDispatcher->notifyMotion(
2552 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2553 .deviceId(mouseDeviceId)
2554 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2555 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2556 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
2557 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002558 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2559
2560 // Second touch pointer down.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002561 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2562 .deviceId(touchDeviceId)
2563 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2564 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2565 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002566 // The pointer_down event should be ignored
2567 window->assertNoEvents();
2568}
2569
2570/**
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002571 * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels
2572 * the injected event.
2573 */
2574TEST_F(InputDispatcherTest, UnfinishedInjectedEvent) {
2575 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2576 sp<FakeWindowHandle> window =
2577 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2578 window->setFrame(Rect(0, 0, 400, 400));
2579
2580 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2581
2582 const int32_t touchDeviceId = 4;
2583 NotifyMotionArgs args;
2584 // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after
2585 // completion.
2586 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2587 injectMotionEvent(mDispatcher,
2588 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2589 .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002590 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002591 .x(50)
2592 .y(50))
2593 .build()));
2594 window->consumeMotionEvent(
2595 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
2596
2597 // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer
2598 // should be canceled and the new gesture should take over.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002599 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2600 .deviceId(touchDeviceId)
2601 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2602 .build());
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002603
2604 window->consumeMotionEvent(
2605 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
2606 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2607}
2608
2609/**
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002610 * This test is similar to the test above, but the sequence of injected events is different.
2611 *
2612 * Two windows: a window on the left and a window on the right.
2613 * Mouse is hovered over the left window.
2614 * Next, we tap on the left window, where the cursor was last seen.
2615 *
2616 * After that, we inject one finger down onto the right window, and then a second finger down onto
2617 * the left window.
2618 * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right
2619 * window (first), and then another on the left window (second).
2620 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2621 * In the buggy implementation, second finger down on the left window would cause a crash.
2622 */
2623TEST_F(InputDispatcherTest, HoverTapAndSplitTouch) {
2624 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2625 sp<FakeWindowHandle> leftWindow =
2626 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2627 leftWindow->setFrame(Rect(0, 0, 200, 200));
2628
2629 sp<FakeWindowHandle> rightWindow =
2630 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2631 rightWindow->setFrame(Rect(200, 0, 400, 200));
2632
2633 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2634
2635 const int32_t mouseDeviceId = 6;
2636 const int32_t touchDeviceId = 4;
2637 // Hover over the left window. Keep the cursor there.
2638 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2639 injectMotionEvent(mDispatcher,
2640 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2641 AINPUT_SOURCE_MOUSE)
2642 .deviceId(mouseDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002643 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002644 .x(50)
2645 .y(50))
2646 .build()));
2647 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2648
2649 // Tap on left window
2650 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2651 injectMotionEvent(mDispatcher,
2652 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2653 AINPUT_SOURCE_TOUCHSCREEN)
2654 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002655 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002656 .x(100)
2657 .y(100))
2658 .build()));
2659
2660 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2661 injectMotionEvent(mDispatcher,
2662 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2663 AINPUT_SOURCE_TOUCHSCREEN)
2664 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002665 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002666 .x(100)
2667 .y(100))
2668 .build()));
2669 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2670 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2671 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2672
2673 // First finger down on right window
2674 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2675 injectMotionEvent(mDispatcher,
2676 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2677 AINPUT_SOURCE_TOUCHSCREEN)
2678 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002679 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002680 .x(300)
2681 .y(100))
2682 .build()));
2683 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2684
2685 // Second finger down on the left window
2686 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2687 injectMotionEvent(mDispatcher,
2688 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2689 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002690 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002691 .x(300)
2692 .y(100))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002693 .pointer(PointerBuilder(1, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002694 .x(100)
2695 .y(100))
2696 .build()));
2697 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2698 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE));
2699
2700 // No more events
2701 leftWindow->assertNoEvents();
2702 rightWindow->assertNoEvents();
2703}
2704
2705/**
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002706 * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs.
2707 * While the touch is down, new hover events from the stylus device should be ignored. After the
2708 * touch is gone, stylus hovering should start working again.
2709 */
2710TEST_F(InputDispatcherTest, StylusHoverAndTouchTap) {
2711 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2712 sp<FakeWindowHandle> window =
2713 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2714 window->setFrame(Rect(0, 0, 200, 200));
2715
2716 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2717
2718 const int32_t stylusDeviceId = 5;
2719 const int32_t touchDeviceId = 4;
2720 // Start hovering with stylus
2721 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2722 injectMotionEvent(mDispatcher,
2723 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2724 AINPUT_SOURCE_STYLUS)
2725 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002726 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002727 .x(50)
2728 .y(50))
2729 .build()));
2730 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2731
2732 // Finger down on the window
2733 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2734 injectMotionEvent(mDispatcher,
2735 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2736 AINPUT_SOURCE_TOUCHSCREEN)
2737 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002738 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002739 .x(100)
2740 .y(100))
2741 .build()));
2742 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2743 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2744
2745 // Try to continue hovering with stylus. Since we are already down, injection should fail
2746 ASSERT_EQ(InputEventInjectionResult::FAILED,
2747 injectMotionEvent(mDispatcher,
2748 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2749 AINPUT_SOURCE_STYLUS)
2750 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002751 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002752 .x(50)
2753 .y(50))
2754 .build()));
2755 // No event should be sent. This event should be ignored because a pointer from another device
2756 // is already down.
2757
2758 // Lift up the finger
2759 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2760 injectMotionEvent(mDispatcher,
2761 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2762 AINPUT_SOURCE_TOUCHSCREEN)
2763 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002764 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002765 .x(100)
2766 .y(100))
2767 .build()));
2768 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2769
2770 // Now that the touch is gone, stylus hovering should start working again
2771 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2772 injectMotionEvent(mDispatcher,
2773 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2774 AINPUT_SOURCE_STYLUS)
2775 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002776 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002777 .x(50)
2778 .y(50))
2779 .build()));
2780 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2781 // No more events
2782 window->assertNoEvents();
2783}
2784
2785/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002786 * A spy window above a window with no input channel.
2787 * Start hovering with a stylus device, and then tap with it.
2788 * Ensure spy window receives the entire sequence.
2789 */
2790TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) {
2791 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2792 sp<FakeWindowHandle> spyWindow =
2793 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2794 spyWindow->setFrame(Rect(0, 0, 200, 200));
2795 spyWindow->setTrustedOverlay(true);
2796 spyWindow->setSpy(true);
2797 sp<FakeWindowHandle> window =
2798 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2799 window->setNoInputChannel(true);
2800 window->setFrame(Rect(0, 0, 200, 200));
2801
2802 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2803
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002804 // Start hovering with stylus
Prabir Pradhan678438e2023-04-13 19:32:51 +00002805 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2806 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2807 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002808 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2809 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00002810 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2811 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2812 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002813 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2814
2815 // Stylus touches down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002816 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2817 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2818 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002819 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2820
2821 // Stylus goes up
Prabir Pradhan678438e2023-04-13 19:32:51 +00002822 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS)
2823 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2824 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002825 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP));
2826
2827 // Again hover
Prabir Pradhan678438e2023-04-13 19:32:51 +00002828 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2829 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2830 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002831 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2832 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00002833 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2834 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2835 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002836 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2837
2838 // No more events
2839 spyWindow->assertNoEvents();
2840 window->assertNoEvents();
2841}
2842
2843/**
2844 * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream.
2845 * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse
2846 * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active.
2847 * While the mouse is down, new move events from the touch device should be ignored.
2848 */
2849TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) {
2850 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2851 sp<FakeWindowHandle> spyWindow =
2852 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2853 spyWindow->setFrame(Rect(0, 0, 200, 200));
2854 spyWindow->setTrustedOverlay(true);
2855 spyWindow->setSpy(true);
2856 sp<FakeWindowHandle> window =
2857 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2858 window->setFrame(Rect(0, 0, 200, 200));
2859
2860 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2861
2862 const int32_t mouseDeviceId = 7;
2863 const int32_t touchDeviceId = 4;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002864
2865 // Hover a bit with mouse first
Prabir Pradhan678438e2023-04-13 19:32:51 +00002866 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2867 .deviceId(mouseDeviceId)
2868 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2869 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002870 spyWindow->consumeMotionEvent(
2871 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2872 window->consumeMotionEvent(
2873 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2874
2875 // Start touching
Prabir Pradhan678438e2023-04-13 19:32:51 +00002876 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2877 .deviceId(touchDeviceId)
2878 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
2879 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002880 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2881 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2882 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2883 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2884
Prabir Pradhan678438e2023-04-13 19:32:51 +00002885 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2886 .deviceId(touchDeviceId)
2887 .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55))
2888 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002889 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2890 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2891
2892 // Pilfer the stream
2893 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
2894 window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
2895
Prabir Pradhan678438e2023-04-13 19:32:51 +00002896 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2897 .deviceId(touchDeviceId)
2898 .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60))
2899 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002900 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2901
2902 // Mouse down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002903 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2904 .deviceId(mouseDeviceId)
2905 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2906 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2907 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002908
2909 spyWindow->consumeMotionEvent(
2910 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
2911 spyWindow->consumeMotionEvent(
2912 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2913 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2914
Prabir Pradhan678438e2023-04-13 19:32:51 +00002915 mDispatcher->notifyMotion(
2916 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2917 .deviceId(mouseDeviceId)
2918 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2919 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2920 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2921 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002922 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2923 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2924
2925 // Mouse move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00002926 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
2927 .deviceId(mouseDeviceId)
2928 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2929 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110))
2930 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002931 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2932 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2933
2934 // Touch move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00002935 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2936 .deviceId(touchDeviceId)
2937 .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65))
2938 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002939
2940 // No more events
2941 spyWindow->assertNoEvents();
2942 window->assertNoEvents();
2943}
2944
2945/**
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002946 * On the display, have a single window, and also an area where there's no window.
2947 * First pointer touches the "no window" area of the screen. Second pointer touches the window.
2948 * Make sure that the window receives the second pointer, and first pointer is simply ignored.
2949 */
2950TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) {
2951 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2952 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002953 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002954
2955 mDispatcher->setInputWindows({{DISPLAY_ID, {window}}});
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002956
2957 // Touch down on the empty space
Prabir Pradhan678438e2023-04-13 19:32:51 +00002958 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002959
2960 mDispatcher->waitForIdle();
2961 window->assertNoEvents();
2962
2963 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00002964 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002965 mDispatcher->waitForIdle();
2966 window->consumeMotionDown();
2967}
2968
2969/**
2970 * Same test as above, but instead of touching the empty space, the first touch goes to
2971 * non-touchable window.
2972 */
2973TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) {
2974 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2975 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002976 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002977 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
2978 window1->setTouchable(false);
2979 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002980 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002981 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
2982
2983 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
2984
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002985 // Touch down on the non-touchable window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002986 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002987
2988 mDispatcher->waitForIdle();
2989 window1->assertNoEvents();
2990 window2->assertNoEvents();
2991
2992 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00002993 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002994 mDispatcher->waitForIdle();
2995 window2->consumeMotionDown();
2996}
2997
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002998/**
2999 * When splitting touch events the downTime should be adjusted such that the downTime corresponds
3000 * to the event time of the first ACTION_DOWN sent to the particular window.
3001 */
3002TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) {
3003 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3004 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003005 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003006 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
3007 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003008 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003009 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
3010
3011 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
3012
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003013 // Touch down on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003014 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003015
3016 mDispatcher->waitForIdle();
3017 InputEvent* inputEvent1 = window1->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003018 ASSERT_NE(inputEvent1, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003019 window2->assertNoEvents();
3020 MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1);
3021 nsecs_t downTimeForWindow1 = motionEvent1.getDownTime();
3022 ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime());
3023
3024 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00003025 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003026 mDispatcher->waitForIdle();
3027 InputEvent* inputEvent2 = window2->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003028 ASSERT_NE(inputEvent2, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003029 MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2);
3030 nsecs_t downTimeForWindow2 = motionEvent2.getDownTime();
3031 ASSERT_NE(downTimeForWindow1, downTimeForWindow2);
3032 ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime());
3033
3034 // Now move the pointer on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003035 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003036 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003037 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003038
3039 // Now add new touch down on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003040 mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003041 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003042 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003043
3044 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
3045 window1->consumeMotionMove();
3046 window1->assertNoEvents();
3047
3048 // Now move the pointer on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003049 mDispatcher->notifyMotion(
3050 generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003051 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003052 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003053
Prabir Pradhan678438e2023-04-13 19:32:51 +00003054 mDispatcher->notifyMotion(
3055 generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003056 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003057 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003058}
3059
Garfield Tandf26e862020-07-01 20:18:19 -07003060TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07003061 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07003062 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003063 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003064 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003065 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003066 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003067 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003068
3069 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3070
3071 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
3072
3073 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003074 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003075 injectMotionEvent(mDispatcher,
3076 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3077 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003078 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003079 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003080 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003081
3082 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003083 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003084 injectMotionEvent(mDispatcher,
3085 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3086 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003087 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003088 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003089 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3090 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003091
3092 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003093 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003094 injectMotionEvent(mDispatcher,
3095 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3096 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003097 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003098 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003099 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3100 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003101
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003102 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003103 injectMotionEvent(mDispatcher,
3104 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3105 AINPUT_SOURCE_MOUSE)
3106 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3107 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003108 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003109 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003110 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003111
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003112 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003113 injectMotionEvent(mDispatcher,
3114 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3115 AINPUT_SOURCE_MOUSE)
3116 .buttonState(0)
3117 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003118 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003119 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003120 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003121
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003122 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003123 injectMotionEvent(mDispatcher,
3124 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3125 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003126 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003127 .build()));
3128 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3129
3130 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003131 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003132 injectMotionEvent(mDispatcher,
3133 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3134 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003135 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003136 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003137 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003138
3139 // No more events
3140 windowLeft->assertNoEvents();
3141 windowRight->assertNoEvents();
3142}
3143
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003144/**
3145 * Put two fingers down (and don't release them) and click the mouse button.
3146 * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the
3147 * currently active gesture should be canceled, and the new one should proceed.
3148 */
3149TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) {
3150 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3151 sp<FakeWindowHandle> window =
3152 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3153 window->setFrame(Rect(0, 0, 600, 800));
3154
3155 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3156
3157 const int32_t touchDeviceId = 4;
3158 const int32_t mouseDeviceId = 6;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003159
3160 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003161 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3162 .deviceId(touchDeviceId)
3163 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3164 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003165
Prabir Pradhan678438e2023-04-13 19:32:51 +00003166 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3167 .deviceId(touchDeviceId)
3168 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3169 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
3170 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003171 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3172 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
3173
3174 // Inject a series of mouse events for a mouse click
Prabir Pradhan678438e2023-04-13 19:32:51 +00003175 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3176 .deviceId(mouseDeviceId)
3177 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3178 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
3179 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003180 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
3181 WithPointerCount(2u)));
3182 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3183
Prabir Pradhan678438e2023-04-13 19:32:51 +00003184 mDispatcher->notifyMotion(
3185 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
3186 .deviceId(mouseDeviceId)
3187 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3188 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
3189 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
3190 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003191 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3192
3193 // Try to send more touch events while the mouse is down. Since it's a continuation of an
3194 // already canceled gesture, it should be ignored.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003195 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3196 .deviceId(touchDeviceId)
3197 .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101))
3198 .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121))
3199 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003200 window->assertNoEvents();
3201}
3202
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003203TEST_F(InputDispatcherTest, HoverWithSpyWindows) {
3204 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3205
3206 sp<FakeWindowHandle> spyWindow =
3207 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3208 spyWindow->setFrame(Rect(0, 0, 600, 800));
3209 spyWindow->setTrustedOverlay(true);
3210 spyWindow->setSpy(true);
3211 sp<FakeWindowHandle> window =
3212 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3213 window->setFrame(Rect(0, 0, 600, 800));
3214
3215 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3216 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
3217
3218 // Send mouse cursor to the window
3219 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3220 injectMotionEvent(mDispatcher,
3221 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3222 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003223 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003224 .x(100)
3225 .y(100))
3226 .build()));
3227
3228 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3229 WithSource(AINPUT_SOURCE_MOUSE)));
3230 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3231 WithSource(AINPUT_SOURCE_MOUSE)));
3232
3233 window->assertNoEvents();
3234 spyWindow->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07003235}
3236
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003237TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) {
3238 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3239
3240 sp<FakeWindowHandle> spyWindow =
3241 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3242 spyWindow->setFrame(Rect(0, 0, 600, 800));
3243 spyWindow->setTrustedOverlay(true);
3244 spyWindow->setSpy(true);
3245 sp<FakeWindowHandle> window =
3246 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3247 window->setFrame(Rect(0, 0, 600, 800));
3248
3249 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3250 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
3251
3252 // Send mouse cursor to the window
3253 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3254 injectMotionEvent(mDispatcher,
3255 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3256 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003257 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003258 .x(100)
3259 .y(100))
3260 .build()));
3261
3262 // Move mouse cursor
3263 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3264 injectMotionEvent(mDispatcher,
3265 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3266 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003267 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003268 .x(110)
3269 .y(110))
3270 .build()));
3271
3272 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3273 WithSource(AINPUT_SOURCE_MOUSE)));
3274 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3275 WithSource(AINPUT_SOURCE_MOUSE)));
3276 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3277 WithSource(AINPUT_SOURCE_MOUSE)));
3278 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3279 WithSource(AINPUT_SOURCE_MOUSE)));
3280 // Touch down on the window
3281 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3282 injectMotionEvent(mDispatcher,
3283 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3284 AINPUT_SOURCE_TOUCHSCREEN)
3285 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003286 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003287 .x(200)
3288 .y(200))
3289 .build()));
3290 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3291 WithSource(AINPUT_SOURCE_MOUSE)));
3292 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3293 WithSource(AINPUT_SOURCE_MOUSE)));
3294 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3295 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3296 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3297 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3298
3299 // pilfer the motion, retaining the gesture on the spy window.
3300 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
3301 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
3302 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3303
3304 // Touch UP on the window
3305 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3306 injectMotionEvent(mDispatcher,
3307 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3308 AINPUT_SOURCE_TOUCHSCREEN)
3309 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003310 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003311 .x(200)
3312 .y(200))
3313 .build()));
3314 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3315 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3316
3317 // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going
3318 // to send a new gesture. It should again go to both windows (spy and the window below), just
3319 // like the first gesture did, before pilfering. The window configuration has not changed.
3320
3321 // One more tap - DOWN
3322 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3323 injectMotionEvent(mDispatcher,
3324 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3325 AINPUT_SOURCE_TOUCHSCREEN)
3326 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003327 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003328 .x(250)
3329 .y(250))
3330 .build()));
3331 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3332 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3333 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3334 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3335
3336 // Touch UP on the window
3337 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3338 injectMotionEvent(mDispatcher,
3339 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3340 AINPUT_SOURCE_TOUCHSCREEN)
3341 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003342 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003343 .x(250)
3344 .y(250))
3345 .build()));
3346 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3347 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3348 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3349 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3350
3351 window->assertNoEvents();
3352 spyWindow->assertNoEvents();
3353}
3354
Garfield Tandf26e862020-07-01 20:18:19 -07003355// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
3356// directly in this test.
3357TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07003358 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07003359 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003360 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003361 window->setFrame(Rect(0, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003362
3363 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3364
3365 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3366
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003367 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003368 injectMotionEvent(mDispatcher,
3369 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3370 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003371 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003372 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003373 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003374 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003375 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003376 injectMotionEvent(mDispatcher,
3377 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3378 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003379 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003380 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003381 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3382 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003383
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003384 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003385 injectMotionEvent(mDispatcher,
3386 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3387 AINPUT_SOURCE_MOUSE)
3388 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3389 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003390 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003391 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003392 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003393
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003394 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003395 injectMotionEvent(mDispatcher,
3396 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3397 AINPUT_SOURCE_MOUSE)
3398 .buttonState(0)
3399 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003400 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003401 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003402 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003403
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003404 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003405 injectMotionEvent(mDispatcher,
3406 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3407 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003408 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003409 .build()));
3410 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3411
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003412 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003413 injectMotionEvent(mDispatcher,
3414 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
3415 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003416 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003417 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003418 window->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07003419}
3420
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003421/**
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003422 * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event
3423 * is generated.
3424 */
3425TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) {
3426 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3427 sp<FakeWindowHandle> window =
3428 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3429 window->setFrame(Rect(0, 0, 1200, 800));
3430
3431 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3432
3433 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3434
3435 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3436 injectMotionEvent(mDispatcher,
3437 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3438 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003439 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003440 .x(300)
3441 .y(400))
3442 .build()));
3443 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
3444
3445 // Remove the window, but keep the channel.
3446 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
3447 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3448}
3449
3450/**
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003451 * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT.
3452 */
3453TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) {
3454 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3455 sp<FakeWindowHandle> window =
3456 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3457 window->setFrame(Rect(0, 0, 100, 100));
3458
3459 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3460
3461 const int32_t mouseDeviceId = 7;
3462 const int32_t touchDeviceId = 4;
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003463
3464 // Start hovering with the mouse
Prabir Pradhan678438e2023-04-13 19:32:51 +00003465 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
3466 .deviceId(mouseDeviceId)
3467 .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10))
3468 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003469 window->consumeMotionEvent(
3470 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3471
3472 // Touch goes down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003473 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3474 .deviceId(touchDeviceId)
3475 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
3476 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003477
3478 window->consumeMotionEvent(
3479 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
3480 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3481}
3482
3483/**
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003484 * Inject a mouse hover event followed by a tap from touchscreen.
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003485 * The tap causes a HOVER_EXIT event to be generated because the current event
3486 * stream's source has been switched.
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003487 */
3488TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) {
3489 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3490 sp<FakeWindowHandle> window =
3491 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3492 window->setFrame(Rect(0, 0, 100, 100));
3493
3494 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3495
3496 // Inject a hover_move from mouse.
3497 NotifyMotionArgs motionArgs =
3498 generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE,
3499 ADISPLAY_ID_DEFAULT, {{50, 50}});
3500 motionArgs.xCursorPosition = 50;
3501 motionArgs.yCursorPosition = 50;
Prabir Pradhan678438e2023-04-13 19:32:51 +00003502 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003503 ASSERT_NO_FATAL_FAILURE(
3504 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3505 WithSource(AINPUT_SOURCE_MOUSE))));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003506
3507 // Tap on the window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003508 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3509 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3510 {{10, 10}}));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003511 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003512 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3513 WithSource(AINPUT_SOURCE_MOUSE))));
3514
3515 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003516 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3517 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
3518
Prabir Pradhan678438e2023-04-13 19:32:51 +00003519 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3520 ADISPLAY_ID_DEFAULT, {{10, 10}}));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003521 ASSERT_NO_FATAL_FAILURE(
3522 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3523 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
3524}
3525
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003526TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) {
3527 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3528 sp<FakeWindowHandle> windowDefaultDisplay =
3529 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
3530 ADISPLAY_ID_DEFAULT);
3531 windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800));
3532 sp<FakeWindowHandle> windowSecondDisplay =
3533 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay",
3534 SECOND_DISPLAY_ID);
3535 windowSecondDisplay->setFrame(Rect(0, 0, 600, 800));
3536
3537 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
3538 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
3539
3540 // Set cursor position in window in default display and check that hover enter and move
3541 // events are generated.
3542 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3543 injectMotionEvent(mDispatcher,
3544 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3545 AINPUT_SOURCE_MOUSE)
3546 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003547 .pointer(PointerBuilder(0, ToolType::MOUSE)
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003548 .x(300)
3549 .y(600))
3550 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003551 windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003552
3553 // Remove all windows in secondary display and check that no event happens on window in
3554 // primary display.
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003555 mDispatcher->setInputWindows(
3556 {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003557 windowDefaultDisplay->assertNoEvents();
3558
3559 // Move cursor position in window in default display and check that only hover move
3560 // event is generated and not hover enter event.
3561 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
3562 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
3563 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3564 injectMotionEvent(mDispatcher,
3565 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3566 AINPUT_SOURCE_MOUSE)
3567 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003568 .pointer(PointerBuilder(0, ToolType::MOUSE)
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003569 .x(400)
3570 .y(700))
3571 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003572 windowDefaultDisplay->consumeMotionEvent(
3573 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3574 WithSource(AINPUT_SOURCE_MOUSE)));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003575 windowDefaultDisplay->assertNoEvents();
3576}
3577
Garfield Tan00f511d2019-06-12 16:55:40 -07003578TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07003579 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07003580
3581 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003582 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003583 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07003584 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003585 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003586 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07003587
3588 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3589
Arthur Hung72d8dc32020-03-28 00:48:39 +00003590 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07003591
3592 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
3593 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003594 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07003595 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003596 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003597 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003598 windowRight->assertNoEvents();
3599}
3600
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003601TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07003602 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003603 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3604 "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07003605 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003606
Arthur Hung72d8dc32020-03-28 00:48:39 +00003607 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003608 setFocusedWindow(window);
3609
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003610 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003611
Prabir Pradhan678438e2023-04-13 19:32:51 +00003612 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003613
3614 // Window should receive key down event.
3615 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3616
3617 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
3618 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003619 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07003620 window->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003621 AKEY_EVENT_FLAG_CANCELED);
3622}
3623
3624TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07003625 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003626 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3627 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003628
Arthur Hung72d8dc32020-03-28 00:48:39 +00003629 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003630
Prabir Pradhan678438e2023-04-13 19:32:51 +00003631 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3632 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003633
3634 // Window should receive motion down event.
3635 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3636
3637 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
3638 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003639 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08003640 window->consumeMotionEvent(
3641 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003642}
3643
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003644TEST_F(InputDispatcherTest, InterceptKeyByPolicy) {
3645 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003646 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3647 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003648 window->setFocusable(true);
3649
3650 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3651 setFocusedWindow(window);
3652
3653 window->consumeFocusEvent(true);
3654
Prabir Pradhan678438e2023-04-13 19:32:51 +00003655 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003656 const std::chrono::milliseconds interceptKeyTimeout = 50ms;
3657 const nsecs_t injectTime = keyArgs.eventTime;
3658 mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout);
Prabir Pradhan678438e2023-04-13 19:32:51 +00003659 mDispatcher->notifyKey(keyArgs);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003660 // The dispatching time should be always greater than or equal to intercept key timeout.
3661 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3662 ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >=
3663 std::chrono::nanoseconds(interceptKeyTimeout).count());
3664}
3665
3666TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) {
3667 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003668 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3669 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003670 window->setFocusable(true);
3671
3672 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3673 setFocusedWindow(window);
3674
3675 window->consumeFocusEvent(true);
3676
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003677 mFakePolicy->setInterceptKeyTimeout(150ms);
Prabir Pradhan678438e2023-04-13 19:32:51 +00003678 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
3679 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003680
3681 // Window should receive key event immediately when same key up.
3682 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3683 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3684}
3685
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003686/**
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003687 * Two windows. First is a regular window. Second does not overlap with the first, and has
3688 * WATCH_OUTSIDE_TOUCH.
3689 * Both windows are owned by the same UID.
3690 * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero
3691 * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID.
3692 */
3693TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) {
3694 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3695 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3696 "First Window", ADISPLAY_ID_DEFAULT);
3697 window->setFrame(Rect{0, 0, 100, 100});
3698
3699 sp<FakeWindowHandle> outsideWindow =
3700 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3701 ADISPLAY_ID_DEFAULT);
3702 outsideWindow->setFrame(Rect{100, 100, 200, 200});
3703 outsideWindow->setWatchOutsideTouch(true);
3704 // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped
3705 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {outsideWindow, window}}});
3706
3707 // Tap on first window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003708 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3709 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3710 {PointF{50, 50}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003711 window->consumeMotionDown();
3712 // The coordinates of the tap in 'outsideWindow' are relative to its top left corner.
3713 // Therefore, we should offset them by (100, 100) relative to the screen's top left corner.
3714 outsideWindow->consumeMotionEvent(
3715 AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50)));
3716}
3717
3718/**
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003719 * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when
3720 * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one
3721 * ACTION_OUTSIDE event is sent per gesture.
3722 */
3723TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) {
3724 // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH.
3725 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003726 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3727 "First Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003728 window->setWatchOutsideTouch(true);
3729 window->setFrame(Rect{0, 0, 100, 100});
3730 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003731 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3732 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003733 secondWindow->setFrame(Rect{100, 100, 200, 200});
3734 sp<FakeWindowHandle> thirdWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003735 sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window",
3736 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003737 thirdWindow->setFrame(Rect{200, 200, 300, 300});
3738 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}});
3739
3740 // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003741 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3742 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3743 {PointF{-10, -10}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003744 window->assertNoEvents();
3745 secondWindow->assertNoEvents();
3746
3747 // The second pointer lands inside `secondWindow`, which should receive a DOWN event.
3748 // Now, `window` should get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003749 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3750 ADISPLAY_ID_DEFAULT,
3751 {PointF{-10, -10}, PointF{105, 105}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003752 const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}};
3753 window->consumeMotionEvent(
3754 AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers)));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003755 secondWindow->consumeMotionDown();
3756 thirdWindow->assertNoEvents();
3757
3758 // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is
3759 // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003760 mDispatcher->notifyMotion(
3761 generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3762 {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003763 window->assertNoEvents();
3764 secondWindow->consumeMotionMove();
3765 thirdWindow->consumeMotionDown();
3766}
3767
Prabir Pradhan814fe082022-07-22 20:22:18 +00003768TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) {
3769 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003770 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3771 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003772 window->setFocusable(true);
3773
Patrick Williamsd828f302023-04-28 17:52:08 -05003774 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00003775 setFocusedWindow(window);
3776
3777 window->consumeFocusEvent(true);
3778
Prabir Pradhan678438e2023-04-13 19:32:51 +00003779 const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3780 const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
3781 mDispatcher->notifyKey(keyDown);
3782 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003783
3784 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3785 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3786
3787 // All windows are removed from the display. Ensure that we can no longer dispatch to it.
Patrick Williamsd828f302023-04-28 17:52:08 -05003788 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00003789
3790 window->consumeFocusEvent(false);
3791
Prabir Pradhan678438e2023-04-13 19:32:51 +00003792 mDispatcher->notifyKey(keyDown);
3793 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003794 window->assertNoEvents();
3795}
3796
Arthur Hung96483742022-11-15 03:30:48 +00003797TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) {
3798 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3799 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3800 "Fake Window", ADISPLAY_ID_DEFAULT);
3801 // Ensure window is non-split and have some transform.
3802 window->setPreventSplitting(true);
3803 window->setWindowOffset(20, 40);
Patrick Williamsd828f302023-04-28 17:52:08 -05003804 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung96483742022-11-15 03:30:48 +00003805
3806 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3807 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3808 {50, 50}))
3809 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3810 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3811
3812 const MotionEvent secondFingerDownEvent =
3813 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3814 .displayId(ADISPLAY_ID_DEFAULT)
3815 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003816 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
3817 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50))
Arthur Hung96483742022-11-15 03:30:48 +00003818 .build();
3819 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3820 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
3821 InputEventInjectionSync::WAIT_FOR_RESULT))
3822 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3823
3824 const MotionEvent* event = window->consumeMotion();
3825 EXPECT_EQ(POINTER_1_DOWN, event->getAction());
3826 EXPECT_EQ(70, event->getX(0)); // 50 + 20
3827 EXPECT_EQ(90, event->getY(0)); // 50 + 40
3828 EXPECT_EQ(-10, event->getX(1)); // -30 + 20
3829 EXPECT_EQ(-10, event->getY(1)); // -50 + 40
3830}
3831
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003832/**
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003833 * Ensure the correct coordinate spaces are used by InputDispatcher.
3834 *
3835 * InputDispatcher works in the display space, so its coordinate system is relative to the display
3836 * panel. Windows get events in the window space, and get raw coordinates in the logical display
3837 * space.
3838 */
3839class InputDispatcherDisplayProjectionTest : public InputDispatcherTest {
3840public:
3841 void SetUp() override {
3842 InputDispatcherTest::SetUp();
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00003843 removeAllWindowsAndDisplays();
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003844 }
3845
3846 void addDisplayInfo(int displayId, const ui::Transform& transform) {
3847 gui::DisplayInfo info;
3848 info.displayId = displayId;
3849 info.transform = transform;
3850 mDisplayInfos.push_back(std::move(info));
Patrick Williamsd828f302023-04-28 17:52:08 -05003851 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003852 }
3853
3854 void addWindow(const sp<WindowInfoHandle>& windowHandle) {
3855 mWindowInfos.push_back(*windowHandle->getInfo());
Patrick Williamsd828f302023-04-28 17:52:08 -05003856 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003857 }
3858
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00003859 void removeAllWindowsAndDisplays() {
3860 mDisplayInfos.clear();
3861 mWindowInfos.clear();
3862 }
3863
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003864 // Set up a test scenario where the display has a scaled projection and there are two windows
3865 // on the display.
3866 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() {
3867 // The display has a projection that has a scale factor of 2 and 4 in the x and y directions
3868 // respectively.
3869 ui::Transform displayTransform;
3870 displayTransform.set(2, 0, 0, 4);
3871 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
3872
3873 std::shared_ptr<FakeApplicationHandle> application =
3874 std::make_shared<FakeApplicationHandle>();
3875
3876 // Add two windows to the display. Their frames are represented in the display space.
3877 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003878 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3879 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003880 firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform);
3881 addWindow(firstWindow);
3882
3883 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003884 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3885 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003886 secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform);
3887 addWindow(secondWindow);
3888 return {std::move(firstWindow), std::move(secondWindow)};
3889 }
3890
3891private:
3892 std::vector<gui::DisplayInfo> mDisplayInfos;
3893 std::vector<gui::WindowInfo> mWindowInfos;
3894};
3895
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00003896TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) {
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003897 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3898 // Send down to the first window. The point is represented in the display space. The point is
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00003899 // selected so that if the hit test was performed with the point and the bounds being in
3900 // different coordinate spaces, the event would end up in the incorrect window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003901 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3902 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3903 {PointF{75, 55}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003904
3905 firstWindow->consumeMotionDown();
3906 secondWindow->assertNoEvents();
3907}
3908
3909// Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API,
3910// the event should be treated as being in the logical display space.
3911TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) {
3912 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3913 // Send down to the first window. The point is represented in the logical display space. The
3914 // point is selected so that if the hit test was done in logical display space, then it would
3915 // end up in the incorrect window.
3916 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3917 PointF{75 * 2, 55 * 4});
3918
3919 firstWindow->consumeMotionDown();
3920 secondWindow->assertNoEvents();
3921}
3922
Prabir Pradhandaa2f142021-12-10 09:30:08 +00003923// Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed
3924// event should be treated as being in the logical display space.
3925TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) {
3926 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3927
3928 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
3929 ui::Transform injectedEventTransform;
3930 injectedEventTransform.set(matrix);
3931 const vec2 expectedPoint{75, 55}; // The injected point in the logical display space.
3932 const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint);
3933
3934 MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3935 .displayId(ADISPLAY_ID_DEFAULT)
3936 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003937 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER)
Prabir Pradhandaa2f142021-12-10 09:30:08 +00003938 .x(untransformedPoint.x)
3939 .y(untransformedPoint.y))
3940 .build();
3941 event.transform(matrix);
3942
3943 injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT,
3944 InputEventInjectionSync::WAIT_FOR_RESULT);
3945
3946 firstWindow->consumeMotionDown();
3947 secondWindow->assertNoEvents();
3948}
3949
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003950TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) {
3951 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3952
3953 // Send down to the second window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003954 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3955 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3956 {PointF{150, 220}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003957
3958 firstWindow->assertNoEvents();
3959 const MotionEvent* event = secondWindow->consumeMotion();
3960 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction());
3961
3962 // Ensure that the events from the "getRaw" API are in logical display coordinates.
3963 EXPECT_EQ(300, event->getRawX(0));
3964 EXPECT_EQ(880, event->getRawY(0));
3965
3966 // Ensure that the x and y values are in the window's coordinate space.
3967 // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in
3968 // the logical display space. This will be the origin of the window space.
3969 EXPECT_EQ(100, event->getX(0));
3970 EXPECT_EQ(80, event->getY(0));
3971}
3972
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00003973/** Ensure consistent behavior of InputDispatcher in all orientations. */
3974class InputDispatcherDisplayOrientationFixture
3975 : public InputDispatcherDisplayProjectionTest,
3976 public ::testing::WithParamInterface<ui::Rotation> {};
3977
3978// This test verifies the touchable region of a window for all rotations of the display by tapping
3979// in different locations on the display, specifically points close to the four corners of a
3980// window.
3981TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) {
3982 constexpr static int32_t displayWidth = 400;
3983 constexpr static int32_t displayHeight = 800;
3984
3985 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3986
3987 const auto rotation = GetParam();
3988
3989 // Set up the display with the specified rotation.
3990 const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270;
3991 const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth;
3992 const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight;
3993 const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation),
3994 logicalDisplayWidth, logicalDisplayHeight);
3995 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
3996
3997 // Create a window with its bounds determined in the logical display.
3998 const Rect frameInLogicalDisplay(100, 100, 200, 300);
3999 const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay);
4000 sp<FakeWindowHandle> window =
4001 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4002 window->setFrame(frameInDisplay, displayTransform);
4003 addWindow(window);
4004
4005 // The following points in logical display space should be inside the window.
4006 static const std::array<vec2, 4> insidePoints{
4007 {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}};
4008 for (const auto pointInsideWindow : insidePoints) {
4009 const vec2 p = displayTransform.inverse().transform(pointInsideWindow);
4010 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004011 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4012 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4013 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004014 window->consumeMotionDown();
4015
Prabir Pradhan678438e2023-04-13 19:32:51 +00004016 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4017 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4018 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004019 window->consumeMotionUp();
4020 }
4021
4022 // The following points in logical display space should be outside the window.
4023 static const std::array<vec2, 5> outsidePoints{
4024 {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}};
4025 for (const auto pointOutsideWindow : outsidePoints) {
4026 const vec2 p = displayTransform.inverse().transform(pointOutsideWindow);
4027 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004028 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4029 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4030 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004031
Prabir Pradhan678438e2023-04-13 19:32:51 +00004032 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4033 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4034 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004035 }
4036 window->assertNoEvents();
4037}
4038
4039// Run the precision tests for all rotations.
4040INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests,
4041 InputDispatcherDisplayOrientationFixture,
4042 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
4043 ui::ROTATION_270),
4044 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
4045 return ftl::enum_string(testParamInfo.param);
4046 });
4047
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004048using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher,
4049 sp<IBinder>, sp<IBinder>)>;
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004050
4051class TransferTouchFixture : public InputDispatcherTest,
4052 public ::testing::WithParamInterface<TransferFunction> {};
4053
4054TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07004055 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004056
4057 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004058 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004059 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4060 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004061 firstWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004062 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004063 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4064 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004065 sp<FakeWindowHandle> wallpaper =
4066 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
4067 wallpaper->setIsWallpaper(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004068 // Add the windows to the dispatcher
Arthur Hungc539dbb2022-12-08 07:45:36 +00004069 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004070
4071 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004072 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4073 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004074
Svet Ganov5d3bc372020-01-26 23:11:07 -08004075 // Only the first window should get the down event
4076 firstWindow->consumeMotionDown();
4077 secondWindow->assertNoEvents();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004078 wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004079
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004080 // Transfer touch to the second window
4081 TransferFunction f = GetParam();
4082 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4083 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004084 // The first window gets cancel and the second gets down
4085 firstWindow->consumeMotionCancel();
4086 secondWindow->consumeMotionDown();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004087 wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004088
4089 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004090 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4091 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004092 // The first window gets no events and the second gets up
4093 firstWindow->assertNoEvents();
4094 secondWindow->consumeMotionUp();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004095 wallpaper->assertNoEvents();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004096}
4097
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004098/**
4099 * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch
4100 * from. When we have spy windows, there are several windows to choose from: either spy, or the
4101 * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most
4102 * natural to the user.
4103 * In this test, we are sending a pointer to both spy window and first window. We then try to
4104 * transfer touch to the second window. The dispatcher should identify the first window as the
4105 * one that should lose the gesture, and therefore the action should be to move the gesture from
4106 * the first window to the second.
4107 * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test
4108 * the other API, as well.
4109 */
4110TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) {
4111 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4112
4113 // Create a couple of windows + a spy window
4114 sp<FakeWindowHandle> spyWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004115 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004116 spyWindow->setTrustedOverlay(true);
4117 spyWindow->setSpy(true);
4118 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004119 sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004120 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004121 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004122
4123 // Add the windows to the dispatcher
4124 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}});
4125
4126 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004127 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4128 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004129 // Only the first window and spy should get the down event
4130 spyWindow->consumeMotionDown();
4131 firstWindow->consumeMotionDown();
4132
4133 // Transfer touch to the second window. Non-spy window should be preferred over the spy window
4134 // if f === 'transferTouch'.
4135 TransferFunction f = GetParam();
4136 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4137 ASSERT_TRUE(success);
4138 // The first window gets cancel and the second gets down
4139 firstWindow->consumeMotionCancel();
4140 secondWindow->consumeMotionDown();
4141
4142 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004143 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4144 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004145 // The first window gets no events and the second+spy get up
4146 firstWindow->assertNoEvents();
4147 spyWindow->consumeMotionUp();
4148 secondWindow->consumeMotionUp();
4149}
4150
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004151TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004152 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004153
4154 PointF touchPoint = {10, 10};
4155
4156 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004157 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004158 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4159 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004160 firstWindow->setPreventSplitting(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004161 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004162 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4163 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004164 secondWindow->setPreventSplitting(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004165
4166 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004167 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004168
4169 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004170 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4171 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4172 {touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004173 // Only the first window should get the down event
4174 firstWindow->consumeMotionDown();
4175 secondWindow->assertNoEvents();
4176
4177 // Send pointer down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004178 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4179 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004180 // Only the first window should get the pointer down event
4181 firstWindow->consumeMotionPointerDown(1);
4182 secondWindow->assertNoEvents();
4183
4184 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004185 TransferFunction f = GetParam();
4186 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4187 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004188 // The first window gets cancel and the second gets down and pointer down
4189 firstWindow->consumeMotionCancel();
4190 secondWindow->consumeMotionDown();
4191 secondWindow->consumeMotionPointerDown(1);
4192
4193 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004194 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4195 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004196 // The first window gets nothing and the second gets pointer up
4197 firstWindow->assertNoEvents();
4198 secondWindow->consumeMotionPointerUp(1);
4199
4200 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004201 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4202 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004203 // The first window gets nothing and the second gets up
4204 firstWindow->assertNoEvents();
4205 secondWindow->consumeMotionUp();
4206}
4207
Arthur Hungc539dbb2022-12-08 07:45:36 +00004208TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) {
4209 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4210
4211 // Create a couple of windows
4212 sp<FakeWindowHandle> firstWindow =
4213 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4214 ADISPLAY_ID_DEFAULT);
4215 firstWindow->setDupTouchToWallpaper(true);
4216 sp<FakeWindowHandle> secondWindow =
4217 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4218 ADISPLAY_ID_DEFAULT);
4219 secondWindow->setDupTouchToWallpaper(true);
4220
4221 sp<FakeWindowHandle> wallpaper1 =
4222 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT);
4223 wallpaper1->setIsWallpaper(true);
4224
4225 sp<FakeWindowHandle> wallpaper2 =
4226 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT);
4227 wallpaper2->setIsWallpaper(true);
4228 // Add the windows to the dispatcher
4229 mDispatcher->setInputWindows(
4230 {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}});
4231
4232 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004233 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4234 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004235
4236 // Only the first window should get the down event
4237 firstWindow->consumeMotionDown();
4238 secondWindow->assertNoEvents();
4239 wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4240 wallpaper2->assertNoEvents();
4241
4242 // Transfer touch focus to the second window
4243 TransferFunction f = GetParam();
4244 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4245 ASSERT_TRUE(success);
4246
4247 // The first window gets cancel and the second gets down
4248 firstWindow->consumeMotionCancel();
4249 secondWindow->consumeMotionDown();
4250 wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4251 wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4252
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));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004256 // The first window gets no events and the second gets up
4257 firstWindow->assertNoEvents();
4258 secondWindow->consumeMotionUp();
4259 wallpaper1->assertNoEvents();
4260 wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4261}
4262
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004263// For the cases of single pointer touch and two pointers non-split touch, the api's
4264// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
4265// for the case where there are multiple pointers split across several windows.
4266INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
4267 ::testing::Values(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004268 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4269 sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004270 return dispatcher->transferTouch(destChannelToken,
4271 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004272 },
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004273 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4274 sp<IBinder> from, sp<IBinder> to) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004275 return dispatcher->transferTouchFocus(from, to,
Harry Cutts33476232023-01-30 19:57:29 +00004276 /*isDragAndDrop=*/false);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004277 }));
4278
Svet Ganov5d3bc372020-01-26 23:11:07 -08004279TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004280 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004281
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004282 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004283 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4284 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004285 firstWindow->setFrame(Rect(0, 0, 600, 400));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004286
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004287 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004288 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4289 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004290 secondWindow->setFrame(Rect(0, 400, 600, 800));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004291
4292 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004293 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004294
4295 PointF pointInFirst = {300, 200};
4296 PointF pointInSecond = {300, 600};
4297
4298 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004299 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4300 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4301 {pointInFirst}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004302 // Only the first window should get the down event
4303 firstWindow->consumeMotionDown();
4304 secondWindow->assertNoEvents();
4305
4306 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004307 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4308 ADISPLAY_ID_DEFAULT,
4309 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004310 // The first window gets a move and the second a down
4311 firstWindow->consumeMotionMove();
4312 secondWindow->consumeMotionDown();
4313
4314 // Transfer touch focus to the second window
4315 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
4316 // The first window gets cancel and the new gets pointer down (it already saw down)
4317 firstWindow->consumeMotionCancel();
4318 secondWindow->consumeMotionPointerDown(1);
4319
4320 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004321 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4322 ADISPLAY_ID_DEFAULT,
4323 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004324 // The first window gets nothing and the second gets pointer up
4325 firstWindow->assertNoEvents();
4326 secondWindow->consumeMotionPointerUp(1);
4327
4328 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004329 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4330 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004331 // The first window gets nothing and the second gets up
4332 firstWindow->assertNoEvents();
4333 secondWindow->consumeMotionUp();
4334}
4335
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004336// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
4337// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
4338// touch is not supported, so the touch should continue on those windows and the transferred-to
4339// window should get nothing.
4340TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
4341 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4342
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004343 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004344 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4345 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004346 firstWindow->setFrame(Rect(0, 0, 600, 400));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004347
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004348 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004349 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4350 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004351 secondWindow->setFrame(Rect(0, 400, 600, 800));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004352
4353 // Add the windows to the dispatcher
4354 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4355
4356 PointF pointInFirst = {300, 200};
4357 PointF pointInSecond = {300, 600};
4358
4359 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004360 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4361 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4362 {pointInFirst}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004363 // Only the first window should get the down event
4364 firstWindow->consumeMotionDown();
4365 secondWindow->assertNoEvents();
4366
4367 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004368 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4369 ADISPLAY_ID_DEFAULT,
4370 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004371 // The first window gets a move and the second a down
4372 firstWindow->consumeMotionMove();
4373 secondWindow->consumeMotionDown();
4374
4375 // Transfer touch focus to the second window
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004376 const bool transferred =
4377 mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004378 // The 'transferTouch' call should not succeed, because there are 2 touched windows
4379 ASSERT_FALSE(transferred);
4380 firstWindow->assertNoEvents();
4381 secondWindow->assertNoEvents();
4382
4383 // The rest of the dispatch should proceed as normal
4384 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004385 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4386 ADISPLAY_ID_DEFAULT,
4387 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004388 // The first window gets MOVE and the second gets pointer up
4389 firstWindow->consumeMotionMove();
4390 secondWindow->consumeMotionUp();
4391
4392 // Send up event to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004393 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4394 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004395 // The first window gets nothing and the second gets up
4396 firstWindow->consumeMotionUp();
4397 secondWindow->assertNoEvents();
4398}
4399
Arthur Hungabbb9d82021-09-01 14:52:30 +00004400// This case will create two windows and one mirrored window on the default display and mirror
4401// two windows on the second display. It will test if 'transferTouchFocus' works fine if we put
4402// the windows info of second display before default display.
4403TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) {
4404 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4405 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004406 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004407 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004408 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004409 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004410 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004411
4412 sp<FakeWindowHandle> mirrorWindowInPrimary =
4413 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
4414 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004415
4416 sp<FakeWindowHandle> firstWindowInSecondary =
4417 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4418 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004419
4420 sp<FakeWindowHandle> secondWindowInSecondary =
4421 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4422 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004423
4424 // Update window info, let it find window handle of second display first.
4425 mDispatcher->setInputWindows(
4426 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4427 {ADISPLAY_ID_DEFAULT,
4428 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4429
4430 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4431 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4432 {50, 50}))
4433 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4434
4435 // Window should receive motion event.
4436 firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4437
4438 // Transfer touch focus
4439 ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(),
4440 secondWindowInPrimary->getToken()));
4441 // The first window gets cancel.
4442 firstWindowInPrimary->consumeMotionCancel();
4443 secondWindowInPrimary->consumeMotionDown();
4444
4445 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4446 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4447 ADISPLAY_ID_DEFAULT, {150, 50}))
4448 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4449 firstWindowInPrimary->assertNoEvents();
4450 secondWindowInPrimary->consumeMotionMove();
4451
4452 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4453 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4454 {150, 50}))
4455 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4456 firstWindowInPrimary->assertNoEvents();
4457 secondWindowInPrimary->consumeMotionUp();
4458}
4459
4460// Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use
4461// 'transferTouch' api.
4462TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
4463 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4464 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004465 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004466 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004467 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004468 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004469 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004470
4471 sp<FakeWindowHandle> mirrorWindowInPrimary =
4472 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
4473 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004474
4475 sp<FakeWindowHandle> firstWindowInSecondary =
4476 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4477 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004478
4479 sp<FakeWindowHandle> secondWindowInSecondary =
4480 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4481 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004482
4483 // Update window info, let it find window handle of second display first.
4484 mDispatcher->setInputWindows(
4485 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4486 {ADISPLAY_ID_DEFAULT,
4487 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4488
4489 // Touch on second display.
4490 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4491 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50}))
4492 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4493
4494 // Window should receive motion event.
4495 firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4496
4497 // Transfer touch focus
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004498 ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004499
4500 // The first window gets cancel.
4501 firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID);
4502 secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4503
4504 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4505 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4506 SECOND_DISPLAY_ID, {150, 50}))
4507 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4508 firstWindowInPrimary->assertNoEvents();
4509 secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID);
4510
4511 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4512 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
4513 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4514 firstWindowInPrimary->assertNoEvents();
4515 secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID);
4516}
4517
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004518TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004519 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004520 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4521 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004522
Vishnu Nair47074b82020-08-14 11:54:47 -07004523 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004524 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004525 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004526
4527 window->consumeFocusEvent(true);
4528
Prabir Pradhan678438e2023-04-13 19:32:51 +00004529 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004530
4531 // Window should receive key down event.
4532 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4533}
4534
4535TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004536 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004537 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4538 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004539
Arthur Hung72d8dc32020-03-28 00:48:39 +00004540 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004541
Prabir Pradhan678438e2023-04-13 19:32:51 +00004542 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004543 mDispatcher->waitForIdle();
4544
4545 window->assertNoEvents();
4546}
4547
4548// If a window is touchable, but does not have focus, it should receive motion events, but not keys
4549TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07004550 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004551 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4552 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004553
Arthur Hung72d8dc32020-03-28 00:48:39 +00004554 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004555
4556 // Send key
Prabir Pradhan678438e2023-04-13 19:32:51 +00004557 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004558 // Send motion
Prabir Pradhan678438e2023-04-13 19:32:51 +00004559 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4560 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004561
4562 // Window should receive only the motion event
4563 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4564 window->assertNoEvents(); // Key event or focus event will not be received
4565}
4566
arthurhungea3f4fc2020-12-21 23:18:53 +08004567TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
4568 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4569
arthurhungea3f4fc2020-12-21 23:18:53 +08004570 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004571 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4572 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004573 firstWindow->setFrame(Rect(0, 0, 600, 400));
arthurhungea3f4fc2020-12-21 23:18:53 +08004574
arthurhungea3f4fc2020-12-21 23:18:53 +08004575 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004576 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4577 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004578 secondWindow->setFrame(Rect(0, 400, 600, 800));
arthurhungea3f4fc2020-12-21 23:18:53 +08004579
4580 // Add the windows to the dispatcher
4581 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4582
4583 PointF pointInFirst = {300, 200};
4584 PointF pointInSecond = {300, 600};
4585
4586 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004587 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4588 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4589 {pointInFirst}));
arthurhungea3f4fc2020-12-21 23:18:53 +08004590 // Only the first window should get the down event
4591 firstWindow->consumeMotionDown();
4592 secondWindow->assertNoEvents();
4593
4594 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004595 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4596 ADISPLAY_ID_DEFAULT,
4597 {pointInFirst, pointInSecond}));
arthurhungea3f4fc2020-12-21 23:18:53 +08004598 // The first window gets a move and the second a down
4599 firstWindow->consumeMotionMove();
4600 secondWindow->consumeMotionDown();
4601
4602 // Send pointer cancel to the second window
4603 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004604 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08004605 {pointInFirst, pointInSecond});
4606 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
Prabir Pradhan678438e2023-04-13 19:32:51 +00004607 mDispatcher->notifyMotion(pointerUpMotionArgs);
arthurhungea3f4fc2020-12-21 23:18:53 +08004608 // The first window gets move and the second gets cancel.
4609 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
4610 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
4611
4612 // Send up event.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004613 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4614 ADISPLAY_ID_DEFAULT));
arthurhungea3f4fc2020-12-21 23:18:53 +08004615 // The first window gets up and the second gets nothing.
4616 firstWindow->consumeMotionUp();
4617 secondWindow->assertNoEvents();
4618}
4619
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004620TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
4621 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4622
4623 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004624 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004625 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4626 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
4627 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
4628 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
4629
Harry Cutts33476232023-01-30 19:57:29 +00004630 window->sendTimeline(/*inputEventId=*/1, graphicsTimeline);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004631 window->assertNoEvents();
4632 mDispatcher->waitForIdle();
4633}
4634
chaviwd1c23182019-12-20 18:44:56 -08004635class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00004636public:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004637 FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004638 int32_t displayId) {
Garfield Tan15601662020-09-22 15:32:38 -07004639 base::Result<std::unique_ptr<InputChannel>> channel =
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08004640 dispatcher->createInputMonitor(displayId, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07004641 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00004642 }
4643
chaviwd1c23182019-12-20 18:44:56 -08004644 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
4645
4646 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004647 mInputReceiver->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
4648 expectedFlags);
chaviwd1c23182019-12-20 18:44:56 -08004649 }
4650
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004651 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
4652
4653 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
4654
chaviwd1c23182019-12-20 18:44:56 -08004655 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004656 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
chaviwd1c23182019-12-20 18:44:56 -08004657 expectedDisplayId, expectedFlags);
4658 }
4659
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004660 void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004661 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004662 expectedDisplayId, expectedFlags);
4663 }
4664
chaviwd1c23182019-12-20 18:44:56 -08004665 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004666 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP,
chaviwd1c23182019-12-20 18:44:56 -08004667 expectedDisplayId, expectedFlags);
4668 }
4669
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004670 void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08004671 mInputReceiver->consumeMotionEvent(
4672 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
4673 WithDisplayId(expectedDisplayId),
4674 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004675 }
4676
Arthur Hungfbfa5722021-11-16 02:45:54 +00004677 void consumeMotionPointerDown(int32_t pointerIdx) {
4678 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
4679 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004680 mInputReceiver->consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00004681 /*expectedFlags=*/0);
Arthur Hungfbfa5722021-11-16 02:45:54 +00004682 }
4683
Evan Rosky84f07f02021-04-16 10:42:42 -07004684 MotionEvent* consumeMotion() {
4685 InputEvent* event = mInputReceiver->consume();
4686 if (!event) {
4687 ADD_FAILURE() << "No event was produced";
4688 return nullptr;
4689 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004690 if (event->getType() != InputEventType::MOTION) {
4691 ADD_FAILURE() << "Expected MotionEvent, got " << *event;
Evan Rosky84f07f02021-04-16 10:42:42 -07004692 return nullptr;
4693 }
4694 return static_cast<MotionEvent*>(event);
4695 }
4696
chaviwd1c23182019-12-20 18:44:56 -08004697 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
4698
4699private:
4700 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00004701};
4702
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004703using InputDispatcherMonitorTest = InputDispatcherTest;
4704
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004705/**
4706 * Two entities that receive touch: A window, and a global monitor.
4707 * The touch goes to the window, and then the window disappears.
4708 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
4709 * for the monitor, as well.
4710 * 1. foregroundWindow
4711 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
4712 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004713TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004714 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4715 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004716 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004717
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004718 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004719
4720 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4721 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4722 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4723 {100, 200}))
4724 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4725
4726 // Both the foreground window and the global monitor should receive the touch down
4727 window->consumeMotionDown();
4728 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
4729
4730 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4731 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4732 ADISPLAY_ID_DEFAULT, {110, 200}))
4733 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4734
4735 window->consumeMotionMove();
4736 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
4737
4738 // Now the foreground window goes away
4739 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
4740 window->consumeMotionCancel();
4741 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
4742
4743 // If more events come in, there will be no more foreground window to send them to. This will
4744 // cause a cancel for the monitor, as well.
4745 ASSERT_EQ(InputEventInjectionResult::FAILED,
4746 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4747 ADISPLAY_ID_DEFAULT, {120, 200}))
4748 << "Injection should fail because the window was removed";
4749 window->assertNoEvents();
4750 // Global monitor now gets the cancel
4751 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
4752}
4753
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004754TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07004755 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004756 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4757 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004758 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00004759
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004760 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004761
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004762 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00004763 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004764 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00004765 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08004766 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004767}
4768
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004769TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
4770 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004771
Chris Yea209fde2020-07-22 13:54:51 -07004772 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004773 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4774 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004775 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00004776
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004777 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00004778 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004779 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08004780 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004781 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004782
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004783 // Pilfer pointers from the monitor.
4784 // This should not do anything and the window should continue to receive events.
4785 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00004786
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004787 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004788 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4789 ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004790 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004791
4792 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
4793 window->consumeMotionMove(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004794}
4795
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004796TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07004797 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004798 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4799 "Fake Window", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07004800 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4801 window->setWindowOffset(20, 40);
4802 window->setWindowTransform(0, 1, -1, 0);
4803
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004804 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07004805
4806 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4807 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4808 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4809 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4810 MotionEvent* event = monitor.consumeMotion();
4811 // Even though window has transform, gesture monitor must not.
4812 ASSERT_EQ(ui::Transform(), event->getTransform());
4813}
4814
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004815TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00004816 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004817 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00004818
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004819 ASSERT_EQ(InputEventInjectionResult::FAILED,
Arthur Hungb3307ee2021-10-14 10:57:37 +00004820 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004821 << "Injection should fail if there is a monitor, but no touchable window";
4822 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00004823}
4824
chaviw81e2bb92019-12-18 15:03:51 -08004825TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004826 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004827 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4828 "Fake Window", ADISPLAY_ID_DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08004829
Arthur Hung72d8dc32020-03-28 00:48:39 +00004830 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08004831
4832 NotifyMotionArgs motionArgs =
4833 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4834 ADISPLAY_ID_DEFAULT);
4835
Prabir Pradhan678438e2023-04-13 19:32:51 +00004836 mDispatcher->notifyMotion(motionArgs);
chaviw81e2bb92019-12-18 15:03:51 -08004837 // Window should receive motion down event.
4838 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4839
4840 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08004841 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08004842 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
4843 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4844 motionArgs.pointerCoords[0].getX() - 10);
4845
Prabir Pradhan678438e2023-04-13 19:32:51 +00004846 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004847 window->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00004848 /*expectedFlags=*/0);
chaviw81e2bb92019-12-18 15:03:51 -08004849}
4850
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004851/**
4852 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
4853 * the device default right away. In the test scenario, we check both the default value,
4854 * and the action of enabling / disabling.
4855 */
4856TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07004857 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004858 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4859 "Test window", ADISPLAY_ID_DEFAULT);
Antonio Kantekea47acb2021-12-23 12:41:25 -08004860 const WindowInfo& windowInfo = *window->getInfo();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004861
4862 // Set focused application.
4863 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07004864 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004865
4866 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00004867 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004868 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00004869 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004870
4871 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07004872 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004873 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00004874 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004875
4876 SCOPED_TRACE("Disable touch mode");
Antonio Kantekea47acb2021-12-23 12:41:25 -08004877 mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00004878 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00004879 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07004880 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004881 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004882 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00004883 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004884
4885 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07004886 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004887 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00004888 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004889
4890 SCOPED_TRACE("Enable touch mode again");
Antonio Kantekea47acb2021-12-23 12:41:25 -08004891 mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00004892 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00004893 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07004894 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004895 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004896 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00004897 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004898
4899 window->assertNoEvents();
4900}
4901
Gang Wange9087892020-01-07 12:17:14 -05004902TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004903 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004904 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4905 "Test window", ADISPLAY_ID_DEFAULT);
Gang Wange9087892020-01-07 12:17:14 -05004906
4907 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07004908 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05004909
Arthur Hung72d8dc32020-03-28 00:48:39 +00004910 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004911 setFocusedWindow(window);
4912
Harry Cutts33476232023-01-30 19:57:29 +00004913 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Gang Wange9087892020-01-07 12:17:14 -05004914
Prabir Pradhan678438e2023-04-13 19:32:51 +00004915 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
4916 mDispatcher->notifyKey(keyArgs);
Gang Wange9087892020-01-07 12:17:14 -05004917
4918 InputEvent* event = window->consume();
4919 ASSERT_NE(event, nullptr);
4920
4921 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
4922 ASSERT_NE(verified, nullptr);
4923 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
4924
4925 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
4926 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
4927 ASSERT_EQ(keyArgs.source, verified->source);
4928 ASSERT_EQ(keyArgs.displayId, verified->displayId);
4929
4930 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
4931
4932 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05004933 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08004934 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05004935 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
4936 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
4937 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
4938 ASSERT_EQ(0, verifiedKey.repeatCount);
4939}
4940
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004941TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004942 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004943 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4944 "Test window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004945
4946 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4947
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07004948 ui::Transform transform;
4949 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
4950
4951 gui::DisplayInfo displayInfo;
4952 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
4953 displayInfo.transform = transform;
4954
Patrick Williamsd828f302023-04-28 17:52:08 -05004955 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004956
Prabir Pradhan678438e2023-04-13 19:32:51 +00004957 const NotifyMotionArgs motionArgs =
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004958 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4959 ADISPLAY_ID_DEFAULT);
Prabir Pradhan678438e2023-04-13 19:32:51 +00004960 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004961
4962 InputEvent* event = window->consume();
4963 ASSERT_NE(event, nullptr);
4964
4965 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
4966 ASSERT_NE(verified, nullptr);
4967 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
4968
4969 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
4970 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
4971 EXPECT_EQ(motionArgs.source, verified->source);
4972 EXPECT_EQ(motionArgs.displayId, verified->displayId);
4973
4974 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
4975
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07004976 const vec2 rawXY =
4977 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
4978 motionArgs.pointerCoords[0].getXYValue());
4979 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
4980 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004981 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004982 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08004983 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004984 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
4985 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
4986}
4987
chaviw09c8d2d2020-08-24 15:48:26 -07004988/**
4989 * Ensure that separate calls to sign the same data are generating the same key.
4990 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
4991 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
4992 * tests.
4993 */
4994TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
4995 KeyEvent event = getTestKeyEvent();
4996 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
4997
4998 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
4999 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
5000 ASSERT_EQ(hmac1, hmac2);
5001}
5002
5003/**
5004 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
5005 */
5006TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
5007 KeyEvent event = getTestKeyEvent();
5008 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5009 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
5010
5011 verifiedEvent.deviceId += 1;
5012 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5013
5014 verifiedEvent.source += 1;
5015 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5016
5017 verifiedEvent.eventTimeNanos += 1;
5018 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5019
5020 verifiedEvent.displayId += 1;
5021 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5022
5023 verifiedEvent.action += 1;
5024 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5025
5026 verifiedEvent.downTimeNanos += 1;
5027 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5028
5029 verifiedEvent.flags += 1;
5030 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5031
5032 verifiedEvent.keyCode += 1;
5033 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5034
5035 verifiedEvent.scanCode += 1;
5036 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5037
5038 verifiedEvent.metaState += 1;
5039 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5040
5041 verifiedEvent.repeatCount += 1;
5042 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5043}
5044
Vishnu Nair958da932020-08-21 17:12:37 -07005045TEST_F(InputDispatcherTest, SetFocusedWindow) {
5046 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5047 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005048 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005049 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005050 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005051 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5052
5053 // Top window is also focusable but is not granted focus.
5054 windowTop->setFocusable(true);
5055 windowSecond->setFocusable(true);
5056 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5057 setFocusedWindow(windowSecond);
5058
5059 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005060 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5061 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005062
5063 // Focused window should receive event.
5064 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5065 windowTop->assertNoEvents();
5066}
5067
5068TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
5069 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5070 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005071 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005072 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5073
5074 window->setFocusable(true);
5075 // Release channel for window is no longer valid.
5076 window->releaseChannel();
5077 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5078 setFocusedWindow(window);
5079
5080 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005081 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5082 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005083
5084 // window channel is invalid, so it should not receive any input event.
5085 window->assertNoEvents();
5086}
5087
5088TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
5089 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5090 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005091 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005092 window->setFocusable(false);
Vishnu Nair958da932020-08-21 17:12:37 -07005093 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5094
Vishnu Nair958da932020-08-21 17:12:37 -07005095 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5096 setFocusedWindow(window);
5097
5098 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005099 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5100 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005101
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005102 // window is not focusable, so it should not receive any input event.
Vishnu Nair958da932020-08-21 17:12:37 -07005103 window->assertNoEvents();
5104}
5105
5106TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
5107 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5108 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005109 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005110 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005111 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005112 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5113
5114 windowTop->setFocusable(true);
5115 windowSecond->setFocusable(true);
5116 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5117 setFocusedWindow(windowTop);
5118 windowTop->consumeFocusEvent(true);
5119
Chavi Weingarten847e8512023-03-29 00:26:09 +00005120 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
5121 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005122 windowSecond->consumeFocusEvent(true);
5123 windowTop->consumeFocusEvent(false);
5124
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005125 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5126 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005127
5128 // Focused window should receive event.
5129 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5130}
5131
Chavi Weingarten847e8512023-03-29 00:26:09 +00005132TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) {
Vishnu Nair958da932020-08-21 17:12:37 -07005133 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5134 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005135 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005136 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005137 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005138 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5139
5140 windowTop->setFocusable(true);
Chavi Weingarten847e8512023-03-29 00:26:09 +00005141 windowSecond->setFocusable(false);
5142 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
Vishnu Nair958da932020-08-21 17:12:37 -07005143 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Chavi Weingarten847e8512023-03-29 00:26:09 +00005144 setFocusedWindow(windowTop);
5145 windowTop->consumeFocusEvent(true);
Vishnu Nair958da932020-08-21 17:12:37 -07005146
Chavi Weingarten847e8512023-03-29 00:26:09 +00005147 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5148 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005149
5150 // Event should be dropped.
Chavi Weingarten847e8512023-03-29 00:26:09 +00005151 windowTop->consumeKeyDown(ADISPLAY_ID_NONE);
Vishnu Nair958da932020-08-21 17:12:37 -07005152 windowSecond->assertNoEvents();
5153}
5154
5155TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
5156 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5157 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005158 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005159 sp<FakeWindowHandle> previousFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005160 sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow",
5161 ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005162 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5163
5164 window->setFocusable(true);
5165 previousFocusedWindow->setFocusable(true);
5166 window->setVisible(false);
5167 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
5168 setFocusedWindow(previousFocusedWindow);
5169 previousFocusedWindow->consumeFocusEvent(true);
5170
5171 // Requesting focus on invisible window takes focus from currently focused window.
5172 setFocusedWindow(window);
5173 previousFocusedWindow->consumeFocusEvent(false);
5174
5175 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005176 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005177 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
5178 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07005179
5180 // Window does not get focus event or key down.
5181 window->assertNoEvents();
5182
5183 // Window becomes visible.
5184 window->setVisible(true);
5185 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5186
5187 // Window receives focus event.
5188 window->consumeFocusEvent(true);
5189 // Focused window receives key down.
5190 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5191}
5192
Vishnu Nair599f1412021-06-21 10:39:58 -07005193TEST_F(InputDispatcherTest, DisplayRemoved) {
5194 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5195 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005196 sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
Vishnu Nair599f1412021-06-21 10:39:58 -07005197 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5198
5199 // window is granted focus.
5200 window->setFocusable(true);
5201 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5202 setFocusedWindow(window);
5203 window->consumeFocusEvent(true);
5204
5205 // When a display is removed window loses focus.
5206 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
5207 window->consumeFocusEvent(false);
5208}
5209
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005210/**
5211 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
5212 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
5213 * of the 'slipperyEnterWindow'.
5214 *
5215 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
5216 * a way so that the touched location is no longer covered by the top window.
5217 *
5218 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
5219 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
5220 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
5221 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
5222 * with ACTION_DOWN).
5223 * Thus, the touch has been transferred from the top window into the bottom window, because the top
5224 * window moved itself away from the touched location and had Flag::SLIPPERY.
5225 *
5226 * Even though the top window moved away from the touched location, it is still obscuring the bottom
5227 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
5228 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
5229 *
5230 * In this test, we ensure that the event received by the bottom window has
5231 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
5232 */
5233TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
Prabir Pradhan5735a322022-04-11 17:23:34 +00005234 constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1;
5235 constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1;
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005236
5237 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5238 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5239
5240 sp<FakeWindowHandle> slipperyExitWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005241 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005242 slipperyExitWindow->setSlippery(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005243 // Make sure this one overlaps the bottom window
5244 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
5245 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
5246 // one. Windows with the same owner are not considered to be occluding each other.
5247 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
5248
5249 sp<FakeWindowHandle> slipperyEnterWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005250 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005251 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
5252
5253 mDispatcher->setInputWindows(
5254 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5255
5256 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
Prabir Pradhan678438e2023-04-13 19:32:51 +00005257 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5258 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5259 {{50, 50}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005260 slipperyExitWindow->consumeMotionDown();
5261 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
5262 mDispatcher->setInputWindows(
5263 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5264
Prabir Pradhan678438e2023-04-13 19:32:51 +00005265 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE,
5266 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5267 {{51, 51}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005268
5269 slipperyExitWindow->consumeMotionCancel();
5270
5271 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
5272 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
5273}
5274
Garfield Tan1c7bc862020-01-28 13:24:04 -08005275class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
5276protected:
5277 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
5278 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
5279
Chris Yea209fde2020-07-22 13:54:51 -07005280 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005281 sp<FakeWindowHandle> mWindow;
5282
5283 virtual void SetUp() override {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005284 mFakePolicy = sp<FakeInputDispatcherPolicy>::make();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005285 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07005286 mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy);
Prabir Pradhan87112a72023-04-20 19:13:39 +00005287 mDispatcher->requestRefreshConfiguration();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005288 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
5289 ASSERT_EQ(OK, mDispatcher->start());
5290
5291 setUpWindow();
5292 }
5293
5294 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07005295 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005296 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005297
Vishnu Nair47074b82020-08-14 11:54:47 -07005298 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005299 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005300 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005301 mWindow->consumeFocusEvent(true);
5302 }
5303
Chris Ye2ad95392020-09-01 13:44:44 -07005304 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005305 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005306 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005307 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005308 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005309
5310 // Window should receive key down event.
5311 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5312 }
5313
5314 void expectKeyRepeatOnce(int32_t repeatCount) {
5315 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
5316 InputEvent* repeatEvent = mWindow->consume();
5317 ASSERT_NE(nullptr, repeatEvent);
5318
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005319 ASSERT_EQ(InputEventType::KEY, repeatEvent->getType());
Garfield Tan1c7bc862020-01-28 13:24:04 -08005320
5321 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
5322 uint32_t eventAction = repeatKeyEvent->getAction();
5323 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
5324 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
5325 }
5326
Chris Ye2ad95392020-09-01 13:44:44 -07005327 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005328 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005329 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005330 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005331 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005332
5333 // Window should receive key down event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005334 mWindow->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005335 /*expectedFlags=*/0);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005336 }
5337};
5338
5339TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Harry Cutts33476232023-01-30 19:57:29 +00005340 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005341 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5342 expectKeyRepeatOnce(repeatCount);
5343 }
5344}
5345
5346TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
Harry Cutts33476232023-01-30 19:57:29 +00005347 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005348 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5349 expectKeyRepeatOnce(repeatCount);
5350 }
Harry Cutts33476232023-01-30 19:57:29 +00005351 sendAndConsumeKeyDown(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005352 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08005353 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5354 expectKeyRepeatOnce(repeatCount);
5355 }
5356}
5357
5358TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005359 sendAndConsumeKeyDown(/*deviceId=*/1);
5360 expectKeyRepeatOnce(/*repeatCount=*/1);
5361 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005362 mWindow->assertNoEvents();
5363}
5364
5365TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005366 sendAndConsumeKeyDown(/*deviceId=*/1);
5367 expectKeyRepeatOnce(/*repeatCount=*/1);
5368 sendAndConsumeKeyDown(/*deviceId=*/2);
5369 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005370 // Stale key up from device 1.
Harry Cutts33476232023-01-30 19:57:29 +00005371 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005372 // Device 2 is still down, keep repeating
Harry Cutts33476232023-01-30 19:57:29 +00005373 expectKeyRepeatOnce(/*repeatCount=*/2);
5374 expectKeyRepeatOnce(/*repeatCount=*/3);
Chris Ye2ad95392020-09-01 13:44:44 -07005375 // Device 2 key up
Harry Cutts33476232023-01-30 19:57:29 +00005376 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005377 mWindow->assertNoEvents();
5378}
5379
5380TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005381 sendAndConsumeKeyDown(/*deviceId=*/1);
5382 expectKeyRepeatOnce(/*repeatCount=*/1);
5383 sendAndConsumeKeyDown(/*deviceId=*/2);
5384 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005385 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
Harry Cutts33476232023-01-30 19:57:29 +00005386 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005387 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08005388 mWindow->assertNoEvents();
5389}
5390
liushenxiang42232912021-05-21 20:24:09 +08005391TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
5392 sendAndConsumeKeyDown(DEVICE_ID);
Harry Cutts33476232023-01-30 19:57:29 +00005393 expectKeyRepeatOnce(/*repeatCount=*/1);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005394 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
liushenxiang42232912021-05-21 20:24:09 +08005395 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
5396 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
5397 mWindow->assertNoEvents();
5398}
5399
Garfield Tan1c7bc862020-01-28 13:24:04 -08005400TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005401 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005402 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005403 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5404 InputEvent* repeatEvent = mWindow->consume();
5405 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5406 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
5407 IdGenerator::getSource(repeatEvent->getId()));
5408 }
5409}
5410
5411TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005412 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005413 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005414
5415 std::unordered_set<int32_t> idSet;
5416 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5417 InputEvent* repeatEvent = mWindow->consume();
5418 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5419 int32_t id = repeatEvent->getId();
5420 EXPECT_EQ(idSet.end(), idSet.find(id));
5421 idSet.insert(id);
5422 }
5423}
5424
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005425/* Test InputDispatcher for MultiDisplay */
5426class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
5427public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005428 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005429 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08005430
Chris Yea209fde2020-07-22 13:54:51 -07005431 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005432 windowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005433 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005434
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005435 // Set focus window for primary display, but focused display would be second one.
5436 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07005437 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005438 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005439 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005440 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08005441
Chris Yea209fde2020-07-22 13:54:51 -07005442 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005443 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005444 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005445 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005446 // Set focus display to second one.
5447 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
5448 // Set focus window for second display.
5449 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07005450 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005451 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005452 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005453 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005454 }
5455
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005456 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005457 InputDispatcherTest::TearDown();
5458
Chris Yea209fde2020-07-22 13:54:51 -07005459 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005460 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07005461 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005462 windowInSecondary.clear();
5463 }
5464
5465protected:
Chris Yea209fde2020-07-22 13:54:51 -07005466 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005467 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07005468 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005469 sp<FakeWindowHandle> windowInSecondary;
5470};
5471
5472TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
5473 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005474 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5475 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5476 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005477 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08005478 windowInSecondary->assertNoEvents();
5479
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005480 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005481 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5482 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5483 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005484 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005485 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08005486}
5487
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005488TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08005489 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005490 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5491 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005492 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005493 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08005494 windowInSecondary->assertNoEvents();
5495
5496 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005497 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005498 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005499 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005500 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08005501
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005502 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00005503 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08005504
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005505 // Old focus should receive a cancel event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005506 windowInSecondary->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005507 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08005508
5509 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005510 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005511 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08005512 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005513 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08005514 windowInSecondary->assertNoEvents();
5515}
5516
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005517// Test per-display input monitors for motion event.
5518TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08005519 FakeMonitorReceiver monitorInPrimary =
5520 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5521 FakeMonitorReceiver monitorInSecondary =
5522 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005523
5524 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005525 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5526 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5527 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005528 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08005529 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005530 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005531 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005532
5533 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005534 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5535 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5536 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005537 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005538 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005539 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08005540 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005541
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08005542 // Lift up the touch from the second display
5543 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5544 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5545 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5546 windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID);
5547 monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID);
5548
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005549 // Test inject a non-pointer motion event.
5550 // If specific a display, it will dispatch to the focused window of particular display,
5551 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005552 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5553 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
5554 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005555 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005556 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005557 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08005558 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005559}
5560
5561// Test per-display input monitors for key event.
5562TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005563 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08005564 FakeMonitorReceiver monitorInPrimary =
5565 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5566 FakeMonitorReceiver monitorInSecondary =
5567 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005568
5569 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005570 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5571 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005572 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005573 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005574 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08005575 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005576}
5577
Vishnu Nair958da932020-08-21 17:12:37 -07005578TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
5579 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005580 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005581 secondWindowInPrimary->setFocusable(true);
5582 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
5583 setFocusedWindow(secondWindowInPrimary);
5584 windowInPrimary->consumeFocusEvent(false);
5585 secondWindowInPrimary->consumeFocusEvent(true);
5586
5587 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005588 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
5589 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005590 windowInPrimary->assertNoEvents();
5591 windowInSecondary->assertNoEvents();
5592 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5593}
5594
Arthur Hungdfd528e2021-12-08 13:23:04 +00005595TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
5596 FakeMonitorReceiver monitorInPrimary =
5597 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5598 FakeMonitorReceiver monitorInSecondary =
5599 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
5600
5601 // Test touch down on primary display.
5602 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5603 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5604 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5605 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5606 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
5607
5608 // Test touch down on second display.
5609 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5610 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5611 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5612 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
5613 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
5614
5615 // Trigger cancel touch.
5616 mDispatcher->cancelCurrentTouch();
5617 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5618 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5619 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
5620 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
5621
5622 // Test inject a move motion event, no window/monitor should receive the event.
5623 ASSERT_EQ(InputEventInjectionResult::FAILED,
5624 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5625 ADISPLAY_ID_DEFAULT, {110, 200}))
5626 << "Inject motion event should return InputEventInjectionResult::FAILED";
5627 windowInPrimary->assertNoEvents();
5628 monitorInPrimary.assertNoEvents();
5629
5630 ASSERT_EQ(InputEventInjectionResult::FAILED,
5631 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5632 SECOND_DISPLAY_ID, {110, 200}))
5633 << "Inject motion event should return InputEventInjectionResult::FAILED";
5634 windowInSecondary->assertNoEvents();
5635 monitorInSecondary.assertNoEvents();
5636}
5637
Jackal Guof9696682018-10-05 12:23:23 +08005638class InputFilterTest : public InputDispatcherTest {
5639protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005640 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
5641 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08005642 NotifyMotionArgs motionArgs;
5643
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005644 motionArgs =
5645 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005646 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005647 motionArgs =
5648 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005649 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005650 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08005651 if (expectToBeFiltered) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005652 const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue());
5653 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08005654 } else {
5655 mFakePolicy->assertFilterInputEventWasNotCalled();
5656 }
5657 }
5658
5659 void testNotifyKey(bool expectToBeFiltered) {
5660 NotifyKeyArgs keyArgs;
5661
5662 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005663 mDispatcher->notifyKey(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08005664 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005665 mDispatcher->notifyKey(keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005666 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08005667
5668 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08005669 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08005670 } else {
5671 mFakePolicy->assertFilterInputEventWasNotCalled();
5672 }
5673 }
5674};
5675
5676// Test InputFilter for MotionEvent
5677TEST_F(InputFilterTest, MotionEvent_InputFilter) {
5678 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
5679 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
5680 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
5681
5682 // Enable InputFilter
5683 mDispatcher->setInputFilterEnabled(true);
5684 // Test touch on both primary and second display, and check if both events are filtered.
5685 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
5686 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
5687
5688 // Disable InputFilter
5689 mDispatcher->setInputFilterEnabled(false);
5690 // Test touch on both primary and second display, and check if both events aren't filtered.
5691 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
5692 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
5693}
5694
5695// Test InputFilter for KeyEvent
5696TEST_F(InputFilterTest, KeyEvent_InputFilter) {
5697 // Since the InputFilter is disabled by default, check if key event aren't filtered.
5698 testNotifyKey(/*expectToBeFiltered*/ false);
5699
5700 // Enable InputFilter
5701 mDispatcher->setInputFilterEnabled(true);
5702 // Send a key event, and check if it is filtered.
5703 testNotifyKey(/*expectToBeFiltered*/ true);
5704
5705 // Disable InputFilter
5706 mDispatcher->setInputFilterEnabled(false);
5707 // Send a key event, and check if it isn't filtered.
5708 testNotifyKey(/*expectToBeFiltered*/ false);
5709}
5710
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005711// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
5712// logical display coordinate space.
5713TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
5714 ui::Transform firstDisplayTransform;
5715 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
5716 ui::Transform secondDisplayTransform;
5717 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
5718
5719 std::vector<gui::DisplayInfo> displayInfos(2);
5720 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
5721 displayInfos[0].transform = firstDisplayTransform;
5722 displayInfos[1].displayId = SECOND_DISPLAY_ID;
5723 displayInfos[1].transform = secondDisplayTransform;
5724
Patrick Williamsd828f302023-04-28 17:52:08 -05005725 mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0});
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005726
5727 // Enable InputFilter
5728 mDispatcher->setInputFilterEnabled(true);
5729
5730 // Ensure the correct transforms are used for the displays.
5731 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform);
5732 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform);
5733}
5734
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005735class InputFilterInjectionPolicyTest : public InputDispatcherTest {
5736protected:
5737 virtual void SetUp() override {
5738 InputDispatcherTest::SetUp();
5739
5740 /**
5741 * We don't need to enable input filter to test the injected event policy, but we enabled it
5742 * here to make the tests more realistic, since this policy only matters when inputfilter is
5743 * on.
5744 */
5745 mDispatcher->setInputFilterEnabled(true);
5746
5747 std::shared_ptr<InputApplicationHandle> application =
5748 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005749 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
5750 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005751
5752 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5753 mWindow->setFocusable(true);
5754 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5755 setFocusedWindow(mWindow);
5756 mWindow->consumeFocusEvent(true);
5757 }
5758
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005759 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
5760 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005761 KeyEvent event;
5762
5763 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5764 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
5765 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
Harry Cutts33476232023-01-30 19:57:29 +00005766 KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005767 const int32_t additionalPolicyFlags =
5768 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
5769 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005770 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005771 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
5772 policyFlags | additionalPolicyFlags));
5773
5774 InputEvent* received = mWindow->consume();
5775 ASSERT_NE(nullptr, received);
5776 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005777 ASSERT_EQ(received->getType(), InputEventType::KEY);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005778 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
5779 ASSERT_EQ(flags, keyEvent.getFlags());
5780 }
5781
5782 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
5783 int32_t flags) {
5784 MotionEvent event;
5785 PointerProperties pointerProperties[1];
5786 PointerCoords pointerCoords[1];
5787 pointerProperties[0].clear();
5788 pointerProperties[0].id = 0;
5789 pointerCoords[0].clear();
5790 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
5791 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
5792
5793 ui::Transform identityTransform;
5794 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5795 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
5796 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
5797 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
5798 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07005799 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07005800 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005801 /*pointerCount*/ 1, pointerProperties, pointerCoords);
5802
5803 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
5804 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005805 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005806 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
5807 policyFlags | additionalPolicyFlags));
5808
5809 InputEvent* received = mWindow->consume();
5810 ASSERT_NE(nullptr, received);
5811 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005812 ASSERT_EQ(received->getType(), InputEventType::MOTION);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005813 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
5814 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005815 }
5816
5817private:
5818 sp<FakeWindowHandle> mWindow;
5819};
5820
5821TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005822 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
5823 // filter. Without it, the event will no different from a regularly injected event, and the
5824 // injected device id will be overwritten.
Harry Cutts33476232023-01-30 19:57:29 +00005825 testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
5826 /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005827}
5828
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005829TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005830 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00005831 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005832 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
5833}
5834
5835TEST_F(InputFilterInjectionPolicyTest,
5836 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
5837 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00005838 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005839 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005840}
5841
5842TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
Harry Cutts33476232023-01-30 19:57:29 +00005843 testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3,
5844 /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005845}
5846
chaviwfd6d3512019-03-25 13:23:49 -07005847class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005848 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07005849 InputDispatcherTest::SetUp();
5850
Chris Yea209fde2020-07-22 13:54:51 -07005851 std::shared_ptr<FakeApplicationHandle> application =
5852 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005853 mUnfocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005854 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07005855 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07005856
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005857 mFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005858 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005859 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07005860
5861 // Set focused application.
5862 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005863 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07005864
5865 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00005866 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005867 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005868 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07005869 }
5870
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005871 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07005872 InputDispatcherTest::TearDown();
5873
5874 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005875 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07005876 }
5877
5878protected:
5879 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005880 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07005881 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07005882};
5883
5884// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
5885// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
5886// the onPointerDownOutsideFocus callback.
5887TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005888 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07005889 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5890 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005891 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07005892 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07005893
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005894 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07005895 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
5896}
5897
5898// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
5899// DOWN on the window that doesn't have focus. Ensure no window received the
5900// onPointerDownOutsideFocus callback.
5901TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005902 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07005903 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005904 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07005905 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07005906
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005907 ASSERT_TRUE(mDispatcher->waitForIdle());
5908 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07005909}
5910
5911// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
5912// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
5913TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005914 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5915 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005916 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07005917 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07005918
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005919 ASSERT_TRUE(mDispatcher->waitForIdle());
5920 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07005921}
5922
5923// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
5924// DOWN on the window that already has focus. Ensure no window received the
5925// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005926TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005927 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005928 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07005929 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005930 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07005931 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07005932
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005933 ASSERT_TRUE(mDispatcher->waitForIdle());
5934 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07005935}
5936
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08005937// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
5938// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
5939TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
5940 const MotionEvent event =
5941 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
5942 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005943 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08005944 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
5945 .build();
5946 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
5947 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5948 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
5949
5950 ASSERT_TRUE(mDispatcher->waitForIdle());
5951 mFakePolicy->assertOnPointerDownWasNotCalled();
5952 // Ensure that the unfocused window did not receive any FOCUS events.
5953 mUnfocusedWindow->assertNoEvents();
5954}
5955
chaviwaf87b3e2019-10-01 16:59:28 -07005956// These tests ensures we can send touch events to a single client when there are multiple input
5957// windows that point to the same client token.
5958class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
5959 virtual void SetUp() override {
5960 InputDispatcherTest::SetUp();
5961
Chris Yea209fde2020-07-22 13:54:51 -07005962 std::shared_ptr<FakeApplicationHandle> application =
5963 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005964 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
5965 ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07005966 mWindow1->setFrame(Rect(0, 0, 100, 100));
5967
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005968 mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2",
5969 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviwaf87b3e2019-10-01 16:59:28 -07005970 mWindow2->setFrame(Rect(100, 100, 200, 200));
5971
Arthur Hung72d8dc32020-03-28 00:48:39 +00005972 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07005973 }
5974
5975protected:
5976 sp<FakeWindowHandle> mWindow1;
5977 sp<FakeWindowHandle> mWindow2;
5978
5979 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05005980 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07005981 vec2 vals = windowInfo->transform.transform(point.x, point.y);
5982 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07005983 }
5984
5985 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
5986 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005987 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07005988 InputEvent* event = window->consume();
5989
5990 ASSERT_NE(nullptr, event) << name.c_str()
5991 << ": consumer should have returned non-NULL event.";
5992
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005993 ASSERT_EQ(InputEventType::MOTION, event->getType())
5994 << name.c_str() << ": expected MotionEvent, got " << *event;
chaviwaf87b3e2019-10-01 16:59:28 -07005995
5996 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005997 assertMotionAction(expectedAction, motionEvent.getAction());
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08005998 ASSERT_EQ(points.size(), motionEvent.getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07005999
6000 for (size_t i = 0; i < points.size(); i++) {
6001 float expectedX = points[i].x;
6002 float expectedY = points[i].y;
6003
6004 EXPECT_EQ(expectedX, motionEvent.getX(i))
6005 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
6006 << ", got " << motionEvent.getX(i);
6007 EXPECT_EQ(expectedY, motionEvent.getY(i))
6008 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
6009 << ", got " << motionEvent.getY(i);
6010 }
6011 }
chaviw9eaa22c2020-07-01 16:21:27 -07006012
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08006013 void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07006014 std::vector<PointF> expectedPoints) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00006015 mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
6016 ADISPLAY_ID_DEFAULT, touchedPoints));
chaviw9eaa22c2020-07-01 16:21:27 -07006017
6018 // Always consume from window1 since it's the window that has the InputReceiver
6019 consumeMotionEvent(mWindow1, action, expectedPoints);
6020 }
chaviwaf87b3e2019-10-01 16:59:28 -07006021};
6022
6023TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
6024 // Touch Window 1
6025 PointF touchedPoint = {10, 10};
6026 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006027 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006028
6029 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006030 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006031
6032 // Touch Window 2
6033 touchedPoint = {150, 150};
6034 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006035 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006036}
6037
chaviw9eaa22c2020-07-01 16:21:27 -07006038TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
6039 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07006040 mWindow2->setWindowScale(0.5f, 0.5f);
6041
6042 // Touch Window 1
6043 PointF touchedPoint = {10, 10};
6044 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006045 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006046 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006047 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006048
6049 // Touch Window 2
6050 touchedPoint = {150, 150};
6051 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006052 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
6053 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006054
chaviw9eaa22c2020-07-01 16:21:27 -07006055 // Update the transform so rotation is set
6056 mWindow2->setWindowTransform(0, -1, 1, 0);
6057 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
6058 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006059}
6060
chaviw9eaa22c2020-07-01 16:21:27 -07006061TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006062 mWindow2->setWindowScale(0.5f, 0.5f);
6063
6064 // Touch Window 1
6065 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6066 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006067 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006068
6069 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006070 touchedPoints.push_back(PointF{150, 150});
6071 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006072 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006073
chaviw9eaa22c2020-07-01 16:21:27 -07006074 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006075 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006076 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006077
chaviw9eaa22c2020-07-01 16:21:27 -07006078 // Update the transform so rotation is set for Window 2
6079 mWindow2->setWindowTransform(0, -1, 1, 0);
6080 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006081 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006082}
6083
chaviw9eaa22c2020-07-01 16:21:27 -07006084TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006085 mWindow2->setWindowScale(0.5f, 0.5f);
6086
6087 // Touch Window 1
6088 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6089 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006090 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006091
6092 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006093 touchedPoints.push_back(PointF{150, 150});
6094 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006095
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006096 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006097
6098 // Move both windows
6099 touchedPoints = {{20, 20}, {175, 175}};
6100 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6101 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6102
chaviw9eaa22c2020-07-01 16:21:27 -07006103 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006104
chaviw9eaa22c2020-07-01 16:21:27 -07006105 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006106 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006107 expectedPoints.pop_back();
6108
6109 // Touch Window 2
6110 mWindow2->setWindowTransform(0, -1, 1, 0);
6111 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006112 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006113
6114 // Move both windows
6115 touchedPoints = {{20, 20}, {175, 175}};
6116 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6117 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6118
6119 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006120}
6121
6122TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
6123 mWindow1->setWindowScale(0.5f, 0.5f);
6124
6125 // Touch Window 1
6126 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6127 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006128 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006129
6130 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006131 touchedPoints.push_back(PointF{150, 150});
6132 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006133
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006134 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006135
6136 // Move both windows
6137 touchedPoints = {{20, 20}, {175, 175}};
6138 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6139 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6140
chaviw9eaa22c2020-07-01 16:21:27 -07006141 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006142}
6143
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07006144/**
6145 * When one of the windows is slippery, the touch should not slip into the other window with the
6146 * same input channel.
6147 */
6148TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) {
6149 mWindow1->setSlippery(true);
6150 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
6151
6152 // Touch down in window 1
6153 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6154 ADISPLAY_ID_DEFAULT, {{50, 50}}));
6155 consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}});
6156
6157 // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip.
6158 // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN
6159 // getting generated.
6160 mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6161 ADISPLAY_ID_DEFAULT, {{150, 150}}));
6162
6163 consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}});
6164}
6165
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006166class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
6167 virtual void SetUp() override {
6168 InputDispatcherTest::SetUp();
6169
Chris Yea209fde2020-07-22 13:54:51 -07006170 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006171 mApplication->setDispatchingTimeout(20ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006172 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
6173 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006174 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05006175 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07006176 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006177
6178 // Set focused application.
6179 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
6180
6181 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006182 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006183 mWindow->consumeFocusEvent(true);
6184 }
6185
6186 virtual void TearDown() override {
6187 InputDispatcherTest::TearDown();
6188 mWindow.clear();
6189 }
6190
6191protected:
Chris Yea209fde2020-07-22 13:54:51 -07006192 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006193 sp<FakeWindowHandle> mWindow;
6194 static constexpr PointF WINDOW_LOCATION = {20, 20};
6195
6196 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006197 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006198 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6199 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006200 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006201 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6202 WINDOW_LOCATION));
6203 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006204
6205 sp<FakeWindowHandle> addSpyWindow() {
6206 sp<FakeWindowHandle> spy =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006207 sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006208 spy->setTrustedOverlay(true);
6209 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006210 spy->setSpy(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006211 spy->setDispatchingTimeout(30ms);
6212 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}});
6213 return spy;
6214 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006215};
6216
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006217// Send a tap and respond, which should not cause an ANR.
6218TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
6219 tapOnWindow();
6220 mWindow->consumeMotionDown();
6221 mWindow->consumeMotionUp();
6222 ASSERT_TRUE(mDispatcher->waitForIdle());
6223 mFakePolicy->assertNotifyAnrWasNotCalled();
6224}
6225
6226// Send a regular key and respond, which should not cause an ANR.
6227TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006228 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006229 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
6230 ASSERT_TRUE(mDispatcher->waitForIdle());
6231 mFakePolicy->assertNotifyAnrWasNotCalled();
6232}
6233
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006234TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
6235 mWindow->setFocusable(false);
6236 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6237 mWindow->consumeFocusEvent(false);
6238
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006239 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006240 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6241 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
6242 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006243 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006244 // Key will not go to window because we have no focused window.
6245 // The 'no focused window' ANR timer should start instead.
6246
6247 // Now, the focused application goes away.
6248 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
6249 // The key should get dropped and there should be no ANR.
6250
6251 ASSERT_TRUE(mDispatcher->waitForIdle());
6252 mFakePolicy->assertNotifyAnrWasNotCalled();
6253}
6254
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006255// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006256// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
6257// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006258TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006259 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006260 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6261 WINDOW_LOCATION));
6262
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006263 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
6264 ASSERT_TRUE(sequenceNum);
6265 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006266 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006267
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006268 mWindow->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006269 mWindow->consumeMotionEvent(
6270 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006271 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006272 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006273}
6274
6275// Send a key to the app and have the app not respond right away.
6276TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
6277 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006278 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006279 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
6280 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006281 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006282 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006283 ASSERT_TRUE(mDispatcher->waitForIdle());
6284}
6285
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006286// We have a focused application, but no focused window
6287TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006288 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006289 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6290 mWindow->consumeFocusEvent(false);
6291
6292 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006293 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006294 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6295 WINDOW_LOCATION));
6296 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
6297 mDispatcher->waitForIdle();
6298 mFakePolicy->assertNotifyAnrWasNotCalled();
6299
6300 // Once a focused event arrives, we get an ANR for this application
6301 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6302 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006303 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006304 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6305 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006306 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006307 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07006308 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006309 ASSERT_TRUE(mDispatcher->waitForIdle());
6310}
6311
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006312/**
6313 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
6314 * there will not be an ANR.
6315 */
6316TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
6317 mWindow->setFocusable(false);
6318 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6319 mWindow->consumeFocusEvent(false);
6320
6321 KeyEvent event;
6322 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
6323 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
6324
6325 // Define a valid key down event that is stale (too old).
6326 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
6327 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A,
Harry Cutts33476232023-01-30 19:57:29 +00006328 AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006329
6330 const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
6331
6332 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006333 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006334 InputEventInjectionSync::WAIT_FOR_RESULT,
6335 INJECT_EVENT_TIMEOUT, policyFlags);
6336 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
6337 << "Injection should fail because the event is stale";
6338
6339 ASSERT_TRUE(mDispatcher->waitForIdle());
6340 mFakePolicy->assertNotifyAnrWasNotCalled();
6341 mWindow->assertNoEvents();
6342}
6343
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006344// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006345// Make sure that we don't notify policy twice about the same ANR.
6346TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006347 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006348 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6349 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006350
6351 // Once a focused event arrives, we get an ANR for this application
6352 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6353 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006354 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006355 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6356 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006357 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Vishnu Naire4df8752022-09-08 09:17:55 -07006358 const std::chrono::duration appTimeout =
6359 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6360 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006361
Vishnu Naire4df8752022-09-08 09:17:55 -07006362 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006363 // ANR should not be raised again. It is up to policy to do that if it desires.
6364 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006365
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006366 // If we now get a focused window, the ANR should stop, but the policy handles that via
6367 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006368 ASSERT_TRUE(mDispatcher->waitForIdle());
6369}
6370
6371// We have a focused application, but no focused window
6372TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006373 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006374 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6375 mWindow->consumeFocusEvent(false);
6376
6377 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006378 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006379 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006380 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
6381 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006382
Vishnu Naire4df8752022-09-08 09:17:55 -07006383 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6384 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006385
6386 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006387 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006388 ASSERT_TRUE(mDispatcher->waitForIdle());
6389 mWindow->assertNoEvents();
6390}
6391
6392/**
6393 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
6394 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
6395 * If we process 1 of the events, but ANR on the second event with the same timestamp,
6396 * the ANR mechanism should still work.
6397 *
6398 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
6399 * DOWN event, while not responding on the second one.
6400 */
6401TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
6402 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
6403 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6404 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6405 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6406 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006407 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006408
6409 // Now send ACTION_UP, with identical timestamp
6410 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
6411 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6412 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6413 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006414 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006415
6416 // We have now sent down and up. Let's consume first event and then ANR on the second.
6417 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6418 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006419 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006420}
6421
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006422// A spy window can receive an ANR
6423TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
6424 sp<FakeWindowHandle> spy = addSpyWindow();
6425
6426 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6427 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6428 WINDOW_LOCATION));
6429 mWindow->consumeMotionDown();
6430
6431 std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN
6432 ASSERT_TRUE(sequenceNum);
6433 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006434 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006435
6436 spy->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006437 spy->consumeMotionEvent(
6438 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006439 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006440 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006441}
6442
6443// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006444// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006445TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
6446 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006447
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006448 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6449 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006450 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006451 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006452
6453 // Stuck on the ACTION_UP
6454 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006455 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006456
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006457 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006458 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006459 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6460 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006461
6462 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6463 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006464 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006465 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006466 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006467}
6468
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006469// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006470// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006471TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
6472 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006473
6474 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006475 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6476 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006477
6478 mWindow->consumeMotionDown();
6479 // Stuck on the ACTION_UP
6480 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006481 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006482
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006483 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006484 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006485 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6486 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006487
6488 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6489 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006490 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006491 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006492 spy->assertNoEvents();
6493}
6494
6495TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
6496 mDispatcher->setMonitorDispatchingTimeoutForTest(30ms);
6497
6498 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6499
6500 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6501 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6502 WINDOW_LOCATION));
6503
6504 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6505 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
6506 ASSERT_TRUE(consumeSeq);
6507
Prabir Pradhanedd96402022-02-15 01:46:16 -08006508 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006509
6510 monitor.finishEvent(*consumeSeq);
6511 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6512
6513 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006514 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006515}
6516
6517// If a window is unresponsive, then you get anr. if the window later catches up and starts to
6518// process events, you don't get an anr. When the window later becomes unresponsive again, you
6519// get an ANR again.
6520// 1. tap -> block on ACTION_UP -> receive ANR
6521// 2. consume all pending events (= queue becomes healthy again)
6522// 3. tap again -> block on ACTION_UP again -> receive ANR second time
6523TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
6524 tapOnWindow();
6525
6526 mWindow->consumeMotionDown();
6527 // Block on ACTION_UP
6528 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006529 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006530 mWindow->consumeMotionUp(); // Now the connection should be healthy again
6531 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006532 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006533 mWindow->assertNoEvents();
6534
6535 tapOnWindow();
6536 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006537 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006538 mWindow->consumeMotionUp();
6539
6540 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006541 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006542 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006543 mWindow->assertNoEvents();
6544}
6545
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006546// If a connection remains unresponsive for a while, make sure policy is only notified once about
6547// it.
6548TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006549 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006550 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6551 WINDOW_LOCATION));
6552
6553 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006554 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006555 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006556 // 'notifyConnectionUnresponsive' should only be called once per connection
6557 mFakePolicy->assertNotifyAnrWasNotCalled();
6558 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006559 mWindow->consumeMotionDown();
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006560 mWindow->consumeMotionEvent(
6561 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006562 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006563 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006564 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006565 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006566}
6567
6568/**
6569 * If a window is processing a motion event, and then a key event comes in, the key event should
6570 * not to to the focused window until the motion is processed.
6571 *
6572 * Warning!!!
6573 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
6574 * and the injection timeout that we specify when injecting the key.
6575 * We must have the injection timeout (10ms) be smaller than
6576 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
6577 *
6578 * If that value changes, this test should also change.
6579 */
6580TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
6581 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
6582 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6583
6584 tapOnWindow();
6585 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
6586 ASSERT_TRUE(downSequenceNum);
6587 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
6588 ASSERT_TRUE(upSequenceNum);
6589 // Don't finish the events yet, and send a key
6590 // Injection will "succeed" because we will eventually give up and send the key to the focused
6591 // window even if motions are still being processed. But because the injection timeout is short,
6592 // we will receive INJECTION_TIMED_OUT as the result.
6593
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006594 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006595 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006596 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
6597 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006598 // Key will not be sent to the window, yet, because the window is still processing events
6599 // and the key remains pending, waiting for the touch events to be processed
6600 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
6601 ASSERT_FALSE(keySequenceNum);
6602
6603 std::this_thread::sleep_for(500ms);
6604 // if we wait long enough though, dispatcher will give up, and still send the key
6605 // to the focused window, even though we have not yet finished the motion event
6606 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6607 mWindow->finishEvent(*downSequenceNum);
6608 mWindow->finishEvent(*upSequenceNum);
6609}
6610
6611/**
6612 * If a window is processing a motion event, and then a key event comes in, the key event should
6613 * not go to the focused window until the motion is processed.
6614 * If then a new motion comes in, then the pending key event should be going to the currently
6615 * focused window right away.
6616 */
6617TEST_F(InputDispatcherSingleWindowAnr,
6618 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
6619 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
6620 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6621
6622 tapOnWindow();
6623 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
6624 ASSERT_TRUE(downSequenceNum);
6625 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
6626 ASSERT_TRUE(upSequenceNum);
6627 // Don't finish the events yet, and send a key
6628 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006629 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006630 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6631 InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006632 // At this point, key is still pending, and should not be sent to the application yet.
6633 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
6634 ASSERT_FALSE(keySequenceNum);
6635
6636 // Now tap down again. It should cause the pending key to go to the focused window right away.
6637 tapOnWindow();
6638 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
6639 // the other events yet. We can finish events in any order.
6640 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
6641 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
6642 mWindow->consumeMotionDown();
6643 mWindow->consumeMotionUp();
6644 mWindow->assertNoEvents();
6645}
6646
6647class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
6648 virtual void SetUp() override {
6649 InputDispatcherTest::SetUp();
6650
Chris Yea209fde2020-07-22 13:54:51 -07006651 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006652 mApplication->setDispatchingTimeout(10ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006653 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
6654 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006655 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006656 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006657 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006658
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006659 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
6660 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05006661 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006662 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006663
6664 // Set focused application.
6665 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07006666 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006667
6668 // Expect one focus window exist in display.
6669 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006670 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006671 mFocusedWindow->consumeFocusEvent(true);
6672 }
6673
6674 virtual void TearDown() override {
6675 InputDispatcherTest::TearDown();
6676
6677 mUnfocusedWindow.clear();
6678 mFocusedWindow.clear();
6679 }
6680
6681protected:
Chris Yea209fde2020-07-22 13:54:51 -07006682 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006683 sp<FakeWindowHandle> mUnfocusedWindow;
6684 sp<FakeWindowHandle> mFocusedWindow;
6685 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
6686 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
6687 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
6688
6689 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
6690
6691 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
6692
6693private:
6694 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006695 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006696 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6697 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006698 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006699 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6700 location));
6701 }
6702};
6703
6704// If we have 2 windows that are both unresponsive, the one with the shortest timeout
6705// should be ANR'd first.
6706TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006707 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006708 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6709 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006710 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006711 mFocusedWindow->consumeMotionDown();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006712 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00006713 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006714 // We consumed all events, so no ANR
6715 ASSERT_TRUE(mDispatcher->waitForIdle());
6716 mFakePolicy->assertNotifyAnrWasNotCalled();
6717
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006718 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006719 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6720 FOCUSED_WINDOW_LOCATION));
6721 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
6722 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006723
6724 const std::chrono::duration timeout =
6725 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006726 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006727 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
6728 // sequence to make it consistent
6729 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006730 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006731 mFocusedWindow->consumeMotionDown();
6732 // This cancel is generated because the connection was unresponsive
6733 mFocusedWindow->consumeMotionCancel();
6734 mFocusedWindow->assertNoEvents();
6735 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006736 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006737 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
6738 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006739 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006740}
6741
6742// If we have 2 windows with identical timeouts that are both unresponsive,
6743// it doesn't matter which order they should have ANR.
6744// But we should receive ANR for both.
6745TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
6746 // Set the timeout for unfocused window to match the focused window
6747 mUnfocusedWindow->setDispatchingTimeout(10ms);
6748 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
6749
6750 tapOnFocusedWindow();
6751 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Prabir Pradhanedd96402022-02-15 01:46:16 -08006752 sp<IBinder> anrConnectionToken1, anrConnectionToken2;
6753 ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms));
6754 ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006755
6756 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006757 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
6758 mFocusedWindow->getToken() == anrConnectionToken2);
6759 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
6760 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006761
6762 ASSERT_TRUE(mDispatcher->waitForIdle());
6763 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006764
6765 mFocusedWindow->consumeMotionDown();
6766 mFocusedWindow->consumeMotionUp();
6767 mUnfocusedWindow->consumeMotionOutside();
6768
Prabir Pradhanedd96402022-02-15 01:46:16 -08006769 sp<IBinder> responsiveToken1, responsiveToken2;
6770 ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken());
6771 ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006772
6773 // Both applications should be marked as responsive, in any order
6774 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
6775 mFocusedWindow->getToken() == responsiveToken2);
6776 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
6777 mUnfocusedWindow->getToken() == responsiveToken2);
6778 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006779}
6780
6781// If a window is already not responding, the second tap on the same window should be ignored.
6782// We should also log an error to account for the dropped event (not tested here).
6783// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
6784TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
6785 tapOnFocusedWindow();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006786 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00006787 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006788 // Receive the events, but don't respond
6789 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
6790 ASSERT_TRUE(downEventSequenceNum);
6791 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
6792 ASSERT_TRUE(upEventSequenceNum);
6793 const std::chrono::duration timeout =
6794 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006795 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006796
6797 // Tap once again
6798 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006799 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006800 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6801 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006802 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006803 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6804 FOCUSED_WINDOW_LOCATION));
6805 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
6806 // valid touch target
6807 mUnfocusedWindow->assertNoEvents();
6808
6809 // Consume the first tap
6810 mFocusedWindow->finishEvent(*downEventSequenceNum);
6811 mFocusedWindow->finishEvent(*upEventSequenceNum);
6812 ASSERT_TRUE(mDispatcher->waitForIdle());
6813 // The second tap did not go to the focused window
6814 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006815 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -08006816 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
6817 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006818 mFakePolicy->assertNotifyAnrWasNotCalled();
6819}
6820
6821// If you tap outside of all windows, there will not be ANR
6822TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006823 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006824 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6825 LOCATION_OUTSIDE_ALL_WINDOWS));
6826 ASSERT_TRUE(mDispatcher->waitForIdle());
6827 mFakePolicy->assertNotifyAnrWasNotCalled();
6828}
6829
6830// Since the focused window is paused, tapping on it should not produce any events
6831TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
6832 mFocusedWindow->setPaused(true);
6833 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
6834
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006835 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006836 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6837 FOCUSED_WINDOW_LOCATION));
6838
6839 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
6840 ASSERT_TRUE(mDispatcher->waitForIdle());
6841 // Should not ANR because the window is paused, and touches shouldn't go to it
6842 mFakePolicy->assertNotifyAnrWasNotCalled();
6843
6844 mFocusedWindow->assertNoEvents();
6845 mUnfocusedWindow->assertNoEvents();
6846}
6847
6848/**
6849 * If a window is processing a motion event, and then a key event comes in, the key event should
6850 * not to to the focused window until the motion is processed.
6851 * If a different window becomes focused at this time, the key should go to that window instead.
6852 *
6853 * Warning!!!
6854 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
6855 * and the injection timeout that we specify when injecting the key.
6856 * We must have the injection timeout (10ms) be smaller than
6857 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
6858 *
6859 * If that value changes, this test should also change.
6860 */
6861TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
6862 // Set a long ANR timeout to prevent it from triggering
6863 mFocusedWindow->setDispatchingTimeout(2s);
6864 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
6865
6866 tapOnUnfocusedWindow();
6867 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
6868 ASSERT_TRUE(downSequenceNum);
6869 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
6870 ASSERT_TRUE(upSequenceNum);
6871 // Don't finish the events yet, and send a key
6872 // Injection will succeed because we will eventually give up and send the key to the focused
6873 // window even if motions are still being processed.
6874
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006875 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006876 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6877 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006878 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006879 // Key will not be sent to the window, yet, because the window is still processing events
6880 // and the key remains pending, waiting for the touch events to be processed
6881 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
6882 ASSERT_FALSE(keySequenceNum);
6883
6884 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07006885 mFocusedWindow->setFocusable(false);
6886 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006887 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006888 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006889
6890 // Focus events should precede the key events
6891 mUnfocusedWindow->consumeFocusEvent(true);
6892 mFocusedWindow->consumeFocusEvent(false);
6893
6894 // Finish the tap events, which should unblock dispatcher
6895 mUnfocusedWindow->finishEvent(*downSequenceNum);
6896 mUnfocusedWindow->finishEvent(*upSequenceNum);
6897
6898 // Now that all queues are cleared and no backlog in the connections, the key event
6899 // can finally go to the newly focused "mUnfocusedWindow".
6900 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6901 mFocusedWindow->assertNoEvents();
6902 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006903 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006904}
6905
6906// When the touch stream is split across 2 windows, and one of them does not respond,
6907// then ANR should be raised and the touch should be canceled for the unresponsive window.
6908// The other window should not be affected by that.
6909TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
6910 // Touch Window 1
Prabir Pradhan678438e2023-04-13 19:32:51 +00006911 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
6912 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6913 {FOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006914 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00006915 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006916
6917 // Touch Window 2
Prabir Pradhan678438e2023-04-13 19:32:51 +00006918 mDispatcher->notifyMotion(
6919 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6920 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006921
6922 const std::chrono::duration timeout =
6923 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006924 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006925
6926 mUnfocusedWindow->consumeMotionDown();
6927 mFocusedWindow->consumeMotionDown();
6928 // Focused window may or may not receive ACTION_MOVE
6929 // But it should definitely receive ACTION_CANCEL due to the ANR
6930 InputEvent* event;
6931 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
6932 ASSERT_TRUE(moveOrCancelSequenceNum);
6933 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
6934 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006935 ASSERT_EQ(event->getType(), InputEventType::MOTION);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006936 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
6937 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
6938 mFocusedWindow->consumeMotionCancel();
6939 } else {
6940 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
6941 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006942 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006943 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
6944 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006945
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006946 mUnfocusedWindow->assertNoEvents();
6947 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006948 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006949}
6950
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05006951/**
6952 * If we have no focused window, and a key comes in, we start the ANR timer.
6953 * The focused application should add a focused window before the timer runs out to prevent ANR.
6954 *
6955 * If the user touches another application during this time, the key should be dropped.
6956 * Next, if a new focused window comes in, without toggling the focused application,
6957 * then no ANR should occur.
6958 *
6959 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
6960 * but in some cases the policy may not update the focused application.
6961 */
6962TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
6963 std::shared_ptr<FakeApplicationHandle> focusedApplication =
6964 std::make_shared<FakeApplicationHandle>();
6965 focusedApplication->setDispatchingTimeout(60ms);
6966 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
6967 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
6968 mFocusedWindow->setFocusable(false);
6969
6970 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
6971 mFocusedWindow->consumeFocusEvent(false);
6972
6973 // Send a key. The ANR timer should start because there is no focused window.
6974 // 'focusedApplication' will get blamed if this timer completes.
6975 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006976 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006977 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6978 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
6979 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006980 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05006981
6982 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
6983 // then the injected touches won't cause the focused event to get dropped.
6984 // The dispatcher only checks for whether the queue should be pruned upon queueing.
6985 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
6986 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
6987 // For this test, it means that the key would get delivered to the window once it becomes
6988 // focused.
6989 std::this_thread::sleep_for(10ms);
6990
6991 // Touch unfocused window. This should force the pending key to get dropped.
Prabir Pradhan678438e2023-04-13 19:32:51 +00006992 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
6993 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6994 {UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05006995
6996 // We do not consume the motion right away, because that would require dispatcher to first
6997 // process (== drop) the key event, and by that time, ANR will be raised.
6998 // Set the focused window first.
6999 mFocusedWindow->setFocusable(true);
7000 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7001 setFocusedWindow(mFocusedWindow);
7002 mFocusedWindow->consumeFocusEvent(true);
7003 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
7004 // to another application. This could be a bug / behaviour in the policy.
7005
7006 mUnfocusedWindow->consumeMotionDown();
7007
7008 ASSERT_TRUE(mDispatcher->waitForIdle());
7009 // Should not ANR because we actually have a focused window. It was just added too slowly.
7010 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
7011}
7012
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007013// These tests ensure we cannot send touch events to a window that's positioned behind a window
7014// that has feature NO_INPUT_CHANNEL.
7015// Layout:
7016// Top (closest to user)
7017// mNoInputWindow (above all windows)
7018// mBottomWindow
7019// Bottom (furthest from user)
7020class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
7021 virtual void SetUp() override {
7022 InputDispatcherTest::SetUp();
7023
7024 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007025 mNoInputWindow =
7026 sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7027 "Window without input channel", ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00007028 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007029 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007030 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7031 // It's perfectly valid for this window to not have an associated input channel
7032
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007033 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
7034 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007035 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
7036
7037 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7038 }
7039
7040protected:
7041 std::shared_ptr<FakeApplicationHandle> mApplication;
7042 sp<FakeWindowHandle> mNoInputWindow;
7043 sp<FakeWindowHandle> mBottomWindow;
7044};
7045
7046TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
7047 PointF touchedPoint = {10, 10};
7048
Prabir Pradhan678438e2023-04-13 19:32:51 +00007049 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7050 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7051 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007052
7053 mNoInputWindow->assertNoEvents();
7054 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
7055 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
7056 // and therefore should prevent mBottomWindow from receiving touches
7057 mBottomWindow->assertNoEvents();
7058}
7059
7060/**
7061 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
7062 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
7063 */
7064TEST_F(InputDispatcherMultiWindowOcclusionTests,
7065 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007066 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7067 "Window with input channel and NO_INPUT_CHANNEL",
7068 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007069
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007070 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007071 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7072 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7073
7074 PointF touchedPoint = {10, 10};
7075
Prabir Pradhan678438e2023-04-13 19:32:51 +00007076 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7077 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7078 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007079
7080 mNoInputWindow->assertNoEvents();
7081 mBottomWindow->assertNoEvents();
7082}
7083
Vishnu Nair958da932020-08-21 17:12:37 -07007084class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
7085protected:
7086 std::shared_ptr<FakeApplicationHandle> mApp;
7087 sp<FakeWindowHandle> mWindow;
7088 sp<FakeWindowHandle> mMirror;
7089
7090 virtual void SetUp() override {
7091 InputDispatcherTest::SetUp();
7092 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007093 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
7094 mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror",
7095 ADISPLAY_ID_DEFAULT, mWindow->getToken());
Vishnu Nair958da932020-08-21 17:12:37 -07007096 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7097 mWindow->setFocusable(true);
7098 mMirror->setFocusable(true);
7099 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7100 }
7101};
7102
7103TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
7104 // Request focus on a mirrored window
7105 setFocusedWindow(mMirror);
7106
7107 // window gets focused
7108 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007109 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7110 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007111 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
7112}
7113
7114// A focused & mirrored window remains focused only if the window and its mirror are both
7115// focusable.
7116TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
7117 setFocusedWindow(mMirror);
7118
7119 // window gets focused
7120 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007121 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7122 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007123 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007124 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7125 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007126 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7127
7128 mMirror->setFocusable(false);
7129 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7130
7131 // window loses focus since one of the windows associated with the token in not focusable
7132 mWindow->consumeFocusEvent(false);
7133
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007134 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7135 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007136 mWindow->assertNoEvents();
7137}
7138
7139// A focused & mirrored window remains focused until the window and its mirror both become
7140// invisible.
7141TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
7142 setFocusedWindow(mMirror);
7143
7144 // window gets focused
7145 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007146 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7147 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007148 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007149 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7150 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007151 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7152
7153 mMirror->setVisible(false);
7154 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7155
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007156 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7157 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007158 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007159 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7160 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007161 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7162
7163 mWindow->setVisible(false);
7164 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7165
7166 // window loses focus only after all windows associated with the token become invisible.
7167 mWindow->consumeFocusEvent(false);
7168
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007169 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7170 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007171 mWindow->assertNoEvents();
7172}
7173
7174// A focused & mirrored window remains focused until both windows are removed.
7175TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
7176 setFocusedWindow(mMirror);
7177
7178 // window gets focused
7179 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007180 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7181 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007182 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007183 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7184 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007185 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7186
7187 // single window is removed but the window token remains focused
7188 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
7189
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007190 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7191 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007192 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007193 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7194 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007195 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7196
7197 // Both windows are removed
7198 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
7199 mWindow->consumeFocusEvent(false);
7200
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007201 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7202 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007203 mWindow->assertNoEvents();
7204}
7205
7206// Focus request can be pending until one window becomes visible.
7207TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
7208 // Request focus on an invisible mirror.
7209 mWindow->setVisible(false);
7210 mMirror->setVisible(false);
7211 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7212 setFocusedWindow(mMirror);
7213
7214 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007215 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007216 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7217 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07007218
7219 mMirror->setVisible(true);
7220 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7221
7222 // window gets focused
7223 mWindow->consumeFocusEvent(true);
7224 // window gets the pending key event
7225 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7226}
Prabir Pradhan99987712020-11-10 18:43:05 -08007227
7228class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
7229protected:
7230 std::shared_ptr<FakeApplicationHandle> mApp;
7231 sp<FakeWindowHandle> mWindow;
7232 sp<FakeWindowHandle> mSecondWindow;
7233
7234 void SetUp() override {
7235 InputDispatcherTest::SetUp();
7236 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007237 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007238 mWindow->setFocusable(true);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007239 mSecondWindow =
7240 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007241 mSecondWindow->setFocusable(true);
7242
7243 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7244 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
7245
7246 setFocusedWindow(mWindow);
7247 mWindow->consumeFocusEvent(true);
7248 }
7249
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007250 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007251 mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request));
Prabir Pradhan99987712020-11-10 18:43:05 -08007252 }
7253
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007254 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
7255 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08007256 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007257 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
7258 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007259 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007260 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08007261 }
7262};
7263
7264TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
7265 // Ensure that capture cannot be obtained for unfocused windows.
7266 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
7267 mFakePolicy->assertSetPointerCaptureNotCalled();
7268 mSecondWindow->assertNoEvents();
7269
7270 // Ensure that capture can be enabled from the focus window.
7271 requestAndVerifyPointerCapture(mWindow, true);
7272
7273 // Ensure that capture cannot be disabled from a window that does not have capture.
7274 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
7275 mFakePolicy->assertSetPointerCaptureNotCalled();
7276
7277 // Ensure that capture can be disabled from the window with capture.
7278 requestAndVerifyPointerCapture(mWindow, false);
7279}
7280
7281TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007282 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007283
7284 setFocusedWindow(mSecondWindow);
7285
7286 // Ensure that the capture disabled event was sent first.
7287 mWindow->consumeCaptureEvent(false);
7288 mWindow->consumeFocusEvent(false);
7289 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007290 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007291
7292 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007293 notifyPointerCaptureChanged({});
7294 notifyPointerCaptureChanged(request);
7295 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08007296 mWindow->assertNoEvents();
7297 mSecondWindow->assertNoEvents();
7298 mFakePolicy->assertSetPointerCaptureNotCalled();
7299}
7300
7301TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007302 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007303
7304 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007305 notifyPointerCaptureChanged({});
7306 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007307
7308 // Ensure that Pointer Capture is disabled.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007309 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007310 mWindow->consumeCaptureEvent(false);
7311 mWindow->assertNoEvents();
7312}
7313
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007314TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
7315 requestAndVerifyPointerCapture(mWindow, true);
7316
7317 // The first window loses focus.
7318 setFocusedWindow(mSecondWindow);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007319 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007320 mWindow->consumeCaptureEvent(false);
7321
7322 // Request Pointer Capture from the second window before the notification from InputReader
7323 // arrives.
7324 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007325 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007326
7327 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007328 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007329
7330 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007331 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007332
7333 mSecondWindow->consumeFocusEvent(true);
7334 mSecondWindow->consumeCaptureEvent(true);
7335}
7336
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007337TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
7338 // App repeatedly enables and disables capture.
7339 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7340 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7341 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7342 mFakePolicy->assertSetPointerCaptureCalled(false);
7343 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7344 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7345
7346 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
7347 // first request is now stale, this should do nothing.
7348 notifyPointerCaptureChanged(firstRequest);
7349 mWindow->assertNoEvents();
7350
7351 // InputReader notifies that the second request was enabled.
7352 notifyPointerCaptureChanged(secondRequest);
7353 mWindow->consumeCaptureEvent(true);
7354}
7355
Prabir Pradhan7092e262022-05-03 16:51:09 +00007356TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
7357 requestAndVerifyPointerCapture(mWindow, true);
7358
7359 // App toggles pointer capture off and on.
7360 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7361 mFakePolicy->assertSetPointerCaptureCalled(false);
7362
7363 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7364 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7365
7366 // InputReader notifies that the latest "enable" request was processed, while skipping over the
7367 // preceding "disable" request.
7368 notifyPointerCaptureChanged(enableRequest);
7369
7370 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
7371 // any notifications.
7372 mWindow->assertNoEvents();
7373}
7374
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007375class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
7376protected:
7377 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00007378
7379 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
7380 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
7381
7382 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
7383 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7384
7385 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
7386 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
7387 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7388 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
7389 MAXIMUM_OBSCURING_OPACITY);
7390
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007391 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007392 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007393 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007394
7395 sp<FakeWindowHandle> mTouchWindow;
7396
7397 virtual void SetUp() override {
7398 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007399 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007400 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
7401 }
7402
7403 virtual void TearDown() override {
7404 InputDispatcherTest::TearDown();
7405 mTouchWindow.clear();
7406 }
7407
chaviw3277faf2021-05-19 16:45:23 -05007408 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode,
7409 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007410 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007411 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007412 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007413 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007414 return window;
7415 }
7416
7417 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
7418 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
7419 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007420 sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007421 // Generate an arbitrary PID based on the UID
7422 window->setOwnerInfo(1777 + (uid % 10000), uid);
7423 return window;
7424 }
7425
7426 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007427 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7428 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7429 points));
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007430 }
7431};
7432
7433TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007434 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007435 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007436 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007437
7438 touch();
7439
7440 mTouchWindow->assertNoEvents();
7441}
7442
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007443TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00007444 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
7445 const sp<FakeWindowHandle>& w =
7446 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
7447 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7448
7449 touch();
7450
7451 mTouchWindow->assertNoEvents();
7452}
7453
7454TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007455 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
7456 const sp<FakeWindowHandle>& w =
7457 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
7458 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7459
7460 touch();
7461
7462 w->assertNoEvents();
7463}
7464
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007465TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007466 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
7467 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007468
7469 touch();
7470
7471 mTouchWindow->consumeAnyMotionDown();
7472}
7473
7474TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007475 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007476 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007477 w->setFrame(Rect(0, 0, 50, 50));
7478 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007479
7480 touch({PointF{100, 100}});
7481
7482 mTouchWindow->consumeAnyMotionDown();
7483}
7484
7485TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007486 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007487 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007488 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7489
7490 touch();
7491
7492 mTouchWindow->consumeAnyMotionDown();
7493}
7494
7495TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
7496 const sp<FakeWindowHandle>& w =
7497 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
7498 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007499
7500 touch();
7501
7502 mTouchWindow->consumeAnyMotionDown();
7503}
7504
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007505TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
7506 const sp<FakeWindowHandle>& w =
7507 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
7508 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7509
7510 touch();
7511
7512 w->assertNoEvents();
7513}
7514
7515/**
7516 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
7517 * inside) while letting them pass-through. Note that even though touch passes through the occluding
7518 * window, the occluding window will still receive ACTION_OUTSIDE event.
7519 */
7520TEST_F(InputDispatcherUntrustedTouchesTest,
7521 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
7522 const sp<FakeWindowHandle>& w =
7523 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007524 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007525 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7526
7527 touch();
7528
7529 w->consumeMotionOutside();
7530}
7531
7532TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
7533 const sp<FakeWindowHandle>& w =
7534 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007535 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007536 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7537
7538 touch();
7539
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007540 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007541}
7542
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007543TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007544 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007545 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7546 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007547 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7548
7549 touch();
7550
7551 mTouchWindow->consumeAnyMotionDown();
7552}
7553
7554TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
7555 const sp<FakeWindowHandle>& w =
7556 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7557 MAXIMUM_OBSCURING_OPACITY);
7558 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007559
7560 touch();
7561
7562 mTouchWindow->consumeAnyMotionDown();
7563}
7564
7565TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007566 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007567 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7568 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007569 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7570
7571 touch();
7572
7573 mTouchWindow->assertNoEvents();
7574}
7575
7576TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
7577 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
7578 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007579 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
7580 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007581 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007582 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
7583 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007584 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
7585
7586 touch();
7587
7588 mTouchWindow->assertNoEvents();
7589}
7590
7591TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
7592 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
7593 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007594 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
7595 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007596 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007597 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
7598 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007599 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
7600
7601 touch();
7602
7603 mTouchWindow->consumeAnyMotionDown();
7604}
7605
7606TEST_F(InputDispatcherUntrustedTouchesTest,
7607 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
7608 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007609 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7610 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007611 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007612 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
7613 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007614 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
7615
7616 touch();
7617
7618 mTouchWindow->consumeAnyMotionDown();
7619}
7620
7621TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
7622 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007623 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7624 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007625 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007626 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
7627 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007628 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007629
7630 touch();
7631
7632 mTouchWindow->assertNoEvents();
7633}
7634
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007635TEST_F(InputDispatcherUntrustedTouchesTest,
7636 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
7637 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007638 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
7639 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007640 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007641 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7642 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007643 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
7644
7645 touch();
7646
7647 mTouchWindow->assertNoEvents();
7648}
7649
7650TEST_F(InputDispatcherUntrustedTouchesTest,
7651 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
7652 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007653 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
7654 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007655 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007656 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7657 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007658 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
7659
7660 touch();
7661
7662 mTouchWindow->consumeAnyMotionDown();
7663}
7664
7665TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
7666 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007667 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
7668 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007669 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7670
7671 touch();
7672
7673 mTouchWindow->consumeAnyMotionDown();
7674}
7675
7676TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
7677 const sp<FakeWindowHandle>& w =
7678 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
7679 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7680
7681 touch();
7682
7683 mTouchWindow->consumeAnyMotionDown();
7684}
7685
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00007686TEST_F(InputDispatcherUntrustedTouchesTest,
7687 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
7688 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
7689 const sp<FakeWindowHandle>& w =
7690 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
7691 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7692
7693 touch();
7694
7695 mTouchWindow->assertNoEvents();
7696}
7697
7698TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
7699 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
7700 const sp<FakeWindowHandle>& w =
7701 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
7702 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7703
7704 touch();
7705
7706 mTouchWindow->consumeAnyMotionDown();
7707}
7708
7709TEST_F(InputDispatcherUntrustedTouchesTest,
7710 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
7711 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
7712 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007713 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7714 OPACITY_ABOVE_THRESHOLD);
7715 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7716
7717 touch();
7718
7719 mTouchWindow->consumeAnyMotionDown();
7720}
7721
7722TEST_F(InputDispatcherUntrustedTouchesTest,
7723 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
7724 const sp<FakeWindowHandle>& w1 =
7725 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
7726 OPACITY_BELOW_THRESHOLD);
7727 const sp<FakeWindowHandle>& w2 =
7728 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7729 OPACITY_BELOW_THRESHOLD);
7730 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
7731
7732 touch();
7733
7734 mTouchWindow->assertNoEvents();
7735}
7736
7737/**
7738 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
7739 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
7740 * (which alone would result in allowing touches) does not affect the blocking behavior.
7741 */
7742TEST_F(InputDispatcherUntrustedTouchesTest,
7743 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
7744 const sp<FakeWindowHandle>& wB =
7745 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
7746 OPACITY_BELOW_THRESHOLD);
7747 const sp<FakeWindowHandle>& wC =
7748 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
7749 OPACITY_BELOW_THRESHOLD);
7750 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
7751
7752 touch();
7753
7754 mTouchWindow->assertNoEvents();
7755}
7756
7757/**
7758 * This test is testing that a window from a different UID but with same application token doesn't
7759 * block the touch. Apps can share the application token for close UI collaboration for example.
7760 */
7761TEST_F(InputDispatcherUntrustedTouchesTest,
7762 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
7763 const sp<FakeWindowHandle>& w =
7764 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
7765 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00007766 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7767
7768 touch();
7769
7770 mTouchWindow->consumeAnyMotionDown();
7771}
7772
arthurhungb89ccb02020-12-30 16:19:01 +08007773class InputDispatcherDragTests : public InputDispatcherTest {
7774protected:
7775 std::shared_ptr<FakeApplicationHandle> mApp;
7776 sp<FakeWindowHandle> mWindow;
7777 sp<FakeWindowHandle> mSecondWindow;
7778 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007779 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007780 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
7781 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +08007782
7783 void SetUp() override {
7784 InputDispatcherTest::SetUp();
7785 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007786 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08007787 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08007788
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007789 mSecondWindow =
7790 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08007791 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08007792
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007793 mSpyWindow =
7794 sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007795 mSpyWindow->setSpy(true);
7796 mSpyWindow->setTrustedOverlay(true);
7797 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
7798
arthurhungb89ccb02020-12-30 16:19:01 +08007799 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007800 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08007801 }
7802
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007803 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
7804 switch (fromSource) {
7805 case AINPUT_SOURCE_TOUCHSCREEN:
7806 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7807 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
7808 ADISPLAY_ID_DEFAULT, {50, 50}))
7809 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7810 break;
7811 case AINPUT_SOURCE_STYLUS:
7812 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7813 injectMotionEvent(
7814 mDispatcher,
7815 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
7816 AINPUT_SOURCE_STYLUS)
7817 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007818 .pointer(PointerBuilder(0, ToolType::STYLUS)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007819 .x(50)
7820 .y(50))
7821 .build()));
7822 break;
7823 case AINPUT_SOURCE_MOUSE:
7824 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7825 injectMotionEvent(
7826 mDispatcher,
7827 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
7828 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
7829 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007830 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007831 .x(50)
7832 .y(50))
7833 .build()));
7834 break;
7835 default:
7836 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
7837 }
arthurhungb89ccb02020-12-30 16:19:01 +08007838
7839 // Window should receive motion event.
7840 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007841 // Spy window should also receive motion event
7842 mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +00007843 }
7844
7845 // Start performing drag, we will create a drag window and transfer touch to it.
7846 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
7847 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007848 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +00007849 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007850 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +00007851 }
arthurhungb89ccb02020-12-30 16:19:01 +08007852
7853 // The drag window covers the entire display
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007854 mDragWindow =
7855 sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007856 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
arthurhungb89ccb02020-12-30 16:19:01 +08007857 mDispatcher->setInputWindows(
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007858 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08007859
7860 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +00007861 bool transferred =
7862 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
Harry Cutts33476232023-01-30 19:57:29 +00007863 /*isDragDrop=*/true);
Arthur Hung54745652022-04-20 07:17:41 +00007864 if (transferred) {
7865 mWindow->consumeMotionCancel();
7866 mDragWindow->consumeMotionDown();
7867 }
7868 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +08007869 }
7870};
7871
7872TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007873 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +08007874
7875 // Move on window.
7876 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7877 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7878 ADISPLAY_ID_DEFAULT, {50, 50}))
7879 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7880 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7881 mWindow->consumeDragEvent(false, 50, 50);
7882 mSecondWindow->assertNoEvents();
7883
7884 // Move to another window.
7885 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7886 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7887 ADISPLAY_ID_DEFAULT, {150, 50}))
7888 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7889 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7890 mWindow->consumeDragEvent(true, 150, 50);
7891 mSecondWindow->consumeDragEvent(false, 50, 50);
7892
7893 // Move back to original window.
7894 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7895 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7896 ADISPLAY_ID_DEFAULT, {50, 50}))
7897 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7898 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7899 mWindow->consumeDragEvent(false, 50, 50);
7900 mSecondWindow->consumeDragEvent(true, -50, 50);
7901
7902 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7903 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
7904 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7905 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7906 mWindow->assertNoEvents();
7907 mSecondWindow->assertNoEvents();
7908}
7909
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007910TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007911 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007912
7913 // No cancel event after drag start
7914 mSpyWindow->assertNoEvents();
7915
7916 const MotionEvent secondFingerDownEvent =
7917 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7918 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007919 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
7920 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007921 .build();
7922 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7923 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7924 InputEventInjectionSync::WAIT_FOR_RESULT))
7925 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7926
7927 // Receives cancel for first pointer after next pointer down
7928 mSpyWindow->consumeMotionCancel();
7929 mSpyWindow->consumeMotionDown();
7930
7931 mSpyWindow->assertNoEvents();
7932}
7933
arthurhungf452d0b2021-01-06 00:19:52 +08007934TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007935 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +08007936
7937 // Move on window.
7938 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7939 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7940 ADISPLAY_ID_DEFAULT, {50, 50}))
7941 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7942 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7943 mWindow->consumeDragEvent(false, 50, 50);
7944 mSecondWindow->assertNoEvents();
7945
7946 // Move to another window.
7947 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7948 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7949 ADISPLAY_ID_DEFAULT, {150, 50}))
7950 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7951 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7952 mWindow->consumeDragEvent(true, 150, 50);
7953 mSecondWindow->consumeDragEvent(false, 50, 50);
7954
7955 // drop to another window.
7956 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7957 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7958 {150, 50}))
7959 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7960 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7961 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
7962 mWindow->assertNoEvents();
7963 mSecondWindow->assertNoEvents();
7964}
7965
arthurhung6d4bed92021-03-17 11:59:33 +08007966TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007967 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +08007968
7969 // Move on window and keep button pressed.
7970 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7971 injectMotionEvent(mDispatcher,
7972 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
7973 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007974 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08007975 .build()))
7976 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7977 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7978 mWindow->consumeDragEvent(false, 50, 50);
7979 mSecondWindow->assertNoEvents();
7980
7981 // Move to another window and release button, expect to drop item.
7982 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7983 injectMotionEvent(mDispatcher,
7984 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
7985 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007986 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08007987 .build()))
7988 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7989 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7990 mWindow->assertNoEvents();
7991 mSecondWindow->assertNoEvents();
7992 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
7993
7994 // nothing to the window.
7995 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7996 injectMotionEvent(mDispatcher,
7997 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
7998 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007999 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008000 .build()))
8001 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8002 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8003 mWindow->assertNoEvents();
8004 mSecondWindow->assertNoEvents();
8005}
8006
Arthur Hung54745652022-04-20 07:17:41 +00008007TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008008 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +08008009
8010 // Set second window invisible.
8011 mSecondWindow->setVisible(false);
8012 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
8013
8014 // Move on window.
8015 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8016 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8017 ADISPLAY_ID_DEFAULT, {50, 50}))
8018 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8019 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8020 mWindow->consumeDragEvent(false, 50, 50);
8021 mSecondWindow->assertNoEvents();
8022
8023 // Move to another window.
8024 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8025 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8026 ADISPLAY_ID_DEFAULT, {150, 50}))
8027 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8028 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8029 mWindow->consumeDragEvent(true, 150, 50);
8030 mSecondWindow->assertNoEvents();
8031
8032 // drop to another window.
8033 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8034 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8035 {150, 50}))
8036 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8037 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8038 mFakePolicy->assertDropTargetEquals(nullptr);
8039 mWindow->assertNoEvents();
8040 mSecondWindow->assertNoEvents();
8041}
8042
Arthur Hung54745652022-04-20 07:17:41 +00008043TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008044 // Ensure window could track pointerIds if it didn't support split touch.
8045 mWindow->setPreventSplitting(true);
8046
Arthur Hung54745652022-04-20 07:17:41 +00008047 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8048 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8049 {50, 50}))
8050 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8051 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8052
8053 const MotionEvent secondFingerDownEvent =
8054 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8055 .displayId(ADISPLAY_ID_DEFAULT)
8056 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008057 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8058 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008059 .build();
8060 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8061 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8062 InputEventInjectionSync::WAIT_FOR_RESULT))
8063 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00008064 mWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
Arthur Hung54745652022-04-20 07:17:41 +00008065
8066 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008067 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008068}
8069
8070TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
8071 // First down on second window.
8072 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8073 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8074 {150, 50}))
8075 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8076
8077 mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8078
8079 // Second down on first window.
8080 const MotionEvent secondFingerDownEvent =
8081 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8082 .displayId(ADISPLAY_ID_DEFAULT)
8083 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008084 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8085 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008086 .build();
8087 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8088 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8089 InputEventInjectionSync::WAIT_FOR_RESULT))
8090 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8091 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8092
8093 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008094 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008095
8096 // Move on window.
8097 const MotionEvent secondFingerMoveEvent =
8098 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
8099 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008100 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8101 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008102 .build();
8103 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8104 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
8105 InputEventInjectionSync::WAIT_FOR_RESULT));
8106 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8107 mWindow->consumeDragEvent(false, 50, 50);
8108 mSecondWindow->consumeMotionMove();
8109
8110 // Release the drag pointer should perform drop.
8111 const MotionEvent secondFingerUpEvent =
8112 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
8113 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008114 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8115 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008116 .build();
8117 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8118 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
8119 InputEventInjectionSync::WAIT_FOR_RESULT));
8120 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8121 mFakePolicy->assertDropTargetEquals(mWindow->getToken());
8122 mWindow->assertNoEvents();
8123 mSecondWindow->consumeMotionMove();
8124}
8125
Arthur Hung3915c1f2022-05-31 07:17:17 +00008126TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008127 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +00008128
8129 // Update window of second display.
8130 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008131 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008132 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8133
8134 // Let second display has a touch state.
8135 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8136 injectMotionEvent(mDispatcher,
8137 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8138 AINPUT_SOURCE_TOUCHSCREEN)
8139 .displayId(SECOND_DISPLAY_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008140 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Arthur Hung3915c1f2022-05-31 07:17:17 +00008141 .build()));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07008142 windowInSecondary->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
Harry Cutts33476232023-01-30 19:57:29 +00008143 SECOND_DISPLAY_ID, /*expectedFlag=*/0);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008144 // Update window again.
8145 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8146
8147 // Move on window.
8148 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8149 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8150 ADISPLAY_ID_DEFAULT, {50, 50}))
8151 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8152 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8153 mWindow->consumeDragEvent(false, 50, 50);
8154 mSecondWindow->assertNoEvents();
8155
8156 // Move to another window.
8157 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8158 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8159 ADISPLAY_ID_DEFAULT, {150, 50}))
8160 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8161 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8162 mWindow->consumeDragEvent(true, 150, 50);
8163 mSecondWindow->consumeDragEvent(false, 50, 50);
8164
8165 // drop to another window.
8166 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8167 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8168 {150, 50}))
8169 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8170 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8171 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8172 mWindow->assertNoEvents();
8173 mSecondWindow->assertNoEvents();
8174}
8175
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008176TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
8177 startDrag(true, AINPUT_SOURCE_MOUSE);
8178 // Move on window.
8179 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8180 injectMotionEvent(mDispatcher,
8181 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8182 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8183 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008184 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008185 .x(50)
8186 .y(50))
8187 .build()))
8188 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8189 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8190 mWindow->consumeDragEvent(false, 50, 50);
8191 mSecondWindow->assertNoEvents();
8192
8193 // Move to another window.
8194 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8195 injectMotionEvent(mDispatcher,
8196 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8197 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8198 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008199 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008200 .x(150)
8201 .y(50))
8202 .build()))
8203 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8204 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8205 mWindow->consumeDragEvent(true, 150, 50);
8206 mSecondWindow->consumeDragEvent(false, 50, 50);
8207
8208 // drop to another window.
8209 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8210 injectMotionEvent(mDispatcher,
8211 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
8212 .buttonState(0)
8213 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008214 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008215 .x(150)
8216 .y(50))
8217 .build()))
8218 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8219 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8220 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8221 mWindow->assertNoEvents();
8222 mSecondWindow->assertNoEvents();
8223}
8224
Vishnu Nair062a8672021-09-03 16:07:44 -07008225class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
8226
8227TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
8228 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008229 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8230 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008231 window->setDropInput(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008232 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8233 window->setFocusable(true);
8234 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8235 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008236 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008237
8238 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008239 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008240 window->assertNoEvents();
8241
Prabir Pradhan678438e2023-04-13 19:32:51 +00008242 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8243 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008244 window->assertNoEvents();
8245
8246 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008247 window->setDropInput(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008248 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8249
Prabir Pradhan678438e2023-04-13 19:32:51 +00008250 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008251 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8252
Prabir Pradhan678438e2023-04-13 19:32:51 +00008253 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8254 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008255 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8256 window->assertNoEvents();
8257}
8258
8259TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
8260 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8261 std::make_shared<FakeApplicationHandle>();
8262 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008263 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8264 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008265 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
8266 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008267 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008268 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008269 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8270 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008271 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008272 window->setOwnerInfo(222, 222);
8273 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8274 window->setFocusable(true);
8275 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8276 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008277 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008278
8279 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008280 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008281 window->assertNoEvents();
8282
Prabir Pradhan678438e2023-04-13 19:32:51 +00008283 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8284 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008285 window->assertNoEvents();
8286
8287 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008288 window->setDropInputIfObscured(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008289 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8290
Prabir Pradhan678438e2023-04-13 19:32:51 +00008291 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008292 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8293
Prabir Pradhan678438e2023-04-13 19:32:51 +00008294 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8295 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008296 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
8297 window->assertNoEvents();
8298}
8299
8300TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
8301 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8302 std::make_shared<FakeApplicationHandle>();
8303 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008304 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8305 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008306 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
8307 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008308 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008309 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008310 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8311 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008312 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008313 window->setOwnerInfo(222, 222);
8314 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8315 window->setFocusable(true);
8316 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8317 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008318 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008319
8320 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008321 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008322 window->assertNoEvents();
8323
Prabir Pradhan678438e2023-04-13 19:32:51 +00008324 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8325 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008326 window->assertNoEvents();
8327
8328 // When the window is no longer obscured because it went on top, it should get input
8329 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}});
8330
Prabir Pradhan678438e2023-04-13 19:32:51 +00008331 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008332 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8333
Prabir Pradhan678438e2023-04-13 19:32:51 +00008334 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8335 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008336 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8337 window->assertNoEvents();
8338}
8339
Antonio Kantekf16f2832021-09-28 04:39:20 +00008340class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
8341protected:
8342 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +00008343 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008344 sp<FakeWindowHandle> mWindow;
8345 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +00008346 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008347
8348 void SetUp() override {
8349 InputDispatcherTest::SetUp();
8350
8351 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +00008352 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008353 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008354 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008355 setFocusedWindow(mWindow);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008356 mSecondWindow =
8357 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008358 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +00008359 mThirdWindow =
8360 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
8361 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
8362 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008363
8364 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Antonio Kantek15beb512022-06-13 22:35:41 +00008365 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}},
8366 {SECOND_DISPLAY_ID, {mThirdWindow}}});
8367 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008368 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008369
Antonio Kantek15beb512022-06-13 22:35:41 +00008370 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +00008371 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008372 WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -07008373 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
8374 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008375 mThirdWindow->assertNoEvents();
8376 }
8377
8378 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
8379 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008380 SECONDARY_WINDOW_UID, /*hasPermission=*/true,
Antonio Kantek15beb512022-06-13 22:35:41 +00008381 SECOND_DISPLAY_ID)) {
8382 mWindow->assertNoEvents();
8383 mSecondWindow->assertNoEvents();
8384 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -07008385 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00008386 }
8387
Antonio Kantek15beb512022-06-13 22:35:41 +00008388 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid,
8389 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -07008390 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
8391 ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008392 mWindow->consumeTouchModeEvent(inTouchMode);
8393 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008394 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +00008395 }
8396};
8397
Antonio Kantek26defcf2022-02-08 01:12:27 +00008398TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008399 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +00008400 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
8401 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008402 /* hasPermission=*/false);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008403}
8404
Antonio Kantek26defcf2022-02-08 01:12:27 +00008405TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
8406 const WindowInfo& windowInfo = *mWindow->getInfo();
8407 int32_t ownerPid = windowInfo.ownerPid;
8408 int32_t ownerUid = windowInfo.ownerUid;
8409 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
8410 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008411 ownerUid, /*hasPermission=*/false,
Antonio Kanteka042c022022-07-06 16:51:07 -07008412 ADISPLAY_ID_DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +00008413 mWindow->assertNoEvents();
8414 mSecondWindow->assertNoEvents();
8415}
8416
8417TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
8418 const WindowInfo& windowInfo = *mWindow->getInfo();
8419 int32_t ownerPid = windowInfo.ownerPid;
8420 int32_t ownerUid = windowInfo.ownerUid;
8421 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
Antonio Kantek15beb512022-06-13 22:35:41 +00008422 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008423 ownerUid, /*hasPermission=*/true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008424}
8425
Antonio Kantekf16f2832021-09-28 04:39:20 +00008426TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008427 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +00008428 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
8429 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008430 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008431 mWindow->assertNoEvents();
8432 mSecondWindow->assertNoEvents();
8433}
8434
Antonio Kantek15beb512022-06-13 22:35:41 +00008435TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
8436 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
8437 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8438 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008439 /*hasPermission=*/true, SECOND_DISPLAY_ID));
Antonio Kantek15beb512022-06-13 22:35:41 +00008440 mWindow->assertNoEvents();
8441 mSecondWindow->assertNoEvents();
8442 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
8443}
8444
Antonio Kantek48710e42022-03-24 14:19:30 -07008445TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
8446 // Interact with the window first.
8447 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
8448 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8449 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
8450
8451 // Then remove focus.
8452 mWindow->setFocusable(false);
8453 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
8454
8455 // Assert that caller can switch touch mode by owning one of the last interacted window.
8456 const WindowInfo& windowInfo = *mWindow->getInfo();
8457 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8458 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008459 /*hasPermission=*/false, ADISPLAY_ID_DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -07008460}
8461
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008462class InputDispatcherSpyWindowTest : public InputDispatcherTest {
8463public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008464 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008465 std::shared_ptr<FakeApplicationHandle> application =
8466 std::make_shared<FakeApplicationHandle>();
8467 std::string name = "Fake Spy ";
8468 name += std::to_string(mSpyCount++);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008469 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher,
8470 name.c_str(), ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008471 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008472 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008473 return spy;
8474 }
8475
8476 sp<FakeWindowHandle> createForeground() {
8477 std::shared_ptr<FakeApplicationHandle> application =
8478 std::make_shared<FakeApplicationHandle>();
8479 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008480 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
8481 ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008482 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008483 return window;
8484 }
8485
8486private:
8487 int mSpyCount{0};
8488};
8489
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008490using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008491/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008492 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
8493 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008494TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
8495 ScopedSilentDeath _silentDeath;
8496
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008497 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008498 spy->setTrustedOverlay(false);
8499 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}),
8500 ".* not a trusted overlay");
8501}
8502
8503/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008504 * Input injection into a display with a spy window but no foreground windows should succeed.
8505 */
8506TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008507 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008508 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}});
8509
8510 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8511 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8512 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8513 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8514}
8515
8516/**
8517 * Verify the order in which different input windows receive events. The touched foreground window
8518 * (if there is one) should always receive the event first. When there are multiple spy windows, the
8519 * spy windows will receive the event according to their Z-order, where the top-most spy window will
8520 * receive events before ones belows it.
8521 *
8522 * Here, we set up a scenario with four windows in the following Z order from the top:
8523 * spy1, spy2, window, spy3.
8524 * We then inject an event and verify that the foreground "window" receives it first, followed by
8525 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
8526 * window.
8527 */
8528TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
8529 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008530 auto spy1 = createSpy();
8531 auto spy2 = createSpy();
8532 auto spy3 = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008533 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}});
8534 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
8535 const size_t numChannels = channels.size();
8536
Michael Wright8e9a8562022-02-09 13:44:29 +00008537 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008538 if (!epollFd.ok()) {
8539 FAIL() << "Failed to create epoll fd";
8540 }
8541
8542 for (size_t i = 0; i < numChannels; i++) {
8543 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
8544 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
8545 FAIL() << "Failed to add fd to epoll";
8546 }
8547 }
8548
8549 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8550 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8551 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8552
8553 std::vector<size_t> eventOrder;
8554 std::vector<struct epoll_event> events(numChannels);
8555 for (;;) {
8556 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
8557 (100ms).count());
8558 if (nFds < 0) {
8559 FAIL() << "Failed to call epoll_wait";
8560 }
8561 if (nFds == 0) {
8562 break; // epoll_wait timed out
8563 }
8564 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -07008565 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -07008566 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008567 channels[i]->consumeMotionDown();
8568 }
8569 }
8570
8571 // Verify the order in which the events were received.
8572 EXPECT_EQ(3u, eventOrder.size());
8573 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
8574 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
8575 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
8576}
8577
8578/**
8579 * A spy window using the NOT_TOUCHABLE flag does not receive events.
8580 */
8581TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
8582 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008583 auto spy = createSpy();
8584 spy->setTouchable(false);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008585 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8586
8587 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8588 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8589 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8590 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8591 spy->assertNoEvents();
8592}
8593
8594/**
8595 * A spy window will only receive gestures that originate within its touchable region. Gestures that
8596 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
8597 * to the window.
8598 */
8599TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
8600 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008601 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008602 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
8603 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8604
8605 // Inject an event outside the spy window's touchable region.
8606 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8607 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8608 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8609 window->consumeMotionDown();
8610 spy->assertNoEvents();
8611 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8612 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8613 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8614 window->consumeMotionUp();
8615 spy->assertNoEvents();
8616
8617 // Inject an event inside the spy window's touchable region.
8618 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8619 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8620 {5, 10}))
8621 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8622 window->consumeMotionDown();
8623 spy->consumeMotionDown();
8624}
8625
8626/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008627 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008628 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008629 */
8630TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
8631 auto window = createForeground();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008632 window->setOwnerInfo(12, 34);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008633 auto spy = createSpy();
8634 spy->setWatchOutsideTouch(true);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008635 spy->setOwnerInfo(56, 78);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008636 spy->setFrame(Rect{0, 0, 20, 20});
8637 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8638
8639 // Inject an event outside the spy window's frame and touchable region.
8640 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008641 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8642 {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008643 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8644 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008645 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008646}
8647
8648/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008649 * Even when a spy window spans over multiple foreground windows, the spy should receive all
8650 * pointers that are down within its bounds.
8651 */
8652TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
8653 auto windowLeft = createForeground();
8654 windowLeft->setFrame({0, 0, 100, 200});
8655 auto windowRight = createForeground();
8656 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008657 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008658 spy->setFrame({0, 0, 200, 200});
8659 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}});
8660
8661 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8662 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8663 {50, 50}))
8664 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8665 windowLeft->consumeMotionDown();
8666 spy->consumeMotionDown();
8667
8668 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008669 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008670 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008671 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8672 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008673 .build();
8674 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8675 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8676 InputEventInjectionSync::WAIT_FOR_RESULT))
8677 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8678 windowRight->consumeMotionDown();
Harry Cutts33476232023-01-30 19:57:29 +00008679 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008680}
8681
8682/**
8683 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
8684 * the spy should receive the second pointer with ACTION_DOWN.
8685 */
8686TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
8687 auto window = createForeground();
8688 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008689 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008690 spyRight->setFrame({100, 0, 200, 200});
8691 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}});
8692
8693 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8694 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8695 {50, 50}))
8696 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8697 window->consumeMotionDown();
8698 spyRight->assertNoEvents();
8699
8700 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008701 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008702 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008703 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8704 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008705 .build();
8706 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8707 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8708 InputEventInjectionSync::WAIT_FOR_RESULT))
8709 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00008710 window->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008711 spyRight->consumeMotionDown();
8712}
8713
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008714/**
8715 * The spy window should not be able to affect whether or not touches are split. Only the foreground
8716 * windows should be allowed to control split touch.
8717 */
8718TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08008719 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008720 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008721 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08008722 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008723
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008724 auto window = createForeground();
8725 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008726
8727 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8728
8729 // First finger down, no window touched.
8730 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8731 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8732 {100, 200}))
8733 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8734 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8735 window->assertNoEvents();
8736
8737 // Second finger down on window, the window should receive touch down.
8738 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008739 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008740 .displayId(ADISPLAY_ID_DEFAULT)
8741 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008742 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
8743 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008744 .build();
8745 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8746 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8747 InputEventInjectionSync::WAIT_FOR_RESULT))
8748 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8749
8750 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00008751 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008752}
8753
8754/**
8755 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
8756 * do not receive key events.
8757 */
8758TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008759 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008760 spy->setFocusable(false);
8761
8762 auto window = createForeground();
8763 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8764 setFocusedWindow(window);
8765 window->consumeFocusEvent(true);
8766
8767 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
8768 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8769 window->consumeKeyDown(ADISPLAY_ID_NONE);
8770
8771 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
8772 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8773 window->consumeKeyUp(ADISPLAY_ID_NONE);
8774
8775 spy->assertNoEvents();
8776}
8777
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008778using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
8779
8780/**
8781 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
8782 * are currently sent to any other windows - including other spy windows - will also be cancelled.
8783 */
8784TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
8785 auto window = createForeground();
8786 auto spy1 = createSpy();
8787 auto spy2 = createSpy();
8788 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}});
8789
8790 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8791 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8792 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8793 window->consumeMotionDown();
8794 spy1->consumeMotionDown();
8795 spy2->consumeMotionDown();
8796
8797 // Pilfer pointers from the second spy window.
8798 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
8799 spy2->assertNoEvents();
8800 spy1->consumeMotionCancel();
8801 window->consumeMotionCancel();
8802
8803 // The rest of the gesture should only be sent to the second spy window.
8804 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8805 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8806 ADISPLAY_ID_DEFAULT))
8807 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8808 spy2->consumeMotionMove();
8809 spy1->assertNoEvents();
8810 window->assertNoEvents();
8811}
8812
8813/**
8814 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
8815 * in the middle of the gesture.
8816 */
8817TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
8818 auto window = createForeground();
8819 auto spy = createSpy();
8820 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8821
8822 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8823 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8824 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8825 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8826 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8827
8828 window->releaseChannel();
8829
8830 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8831
8832 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8833 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8834 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8835 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8836}
8837
8838/**
8839 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
8840 * the spy, but not to any other windows.
8841 */
8842TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
8843 auto spy = createSpy();
8844 auto window = createForeground();
8845
8846 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8847
8848 // First finger down on the window and the spy.
8849 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8850 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8851 {100, 200}))
8852 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8853 spy->consumeMotionDown();
8854 window->consumeMotionDown();
8855
8856 // Spy window pilfers the pointers.
8857 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8858 window->consumeMotionCancel();
8859
8860 // Second finger down on the window and spy, but the window should not receive the pointer down.
8861 const MotionEvent secondFingerDownEvent =
8862 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8863 .displayId(ADISPLAY_ID_DEFAULT)
8864 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008865 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
8866 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008867 .build();
8868 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8869 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8870 InputEventInjectionSync::WAIT_FOR_RESULT))
8871 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8872
Harry Cutts33476232023-01-30 19:57:29 +00008873 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008874
8875 // Third finger goes down outside all windows, so injection should fail.
8876 const MotionEvent thirdFingerDownEvent =
8877 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8878 .displayId(ADISPLAY_ID_DEFAULT)
8879 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008880 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
8881 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
8882 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008883 .build();
8884 ASSERT_EQ(InputEventInjectionResult::FAILED,
8885 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
8886 InputEventInjectionSync::WAIT_FOR_RESULT))
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08008887 << "Inject motion event should return InputEventInjectionResult::FAILED";
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008888
8889 spy->assertNoEvents();
8890 window->assertNoEvents();
8891}
8892
8893/**
8894 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
8895 */
8896TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
8897 auto spy = createSpy();
8898 spy->setFrame(Rect(0, 0, 100, 100));
8899 auto window = createForeground();
8900 window->setFrame(Rect(0, 0, 200, 200));
8901
8902 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8903
8904 // First finger down on the window only
8905 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8906 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8907 {150, 150}))
8908 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8909 window->consumeMotionDown();
8910
8911 // Second finger down on the spy and window
8912 const MotionEvent secondFingerDownEvent =
8913 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8914 .displayId(ADISPLAY_ID_DEFAULT)
8915 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008916 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
8917 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008918 .build();
8919 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8920 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8921 InputEventInjectionSync::WAIT_FOR_RESULT))
8922 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8923 spy->consumeMotionDown();
8924 window->consumeMotionPointerDown(1);
8925
8926 // Third finger down on the spy and window
8927 const MotionEvent thirdFingerDownEvent =
8928 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8929 .displayId(ADISPLAY_ID_DEFAULT)
8930 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008931 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
8932 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
8933 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008934 .build();
8935 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8936 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
8937 InputEventInjectionSync::WAIT_FOR_RESULT))
8938 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8939 spy->consumeMotionPointerDown(1);
8940 window->consumeMotionPointerDown(2);
8941
8942 // Spy window pilfers the pointers.
8943 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8944 window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
8945 window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
8946
8947 spy->assertNoEvents();
8948 window->assertNoEvents();
8949}
8950
8951/**
8952 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
8953 * other windows should be canceled. If this results in the cancellation of all pointers for some
8954 * window, then that window should receive ACTION_CANCEL.
8955 */
8956TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
8957 auto spy = createSpy();
8958 spy->setFrame(Rect(0, 0, 100, 100));
8959 auto window = createForeground();
8960 window->setFrame(Rect(0, 0, 200, 200));
8961
8962 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8963
8964 // First finger down on both spy and window
8965 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8966 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8967 {10, 10}))
8968 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8969 window->consumeMotionDown();
8970 spy->consumeMotionDown();
8971
8972 // Second finger down on the spy and window
8973 const MotionEvent secondFingerDownEvent =
8974 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8975 .displayId(ADISPLAY_ID_DEFAULT)
8976 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008977 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
8978 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008979 .build();
8980 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8981 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8982 InputEventInjectionSync::WAIT_FOR_RESULT))
8983 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8984 spy->consumeMotionPointerDown(1);
8985 window->consumeMotionPointerDown(1);
8986
8987 // Spy window pilfers the pointers.
8988 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8989 window->consumeMotionCancel();
8990
8991 spy->assertNoEvents();
8992 window->assertNoEvents();
8993}
8994
8995/**
8996 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
8997 * be sent to other windows
8998 */
8999TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
9000 auto spy = createSpy();
9001 spy->setFrame(Rect(0, 0, 100, 100));
9002 auto window = createForeground();
9003 window->setFrame(Rect(0, 0, 200, 200));
9004
9005 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9006
9007 // First finger down on both window and spy
9008 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9009 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9010 {10, 10}))
9011 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9012 window->consumeMotionDown();
9013 spy->consumeMotionDown();
9014
9015 // Spy window pilfers the pointers.
9016 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9017 window->consumeMotionCancel();
9018
9019 // Second finger down on the window only
9020 const MotionEvent secondFingerDownEvent =
9021 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9022 .displayId(ADISPLAY_ID_DEFAULT)
9023 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009024 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9025 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009026 .build();
9027 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9028 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9029 InputEventInjectionSync::WAIT_FOR_RESULT))
9030 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9031 window->consumeMotionDown();
9032 window->assertNoEvents();
9033
9034 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
9035 spy->consumeMotionMove();
9036 spy->assertNoEvents();
9037}
9038
Prabir Pradhand65552b2021-10-07 11:23:50 -07009039class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
9040public:
9041 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
9042 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9043 std::make_shared<FakeApplicationHandle>();
9044 sp<FakeWindowHandle> overlay =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009045 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
9046 "Stylus interceptor window", ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009047 overlay->setFocusable(false);
9048 overlay->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009049 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009050 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009051 overlay->setTrustedOverlay(true);
9052
9053 std::shared_ptr<FakeApplicationHandle> application =
9054 std::make_shared<FakeApplicationHandle>();
9055 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009056 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
9057 ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009058 window->setFocusable(true);
9059 window->setOwnerInfo(222, 222);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009060
9061 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
9062 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9063 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00009064 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009065 return {std::move(overlay), std::move(window)};
9066 }
9067
9068 void sendFingerEvent(int32_t action) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00009069 mDispatcher->notifyMotion(
Prabir Pradhand65552b2021-10-07 11:23:50 -07009070 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
Prabir Pradhan678438e2023-04-13 19:32:51 +00009071 ADISPLAY_ID_DEFAULT, {PointF{20, 20}}));
Prabir Pradhand65552b2021-10-07 11:23:50 -07009072 }
9073
9074 void sendStylusEvent(int32_t action) {
9075 NotifyMotionArgs motionArgs =
9076 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
9077 ADISPLAY_ID_DEFAULT, {PointF{30, 40}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009078 motionArgs.pointerProperties[0].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +00009079 mDispatcher->notifyMotion(motionArgs);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009080 }
9081};
9082
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08009083using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
9084
9085TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
9086 ScopedSilentDeath _silentDeath;
9087
Prabir Pradhand65552b2021-10-07 11:23:50 -07009088 auto [overlay, window] = setupStylusOverlayScenario();
9089 overlay->setTrustedOverlay(false);
9090 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
9091 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}),
9092 ".* not a trusted overlay");
9093}
9094
9095TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
9096 auto [overlay, window] = setupStylusOverlayScenario();
9097 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9098
9099 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9100 overlay->consumeMotionDown();
9101 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9102 overlay->consumeMotionUp();
9103
9104 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9105 window->consumeMotionDown();
9106 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9107 window->consumeMotionUp();
9108
9109 overlay->assertNoEvents();
9110 window->assertNoEvents();
9111}
9112
9113TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
9114 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009115 overlay->setSpy(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009116 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9117
9118 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9119 overlay->consumeMotionDown();
9120 window->consumeMotionDown();
9121 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9122 overlay->consumeMotionUp();
9123 window->consumeMotionUp();
9124
9125 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9126 window->consumeMotionDown();
9127 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9128 window->consumeMotionUp();
9129
9130 overlay->assertNoEvents();
9131 window->assertNoEvents();
9132}
9133
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00009134/**
9135 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
9136 * The scenario is as follows:
9137 * - The stylus interceptor overlay is configured as a spy window.
9138 * - The stylus interceptor spy receives the start of a new stylus gesture.
9139 * - It pilfers pointers and then configures itself to no longer be a spy.
9140 * - The stylus interceptor continues to receive the rest of the gesture.
9141 */
9142TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
9143 auto [overlay, window] = setupStylusOverlayScenario();
9144 overlay->setSpy(true);
9145 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9146
9147 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9148 overlay->consumeMotionDown();
9149 window->consumeMotionDown();
9150
9151 // The interceptor pilfers the pointers.
9152 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
9153 window->consumeMotionCancel();
9154
9155 // The interceptor configures itself so that it is no longer a spy.
9156 overlay->setSpy(false);
9157 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9158
9159 // It continues to receive the rest of the stylus gesture.
9160 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
9161 overlay->consumeMotionMove();
9162 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9163 overlay->consumeMotionUp();
9164
9165 window->assertNoEvents();
9166}
9167
Prabir Pradhan5735a322022-04-11 17:23:34 +00009168struct User {
9169 int32_t mPid;
9170 int32_t mUid;
9171 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
9172 std::unique_ptr<InputDispatcher>& mDispatcher;
9173
9174 User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid)
9175 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
9176
9177 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
9178 return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
9179 ADISPLAY_ID_DEFAULT, {100, 200},
9180 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
9181 AMOTION_EVENT_INVALID_CURSOR_POSITION},
9182 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
9183 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
9184 }
9185
9186 InputEventInjectionResult injectTargetedKey(int32_t action) const {
Harry Cutts33476232023-01-30 19:57:29 +00009187 return inputdispatcher::injectKey(mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +00009188 InputEventInjectionSync::WAIT_FOR_RESULT,
Harry Cutts33476232023-01-30 19:57:29 +00009189 INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid},
Prabir Pradhan5735a322022-04-11 17:23:34 +00009190 mPolicyFlags);
9191 }
9192
9193 sp<FakeWindowHandle> createWindow() const {
9194 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9195 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009196 sp<FakeWindowHandle> window =
9197 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window",
9198 ADISPLAY_ID_DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +00009199 window->setOwnerInfo(mPid, mUid);
9200 return window;
9201 }
9202};
9203
9204using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
9205
9206TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
9207 auto owner = User(mDispatcher, 10, 11);
9208 auto window = owner.createWindow();
9209 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9210
9211 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9212 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9213 window->consumeMotionDown();
9214
9215 setFocusedWindow(window);
9216 window->consumeFocusEvent(true);
9217
9218 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9219 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9220 window->consumeKeyDown(ADISPLAY_ID_NONE);
9221}
9222
9223TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
9224 auto owner = User(mDispatcher, 10, 11);
9225 auto window = owner.createWindow();
9226 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9227
9228 auto rando = User(mDispatcher, 20, 21);
9229 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9230 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9231
9232 setFocusedWindow(window);
9233 window->consumeFocusEvent(true);
9234
9235 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9236 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9237 window->assertNoEvents();
9238}
9239
9240TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
9241 auto owner = User(mDispatcher, 10, 11);
9242 auto window = owner.createWindow();
9243 auto spy = owner.createWindow();
9244 spy->setSpy(true);
9245 spy->setTrustedOverlay(true);
9246 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9247
9248 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9249 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9250 spy->consumeMotionDown();
9251 window->consumeMotionDown();
9252}
9253
9254TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
9255 auto owner = User(mDispatcher, 10, 11);
9256 auto window = owner.createWindow();
9257
9258 auto rando = User(mDispatcher, 20, 21);
9259 auto randosSpy = rando.createWindow();
9260 randosSpy->setSpy(true);
9261 randosSpy->setTrustedOverlay(true);
9262 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9263
9264 // The event is targeted at owner's window, so injection should succeed, but the spy should
9265 // not receive the event.
9266 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9267 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9268 randosSpy->assertNoEvents();
9269 window->consumeMotionDown();
9270}
9271
9272TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
9273 auto owner = User(mDispatcher, 10, 11);
9274 auto window = owner.createWindow();
9275
9276 auto rando = User(mDispatcher, 20, 21);
9277 auto randosSpy = rando.createWindow();
9278 randosSpy->setSpy(true);
9279 randosSpy->setTrustedOverlay(true);
9280 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9281
9282 // A user that has injection permission can inject into any window.
9283 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9284 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
9285 ADISPLAY_ID_DEFAULT));
9286 randosSpy->consumeMotionDown();
9287 window->consumeMotionDown();
9288
9289 setFocusedWindow(randosSpy);
9290 randosSpy->consumeFocusEvent(true);
9291
9292 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
9293 randosSpy->consumeKeyDown(ADISPLAY_ID_NONE);
9294 window->assertNoEvents();
9295}
9296
9297TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) {
9298 auto owner = User(mDispatcher, 10, 11);
9299 auto window = owner.createWindow();
9300
9301 auto rando = User(mDispatcher, 20, 21);
9302 auto randosWindow = rando.createWindow();
9303 randosWindow->setFrame(Rect{-10, -10, -5, -5});
9304 randosWindow->setWatchOutsideTouch(true);
9305 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}});
9306
9307 // We allow generation of ACTION_OUTSIDE events into windows owned by different uids.
9308 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9309 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9310 window->consumeMotionDown();
9311 randosWindow->consumeMotionOutside();
9312}
9313
Garfield Tane84e6f92019-08-29 17:28:41 -07009314} // namespace android::inputdispatcher